1
- /*
2
- * filter: inliner warning\(s\); re-run with -Yinline-warnings for details
3
- */
4
1
import scala .util .parsing .json ._
5
2
import scala .collection .immutable .TreeMap
6
3
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 {
9
8
/* This method converts parsed JSON back into real JSON notation with objects in
10
9
* sorted-key order. Not required by the spec, but it allows us to do a stable
11
10
* toString comparison. */
@@ -32,12 +31,8 @@ object Test extends App {
32
31
}
33
32
34
33
// 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)
41
36
42
37
// For this usage, do a raw parse (to JSONObject/JSONArray)
43
38
def printJSON (given : String , expected : JSONType ) {
@@ -52,76 +47,94 @@ object Test extends App {
52
47
// For this usage, do configurable parsing so that you can do raw if desired
53
48
def printJSON [T ](given : String , parser : String => T , expected : Any ) {
54
49
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) {
59
52
val eStr = sortJSON(expected).toString
60
53
val pStr = sortJSON(parsed).toString
61
- stringDiff(eStr,pStr)
54
+ assertTrue( stringDiff(eStr,pStr), false )
62
55
}
63
56
}
64
57
}
65
58
66
- def stringDiff (expected : String , actual : String ) {
59
+ def stringDiff (expected : String , actual : String ): String = {
67
60
if (expected != actual) {
68
61
// Figure out where the Strings differ and generate a marker
69
62
val mismatchPosition = expected.toList.zip(actual.toList).indexWhere({case (x,y) => x != y}) match {
70
63
case - 1 => Math .min(expected.length, actual.length)
71
64
case x => x
72
65
}
73
66
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)
75
68
76
69
} else {
77
- println( " Passed compare: " + actual)
70
+ " Passed compare: " + actual
78
71
}
79
72
}
80
73
81
-
82
74
// 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
+ }
87
82
88
83
// Unicode escapes should be handled properly
89
- printJSON(" {\" name\" : \"\\ u0022\" }" )
84
+ @ Test
85
+ def testEscapes : Unit =
86
+ printJSON(" {\" name\" : \"\\ u0022\" }" )
90
87
91
88
// 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" ))
93
92
94
93
// 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
+
96
98
97
99
// 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
+ }
100
105
101
106
// 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 )))
103
110
104
111
// Additional tests
112
+ @ Test
113
+ def testAdditional : Unit =
105
114
printJSON(" {\" age\" : 0}" )
106
115
107
116
// 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())
111
122
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
+ }
113
125
114
126
// A test method that escapes all characters in strings
115
127
def escapeEverything (in : Any ) : String = in match {
116
128
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 _ )
119
131
case other => other.toString
120
132
}
121
133
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 _))
123
137
124
- println
125
138
126
139
// from http://en.wikipedia.org/wiki/JSON
127
140
val sample1 = """
@@ -156,9 +169,9 @@ object Test extends App {
156
169
)
157
170
)
158
171
159
-
160
- printJSONFull(sample1, sample1Obj)
161
- println
172
+ @ Test
173
+ def testSample1 : Unit =
174
+ printJSONFull(sample1, sample1Obj)
162
175
163
176
// from http://www.developer.com/lang/jscript/article.php/3596836
164
177
val sample2 = """
@@ -186,8 +199,9 @@ object Test extends App {
186
199
]
187
200
}"""
188
201
189
- printJSON(sample2)
190
- println
202
+ @ Test
203
+ def testSampl2 : Unit =
204
+ printJSON(sample2)
191
205
192
206
// from http://json.org/example.html
193
207
val sample3 = """
@@ -282,6 +296,7 @@ object Test extends App {
282
296
}
283
297
}"""
284
298
285
- printJSON(sample3)
286
- println
299
+ @ Test
300
+ def testSample3 =
301
+ printJSON(sample3)
287
302
}
0 commit comments