Skip to content

Commit d49b1a7

Browse files
committed
Partial fix for lampepfl#7113: Add Scala.js handling of @test annotations with arguments
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 cdfd672 commit d49b1a7

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,16 @@ 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.collect{
230+
// Since classOf[...] in annotations would not be transformed, grab the resulting class constant here
231+
case NamedArg(expectedName, typeApply @ TypeApply(classOfIdent, fstArg :: _)) => clsOf(fstArg.tpe)
232+
// The only other valid argument to @Test annotations is timeout
233+
case NamedArg(timeoutName, timeoutLiteral) => timeoutLiteral
234+
}
235+
236+
val reifiedAnnot = resolveConstructor(junitdefn.TestAnnotType, mappedArguments)
233237
New(junitdefn.TestMetadataType, List(name, ignored, reifiedAnnot))
234238
}
235239
JavaSeqLiteral(metadata, TypeTree(junitdefn.TestMetadataType))

project/Build.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ object Build {
957957
val dir = fetchScalaJSSource.value / "test-suite"
958958
(
959959
(dir / "shared/src/test/scala/org/scalajs/testsuite/compiler" ** (("*.scala":FileFilter) -- "RegressionTest.scala" -- "ReflectiveCallTest.scala")).get
960+
960961
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/javalib/lang" ** (("*.scala": FileFilter) -- "StringTest.scala")).get
961962
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/javalib/io" ** (("*.scala": FileFilter) -- "ByteArrayInputStreamTest.scala" -- "ByteArrayOutputStreamTest.scala" -- "DataInputStreamTest.scala" -- "DataOutputStreamTest.scala" -- "InputStreamTest.scala" -- "OutputStreamWriterTest.scala" -- "PrintStreamTest.scala" -- "CommonStreamsTests.scala")).get
962963
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/javalib/math" ** "*.scala").get
@@ -975,7 +976,7 @@ object Build {
975976
-- "NavigableSetTest.scala" -- "SetTest.scala" -- "SortedMapTest.scala" -- "SortedSetTest.scala" -- "TreeSetTest.scala")).get
976977

977978
++ (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
979+
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/junit" ** (("*.scala": FileFilter))).get
979980
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/niobuffer" ** (("*.scala": FileFilter) -- "ByteBufferTest.scala")).get
980981
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/niocharset" ** (("*.scala": FileFilter) -- "BaseCharsetTest.scala" -- "Latin1Test.scala" -- "USASCIITest.scala" -- "UTF16Test.scala" -- "UTF8Test.scala")).get
981982
++ (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)