Skip to content

Commit 78e9a02

Browse files
committed
Add Scala.js handling of @test annotations with arguments
Partial fix for lampepfl#7113 To be able to use the `expected` and `timeout` annotation arguments in JUnit @test annotations, add handling for these arguments by supplying them as arguments to the resolved constructor for the JUnit test class. Enable the JUnitAnnotationsParamTest, which depends on this functionality.
1 parent d2db509 commit 78e9a02

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

compiler/src/dotty/tools/backend/sjs/JUnitBootstrappers.scala

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,26 @@ class JUnitBootstrappers extends MiniPhase {
224224
val metadata = for (test <- tests) yield {
225225
val name = Literal(Constant(test.name.toString))
226226
val ignored = Literal(Constant(test.hasAnnotation(junitdefn.IgnoreAnnotClass)))
227-
// TODO Handle @Test annotations with arguments
228-
// val reifiedAnnot = New(mydefn.TestAnnotType, test.getAnnotation(mydefn.TestAnnotClass).get.arguments)
229227
val testAnnot = test.getAnnotation(junitdefn.TestAnnotClass).get
230-
if (testAnnot.arguments.nonEmpty)
231-
ctx.error("@Test annotations with arguments are not yet supported in Scala.js for dotty", testAnnot.tree.sourcePos)
232-
val reifiedAnnot = resolveConstructor(junitdefn.TestAnnotType, Nil)
228+
229+
val mappedArguments = testAnnot.arguments.flatMap{
230+
// Since classOf[...] in annotations would not be transformed, grab the resulting class constant here
231+
case NamedArg(expectedName: SimpleName, TypeApply(Ident(nme.classOf), fstArg :: _))
232+
if expectedName.toString == "expected" => Some(clsOf(fstArg.tpe))
233+
// The only other valid argument to @Test annotations is timeout
234+
case NamedArg(timeoutName: TermName, timeoutLiteral: Literal)
235+
if timeoutName.toString == "timeout" => Some(timeoutLiteral)
236+
case other => {
237+
val shownName = other match {
238+
case NamedArg(name, _) => name.show(ctx)
239+
case other => other.show(ctx)
240+
}
241+
ctx.error(s"$shownName is an unsupported argument for the JUnit @Test annotation in this position", other.sourcePos)
242+
None
243+
}
244+
}
245+
246+
val reifiedAnnot = resolveConstructor(junitdefn.TestAnnotType, mappedArguments)
233247
New(junitdefn.TestMetadataType, List(name, ignored, reifiedAnnot))
234248
}
235249
JavaSeqLiteral(metadata, TypeTree(junitdefn.TestMetadataType))

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ object Build {
975975
-- "NavigableSetTest.scala" -- "SetTest.scala" -- "SortedMapTest.scala" -- "SortedSetTest.scala" -- "TreeSetTest.scala")).get
976976

977977
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/utils" ** "*.scala").get
978-
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/junit" ** (("*.scala": FileFilter) -- "JUnitAnnotationsParamTest.scala")).get
978+
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/junit" ** (("*.scala": FileFilter))).get
979979
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/niobuffer" ** (("*.scala": FileFilter) -- "ByteBufferTest.scala")).get
980980
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/niocharset" ** (("*.scala": FileFilter) -- "BaseCharsetTest.scala" -- "Latin1Test.scala" -- "USASCIITest.scala" -- "UTF16Test.scala" -- "UTF8Test.scala")).get
981981
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/scalalib" ** (("*.scala": FileFilter) -- "ArrayBuilderTest.scala" -- "ClassTagTest.scala" -- "EnumerationTest.scala" -- "RangesTest.scala" -- "SymbolTest.scala")).get

0 commit comments

Comments
 (0)