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

Unified 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: Provide better types for bind. Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: tests/lib/convert/streamed_conversion_json_utf8_decode_test.dart
diff --git a/tests/lib/convert/streamed_conversion_json_utf8_decode_test.dart b/tests/lib/convert/streamed_conversion_json_utf8_decode_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..45a62766c5959a2a82e38eb0bcba81328b656311
--- /dev/null
+++ b/tests/lib/convert/streamed_conversion_json_utf8_decode_test.dart
@@ -0,0 +1,80 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "package:expect/expect.dart";
+import 'dart:async';
+import 'dart:convert';
+import 'json_unicode_tests.dart';
+import '../../async_helper.dart';
+
+final JSON_UTF8 = JSON.fuse(UTF8);
+
+bool isJsonEqual(o1, o2) {
+ if (o1 == o2) return true;
+ if (o1 is List && o2 is List) {
+ if (o1.length != o2.length) return false;
+ for (int i = 0; i < o1.length; i++) {
+ if (!isJsonEqual(o1[i], o2[i])) return false;
+ }
+ return true;
+ }
+ if (o1 is Map && o2 is Map) {
+ if (o1.length != o2.length) return false;
+ for (var key in o1.keys) {
+ Expect.isTrue(key is String);
+ if (!o2.containsKey(key)) return false;
+ if (!isJsonEqual(o1[key], o2[key])) return false;
+ }
+ return true;
+ }
+ return false;
+}
+
+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.
+ var controller;
+ controller = new StreamController(onListen: () {
+ chunks.forEach(controller.add);
+ controller.close();
+ });
+ return controller.stream.transform(JSON_UTF8.decoder);
+}
+
+Stream decode(List<int> bytes) {
+ return createStream([bytes]);
+}
+
+Stream decodeChunked(List<int> bytes, int chunkSize) {
+ List<List<int>> chunked = <List<int>>[];
+ int i = 0;
+ while (i < bytes.length) {
+ if (i + chunkSize <= bytes.length) {
+ chunked.add(bytes.sublist(i, i + chunkSize));
+ } else {
+ chunked.add(bytes.sublist(i));
+ }
+ i += chunkSize;
+ }
+ return createStream(chunked);
+}
+
+checkIsJsonEqual(expected, stream) {
+ asyncStart();
+ stream.single.then((o) {
+ Expect.isTrue(isJsonEqual(expected, o));
+ asyncEnd();
+ });
+}
+
+main() {
+ for (var test in JSON_UNICODE_TESTS) {
+ var bytes = test[0];
+ var o = test[1];
+ checkIsJsonEqual(o, decode(bytes));
+ checkIsJsonEqual(o, decodeChunked(bytes, 1));
+ checkIsJsonEqual(o, decodeChunked(bytes, 2));
+ checkIsJsonEqual(o, decodeChunked(bytes, 3));
+ checkIsJsonEqual(o, decodeChunked(bytes, 4));
+ checkIsJsonEqual(o, decodeChunked(bytes, 5));
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698