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

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

Issue 19883003: Add chunked conversion to converters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. 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:convert';
7
8 final TESTS = [
9 [ 5, '5' ],
10 [ -42, '-42' ],
11 [ 3.14, '3.14' ],
12 [ true, 'true' ],
13 [ false, 'false' ],
14 [ null, 'null' ],
15 [ 'quote"or\'', '"quote\\"or\'"' ],
16 [ '', '""' ],
17 [ [], "[]" ],
18 [ [3, -4.5, true, "hi", false], '[3,-4.5,true,"hi",false]' ],
19 [ [null], "[null]" ],
20 [ [[null]], "[[null]]" ],
21 [ [[3]], "[[3]]" ],
22 [ {}, "{}" ],
23 [ { "x": 3, "y": 4.5, "z": "hi", "u": true, "v": false },
24 '{"x":3,"y":4.5,"z":"hi","u":true,"v":false}' ],
25 [ { "x": null }, '{"x":null}' ],
26 [ { "x": {} }, '{"x":{}}' ],
27 // Note that -0.0 won't be treated the same in JS. The Json spec seems to
28 // allow it, though.
29 [ { "hi there": 499, "'":-0.0 }, '{"hi there":499,"\'":-0.0}' ],
30 [ r'\foo', r'"\\foo"' ],
31 ];
32
33 class MyStringConversionSink extends StringConversionSinkBase {
34 var buffer = new StringBuffer();
35 var callback;
36
37 MyStringConversionSink(this.callback);
38
39 addSlice(str, start, end, bool isLast) {
40 buffer.write(str.substring(start, end));
41 if (isLast) close();
42 }
43
44 close() {
45 callback(buffer.toString());
46 }
47 }
48
49 String encode(Object o) {
50 var result;
51 var encoder = new JsonEncoder();
52 ChunkedConversionSink stringSink =
53 new MyStringConversionSink((x) => result = x);
54 var objectSink = new JsonEncoder().startChunkedConversion(stringSink);
55 objectSink.add(o);
56 objectSink.close();
57 return result;
58 }
59
60 String encode2(Object o) {
61 var result;
62 var encoder = new JsonEncoder();
63 ChunkedConversionSink stringSink =
64 new StringConversionSink.withCallback((x) => result = x);
65 var objectSink = encoder.startChunkedConversion(stringSink);
66 objectSink.add(o);
67 objectSink.close();
68 return result;
69 }
70
71 main() {
72 for (var test in TESTS) {
73 var o = test[0];
74 var expected = test[1];
75 Expect.equals(expected, encode(o));
76 Expect.equals(expected, encode2(o));
77 }
78 }
OLDNEW
« no previous file with comments | « tests/lib/convert/chunked_conversion_json_decode1_test.dart ('k') | tests/lib/convert/chunked_conversion_utf82_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698