Skip to content

Commit 76ccec7

Browse files
committed
Merge pull request #10 from JamesIry/junit_conversion
Move tests from partests to junit
2 parents ae10b8e + aff1f1b commit 76ccec7

20 files changed

+244
-317
lines changed

build.sbt

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -83,62 +83,6 @@ pomExtra := (
8383

8484
libraryDependencies ++= Seq("junit" % "junit" % "4.11" % "test", "com.novocode" % "junit-interface" % "0.10" % "test")
8585

86-
// default value must be set here
87-
TestKeys.includeTestDependencies := true
88-
89-
// default
90-
TestKeys.partestVersion := "1.0.0-RC7"
91-
92-
// the actual partest the interface calls into -- must be binary version close enough to ours
93-
// so that it can link to the compiler/lib we're using (testing)
94-
// NOTE: not sure why, but the order matters (maybe due to the binary version conflicts for xml/parser combinators pulled in for scaladoc?)
95-
libraryDependencies ++= (
96-
if (TestKeys.includeTestDependencies.value) {
97-
/**
98-
* Exclude all transitive dependencies of partest that include scala-parser-combinators.
99-
* This way we avoid having two (or more) versions of scala-parser-combinators on a classpath.
100-
* See this comment which describes the same issue for scala-xml
101-
* https://github.com/scala/scala-xml/pull/6#issuecomment-26614894
102-
*
103-
* Note that we are using ModuleID.exclude instead of more flexible ModuleID.excludeAll
104-
* (describe here: http://www.scala-sbt.org/release/docs/Detailed-Topics/Library-Management#exclude-transitive-dependencies)
105-
* because only plain excludes are incorporated in generated pom.xml. There are two ways
106-
* to address this problem:
107-
*
108-
* 1. Figure out how to depend on partest in non-transitive way: not include that dependency
109-
* in generated pom.xml for scala-parser-combinators.
110-
* 2. Declare dependencies in partest as provided so they are not includeded transitively.
111-
*/
112-
def excludeScalaXml(dep: ModuleID): ModuleID =
113-
(dep exclude("org.scala-lang.modules", "scala-parser-combinators_2.11.0-M4")
114-
exclude("org.scala-lang.modules", "scala-parser-combinators_2.11.0-M5")
115-
exclude("org.scala-lang.modules", "scala-parser-combinators_2.11.0-M6"))
116-
// (this space intentionally not left blank)
117-
Seq("org.scala-lang.modules" % s"scala-partest-interface_${scalaBinaryVersion.value}" % "0.2" % "test" intransitive,
118-
"org.scala-lang.modules" % s"scala-partest_${scalaBinaryVersion.value}" % TestKeys.partestVersion.value % "test" intransitive,
119-
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0" % "test", // diffutils is needed by partest
120-
"org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").map(excludeScalaXml)
121-
}
122-
else Seq.empty
123-
)
124-
125-
fork in Test := true
126-
127-
javaOptions in Test += "-Xmx1G"
128-
129-
testFrameworks += new TestFramework("scala.tools.partest.Framework")
130-
131-
definedTests in Test += (
132-
new sbt.TestDefinition(
133-
"partest",
134-
// marker fingerprint since there are no test classes
135-
// to be discovered by sbt:
136-
new sbt.testing.AnnotatedFingerprint {
137-
def isModule = true
138-
def annotationName = "partest"
139-
}, true, Array())
140-
)
141-
14286
osgiSettings
14387

14488
val osgiVersion = version(_.replace('-', '.'))

test/files/run/json.scala renamed to src/test/scala/scala/util/parsing/combinator/JsonTest.scala

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
/*
2-
* filter: inliner warning\(s\); re-run with -Yinline-warnings for details
3-
*/
41
import scala.util.parsing.json._
52
import scala.collection.immutable.TreeMap
63

7-
@deprecated("Suppress warnings", since="2.11")
8-
object Test extends App {
4+
import org.junit.Test
5+
import org.junit.Assert.{assertEquals, assertTrue}
6+
7+
class JsonTest {
98
/* This method converts parsed JSON back into real JSON notation with objects in
109
* sorted-key order. Not required by the spec, but it allows us to do a stable
1110
* toString comparison. */
@@ -32,12 +31,8 @@ object Test extends App {
3231
}
3332

3433
// For this one, just parsing should be considered a pass
35-
def printJSON(given : String) {
36-
JSON parseRaw given match {
37-
case None => println("Parse failed for \"%s\"".format(given))
38-
case Some(parsed) => println("Passed parse : " + sortJSON(parsed))
39-
}
40-
}
34+
def printJSON(given : String) : Unit =
35+
assertTrue("Parse failed for \"%s\"".format(given), (JSON parseRaw given).isDefined)
4136

4237
// For this usage, do a raw parse (to JSONObject/JSONArray)
4338
def printJSON(given : String, expected : JSONType) {
@@ -52,76 +47,94 @@ object Test extends App {
5247
// For this usage, do configurable parsing so that you can do raw if desired
5348
def printJSON[T](given : String, parser : String => T, expected : Any) {
5449
parser(given) match {
55-
case None => println("Parse failed for \"%s\"".format(given))
56-
case Some(parsed) => if (parsed == expected) {
57-
println("Passed compare: " + parsed)
58-
} else {
50+
case None => assertTrue("Parse failed for \"%s\"".format(given), false)
51+
case Some(parsed) => if (parsed != expected) {
5952
val eStr = sortJSON(expected).toString
6053
val pStr = sortJSON(parsed).toString
61-
stringDiff(eStr,pStr)
54+
assertTrue(stringDiff(eStr,pStr), false)
6255
}
6356
}
6457
}
6558

66-
def stringDiff (expected : String, actual : String) {
59+
def stringDiff (expected : String, actual : String): String = {
6760
if (expected != actual) {
6861
// Figure out where the Strings differ and generate a marker
6962
val mismatchPosition = expected.toList.zip(actual.toList).indexWhere({case (x,y) => x != y}) match {
7063
case -1 => Math.min(expected.length, actual.length)
7164
case x => x
7265
}
7366
val reason = (" " * mismatchPosition) + "^"
74-
println("Expected: %s\nGot : %s \n %s".format(expected, actual, reason))
67+
"Expected: %s\nGot : %s \n %s".format(expected, actual, reason)
7568

7669
} else {
77-
println("Passed compare: " + actual)
70+
"Passed compare: " + actual
7871
}
7972
}
8073

81-
8274
// The library should differentiate between lower case "l" and number "1" (ticket #136)
83-
printJSON("{\"name\" : \"value\"}", JSONObject(Map("name" -> "value")))
84-
printJSON("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")))
85-
printJSON("{\"name\" : { \"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\" } }",
86-
JSONObject(Map("name" -> JSONObject(Map("name1" -> "va1ue1", "name2" -> "va1ue2")))))
75+
@Test
76+
def testEllVsOne: Unit = {
77+
printJSON("{\"name\" : \"value\"}", JSONObject(Map("name" -> "value")))
78+
printJSON("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")))
79+
printJSON("{\"name\" : { \"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\" } }",
80+
JSONObject(Map("name" -> JSONObject(Map("name1" -> "va1ue1", "name2" -> "va1ue2")))))
81+
}
8782

8883
// Unicode escapes should be handled properly
89-
printJSON("{\"name\" : \"\\u0022\"}")
84+
@Test
85+
def testEscapes: Unit =
86+
printJSON("{\"name\" : \"\\u0022\"}")
9087

9188
// The library should return a map for JSON objects (ticket #873)
92-
printJSONFull("{\"function\" : \"add_symbol\"}", Map("function" -> "add_symbol"))
89+
@Test
90+
def testMap: Unit =
91+
printJSONFull("{\"function\" : \"add_symbol\"}", Map("function" -> "add_symbol"))
9392

9493
// The library should recurse into arrays to find objects (ticket #2207)
95-
printJSON("[{\"a\" : \"team\"},{\"b\" : 52}]", JSONArray(List(JSONObject(Map("a" -> "team")), JSONObject(Map("b" -> 52.0)))))
94+
@Test
95+
def testObjectsInArrays: Unit =
96+
printJSON("[{\"a\" : \"team\"},{\"b\" : 52}]", JSONArray(List(JSONObject(Map("a" -> "team")), JSONObject(Map("b" -> 52.0)))))
97+
9698

9799
// The library should differentiate between empty maps and lists (ticket #3284)
98-
printJSONFull("{}", Map())
99-
printJSONFull("[]", List())
100+
@Test
101+
def testEmptyMapsVsLists: Unit = {
102+
printJSONFull("{}", Map())
103+
printJSONFull("[]", List())
104+
}
100105

101106
// Lists should be returned in the same order as specified
102-
printJSON("[4,1,3,2,6,5,8,7]", JSONArray(List[Double](4,1,3,2,6,5,8,7)))
107+
@Test
108+
def testListOrder: Unit =
109+
printJSON("[4,1,3,2,6,5,8,7]", JSONArray(List[Double](4,1,3,2,6,5,8,7)))
103110

104111
// Additional tests
112+
@Test
113+
def testAdditional: Unit =
105114
printJSON("{\"age\": 0}")
106115

107116
// The library should do a proper toString representation using default and custom renderers (ticket #3605)
108-
stringDiff("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")).toString)
109-
stringDiff("{\"name\" : {\"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\"}}",
110-
JSONObject(Map("name" -> JSONObject(TreeMap("name1" -> "va1ue1", "name2" -> "va1ue2")))).toString)
117+
@Test
118+
def testDefaultAndCustomRenderers: Unit = {
119+
assertEquals("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")).toString())
120+
assertEquals("{\"name\" : {\"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\"}}",
121+
JSONObject(Map("name" -> JSONObject(TreeMap("name1" -> "va1ue1", "name2" -> "va1ue2")))).toString())
111122

