OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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_test; | 5 library json_test; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import "dart:convert"; | 8 import "dart:convert"; |
9 | 9 |
10 bool badFormat(e) => e is FormatException; | 10 bool badFormat(e) => e is FormatException; |
11 | 11 |
12 jsonTest(testName, expect, action(sink)) { | 12 jsonTest(testName, expect, action(sink)) { |
13 var sink = new ChunkedConversionSink.withCallback((values) { | 13 var sink = new ChunkedConversionSink.withCallback((values) { |
14 var value = values[0]; | 14 var value = values[0]; |
15 Expect.equals(expect, value, "$testName:$value"); | 15 Expect.equals(expect, value, "$testName:$value"); |
16 }); | 16 }); |
17 var decoderSink = JSON.decoder.startChunkedConversion(sink); | 17 var decoderSink = JSON.decoder.startChunkedConversion(sink); |
18 action(decoderSink); | 18 action(decoderSink); |
19 } | 19 } |
20 | 20 |
21 jsonThrowsTest(testName, action(sink)) { | 21 jsonThrowsTest(testName, action(sink)) { |
22 var sink = new ChunkedConversionSink.withCallback((values) { | 22 var sink = new ChunkedConversionSink.withCallback((values) { |
23 Expect.fail("Should have thrown: $testName"); | 23 Expect.fail("Should have thrown: $testName"); |
24 }); | 24 }); |
25 var decoderSink = JSON.decoder.startChunkedConversion(sink); | 25 var decoderSink = JSON.decoder.startChunkedConversion(sink); |
26 Expect.throws(() { action(decoderSink); }, (e) => e is FormatException, | 26 Expect.throws(() { |
27 testName); | 27 action(decoderSink); |
| 28 }, (e) => e is FormatException, testName); |
28 } | 29 } |
29 | 30 |
30 main() { | 31 main() { |
31 testNumbers(); | 32 testNumbers(); |
32 testStrings(); | 33 testStrings(); |
33 testKeywords(); | 34 testKeywords(); |
34 } | 35 } |
35 | 36 |
36 void testStrings() { | 37 void testStrings() { |
37 var s = r'"abc\f\n\r\t\b\"\/\\\u0001\u9999\uffff"'; | 38 var s = r'"abc\f\n\r\t\b\"\/\\\u0001\u9999\uffff"'; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 var p2b = number.substring(j); | 84 var p2b = number.substring(j); |
84 jsonTest("$p1|$p2a|$p2b", expected, (sink) { | 85 jsonTest("$p1|$p2a|$p2b", expected, (sink) { |
85 sink.add(p1); | 86 sink.add(p1); |
86 sink.add(p2a); | 87 sink.add(p2a); |
87 sink.add(p2b); | 88 sink.add(p2b); |
88 sink.close(); | 89 sink.close(); |
89 }); | 90 }); |
90 } | 91 } |
91 } | 92 } |
92 } | 93 } |
| 94 |
93 for (var sign in ["-", ""]) { | 95 for (var sign in ["-", ""]) { |
94 for (var intPart in ["0", "1", "99"]) { | 96 for (var intPart in ["0", "1", "99"]) { |
95 for (var decimalPoint in [".", ""]) { | 97 for (var decimalPoint in [".", ""]) { |
96 for (var decimals in decimalPoint.isEmpty ? [""] : ["0", "99"]) { | 98 for (var decimals in decimalPoint.isEmpty ? [""] : ["0", "99"]) { |
97 for (var e in ["e", "e-", "e+", ""]) { | 99 for (var e in ["e", "e-", "e+", ""]) { |
98 for (var exp in e.isEmpty ? [""] : ["0", "2", "22", "34"]) { | 100 for (var exp in e.isEmpty ? [""] : ["0", "2", "22", "34"]) { |
99 testNumber("$sign$intPart$decimalPoint$decimals$e$exp"); | 101 testNumber("$sign$intPart$decimalPoint$decimals$e$exp"); |
100 } | 102 } |
101 } | 103 } |
102 } | 104 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 jsonTest("$s1|$s2a|$s2b", expected, (sink) { | 166 jsonTest("$s1|$s2a|$s2b", expected, (sink) { |
165 sink.add(s1); | 167 sink.add(s1); |
166 sink.add(s2a); | 168 sink.add(s2a); |
167 sink.add(s2b); | 169 sink.add(s2b); |
168 sink.close(); | 170 sink.close(); |
169 }); | 171 }); |
170 } | 172 } |
171 } | 173 } |
172 } | 174 } |
173 } | 175 } |
OLD | NEW |