-
Notifications
You must be signed in to change notification settings - Fork 118
/
RunSnippetTestDefinitions.scala
171 lines (160 loc) · 5.53 KB
/
RunSnippetTestDefinitions.scala
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
171
package scala.cli.integration
import com.eed3si9n.expecty.Expecty.expect
trait RunSnippetTestDefinitions { _: RunTestDefinitions =>
test("correctly run a script snippet") {
emptyInputs.fromRoot { root =>
val msg = "Hello world"
val quotation = TestUtil.argQuotationMark
val res =
os.proc(
TestUtil.cli,
"run",
"--script-snippet",
s"println($quotation$msg$quotation)",
extraOptions
)
.call(cwd = root)
expect(res.out.trim() == msg)
}
}
test("correctly run a scala snippet") {
emptyInputs.fromRoot { root =>
val msg = "Hello world"
val quotation = TestUtil.argQuotationMark
val res =
os.proc(
TestUtil.cli,
"run",
"--scala-snippet",
s"object Hello extends App { println($quotation$msg$quotation) }",
extraOptions
)
.call(cwd = root)
expect(res.out.trim() == msg)
}
}
test("correctly run a java snippet") {
emptyInputs.fromRoot { root =>
val quotation = TestUtil.argQuotationMark
val msg = "Hello world"
val res = os.proc(
TestUtil.cli,
"run",
"--java-snippet",
s"public class Main { public static void main(String[] args) { System.out.println($quotation$msg$quotation); } }",
extraOptions
)
.call(cwd = root)
expect(res.out.trim() == msg)
}
}
test("correctly run a markdown snippet") {
emptyInputs.fromRoot { root =>
val msg = "Hello world"
val quotation = TestUtil.argQuotationMark
val res =
os.proc(
TestUtil.cli,
"run",
"--markdown-snippet",
s"""# A Markdown snippet
|With some scala code
|```scala
|println($quotation$msg$quotation)
|```""".stripMargin,
extraOptions
)
.call(cwd = root)
expect(res.out.trim() == msg)
}
}
test("correctly run multiple snippets") {
emptyInputs.fromRoot { root =>
val quotation = TestUtil.argQuotationMark
val scriptSnippetOption = "--script-snippet"
val scriptMessages @ Seq(scriptMsg1, scriptMsg2, scriptMsg3) =
Seq("hello script 1", "hello script 2", "hello script 3")
val script0 =
s"""def printAll(): Unit = println(
| Seq(
| snippet1.scriptMsg1, snippet2.scriptMsg2, snippet3.scriptMsg3,
| ScalaSnippet1.msg, ScalaSnippet2.msg, ScalaSnippet3.msg,
| JavaSnippet1.msg, JavaSnippet2.msg, JavaSnippet3.msg
| ).mkString($quotation;$quotation)
|)""".stripMargin
val script1 = s"def scriptMsg1: String = $quotation$scriptMsg1$quotation"
val script2 = s"def scriptMsg2: String = $quotation$scriptMsg2$quotation"
val script3 = s"def scriptMsg3: String = $quotation$scriptMsg3$quotation"
val scalaSnippetOption = "--scala-snippet"
val scalaMessages @ Seq(scalaMsg1, scalaMsg2, scalaMsg3) =
Seq("hello scala 1", "hello scala 2", "hello scala 3")
val scala0 = "object SnippetMain extends App { snippet.printAll() }"
val scala1 = s"object ScalaSnippet1 { def msg: String = $quotation$scalaMsg1$quotation }"
val scala2 = s"object ScalaSnippet2 { def msg: String = $quotation$scalaMsg2$quotation }"
val scala3 = s"object ScalaSnippet3 { def msg: String = $quotation$scalaMsg3$quotation }"
val javaSnippetOption = "--java-snippet"
val javaMessages @ Seq(javaMsg1, javaMsg2, javaMsg3) =
Seq("hello scala 1", "hello scala 2", "hello scala 3")
val java1 =
s"public class JavaSnippet1 { public static String msg = $quotation$javaMsg1$quotation; }"
val java2 =
s"public class JavaSnippet2 { public static String msg = $quotation$javaMsg2$quotation; }"
val java3 =
s"public class JavaSnippet3 { public static String msg = $quotation$javaMsg3$quotation; }"
val expectedOutput = (scriptMessages ++ scalaMessages ++ javaMessages).mkString(";")
val res = os.proc(
TestUtil.cli,
"run",
scriptSnippetOption,
script0,
scriptSnippetOption,
script1,
scriptSnippetOption,
script2,
scriptSnippetOption,
script3,
scalaSnippetOption,
scala0,
scalaSnippetOption,
scala1,
scalaSnippetOption,
scala2,
scalaSnippetOption,
scala3,
javaSnippetOption,
java1,
javaSnippetOption,
java2,
javaSnippetOption,
java3,
extraOptions
)
.call(cwd = root)
expect(res.out.trim() == expectedOutput)
}
}
test("running a script snippet should not create the workspace dir in cwd") {
emptyInputs.fromRoot { root =>
val quotation = TestUtil.argQuotationMark
val msg = "Hello"
os.proc(TestUtil.cli, "-e", s"println($quotation$msg$quotation)", extraOptions)
.call(cwd = root)
.out.trim()
expect(!os.exists(root / Constants.workspaceDirName))
}
}
test("running a script snippet with one source file should create the workspace dir in cwd") {
val inputs = TestInputs(
os.rel / "Hello.scala" ->
s"""object Hello {
| val hello = "Hello World"
|}""".stripMargin
)
inputs.fromRoot { root =>
os.proc(TestUtil.cli, "-e", s"println(Hello.hello)", ".", extraOptions)
.call(cwd = root)
.out.trim()
expect(os.exists(root / Constants.workspaceDirName))
}
}
}