|
1 | 1 | package scala.cli.integration
|
2 | 2 |
|
3 |
| -class CompileTests213 extends CompileTestDefinitions with Test213 |
| 3 | +import scala.util.Properties |
| 4 | + |
| 5 | +class CompileTests213 extends CompileTestDefinitions with Test213 { |
| 6 | + |
| 7 | + test("test-macro-output") { |
| 8 | + val triple = "\"\"\"" |
| 9 | + TestInputs( |
| 10 | + os.rel / "Main.scala" -> |
| 11 | + s"""|//> using scala ${Constants.scala213} |
| 12 | + |//> using dep org.scala-lang:scala-reflect:${Constants.scala213} |
| 13 | + |package example |
| 14 | + |import scala.reflect.macros.blackbox |
| 15 | + |import scala.language.experimental.macros |
| 16 | + | |
| 17 | + |object Scala2Example { |
| 18 | + | def macroMethod[A](a: A): String = |
| 19 | + | macro Scala2Example.macroMethodImpl[A] |
| 20 | + | |
| 21 | + | def macroMethodImpl[A: c.WeakTypeTag]( |
| 22 | + | c: blackbox.Context |
| 23 | + | )(a: c.Expr[A]): c.Expr[String] = { |
| 24 | + | import c.universe._ |
| 25 | + | val output = s$triple$${show(a.tree)} |
| 26 | + | |$${showCode(a.tree)} |
| 27 | + | |$${showRaw(a.tree)} |
| 28 | + | |$${weakTypeTag[A]} |
| 29 | + | |$${weakTypeOf[A]} |
| 30 | + | |$${showRaw(weakTypeOf[A])}$triple.stripMargin |
| 31 | + | c.echo(c.enclosingPosition, output) |
| 32 | + | c.warning(c.enclosingPosition, "example error message") |
| 33 | + | c.abort(c.enclosingPosition, "example error message") |
| 34 | + | } |
| 35 | + |} |
| 36 | + |""".stripMargin, |
| 37 | + os.rel / "Test.test.scala" -> |
| 38 | + """|//> using test.dep org.scalameta::munit::1.0.0 |
| 39 | + |package example |
| 40 | + | |
| 41 | + |class Tests extends munit.FunSuite { |
| 42 | + | test("macro works OK") { |
| 43 | + | Scala2Example.macroMethod(1 -> "test") |
| 44 | + | } |
| 45 | + |}""".stripMargin |
| 46 | + ).fromRoot { root => |
| 47 | + val result = os.proc(TestUtil.cli, "test", ".").call( |
| 48 | + cwd = root, |
| 49 | + check = false, |
| 50 | + // stdout = ProcessOutput.Readlines{ str => stringBuffer.append(str)}, |
| 51 | + mergeErrIntoOut = true |
| 52 | + ) |
| 53 | + val separator = if (Properties.isWin) "\\" else "/" |
| 54 | + |
| 55 | + val expectedOutput = |
| 56 | + s"""|Compiling project (Scala ${Constants.scala213}, JVM (17)) |
| 57 | + |Compiled project (Scala ${Constants.scala213}, JVM (17)) |
| 58 | + |Compiling project (test, Scala ${Constants.scala213}, JVM (17)) |
| 59 | + |[info] .${separator}Test.test.scala:6:5 |
| 60 | + |[info] scala.Predef.ArrowAssoc[Int](1).->[String]("test") |
| 61 | + |[info] scala.Predef.ArrowAssoc[Int](1).->[String]("test") |
| 62 | + |[info] Apply(TypeApply(Select(Apply(TypeApply(Select(Select(Ident(scala), scala.Predef), TermName("ArrowAssoc")), List(TypeTree())), List(Literal(Constant(1)))), TermName("$$minus$$greater")), List(TypeTree())), List(Literal(Constant("test")))) |
| 63 | + |[info] WeakTypeTag[(Int, String)] |
| 64 | + |[info] (Int, String) |
| 65 | + |[info] TypeRef(ThisType(scala), scala.Tuple2, List(TypeRef(ThisType(scala), scala.Int, List()), TypeRef(ThisType(java.lang), java.lang.String, List()))) |
| 66 | + |[info] Scala2Example.macroMethod(1 -> "test") |
| 67 | + |[info] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 68 | + |[error] .${separator}Test.test.scala:6:5 |
| 69 | + |[error] example error message |
| 70 | + |[error] Scala2Example.macroMethod(1 -> "test") |
| 71 | + |[error] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 | + |Error compiling project (test, Scala ${Constants.scala213}, JVM (17)) |
| 73 | + |Compilation failed |
| 74 | + |""".stripMargin |
| 75 | + |
| 76 | + assertNoDiff( |
| 77 | + result.toString.trim().linesIterator.filterNot { str => |
| 78 | + // these lines are not stable and can easily change |
| 79 | + val shouldNotContain = Set("Starting compilation server", "hint", "Download", "Result of") |
| 80 | + shouldNotContain.exists(str.contains) |
| 81 | + }.mkString("\n"), |
| 82 | + expectedOutput |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments