Skip to content

Add Scala.js handling of @Test annotations with arguments #7462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions compiler/src/dotty/tools/backend/sjs/JUnitBootstrappers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,26 @@ class JUnitBootstrappers extends MiniPhase {
val metadata = for (test <- tests) yield {
val name = Literal(Constant(test.name.toString))
val ignored = Literal(Constant(test.hasAnnotation(junitdefn.IgnoreAnnotClass)))
// TODO Handle @Test annotations with arguments
// val reifiedAnnot = New(mydefn.TestAnnotType, test.getAnnotation(mydefn.TestAnnotClass).get.arguments)
val testAnnot = test.getAnnotation(junitdefn.TestAnnotClass).get
if (testAnnot.arguments.nonEmpty)
ctx.error("@Test annotations with arguments are not yet supported in Scala.js for dotty", testAnnot.tree.sourcePos)
val reifiedAnnot = resolveConstructor(junitdefn.TestAnnotType, Nil)

val mappedArguments = testAnnot.arguments.flatMap{
// Since classOf[...] in annotations would not be transformed, grab the resulting class constant here
case NamedArg(expectedName: SimpleName, TypeApply(Ident(nme.classOf), fstArg :: _))
if expectedName.toString == "expected" => Some(clsOf(fstArg.tpe))
// The only other valid argument to @Test annotations is timeout
case NamedArg(timeoutName: TermName, timeoutLiteral: Literal)
if timeoutName.toString == "timeout" => Some(timeoutLiteral)
case other => {
val shownName = other match {
case NamedArg(name, _) => name.show(ctx)
case other => other.show(ctx)
}
ctx.error(s"$shownName is an unsupported argument for the JUnit @Test annotation in this position", other.sourcePos)
None
}
}

val reifiedAnnot = resolveConstructor(junitdefn.TestAnnotType, mappedArguments)
New(junitdefn.TestMetadataType, List(name, ignored, reifiedAnnot))
}
JavaSeqLiteral(metadata, TypeTree(junitdefn.TestMetadataType))
Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ object Build {
-- "NavigableSetTest.scala" -- "SetTest.scala" -- "SortedMapTest.scala" -- "SortedSetTest.scala" -- "TreeSetTest.scala")).get

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