Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: tests/lib/convert/streamed_conversion_json_utf8_decode_test.dart

Issue 20374003: Add transformer support to Converters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Upload Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 Stream<Object> createStream(List<List<int>> chunks) {
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<Object> decode(List<int> bytes) {
44 return createStream([bytes]);
45 }
46
47 Stream<Object> 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 void 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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698