OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #library('json_tests'); | 5 #library('json_tests'); |
6 | 6 |
7 #import('dart:json'); | 7 #import('dart:json'); |
8 #import('dart:html'); | 8 #import('dart:html'); |
9 #import('../../lib/unittest/unittest.dart'); | 9 #import('../../lib/unittest/unittest.dart'); |
10 #import('../../lib/unittest/html_config.dart'); | 10 #import('../../lib/unittest/html_config.dart'); |
(...skipping 10 matching lines...) Expand all Loading... |
21 expect(JSON.parse(' false'), isFalse); | 21 expect(JSON.parse(' false'), isFalse); |
22 expect(JSON.parse(' null '), isNull); | 22 expect(JSON.parse(' null '), isNull); |
23 expect(JSON.parse('\n\rnull\t'), isNull); | 23 expect(JSON.parse('\n\rnull\t'), isNull); |
24 expect(JSON.parse(' "hi there\\" bob" '), equals('hi there" bob')); | 24 expect(JSON.parse(' "hi there\\" bob" '), equals('hi there" bob')); |
25 expect(JSON.parse(' "" '), isEmpty); | 25 expect(JSON.parse(' "" '), isEmpty); |
26 | 26 |
27 // Lists. | 27 // Lists. |
28 expect(JSON.parse(' [] '), isEmpty); | 28 expect(JSON.parse(' [] '), isEmpty); |
29 expect(JSON.parse('[ ]'), isEmpty); | 29 expect(JSON.parse('[ ]'), isEmpty); |
30 expect(JSON.parse(' [3, -4.5, true, "hi", false] '), | 30 expect(JSON.parse(' [3, -4.5, true, "hi", false] '), |
31 orderedEquals([3, -4.5, true, 'hi', false])); | 31 equals([3, -4.5, true, 'hi', false])); |
32 // Nulls are tricky. | 32 // Nulls are tricky. |
33 expect(JSON.parse('[null]'), orderedEquals([null])); | 33 expect(JSON.parse('[null]'), orderedEquals([null])); |
34 expect(JSON.parse(' [3, -4.5, null, true, "hi", false] '), | 34 expect(JSON.parse(' [3, -4.5, null, true, "hi", false] '), |
35 orderedEquals([3, -4.5, null, true, 'hi', false])); | 35 equals([3, -4.5, null, true, 'hi', false])); |
36 expect(JSON.parse('[[null]]'), | 36 expect(JSON.parse('[[null]]'), equals([[null]])); |
37 recursivelyMatches([[null]])); | |
38 expect(JSON.parse(' [ [3], [], [null], ["hi", true]] '), | 37 expect(JSON.parse(' [ [3], [], [null], ["hi", true]] '), |
39 recursivelyMatches([[3], [], [null], ['hi', true]])); | 38 equals([[3], [], [null], ['hi', true]])); |
40 | 39 |
41 // Maps. | 40 // Maps. |
42 expect(JSON.parse(' {} '), isEmpty); | 41 expect(JSON.parse(' {} '), isEmpty); |
43 expect(JSON.parse('{ }'), isEmpty); | 42 expect(JSON.parse('{ }'), isEmpty); |
44 | 43 |
45 expect(JSON.parse( | 44 expect(JSON.parse( |
46 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '), | 45 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '), |
47 recursivelyMatches({"x":3, "y": -4.5, "z" : "hi", | 46 equals({"x":3, "y": -4.5, "z" : "hi", "u" : true, "v": false })); |
48 "u" : true, "v": false })); | |
49 | 47 |
50 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi" } '), | 48 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi" } '), |
51 recursivelyMatches({"x":3, "y": -4.5, "z" : "hi" })); | 49 equals({"x":3, "y": -4.5, "z" : "hi" })); |
52 | 50 |
53 expect(JSON.parse(' {"y": -4.5, "z" : "hi" ,"x":3 } '), | 51 expect(JSON.parse(' {"y": -4.5, "z" : "hi" ,"x":3 } '), |
54 recursivelyMatches({"y": -4.5, "z" : "hi" ,"x":3 })); | 52 equals({"y": -4.5, "z" : "hi" ,"x":3 })); |
55 | 53 |
56 expect(JSON.parse('{ " hi bob " :3, "": 4.5}'), | 54 expect(JSON.parse('{ " hi bob " :3, "": 4.5}'), |
57 recursivelyMatches({ " hi bob " :3, "": 4.5})); | 55 equals({ " hi bob " :3, "": 4.5})); |
58 | 56 |
59 expect(JSON.parse(' { "x" : { } } '), | 57 expect(JSON.parse(' { "x" : { } } '), equals({ 'x' : {}})); |
60 recursivelyMatches({ 'x' : {}})); | 58 expect(JSON.parse('{"x":{}}'), equals({ 'x' : {}})); |
61 expect(JSON.parse('{"x":{}}'), | |
62 recursivelyMatches({ 'x' : {}})); | |
63 | 59 |
64 // Nulls are tricky. | 60 // Nulls are tricky. |
65 expect(JSON.parse('{"w":null}'), | 61 expect(JSON.parse('{"w":null}'), equals({ 'w' : null})); |
66 recursivelyMatches({ 'w' : null})); | |
67 | 62 |
68 expect(JSON.parse('{"x":{"w":null}}'), | 63 expect(JSON.parse('{"x":{"w":null}}'), equals({"x":{"w":null}})); |
69 recursivelyMatches({"x":{"w":null}})); | |
70 | 64 |
71 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi",' | 65 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi",' |
72 '"w":null, "u" : true, "v": false } '), | 66 '"w":null, "u" : true, "v": false } '), |
73 recursivelyMatches({"x":3, "y": -4.5, "z" : "hi", | 67 equals({"x":3, "y": -4.5, "z" : "hi", |
74 "w":null, "u" : true, "v": false })); | 68 "w":null, "u" : true, "v": false })); |
75 | 69 |
76 expect(JSON.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 70 expect(JSON.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
77 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 71 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |
78 recursivelyMatches({"x": {"a":3, "b": -4.5}, "y":[{}], | 72 equals({"x": {"a":3, "b": -4.5}, "y":[{}], |
79 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 73 "z":"hi","w":{"c":null,"d":true}, "v":null})); |
80 | 74 |
81 test('stringify', () { | 75 test('stringify', () { |
82 // Scalars. | 76 // Scalars. |
83 expect(JSON.stringify(5), equals('5')); | 77 expect(JSON.stringify(5), equals('5')); |
84 expect(JSON.stringify(-42), equals('-42')); | 78 expect(JSON.stringify(-42), equals('-42')); |
85 // Dart does not guarantee a formatting for doubles, | 79 // Dart does not guarantee a formatting for doubles, |
86 // so reparse and compare to the original. | 80 // so reparse and compare to the original. |
87 validateRoundTrip(3.14); | 81 validateRoundTrip(3.14); |
88 expect(JSON.stringify(true), equals('true')); | 82 expect(JSON.stringify(true), equals('true')); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 String y; | 129 String y; |
136 | 130 |
137 TestClass() : x = 3, y = 'joe' { } | 131 TestClass() : x = 3, y = 'joe' { } |
138 } | 132 } |
139 | 133 |
140 /** | 134 /** |
141 * Checks that the argument can be converted to a JSON string and | 135 * Checks that the argument can be converted to a JSON string and |
142 * back, and produce something equivalent to the argument. | 136 * back, and produce something equivalent to the argument. |
143 */ | 137 */ |
144 validateRoundTrip(expected) { | 138 validateRoundTrip(expected) { |
145 expect(JSON.parse(JSON.stringify(expected)), recursivelyMatches(expected)); | 139 expect(JSON.parse(JSON.stringify(expected)), equals(expected)); |
146 } | 140 } |
147 | 141 |
148 | 142 |
OLD | NEW |