diff options
author | George Leontiev <folone@gmail.com> | 2014-07-01 21:40:50 +0000 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-11-07 02:08:39 -0500 |
commit | 28a6f3c76ff38437daacc5c5ae4f5136c143637d (patch) | |
tree | 08a5a33398b9ff4d0fe444524420f5090d3f7c32 /libraries/eval/test | |
parent | 2fb5170362493cce98b1818a7ce847abb879bcd1 (diff) | |
download | cbt-28a6f3c76ff38437daacc5c5ae4f5136c143637d.tar.gz cbt-28a6f3c76ff38437daacc5c5ae4f5136c143637d.tar.bz2 cbt-28a6f3c76ff38437daacc5c5ae4f5136c143637d.zip |
[split] util-* Convert all tests in util to scalatest
Problem
specs is deprecated and throws a fair number of NullPointerExceptions in scala 2.10
Also all of our tests are disabled in scala 2.10 for sbt
Solution
Port all our tests to scalatest
This is based off of this PR: https://github.com/twitter/util/pull/97
Result
Tests pass
RB_ID=397681
Diffstat (limited to 'libraries/eval/test')
-rw-r--r-- | libraries/eval/test/EvalTest.scala | 86 | ||||
-rw-r--r-- | libraries/eval/test/resources/OnePlusOne.scala | 2 |
2 files changed, 49 insertions, 39 deletions
diff --git a/libraries/eval/test/EvalTest.scala b/libraries/eval/test/EvalTest.scala index 6e86327..2f80894 100644 --- a/libraries/eval/test/EvalTest.scala +++ b/libraries/eval/test/EvalTest.scala @@ -1,27 +1,32 @@ package com.twitter.util -import org.specs.SpecificationWithJUnit -import java.io.{File, FileOutputStream, FileWriter} +import java.io.{File, FileWriter} + import scala.io.Source +import org.junit.runner.RunWith +import org.scalatest.WordSpec +import org.scalatest.junit.JUnitRunner + import com.twitter.io.TempFile -class EvalSpec extends SpecificationWithJUnit { +@RunWith(classOf[JUnitRunner]) +class EvalTest extends WordSpec { "Evaluator" should { "apply('expression')" in { - (new Eval).apply[Int]("1 + 1") mustEqual 2 + assert((new Eval).apply[Int]("1 + 1") == 2) } "apply(new File(...))" in { - (new Eval).apply[Int](TempFile.fromResourcePath("/OnePlusOne.scala")) mustEqual 2 + assert((new Eval).apply[Int](TempFile.fromResourcePath("/OnePlusOne.scala")) == 2) } "apply(new File(...), new File(...))" in { val derived = (new Eval).apply[() => String]( TempFile.fromResourcePath("/Base.scala"), TempFile.fromResourcePath("/Derived.scala")) - derived() mustEqual "hello" + assert(derived() == "hello") } "apply(new File(...) with a dash in the name with target" in { @@ -31,14 +36,14 @@ class EvalSpec extends SpecificationWithJUnit { val e = new Eval(Some(f)) val sourceFile = TempFile.fromResourcePath("/file-with-dash.scala") val res: String = e(sourceFile) - res mustEqual "hello" + assert(res == "hello") val className = e.fileToClassName(sourceFile) val processedSource = e.sourceForString(Source.fromFile(sourceFile).getLines.mkString("\n")) val fullClassName = "Evaluator__%s_%s.class".format( className, e.uniqueId(processedSource, None)) val targetFileName = f.getAbsolutePath() + File.separator + fullClassName val targetFile = new File(targetFileName) - targetFile.exists must be_==(true) + assert(targetFile.exists) } "apply(new File(...) with target" in { @@ -48,7 +53,7 @@ class EvalSpec extends SpecificationWithJUnit { val e = new Eval(Some(f)) val sourceFile = TempFile.fromResourcePath("/OnePlusOne.scala") val res: Int = e(sourceFile) - res mustEqual 2 + assert(res == 2) // make sure it created a class file with the expected name val className = e.fileToClassName(sourceFile) @@ -57,104 +62,109 @@ class EvalSpec extends SpecificationWithJUnit { className, e.uniqueId(processedSource, None)) val targetFileName = f.getAbsolutePath() + File.separator + fullClassName val targetFile = new File(targetFileName) - targetFile.exists must be_==(true) + assert(targetFile.exists) val targetMod = targetFile.lastModified // eval again, make sure it works val res2: Int = e(sourceFile) // and make sure it didn't create a new file (1 + checksum) - f.listFiles.length mustEqual 2 + assert(f.listFiles.length == 2) // and make sure it didn't update the file val targetFile2 = new File(targetFileName) - targetFile2.lastModified mustEqual targetMod + assert(targetFile2.lastModified == targetMod) // touch source, ensure no-recompile (checksum hasn't changed) sourceFile.setLastModified(System.currentTimeMillis()) val res3: Int = e(sourceFile) - res3 mustEqual 2 + assert(res3 == 2) // and make sure it didn't create a different file - f.listFiles.length mustEqual 2 + assert(f.listFiles.length == 2) // and make sure it updated the file val targetFile3 = new File(targetFileName) - targetFile3.lastModified mustEqual targetMod + assert(targetFile3.lastModified == targetMod) // append a newline, altering checksum, verify recompile val writer = new FileWriter(sourceFile) writer.write("//a comment\n2\n") writer.close val res4: Int = e(sourceFile) - res4 mustEqual 2 + assert(res4 == 2) // and make sure it created a new file val targetFile4 = new File(targetFileName) - targetFile4.exists must beFalse + assert(!targetFile4.exists) } "apply(InputStream)" in { - (new Eval).apply[Int](getClass.getResourceAsStream("/OnePlusOne.scala")) mustEqual 2 + assert((new Eval).apply[Int](getClass.getResourceAsStream("/OnePlusOne.scala")) == 2) } "uses deprecated" in { val deprecated = (new Eval).apply[() => String]( TempFile.fromResourcePath("/Deprecated.scala")) - deprecated() mustEqual "hello" + assert(deprecated() == "hello") } "inPlace('expression')" in { // Old object API works Eval.compile("object Doubler { def apply(n: Int) = n * 2 }") - Eval.inPlace[Int]("Doubler(2)") mustEqual 4 - Eval.inPlace[Int]("Doubler(14)") mustEqual 28 + assert(Eval.inPlace[Int]("Doubler(2)") == 4) + assert(Eval.inPlace[Int]("Doubler(14)") == 28) // New class API fails // val eval = new Eval // eval.compile("object Doubler { def apply(n: Int) = n * 2 }") - // eval.inPlace[Int]("Doubler(2)") mustEqual 4 - // eval.inPlace[Int]("Doubler(14)") mustEqual 28 + // assert(eval.inPlace[Int]("Doubler(2)") === 4) + // assert(eval.inPlace[Int]("Doubler(14)") === 28) } "check" in { - (new Eval).check("23") mustEqual () - (new Eval).check("invalid") must throwA[Eval.CompilerException] + (new Eval).check("23") + intercept[Eval.CompilerException] { + (new Eval).check("invalid") + } } "#include" in { val derived = Eval[() => String]( TempFile.fromResourcePath("/Base.scala"), TempFile.fromResourcePath("/DerivedWithInclude.scala")) - derived() mustEqual "hello" - derived.toString mustEqual "hello, joe" + assert(derived() == "hello") + assert(derived.toString == "hello, joe") } "recursive #include" in { val derived = Eval[() => String]( TempFile.fromResourcePath("/Base.scala"), TempFile.fromResourcePath("/IncludeInclude.scala")) - derived() mustEqual "hello" - derived.toString mustEqual "hello, joe; hello, joe" + assert(derived() == "hello") + assert(derived.toString == "hello, joe; hello, joe") } "toSource returns post-processed code" in { val derived = Eval.toSource(TempFile.fromResourcePath("/DerivedWithInclude.scala")) - derived must include("hello, joe") - derived must include("new Base") + assert(derived.contains("hello, joe")) + assert(derived.contains("new Base")) } "throws a compilation error when Ruby is #included" in { - Eval[() => String]( - TempFile.fromResourcePath("RubyInclude.scala")) must throwA[Throwable] + intercept[Throwable] { + Eval[() => String]( + TempFile.fromResourcePath("RubyInclude.scala") + ) + } } "clean class names" in { val e = new Eval() // regular old scala file - e.fileToClassName(new File("foo.scala")) mustEqual "foo" + assert(e.fileToClassName(new File("foo.scala")) == "foo") // without an extension - e.fileToClassName(new File("foo")) mustEqual "foo" + assert(e.fileToClassName(new File("foo")) == "foo") // with lots o dots - e.fileToClassName(new File("foo.bar.baz")) mustEqual "foo$2ebar" + assert(e.fileToClassName(new File("foo.bar.baz")) == "foo$2ebar") // with dashes - e.fileToClassName(new File("foo-bar-baz.scala")) mustEqual "foo$2dbar$2dbaz" + assert(e.fileToClassName(new File("foo-bar-baz.scala")) == "foo$2dbar$2dbaz") // with crazy things - e.fileToClassName(new File("foo$! -@@@")) mustEqual "foo$24$21$20$2d$40$40$40" + assert(e.fileToClassName(new File("foo$! -@@@")) == "foo$24$21$20$2d$40$40$40") } } } diff --git a/libraries/eval/test/resources/OnePlusOne.scala b/libraries/eval/test/resources/OnePlusOne.scala index 1a5a117..8d2f097 100644 --- a/libraries/eval/test/resources/OnePlusOne.scala +++ b/libraries/eval/test/resources/OnePlusOne.scala @@ -1 +1 @@ -1 + 1
\ No newline at end of file +1 + 1 |