Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, 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 library json_pretty_test; | |
| 6 | |
| 7 import 'dart:convert'; | |
| 8 | |
| 9 import 'package:matcher/matcher.dart'; | |
| 10 | |
| 11 void main() { | |
| 12 _expect(null, 'null'); | |
| 13 | |
| 14 _expect([1, 2, 3, 4], ''' | |
| 15 [ | |
| 16 1, | |
| 17 2, | |
| 18 3, | |
| 19 4 | |
| 20 ]'''); | |
| 21 | |
| 22 _expect([true, null, 'hello', 42.42], | |
| 23 ''' | |
| 24 [ | |
| 25 true, | |
| 26 null, | |
| 27 "hello", | |
| 28 42.42 | |
| 29 ]'''); | |
| 30 | |
| 31 _expect({"hello": [], "goodbye": {} } , | |
| 32 '''{ | |
| 33 "hello": [], | |
| 34 "goodbye": {} | |
| 35 }'''); | |
| 36 | |
| 37 _expect(["test", 1, 2, 33234.324, true, false, null, { | |
| 38 "test1": "test2", | |
| 39 "test3": "test4", | |
| 40 "grace": 5, | |
| 41 "shanna": [0, 1, 2] | |
| 42 }, { | |
| 43 "lib": "app.dart", | |
| 44 "src": ["foo.dart", "bar.dart"] | |
| 45 }], | |
| 46 '''[ | |
| 47 "test", | |
| 48 1, | |
| 49 2, | |
| 50 33234.324, | |
| 51 true, | |
| 52 false, | |
| 53 null, | |
| 54 { | |
| 55 "test1": "test2", | |
| 56 "test3": "test4", | |
| 57 "grace": 5, | |
| 58 "shanna": [ | |
| 59 0, | |
| 60 1, | |
| 61 2 | |
| 62 ] | |
| 63 }, | |
| 64 { | |
| 65 "lib": "app.dart", | |
| 66 "src": [ | |
| 67 "foo.dart", | |
| 68 "bar.dart" | |
| 69 ] | |
| 70 } | |
| 71 ]'''); | |
| 72 } | |
| 73 | |
|
Lasse Reichstein Nielsen
2014/04/05 19:34:43
Test with "\x00" as indentation.
Test [[],[[]]] fo
kevmoo
2014/04/05 20:08:58
Done.
The output from the null character seems to
| |
| 74 void _expect(Object object, String expected) { | |
| 75 var encoder = const JsonEncoder.withIndent(' '); | |
| 76 var prettyOutput = encoder.convert(object); | |
| 77 | |
| 78 expect(prettyOutput, expected); | |
| 79 | |
| 80 encoder = const JsonEncoder.withIndent(''); | |
| 81 | |
| 82 var flatOutput = encoder.convert(object); | |
| 83 | |
| 84 var flatExpected = const LineSplitter().convert(expected) | |
| 85 .map((line) => line.trimLeft()) | |
| 86 .join('\n'); | |
| 87 | |
| 88 expect(flatOutput, flatExpected); | |
| 89 | |
| 90 var compactOutput = JSON.encode(object); | |
| 91 | |
| 92 encoder = const JsonEncoder.withIndent(null); | |
| 93 expect(encoder.convert(object), compactOutput); | |
| 94 | |
| 95 var prettyDecoded = JSON.decode(prettyOutput); | |
| 96 | |
| 97 expect(JSON.encode(prettyDecoded), compactOutput); | |
| 98 } | |
| OLD | NEW |