Skip to content

Commit f1a6cbf

Browse files
committed
Making progress in CompileMacro for Dotty.
1 parent 1f91854 commit f1a6cbf

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

project/GenScalaTestDotty.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ object GenScalaTestDotty {
176176
def genTest(targetDir: File, version: String, scalaVersion: String): Seq[File] = {
177177
copyFiles("scalatest-test/src/test/scala/org/scalatest", "org/scalatest", targetDir,
178178
List(
179-
"AssertionsSpec.scala"
179+
"AssertionsSpec.scala",
180+
"ShouldCompileSpec.scala"
180181
)
181182
) /*++
182183
copyDir("scalatest-test/src/test/scala/org/scalatest/concurrent", "org/scalatest/concurrent", targetDir,

scalatest-test/src/test/scala/org/scalatest/ShouldCompileSpec.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ShouldCompileSpec extends FunSpec {
4242
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 6)))
4343
}
4444

45+
// SKIP-DOTTY-START
4546
it("should throw TestFailedException with correct message and stack depth when parse failed") {
4647
val e = intercept[TestFailedException] {
4748
"println(\"test)" should compile
@@ -52,9 +53,43 @@ class ShouldCompileSpec extends FunSpec {
5253
assert(e.failedCodeFileName === (Some(fileName)))
5354
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 6)))
5455
}
56+
// SKIP-DOTTY-END
5557

5658
}
5759

60+
describe("when work with string literal with triple double quotes") {
61+
62+
it("should do nothing when type check passed") {
63+
"""val a = 1""" should compile
64+
}
65+
66+
it("should throw TestFailedException with correct message and stack depth when type check failed") {
67+
val e = intercept[TestFailedException] {
68+
"""val a: String = 2""" should compile
69+
}
70+
val errMsg = Resources.expectedNoErrorButGotTypeError("", "")
71+
assert(e.message.get.startsWith(errMsg.substring(0, errMsg.indexOf(':'))))
72+
assert(e.message.get.indexOf("val a: String = 2") >= 0)
73+
assert(e.failedCodeFileName === (Some(fileName)))
74+
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 6)))
75+
}
76+
77+
// SKIP-DOTTY-START
78+
it("should throw TestFailedException with correct message and stack depth when parse failed") {
79+
val e = intercept[TestFailedException] {
80+
"""println(\"test)""" should compile
81+
}
82+
val errMsg = Resources.expectedNoErrorButGotParseError("", "")
83+
assert(e.message.get.startsWith(errMsg.substring(0, errMsg.indexOf(':'))))
84+
assert(e.message.get.indexOf("println(\"test)") >= 0)
85+
assert(e.failedCodeFileName === (Some(fileName)))
86+
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 6)))
87+
}
88+
// SKIP-DOTTY-END
89+
90+
}
91+
92+
// SKIP-DOTTY-START
5893
describe("when work with triple quotes string literal with stripMargin") {
5994

6095
it("should do nothing when type check passed") {
@@ -89,5 +124,6 @@ class ShouldCompileSpec extends FunSpec {
89124
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 6)))
90125
}
91126
}
127+
// SKIP-DOTTY-END
92128
}
93129
}

scalatest.dotty/src/main/scala/org/scalatest/CompileMacro.scala

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@ object CompileMacro {
176176
}
177177

178178
self.unseal.underlyingArgument match {
179-
case Apply(
179+
/*case Apply(
180180
Apply(
181181
Select(
182182
Apply(
183183
Apply(
184184
_,
185185
List(
186186
Literal(
187-
String(code)
187+
code
188188
)
189189
)
190190
),
@@ -197,7 +197,20 @@ object CompileMacro {
197197
_
198198
) if shouldOrMustTermName == shouldOrMust =>
199199
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
200+
checkNotCompile(code.toString)*/
201+
202+
case Apply(
203+
Apply(
204+
Select(_, shouldOrMustTerconvertToStringShouldOrMustWrapperTermName),
205+
List(
206+
Literal(code)
207+
)
208+
),
209+
_
210+
) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" =>
211+
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
200212
checkNotCompile(code.toString)
213+
201214
case other =>
202215
throw QuoteError("The '" + shouldOrMust + " compile' syntax only works with String literals.")
203216
}
@@ -276,7 +289,7 @@ object CompileMacro {
276289
}
277290

278291
self.unseal.underlyingArgument match {
279-
case Apply(
292+
/*case Apply(
280293
Apply(
281294
Select(
282295
Apply(
@@ -297,8 +310,34 @@ object CompileMacro {
297310
_
298311
) if shouldOrMustTermName == shouldOrMust =>
299312
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
313+
checkCompile(code.toString)*/
314+
315+
case Apply(
316+
Apply(
317+
Select(_, shouldOrMustTerconvertToStringShouldOrMustWrapperTermName),
318+
List(
319+
Literal(String(code))
320+
)
321+
),
322+
_
323+
) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" =>
324+
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
300325
checkCompile(code.toString)
326+
327+
case Apply(
328+
Apply(
329+
Ident(shouldOrMustTerconvertToStringShouldOrMustWrapperTermName),
330+
List(
331+
Literal(String(code))
332+
)
333+
),
334+
_
335+
) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" =>
336+
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
337+
checkCompile(code.toString)
338+
301339
case other =>
340+
println("###other: " + other)
302341
throw QuoteError("The '" + shouldOrMust + " compile' syntax only works with String literals.")
303342
}
304343
}

0 commit comments

Comments
 (0)