1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
package com.twitter.util
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
@RunWith(classOf[JUnitRunner])
class EvalTest extends WordSpec {
"Evaluator" should {
"apply('expression')" in {
assert((new Eval).apply[Int]("1 + 1") == 2)
}
"apply(new File(...))" in {
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"))
assert(derived() == "hello")
}
"apply(new File(...) with a dash in the name with target" in {
val f = File.createTempFile("eval", "target")
f.delete()
f.mkdir()
val e = new Eval(Some(f))
val sourceFile = TempFile.fromResourcePath("/file-with-dash.scala")
val res: String = e(sourceFile)
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)
assert(targetFile.exists)
}
"apply(new File(...) with target" in {
val f = File.createTempFile("eval", "target")
f.delete()
f.mkdir()
val e = new Eval(Some(f))
val sourceFile = TempFile.fromResourcePath("/OnePlusOne.scala")
val res: Int = e(sourceFile)
assert(res == 2)
// make sure it created a class file with the expected name
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)
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)
assert(f.listFiles.length == 2)
// and make sure it didn't update the file
val targetFile2 = new File(targetFileName)
assert(targetFile2.lastModified == targetMod)
// touch source, ensure no-recompile (checksum hasn't changed)
sourceFile.setLastModified(System.currentTimeMillis())
val res3: Int = e(sourceFile)
assert(res3 == 2)
// and make sure it didn't create a different file
assert(f.listFiles.length == 2)
// and make sure it updated the file
val targetFile3 = new File(targetFileName)
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)
assert(res4 == 2)
// and make sure it created a new file
val targetFile4 = new File(targetFileName)
assert(!targetFile4.exists)
}
"apply(InputStream)" in {
assert((new Eval).apply[Int](getClass.getResourceAsStream("/OnePlusOne.scala")) == 2)
}
"uses deprecated" in {
val deprecated = (new Eval).apply[() => String](
TempFile.fromResourcePath("/Deprecated.scala"))
assert(deprecated() == "hello")
}
"inPlace('expression')" in {
// Old object API works
Eval.compile("object Doubler { def apply(n: Int) = n * 2 }")
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 }")
// assert(eval.inPlace[Int]("Doubler(2)") === 4)
// assert(eval.inPlace[Int]("Doubler(14)") === 28)
}
"check" in {
(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"))
assert(derived() == "hello")
assert(derived.toString == "hello, joe")
}
"recursive #include" in {
val derived = Eval[() => String](
TempFile.fromResourcePath("/Base.scala"),
TempFile.fromResourcePath("/IncludeInclude.scala"))
assert(derived() == "hello")
assert(derived.toString == "hello, joe; hello, joe")
}
"toSource returns post-processed code" in {
val derived = Eval.toSource(TempFile.fromResourcePath("/DerivedWithInclude.scala"))
assert(derived.contains("hello, joe"))
assert(derived.contains("new Base"))
}
"throws a compilation error when Ruby is #included" in {
intercept[Throwable] {
Eval[() => String](
TempFile.fromResourcePath("RubyInclude.scala")
)
}
}
"clean class names" in {
val e = new Eval()
// regular old scala file
assert(e.fileToClassName(new File("foo.scala")) == "foo")
// without an extension
assert(e.fileToClassName(new File("foo")) == "foo")
// with lots o dots
assert(e.fileToClassName(new File("foo.bar.baz")) == "foo$2ebar")
// with dashes
assert(e.fileToClassName(new File("foo-bar-baz.scala")) == "foo$2dbar$2dbaz")
// with crazy things
assert(e.fileToClassName(new File("foo$! -@@@")) == "foo$24$21$20$2d$40$40$40")
}
}
}
|