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

Side by Side Diff: tests/lib/convert/streamed_conversion_json_encode1_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, 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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 import "package:expect/expect.dart"; 5 import "package:expect/expect.dart";
6 import 'dart:async';
6 import 'dart:convert'; 7 import 'dart:convert';
8 import '../../async_helper.dart';
7 9
8 final TESTS = [ 10 final TESTS = [
9 [ 5, '5' ], 11 [ 5, '5' ],
10 [ -42, '-42' ], 12 [ -42, '-42' ],
11 [ 3.14, '3.14' ], 13 [ 3.14, '3.14' ],
12 [ true, 'true' ], 14 [ true, 'true' ],
13 [ false, 'false' ], 15 [ false, 'false' ],
14 [ null, 'null' ], 16 [ null, 'null' ],
15 [ 'quote"or\'', '"quote\\"or\'"' ], 17 [ 'quote"or\'', '"quote\\"or\'"' ],
16 [ '', '""' ], 18 [ '', '""' ],
17 [ [], "[]" ], 19 [ [], "[]" ],
18 [ [3, -4.5, true, "hi", false], '[3,-4.5,true,"hi",false]' ], 20 [ [3, -4.5, true, "hi", false], '[3,-4.5,true,"hi",false]' ],
19 [ [null], "[null]" ], 21 [ [null], "[null]" ],
20 [ [[null]], "[[null]]" ], 22 [ [[null]], "[[null]]" ],
21 [ [[3]], "[[3]]" ], 23 [ [[3]], "[[3]]" ],
22 [ {}, "{}" ], 24 [ {}, "{}" ],
23 [ { "x": 3, "y": 4.5, "z": "hi", "u": true, "v": false }, 25 [ { "x": 3, "y": 4.5, "z": "hi", "u": true, "v": false },
24 '{"x":3,"y":4.5,"z":"hi","u":true,"v":false}' ], 26 '{"x":3,"y":4.5,"z":"hi","u":true,"v":false}' ],
25 [ { "x": null }, '{"x":null}' ], 27 [ { "x": null }, '{"x":null}' ],
26 [ { "x": {} }, '{"x":{}}' ], 28 [ { "x": {} }, '{"x":{}}' ],
27 // Note that -0.0 won't be treated the same in JS. The Json spec seems to 29 // Note that -0.0 won't be treated the same in JS. The Json spec seems to
28 // allow it, though. 30 // allow it, though.
29 [ { "hi there": 499, "'":-0.0 }, '{"hi there":499,"\'":-0.0}' ], 31 [ { "hi there": 499, "'":-0.0 }, '{"hi there":499,"\'":-0.0}' ],
30 [ r'\foo', r'"\\foo"' ], 32 [ r'\foo', r'"\\foo"' ],
31 ]; 33 ];
32 34
33 class MyStringConversionSink extends StringConversionSinkBase { 35 Stream<String> encode(Object o) {
34 var buffer = new StringBuffer(); 36 var encoder = new JsonEncoder();
35 var callback; 37 StreamController controller;
36 38 controller = new StreamController(onListen: () {
37 MyStringConversionSink(this.callback); 39 controller.add(o);
38 40 controller.close();
39 addSlice(str, start, end, bool isLast) { 41 });
40 buffer.write(str.substring(start, end)); 42 return controller.stream.transform(encoder);
41 if (isLast) close();
42 }
43
44 close() {
45 callback(buffer.toString());
46 }
47 } 43 }
48 44
49 String encode(Object o) { 45 void testNoPause(String expected, Object o) {
50 var result; 46 asyncStart();
51 var encoder = new JsonEncoder(); 47 Stream stream = encode(o);
52 ChunkedConversionSink stringSink = 48 stream.toList().then((list) {
53 new MyStringConversionSink((x) => result = x); 49 StringBuffer buffer = new StringBuffer();
54 var objectSink = new JsonEncoder().startChunkedConversion(stringSink); 50 buffer.writeAll(list);
55 objectSink.add(o); 51 Expect.stringEquals(expected, buffer.toString());
56 objectSink.close(); 52 asyncEnd();
57 return result; 53 });
58 } 54 }
59 55
60 String encode2(Object o) { 56 void testWithPause(String expected, Object o) {
61 var result; 57 asyncStart();
62 var encoder = new JsonEncoder(); 58 Stream stream = encode(o);
63 ChunkedConversionSink stringSink = 59 StringBuffer buffer = new StringBuffer();
64 new StringConversionSink.withCallback((x) => result = x); 60 var sub;
65 var objectSink = encoder.startChunkedConversion(stringSink); 61 sub = stream.listen((x) {
66 objectSink.add(o); 62 buffer.write(x);
67 objectSink.close(); 63 sub.pause(new Future.delayed(Duration.ZERO));
68 return result; 64 }, onDone: () {
65 Expect.stringEquals(expected, buffer.toString());
66 asyncEnd();
67 });
69 } 68 }
70 69
71 main() { 70 void main() {
72 for (var test in TESTS) { 71 for (var test in TESTS) {
73 var o = test[0]; 72 var o = test[0];
74 var expected = test[1]; 73 var expected = test[1];
75 Expect.equals(expected, encode(o)); 74 testNoPause(expected, o);
76 Expect.equals(expected, encode2(o)); 75 testWithPause(expected, o);
77 } 76 }
78 } 77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698