码迷,mamicode.com
首页 > 其他好文 > 详细

Scala编程思想测试类AtomicTest.scala

时间:2017-11-17 00:17:46      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:boolean   erro   option   mic   bool   close   lan   测试   atom   

Scala编程思想附录1的测试类。

package com.atomicscala
import language.implicitConversions
import java.io.FileWriter

class AtomicTest[T](val target:T) {
  val errorLog = "_AtomicTestErrors.txt"
  def tst[E](expected:E)(test: => Boolean){
    println(target)
    if(test == false) {
      val msg = "[Error] expected:\n" +
        expected
      println(msg)
      val el = new FileWriter(errorLog,true)
      el.write(target + msg + "\n")
      el.close()
  }
}
def str = // Safely convert to a string
  Option(target).getOrElse("").toString
def is(expected:String) = tst(expected) {
  expected.replaceAll("\r\n","\n") == str
}
def is[E](expected:E) = tst(expected) {
  expected == target
}
def beginsWith(exp:String) = tst(exp) {
  str.startsWith(
    exp.replaceAll("\r\n","\n"))
  }
}

object AtomicTest {
  implicit def any2Atomic[T](target:T) =
    new AtomicTest(target)
}

Scala编程思想测试类AtomicTest.scala

标签:boolean   erro   option   mic   bool   close   lan   测试   atom   

原文地址:http://www.cnblogs.com/flymercurial/p/7846689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!