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 'json_unicode_tests.dart'; | |
| 9 import '../../async_helper.dart'; | |
| 10 | |
| 11 final JSON_UTF8 = JSON.fuse(UTF8); | |
| 12 | |
| 13 bool isJsonEqual(o1, o2) { | |
| 14 if (o1 == o2) return true; | |
| 15 if (o1 is List && o2 is List) { | |
| 16 if (o1.length != o2.length) return false; | |
| 17 for (int i = 0; i < o1.length; i++) { | |
| 18 if (!isJsonEqual(o1[i], o2[i])) return false; | |
| 19 } | |
| 20 return true; | |
| 21 } | |
| 22 if (o1 is Map && o2 is Map) { | |
| 23 if (o1.length != o2.length) return false; | |
| 24 for (var key in o1.keys) { | |
| 25 Expect.isTrue(key is String); | |
| 26 if (!o2.containsKey(key)) return false; | |
| 27 if (!isJsonEqual(o1[key], o2[key])) return false; | |
| 28 } | |
| 29 return true; | |
| 30 } | |
| 31 return false; | |
| 32 } | |
| 33 | |
| 34 createStream(List<List<int>> chunks) { | |
|
Søren Gjesse
2013/07/26 07:37:23
Add return type Stream
floitsch
2013/07/26 10:35:32
Done.
| |
| 35 var controller; | |
| 36 controller = new StreamController(onListen: () { | |
| 37 chunks.forEach(controller.add); | |
| 38 controller.close(); | |
| 39 }); | |
| 40 return controller.stream.transform(JSON_UTF8.decoder); | |
| 41 } | |
| 42 | |
| 43 Stream decode(List<int> bytes) { | |
| 44 return createStream([bytes]); | |
| 45 } | |
| 46 | |
| 47 Stream decodeChunked(List<int> bytes, int chunkSize) { | |
| 48 List<List<int>> chunked = <List<int>>[]; | |
| 49 int i = 0; | |
| 50 while (i < bytes.length) { | |
| 51 if (i + chunkSize <= bytes.length) { | |
| 52 chunked.add(bytes.sublist(i, i + chunkSize)); | |
| 53 } else { | |
| 54 chunked.add(bytes.sublist(i)); | |
| 55 } | |
| 56 i += chunkSize; | |
| 57 } | |
| 58 return createStream(chunked); | |
| 59 } | |
| 60 | |
| 61 checkIsJsonEqual(expected, stream) { | |
| 62 asyncStart(); | |
| 63 stream.single.then((o) { | |
| 64 Expect.isTrue(isJsonEqual(expected, o)); | |
| 65 asyncEnd(); | |
| 66 }); | |
| 67 } | |
| 68 | |
| 69 main() { | |
| 70 for (var test in JSON_UNICODE_TESTS) { | |
| 71 var bytes = test[0]; | |
| 72 var o = test[1]; | |
| 73 checkIsJsonEqual(o, decode(bytes)); | |
| 74 checkIsJsonEqual(o, decodeChunked(bytes, 1)); | |
| 75 checkIsJsonEqual(o, decodeChunked(bytes, 2)); | |
| 76 checkIsJsonEqual(o, decodeChunked(bytes, 3)); | |
| 77 checkIsJsonEqual(o, decodeChunked(bytes, 4)); | |
| 78 checkIsJsonEqual(o, decodeChunked(bytes, 5)); | |
| 79 } | |
| 80 } | |
| OLD | NEW |