Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 import "package:expect/expect.dart"; | |
| 6 import 'dart:async'; | |
| 7 import 'dart:convert'; | |
| 8 import '../../async_helper.dart'; | |
| 9 | |
| 10 final TESTS = [ | |
| 11 [ 5, '5' ], | |
| 12 [ -42, '-42' ], | |
| 13 [ 3.14, '3.14' ], | |
| 14 [ true, 'true' ], | |
| 15 [ false, 'false' ], | |
| 16 [ null, 'null' ], | |
| 17 [ 'quote"or\'', '"quote\\"or\'"' ], | |
| 18 [ '', '""' ], | |
| 19 [ [], "[]" ], | |
| 20 [ [3, -4.5, true, "hi", false], '[3,-4.5,true,"hi",false]' ], | |
| 21 [ [null], "[null]" ], | |
| 22 [ [[null]], "[[null]]" ], | |
| 23 [ [[3]], "[[3]]" ], | |
| 24 [ {}, "{}" ], | |
| 25 [ { "x": 3, "y": 4.5, "z": "hi", "u": true, "v": false }, | |
| 26 '{"x":3,"y":4.5,"z":"hi","u":true,"v":false}' ], | |
| 27 [ { "x": null }, '{"x":null}' ], | |
| 28 [ { "x": {} }, '{"x":{}}' ], | |
| 29 // Note that -0.0 won't be treated the same in JS. The Json spec seems to | |
| 30 // allow it, though. | |
| 31 [ { "hi there": 499, "'":-0.0 }, '{"hi there":499,"\'":-0.0}' ], | |
| 32 [ r'\foo', r'"\\foo"' ], | |
| 33 ]; | |
| 34 | |
| 35 bool isJsonEqual(o1, o2) { | |
| 36 if (o1 == o2) return true; | |
| 37 if (o1 is List && o2 is List) { | |
| 38 if (o1.length != o2.length) return false; | |
| 39 for (int i = 0; i < o1.length; i++) { | |
| 40 if (!isJsonEqual(o1[i], o2[i])) return false; | |
| 41 } | |
| 42 return true; | |
| 43 } | |
| 44 if (o1 is Map && o2 is Map) { | |
| 45 if (o1.length != o2.length) return false; | |
| 46 for (var key in o1.keys) { | |
| 47 Expect.isTrue(key is String); | |
| 48 if (!o2.containsKey(key)) return false; | |
| 49 if (!isJsonEqual(o1[key], o2[key])) return false; | |
| 50 } | |
| 51 return true; | |
| 52 } | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 createStream(List<String> chunks) { | |
|
Søren Gjesse
2013/07/26 07:37:23
Add return type Stream.
floitsch
2013/07/26 10:35:32
Done.
| |
| 57 var decoder = new JsonDecoder(null); | |
| 58 var controller; | |
| 59 controller = new StreamController(onListen: () { | |
| 60 chunks.forEach(controller.add); | |
| 61 controller.close(); | |
| 62 }); | |
| 63 return controller.stream.transform(decoder); | |
| 64 } | |
| 65 | |
| 66 Stream decode(String str) { | |
| 67 return createStream([str]); | |
| 68 } | |
| 69 | |
| 70 Stream decode2(String str) { | |
| 71 return createStream(str.split("")); | |
| 72 } | |
| 73 | |
| 74 checkIsJsonEqual(expected, stream) { | |
| 75 asyncStart(); | |
| 76 stream.single.then((o) { | |
| 77 Expect.isTrue(isJsonEqual(expected, o)); | |
| 78 asyncEnd(); | |
| 79 }); | |
| 80 } | |
| 81 | |
| 82 main() { | |
| 83 var tests = TESTS.expand((test) { | |
| 84 var object = test[0]; | |
| 85 var string = test[1]; | |
| 86 var longString = " " | |
| 87 " " | |
| 88 "$string" | |
| 89 " " | |
| 90 " "; | |
| 91 return [test, [object, longString]]; | |
| 92 }); | |
| 93 for (var test in TESTS) { | |
| 94 var o = test[0]; | |
| 95 var string = test[1]; | |
| 96 checkIsJsonEqual(o, decode(string)); | |
| 97 checkIsJsonEqual(o, decode2(string)); | |
| 98 } | |
| 99 } | |
| OLD | NEW |