|
1 gTestsubsuite='JSON'; |
|
2 |
|
3 function testJSON(str, expectSyntaxError) |
|
4 { |
|
5 // Leading and trailing whitespace never affect parsing, so test the string |
|
6 // multiple times with and without whitespace around it as it's easy and can |
|
7 // potentially detect bugs. |
|
8 |
|
9 // Try the provided string |
|
10 try |
|
11 { |
|
12 JSON.parse(str); |
|
13 reportCompare(false, expectSyntaxError, |
|
14 "string <" + str + "> " + |
|
15 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
16 "have parsed as JSON"); |
|
17 } |
|
18 catch (e) |
|
19 { |
|
20 if (!(e instanceof SyntaxError)) |
|
21 { |
|
22 reportCompare(true, false, |
|
23 "parsing string <" + str + "> threw a non-SyntaxError " + |
|
24 "exception: " + e); |
|
25 } |
|
26 else |
|
27 { |
|
28 reportCompare(true, expectSyntaxError, |
|
29 "string <" + str + "> " + |
|
30 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
31 "have parsed as JSON, exception: " + e); |
|
32 } |
|
33 } |
|
34 |
|
35 // Now try the provided string with trailing whitespace |
|
36 try |
|
37 { |
|
38 JSON.parse(str + " "); |
|
39 reportCompare(false, expectSyntaxError, |
|
40 "string <" + str + " > " + |
|
41 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
42 "have parsed as JSON"); |
|
43 } |
|
44 catch (e) |
|
45 { |
|
46 if (!(e instanceof SyntaxError)) |
|
47 { |
|
48 reportCompare(true, false, |
|
49 "parsing string <" + str + " > threw a non-SyntaxError " + |
|
50 "exception: " + e); |
|
51 } |
|
52 else |
|
53 { |
|
54 reportCompare(true, expectSyntaxError, |
|
55 "string <" + str + " > " + |
|
56 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
57 "have parsed as JSON, exception: " + e); |
|
58 } |
|
59 } |
|
60 |
|
61 // Now try the provided string with leading whitespace |
|
62 try |
|
63 { |
|
64 JSON.parse(" " + str); |
|
65 reportCompare(false, expectSyntaxError, |
|
66 "string < " + str + "> " + |
|
67 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
68 "have parsed as JSON"); |
|
69 } |
|
70 catch (e) |
|
71 { |
|
72 if (!(e instanceof SyntaxError)) |
|
73 { |
|
74 reportCompare(true, false, |
|
75 "parsing string < " + str + "> threw a non-SyntaxError " + |
|
76 "exception: " + e); |
|
77 } |
|
78 else |
|
79 { |
|
80 reportCompare(true, expectSyntaxError, |
|
81 "string < " + str + "> " + |
|
82 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
83 "have parsed as JSON, exception: " + e); |
|
84 } |
|
85 } |
|
86 |
|
87 // Now try the provided string with whitespace surrounding it |
|
88 try |
|
89 { |
|
90 JSON.parse(" " + str + " "); |
|
91 reportCompare(false, expectSyntaxError, |
|
92 "string < " + str + " > " + |
|
93 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
94 "have parsed as JSON"); |
|
95 } |
|
96 catch (e) |
|
97 { |
|
98 if (!(e instanceof SyntaxError)) |
|
99 { |
|
100 reportCompare(true, false, |
|
101 "parsing string < " + str + " > threw a non-SyntaxError " + |
|
102 "exception: " + e); |
|
103 } |
|
104 else |
|
105 { |
|
106 reportCompare(true, expectSyntaxError, |
|
107 "string < " + str + " > " + |
|
108 "should" + (expectSyntaxError ? "n't" : "") + " " + |
|
109 "have parsed as JSON, exception: " + e); |
|
110 } |
|
111 } |
|
112 } |