aboutsummaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorGeorge Leontiev <folone@gmail.com>2014-07-01 21:40:50 +0000
committerChristopher Vogt <oss.nsp@cvogt.org>2016-11-07 02:08:39 -0500
commit28a6f3c76ff38437daacc5c5ae4f5136c143637d (patch)
tree08a5a33398b9ff4d0fe444524420f5090d3f7c32 /libraries
parent2fb5170362493cce98b1818a7ce847abb879bcd1 (diff)
downloadcbt-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')
-rw-r--r--libraries/eval/Eval.scala12
-rw-r--r--libraries/eval/test/EvalTest.scala86
-rw-r--r--libraries/eval/test/resources/OnePlusOne.scala2
3 files changed, 56 insertions, 44 deletions
diff --git a/libraries/eval/Eval.scala b/libraries/eval/Eval.scala
index b65e0b6..e46fa6e 100644
--- a/libraries/eval/Eval.scala
+++ b/libraries/eval/Eval.scala
@@ -16,27 +16,29 @@
package com.twitter.util
-import com.twitter.io.StreamIO
-import com.twitter.conversions.string._
import java.io._
import java.math.BigInteger
import java.net.URLClassLoader
import java.security.MessageDigest
import java.util.Random
import java.util.jar.JarFile
+
import scala.collection.mutable
import scala.io.Source
-import scala.tools.nsc.{Global, Settings}
import scala.tools.nsc.interpreter.AbstractFileClassLoader
import scala.tools.nsc.io.{AbstractFile, VirtualDirectory}
import scala.tools.nsc.reporters.AbstractReporter
import scala.tools.nsc.util.{BatchSourceFile, Position}
+import scala.tools.nsc.{Global, Settings}
import scala.util.matching.Regex
+import com.twitter.conversions.string._
+import com.twitter.io.StreamIO
+
/**
* Evaluate a file or string and return the result.
*/
-@deprecated("use a throw-away instance of Eval instead")
+@deprecated("use a throw-away instance of Eval instead", "1.8.1")
object Eval extends Eval {
private val jvmId = java.lang.Math.abs(new Random().nextInt())
val classCleaner: Regex = "\\W".r
@@ -301,7 +303,7 @@ class Eval(target: Option[File]) {
/*
* For a given FQ classname, trick the resource finder into telling us the containing jar.
*/
- private def classPathOfClass(className: String) = try {
+ private def classPathOfClass(className: String) = {
val resource = className.split('.').mkString("/", "/", ".class")
val path = getClass.getResource(resource).getPath
if (path.indexOf("file:") >= 0) {
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