1- /*
2- * filter: inliner warning\(s\); re-run with -Yinline-warnings for details
3- */
41import scala .util .parsing .json ._
52import 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\n Got : %s \n %s" .format(expected, actual, reason) )
67+ " Expected: %s\n Got : %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}
0 commit comments