112-
stringDiff("[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]", JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString)
123+
assertEquals("[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]", JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString())
124+
}
113125

114126
// A test method that escapes all characters in strings
115127
def escapeEverything (in : Any) : String = in match {
116128
case s : String => "\"" + s.map(c => "\\u%04x".format(c : Int)).mkString + "\""
117-
case jo : JSONObject => jo.toString(escapeEverything)
118-
case ja : JSONArray => ja.toString(escapeEverything)
129+
case jo : JSONObject => jo.toString(escapeEverything _)
130+
case ja : JSONArray => ja.toString(escapeEverything _)
119131
case other => other.toString
120132
}
121133

122-
stringDiff("{\"\\u006e\\u0061\\u006d\\u0065\" : \"\\u0076\\u0061\\u006c\"}", JSONObject(Map("name" -> "val")).toString(escapeEverything))
134+
@Test
135+
def testEscapeEverything: Unit =
136+
assertEquals("{\"\\u006e\\u0061\\u006d\\u0065\" : \"\\u0076\\u0061\\u006c\"}", JSONObject(Map("name" -> "val")).toString(escapeEverything _))
123137

124-
println
125138

126139
// from http://en.wikipedia.org/wiki/JSON
127140
val sample1 = """
@@ -156,9 +169,9 @@ object Test extends App {
156169
)
157170
)
158171

159-
160-
printJSONFull(sample1, sample1Obj)
161-
println
172+
@Test
173+
def testSample1: Unit =
174+
printJSONFull(sample1, sample1Obj)
162175

163176
// from http://www.developer.com/lang/jscript/article.php/3596836
164177
val sample2 = """
@@ -186,8 +199,9 @@ object Test extends App {
186199
]
187200
}"""
188201

189-
printJSON(sample2)
190-
println
202+
@Test
203+
def testSampl2: Unit =
204+
printJSON(sample2)
191205

192206
// from http://json.org/example.html
193207
val sample3 = """
@@ -282,6 +296,7 @@ object Test extends App {
282296
}
283297
}"""
284298

285-
printJSON(sample3)
286-
println
299+
@Test
300+
def testSample3 =
301+
printJSON(sample3)
287302
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import org.junit.Test
2+
import org.junit.Assert.assertEquals
3+
4+
class UnitTestIO {
5+
6+
@Test
7+
def testUTF8 {
8+
def decode(ch: Int) = new String(Array(ch), 0, 1).getBytes("UTF-8")
9+
10+
assert(new String( decode(0x004D), "utf8") == new String(Array(0x004D.asInstanceOf[Char])))
11+
assert(new String( decode(0x0430), "utf8") == new String(Array(0x0430.asInstanceOf[Char])))
12+
assert(new String( decode(0x4E8C), "utf8") == new String(Array(0x4E8C.asInstanceOf[Char])))
13+
assert(new String(decode(0x10302), "utf8") == new String(Array(0xD800.asInstanceOf[Char],
14+
0xDF02.asInstanceOf[Char])))
15+
// a client
16+
val test = "{\"a\":\"\\u0022\"}"
17+
val expected = "a" -> "\""
18+
19+
val parsed = scala.util.parsing.json.JSON.parseFull(test)
20+
val result = parsed == Some(Map(expected))
21+
assertEquals(Some(Map(expected)), parsed)
22+
}
23+
24+
@Test
25+
def testSource {
26+
val s = "Here is a test string"
27+
val f = io.Source.fromBytes(s.getBytes("utf-8"))
28+
val b = new collection.mutable.ArrayBuffer[Char]()
29+
f.copyToBuffer(b)
30+
assertEquals(new String(b.toArray), s)
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.io.{File,StringReader}
2+
3+
import scala.util.parsing.combinator.Parsers
4+
import scala.util.parsing.input.{CharArrayReader, StreamReader}
5+
6+
import org.junit.Test
7+
import org.junit.Assert.assertEquals
8+
9+
class T0700 {
10+
class TestParsers extends Parsers {
11+
type Elem = Char
12+
13+
def p: Parser[List[Int]] = rep(p1 | p2)
14+
def p1: Parser[Int] = 'a' ~ nl ~ 'b' ~ nl ^^^ 1
15+
def p2: Parser[Int] = 'a' ~ nl ^^^ 2
16+
def nl: Parser[Int] = rep(accept('\n') | accept('\r')) ^^^ 0
17+
}
18+
19+
@Test
20+
def test: Unit = {
21+
val tstParsers = new TestParsers
22+
val s = "a\na\na"
23+
val r1 = new CharArrayReader(s.toCharArray())
24+
val r2 = StreamReader(new StringReader(s))
25+
assertEquals("[3.2] parsed: List(2, 2, 2)", tstParsers.p(r1).toString)
26+
assertEquals("[3.2] parsed: List(2, 2, 2)", tstParsers.p(r2).toString)
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import scala.util.parsing.combinator.Parsers
2+
import scala.util.parsing.input.CharSequenceReader
3+
4+
import java.io.{File,StringReader}
5+
6+
import scala.util.parsing.combinator.Parsers
7+
import scala.util.parsing.input.{CharArrayReader, StreamReader}
8+
9+
import org.junit.Test
10+
import org.junit.Assert.assertEquals
11+
12+
class T1100 {
13+
class TestParsers extends Parsers {
14+
type Elem = Char
15+
16+
def p: Parser[List[Char]] = rep1(p1)
17+
def p1: Parser[Char] = accept('a') | err("errors are propagated")
18+
}
19+
20+
val expected = """[1.4] error: errors are propagated
21+
22+
aaab
23+
^"""
24+
25+
@Test
26+
def test(): Unit = {
27+
val tstParsers = new TestParsers
28+
val s = new CharSequenceReader("aaab")
29+
assertEquals(expected, tstParsers.p(s).toString)
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import org.junit.Test
2+
import org.junit.Assert.assertEquals
3+
4+
class T4138 {
5+
object p extends scala.util.parsing.combinator.JavaTokenParsers
6+
7+
@Test
8+
def test: Unit = {
9+
assertEquals("""[1.45] parsed: "lir 'de\' ' \\ \n / upa \"new\" \t parsing"""", p.parse(p.stringLiteral, """"lir 'de\' ' \\ \n / upa \"new\" \t parsing"""").toString)
10+
assertEquals("""[1.5] parsed: "s """", p.parse(p.stringLiteral, """"s " lkjse"""").toString)
11+
}
12+
}

0 commit comments

Comments
 (0)