Skip to content

Commit 5c76629

Browse files
committed
Implemented shouldNot compile macro, not working yet for stripMargin version.
1 parent 2561e18 commit 5c76629

File tree

3 files changed

+55
-45
lines changed

3 files changed

+55
-45
lines changed

project/GenScalaTestDotty.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ object GenScalaTestDotty {
177177
copyFiles("scalatest-test/src/test/scala/org/scalatest", "org/scalatest", targetDir,
178178
List(
179179
"AssertionsSpec.scala",
180-
"ShouldCompileSpec.scala"
180+
"ShouldCompileSpec.scala",
181+
"ShouldNotCompileSpec.scala"
181182
)
182183
) /*++
183184
copyDir("scalatest-test/src/test/scala/org/scalatest/concurrent", "org/scalatest/concurrent", targetDir,

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,47 @@ class ShouldNotCompileSpec extends FunSpec {
6666
}
6767
}
6868

69+
describe("when work with triple double quotes string literal") {
70+
71+
it("should do nothing when type check failed") {
72+
"""val a: String = 2""" shouldNot compile
73+
}
74+
75+
it("should throw TestFailedException with correct message and stack depth when type check passed") {
76+
val e = intercept[TestFailedException] {
77+
"""val a = 1""" shouldNot compile
78+
}
79+
assert(e.message == Some(Resources.expectedCompileErrorButGotNone("val a = 1")))
80+
assert(e.failedCodeFileName === (Some(fileName)))
81+
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 4)))
82+
}
83+
84+
it("should do nothing when parse failed") {
85+
"""println("test)""" shouldNot compile
86+
}
87+
88+
it("should do nothing when used with 'val i: Int = null") {
89+
"""val i: Int = null""" shouldNot compile
90+
}
91+
92+
it("should work correctly with the implicit view is in scope") {
93+
import scala.collection.JavaConverters._
94+
95+
val arrayList: java.util.ArrayList[String] = new java.util.ArrayList[String]()
96+
97+
arrayList.add("Foo")
98+
arrayList.add("Bar")
99+
100+
val e = intercept[TestFailedException] {
101+
"""arrayList.asScala""" shouldNot compile
102+
}
103+
assert(e.message == Some(Resources.expectedCompileErrorButGotNone("arrayList.asScala")))
104+
assert(e.failedCodeFileName === (Some(fileName)))
105+
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 4)))
106+
}
107+
}
108+
109+
// SKIP-DOTTY-START
69110
describe("when work with triple quotes string literal with stripMargin") {
70111

71112
it("should do nothing when type check failed") {
@@ -115,6 +156,7 @@ class ShouldNotCompileSpec extends FunSpec {
115156
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 4)))
116157
}
117158
}
159+
// SKIP-DOTTY-END
118160

119161
}
120162

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

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -176,28 +176,6 @@ object CompileMacro {
176176
}
177177

178178
self.unseal.underlyingArgument match {
179-
/*case Apply(
180-
Apply(
181-
Select(
182-
Apply(
183-
Apply(
184-
_,
185-
List(
186-
Literal(
187-
code
188-
)
189-
)
190-
),
191-
_
192-
),
193-
shouldOrMustTermName
194-
),
195-
_
196-
),
197-
_
198-
) if shouldOrMustTermName == shouldOrMust =>
199-
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
200-
checkNotCompile(code.toString)*/
201179

202180
case Apply(
203181
Apply(
@@ -211,6 +189,17 @@ object CompileMacro {
211189
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
212190
checkNotCompile(code.toString)
213191

192+
case Apply(
193+
Apply(
194+
Ident(shouldOrMustTerconvertToStringShouldOrMustWrapperTermName),
195+
List(
196+
Literal(String(code))
197+
)
198+
),
199+
_
200+
) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" =>
201+
checkNotCompile(code.toString)
202+
214203
case other =>
215204
throw QuoteError("The '" + shouldOrMust + " compile' syntax only works with String literals.")
216205
}
@@ -289,28 +278,6 @@ object CompileMacro {
289278
}
290279

291280
self.unseal.underlyingArgument match {
292-
/*case Apply(
293-
Apply(
294-
Select(
295-
Apply(
296-
Apply(
297-
_,
298-
List(
299-
Literal(
300-
String(code)
301-
)
302-
)
303-
),
304-
_
305-
),
306-
shouldOrMustTermName
307-
),
308-
_
309-
),
310-
_
311-
) if shouldOrMustTermName == shouldOrMust =>
312-
// LHS is a normal string literal, call checkCompile with the extracted code string to generate code
313-
checkCompile(code.toString)*/
314281

315282
case Apply(
316283
Apply(

0 commit comments

Comments
 (0)