Skip to content

Commit ae19672

Browse files
committed
Changed to use parser combinator for parsing JSON in test, fixing link problem when running scalatest tests in scala-js. Please note that for scala-js 2.13.0-M2 to work we'll need to wait until parser combinator 1.0.7 is released, as 1.0.6 jar is empty and expected to be fixed in 1.0.7 (scala/scala-parser-combinators#130).
1 parent f15205d commit ae19672

File tree

4 files changed

+615
-630
lines changed

4 files changed

+615
-630
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2001-2017 Artima, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.scalatest
17+
18+
import scala.util.parsing.combinator._
19+
20+
object JSON extends JavaTokenParsers {
21+
22+
def toJsVal(v: Any): JsVal =
23+
v match {
24+
case j: JsVal => j
25+
case m: Map[String, JsVal] => JsObj(m)
26+
case l: List[JsVal] => JsArr(l)
27+
case s: String => JsStr(s.take(s.length - 1).drop(1))
28+
case n: Double => JsNum(n)
29+
case false => JsFalse
30+
case true => JsTrue
31+
case null => JsNULL
32+
}
33+
34+
sealed trait JsVal extends Any {
35+
def value: Any
36+
def \(key: String): Option[JsVal] = this.asInstanceOf[JsObj].value.get(key)
37+
}
38+
case class JsStr(value: String) extends AnyVal with JsVal
39+
case class JsObj(value: Map[String, JsVal]) extends AnyVal with JsVal
40+
case class JsArr(value: List[JsVal]) extends AnyVal with JsVal
41+
case class JsNum(value: Double) extends AnyVal with JsVal
42+
case object JsFalse extends JsVal {
43+
def value = false
44+
}
45+
case object JsTrue extends JsVal {
46+
def value = true
47+
}
48+
case object JsNULL extends JsVal {
49+
def value = null
50+
}
51+
52+
def obj: Parser[JsObj] =
53+
"{" ~> repsep(member, ",") <~ "}" ^^ (v => JsObj(Map.empty[String, JsVal] ++ v))
54+
def arr: Parser[List[JsVal]] =
55+
"[" ~> repsep(value, ",") <~ "]" ^^ (v => v.map(toJsVal))
56+
def member: Parser[(String, JsVal)] =
57+
stringLiteral ~ ":" ~ value ^^
58+
{ case name ~ ":" ~ value => (name.take(name.length - 1).drop(1), toJsVal(value)) }
59+
def value: Parser[Any] = (
60+
obj
61+
| arr
62+
| stringLiteral
63+
| floatingPointNumber ^^ (_.toDouble)
64+
| "null" ^^ (x => null)
65+
| "true" ^^ (x => true)
66+
| "false" ^^ (x => false)
67+
)
68+
69+
def parseJson(jsStr: String) = parseAll(value, jsStr).get.asInstanceOf[JsVal]
70+
}

project/GenCommonTestJS.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ object GenCommonTestJS {
8080
"BookPropertyMatchers.scala",
8181
"EmptyMocks.scala",
8282
"FileMocks.scala",
83-
"StringFixture.scala"
83+
"StringFixture.scala",
84+
"JSON.scala"
8485
), targetDir) ++
8586
copyDir("common-test/src/main/scala/org/scalatest/path", "org/scalatest/path",
8687
List("ExampleLikeSpecs.scala"), targetDir)

project/scalatest.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ object ScalatestBuild extends Build {
179179
case Some((2, scalaMajor)) if scalaMajor >= 11 =>
180180
Seq(
181181
"org.scala-lang.modules" %% "scala-xml" % "1.0.6",
182-
//"org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.6", This is needed only by SbtCommandParser, but we are not support it currently.
183182
scalacheckDependency("optional")
184183
)
185184
case _ =>
@@ -222,8 +221,7 @@ object ScalatestBuild extends Build {
222221
Seq(
223222
"commons-io" % "commons-io" % "1.3.2" % "test",
224223
"org.eclipse.jetty" % "jetty-server" % "8.1.18.v20150929" % "test",
225-
"org.eclipse.jetty" % "jetty-webapp" % "8.1.18.v20150929" % "test",
226-
"io.spray" %% "spray-json" % "1.3.4" % "test"
224+
"org.eclipse.jetty" % "jetty-webapp" % "8.1.18.v20150929" % "test"
227225
)
228226

229227
def scalatestJSLibraryDependencies =
@@ -293,6 +291,7 @@ object ScalatestBuild extends Build {
293291
.settings(
294292
projectTitle := "Common test classes used by scalactic and scalatest",
295293
libraryDependencies += scalacheckDependency("optional"),
294+
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.6",
296295
libraryDependencies ++= crossBuildTestLibraryDependencies(scalaVersion.value)
297296
).dependsOn(scalacticMacro, LocalProject("scalatest"))
298297

@@ -301,6 +300,9 @@ object ScalatestBuild extends Build {
301300
.settings(
302301
projectTitle := "Common test classes used by scalactic.js and scalatest.js",
303302
libraryDependencies += scalacheckDependency("optional"),
303+
// Currently 1.0.6 for scala-js does not contain anything due to a reported bug: https://github.com/scala/scala-parser-combinators/issues/119
304+
// 1.0.5 works for scala 2.12 but not 2.13, we should use 1.0.7 once it is published which suppose to support scala 2.13.
305+
libraryDependencies += "org.scala-lang.modules" %%% "scala-parser-combinators" % "1.0.5",
304306
libraryDependencies ++= crossBuildTestLibraryDependencies(scalaVersion.value),
305307
sourceGenerators in Compile += {
306308
Def.task{
@@ -686,7 +688,6 @@ object ScalatestBuild extends Build {
686688
organization := "org.scalatest",
687689
libraryDependencies ++= crossBuildLibraryDependencies(scalaVersion.value),
688690
libraryDependencies += "org.scalacheck" %%% "scalacheck" % scalacheckVersion % "test",
689-
libraryDependencies += "io.spray" %% "spray-json" % "1.3.4" % "optional",
690691
//jsDependencies += RuntimeDOM % "test",
691692
scalaJSOptimizerOptions ~= { _.withDisableOptimizer(true) },
692693
//jsEnv := NodeJSEnv(executable = "node").value,
@@ -855,8 +856,7 @@ object ScalatestBuild extends Build {
855856
"junit" % "junit" % junitVersion % "optional",
856857
"org.testng" % "testng" % testngVersion % "optional",
857858
"org.jmock" % "jmock-legacy" % jmockVersion % "optional",
858-
"org.pegdown" % "pegdown" % pegdownVersion % "optional",
859-
"io.spray" %% "spray-json" % "1.3.4" % "optional"
859+
"org.pegdown" % "pegdown" % pegdownVersion % "optional"
860860

861861
)
862862

0 commit comments

Comments
 (0)