package com.AtomicTest
import java.io.FileWriter
/**
* A tiny little testing framework,to display results and to introduce & promote
* unit testing early in the learning curve.To use in a script or App, include:
* import com.AtomicTest.AtomicTest._
* Created by cyz on 2016/12/27.
*/
object AtomicTest {
implicit def any2Atomic[T](target: T) = {
new AtomicTest(target)
}
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 = Option(target).getOrElse("").toString
//Safely convert to a String
def is(expected: String) = tst(expected) {
expected.replaceAll("\r\n", "\n") == str
}
def is[E](expected: E) = tst(expected) {
expected == target
}
def beginWith(exp: String) = tst(exp) {
str.startsWith(exp.replaceAll("\r\n", "\n"))
}
}
}
上述是测试小框架源码,打包之后就可以导入到自己所写的程序中利用“is” 关键字来在程序内部测试代码是否达到自己想要的结果,打包方式见IntelJ IDEA的打包方法