package scala.scalajs.testsuite.compiler import org.scalajs.jasminetest.{JasmineTest, JasmineTestFramework} /** The test counter */ private[testsuite] object TC { var testNum: Int = _ def is(x: Int): Boolean = testNum == x } /** Exception to test source maps. Not a ControlThrowable, because it has * NoStackTrace which would defeat its purpose */ case class TestException(lineNo: Int) extends Exception /** * Template to generate source-map tests, verifying that the line numbers * reported in the source-mapped stacktraces match up with the line number * that the error originated from. * * /two-star/s in this file are replaced with a code-snippet to throw * an exception if `testNum` is set to the /two-star/'s index. The * exception is then caught and its stacktrace checked to see * that it reports the line number expected (stored in the error * message). /three-star/s in are replaced with a dangling else (to * allow throwing in expression position). * `0/**/` is replaced by the number of /n-star/s in the * file. */ object SourceMapTest extends JasmineTest { val testCount: Int = 0/**/ if (JasmineTestFramework.hasTag("nodejs")) { scalajs.js.Dynamic.global.require("source-map-support").install() } when("source-maps"). describe("Source Maps") { for (i <- 0 until testCount) { it(s"work (test $i)") { TC.testNum = i try { run() sys.error("No exception thrown") } catch { case e @ TestException(lineNo) => val trace0 = e.getStackTrace.toList val trace1 = trace0.dropWhile( _.getFileName.endsWith("/scala/scalajs/runtime/StackTrace.scala")) val trace2 = trace1.dropWhile( _.getFileName.endsWith("/java/lang/Throwables.scala")) val exSte :: throwSte :: _ = trace2 expect(exSte.getFileName).toContain("/SourceMapTest.scala") // line where `case class TestException is written` above expect(exSte.getLineNumber).toBe(15) expect(throwSte.getFileName).toContain("/SourceMapTest.scala") expect(throwSte.getLineNumber).toBe(lineNo) } } } } def get(json: JsValue, index: Int) = { /**/ /**/json.asInstanceOf[JsArray].value(index).value } def get(json: JsValue, index: Int, fieldName: String) = { /**/ /**//***/json.asInstanceOf[JsArray].value(index).asInstanceOf[JsObject].value(fieldName).value } def run() = { /**/ /**/val ugly = """ |[ | "JSON Test Pattern pass1", | {"object with 1 member":["array with 1 element"]}, | {}, | [], | -42, | true, | false, | null, | { | "integer": 1234567890, | "real": -9876.543210, | "e": 0.123456789e-12, | "E": 1.234567890E+34, | "": 23456789012E66, | "zero": 0, | "one": 1, | "space": " ", | "quote": "\"", | "backslash": "\\", | "controls": "\b\f\n\r\t", | "slash": "/ & \/", | "alpha": "abcdefghijklmnopqrstuvwyz", | "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", | "digit": "0123456789", | "0123456789": "digit", | "special": "`1~!@#$%^&*()_+-={':[,]}|;.?", | "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", | "true": true, | "false": false, | "null": null, | "array":[ ], | "object":{ }, | "address": "50 St. James Street", | "url": "http://www.JSON.org/", | "comment": "// /* */": " ", | " s p a c e d " :[1,2 , 3 | |, | |4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], | "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", | "quotes": "" \u005Cu0022 %22 0x22 034 "", | "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" |: "A key can be any string" | }, | 0.5 ,98.6 |, |99.44 |, | |1066, |1e1, |0.1e1, |1e-1, |1e00,2e+00,2e-00 |,"rosebud"] """.stripMargin/**/ /**/val json = new Json()/**/ /**/val parsed = json.read(ugly)/**/ /**/ /**/val unparsed = json.write(parsed)/**/ /**/val reparsed = json.read(unparsed)/**/ for (json <- Seq(parsed, reparsed)){/**/ assert(get(json, 0) == "JSON Test Pattern pass1") /**/ assert(get(json, 5) == true) assert(get(json, 6) == false) assert(get(json, 8, "real") == "-9876.543210")/**/ /**/assert(get(json, 8, "comment") == "// /*