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:
" +
expected
println(msg)
val el = new FileWriter(errorLog,true)
el.write(target + msg + "
")
el.close()
}
}
def str = // Safely convert to a string
Option(target).getOrElse("").toString
def is(expected:String) = tst(expected) {
expected.replaceAll("
","
") == str
}
def is[E](expected:E) = tst(expected) {
expected == target
}
def beginsWith(exp:String) = tst(exp) {
str.startsWith(
exp.replaceAll("
","
"))
}
}
object AtomicTest {
implicit def any2Atomic[T](target:T) =
new AtomicTest(target)
}