Scala编程程序中的测试小框架

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的打包方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值