| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Note: This test relies on LF line endings in the source file. | 5 // Note: This test relies on LF line endings in the source file. |
| 6 | 6 |
| 7 library json_pretty_test; | 7 library json_pretty_test; |
| 8 | 8 |
| 9 import 'dart:convert'; | 9 import 'dart:convert'; |
| 10 | 10 |
| 11 import "package:expect/expect.dart"; | 11 import "package:expect/expect.dart"; |
| 12 | 12 |
| 13 void _testIndentWithNullChar() { | 13 void _testIndentWithNullChar() { |
| 14 var encoder = const JsonEncoder.withIndent('\x00'); | 14 var encoder = const JsonEncoder.withIndent('\x00'); |
| 15 var encoded = encoder.convert([[],[[]]]); | 15 var encoded = encoder.convert([ |
| 16 [], |
| 17 [[]] |
| 18 ]); |
| 16 Expect.equals("[\n\x00[],\n\x00[\n\x00\x00[]\n\x00]\n]", encoded); | 19 Expect.equals("[\n\x00[],\n\x00[\n\x00\x00[]\n\x00]\n]", encoded); |
| 17 } | 20 } |
| 18 | 21 |
| 19 void main() { | 22 void main() { |
| 20 _testIndentWithNullChar(); | 23 _testIndentWithNullChar(); |
| 21 | 24 |
| 22 _expect(null, 'null'); | 25 _expect(null, 'null'); |
| 23 | 26 |
| 24 _expect([[],[[]]], ''' | 27 _expect( |
| 28 [ |
| 29 [], |
| 30 [[]] |
| 31 ], |
| 32 ''' |
| 25 [ | 33 [ |
| 26 [], | 34 [], |
| 27 [ | 35 [ |
| 28 [] | 36 [] |
| 29 ] | 37 ] |
| 30 ]'''); | 38 ]'''); |
| 31 | 39 |
| 32 _expect([1, 2, 3, 4], ''' | 40 _expect( |
| 41 [1, 2, 3, 4], |
| 42 ''' |
| 33 [ | 43 [ |
| 34 1, | 44 1, |
| 35 2, | 45 2, |
| 36 3, | 46 3, |
| 37 4 | 47 4 |
| 38 ]'''); | 48 ]'''); |
| 39 | 49 |
| 40 _expect([true, null, 'hello', 42.42], | 50 _expect( |
| 51 [true, null, 'hello', 42.42], |
| 41 ''' | 52 ''' |
| 42 [ | 53 [ |
| 43 true, | 54 true, |
| 44 null, | 55 null, |
| 45 "hello", | 56 "hello", |
| 46 42.42 | 57 42.42 |
| 47 ]'''); | 58 ]'''); |
| 48 | 59 |
| 49 _expect({"hello": [], "goodbye": {} } , | 60 _expect( |
| 50 '''{ | 61 {"hello": [], "goodbye": {}}, |
| 62 '''{ |
| 51 "hello": [], | 63 "hello": [], |
| 52 "goodbye": {} | 64 "goodbye": {} |
| 53 }'''); | 65 }'''); |
| 54 | 66 |
| 55 _expect(["test", 1, 2, 33234.324, true, false, null, { | 67 _expect( |
| 56 "test1": "test2", | 68 [ |
| 57 "test3": "test4", | 69 "test", |
| 58 "grace": 5, | 70 1, |
| 59 "shanna": [0, 1, 2] | 71 2, |
| 60 }, { | 72 33234.324, |
| 61 "lib": "app.dart", | 73 true, |
| 62 "src": ["foo.dart", "bar.dart"] | 74 false, |
| 63 }], | 75 null, |
| 64 '''[ | 76 { |
| 77 "test1": "test2", |
| 78 "test3": "test4", |
| 79 "grace": 5, |
| 80 "shanna": [0, 1, 2] |
| 81 }, |
| 82 { |
| 83 "lib": "app.dart", |
| 84 "src": ["foo.dart", "bar.dart"] |
| 85 } |
| 86 ], |
| 87 '''[ |
| 65 "test", | 88 "test", |
| 66 1, | 89 1, |
| 67 2, | 90 2, |
| 68 33234.324, | 91 33234.324, |
| 69 true, | 92 true, |
| 70 false, | 93 false, |
| 71 null, | 94 null, |
| 72 { | 95 { |
| 73 "test1": "test2", | 96 "test1": "test2", |
| 74 "test3": "test4", | 97 "test3": "test4", |
| (...skipping 17 matching lines...) Expand all Loading... |
| 92 void _expect(Object object, String expected) { | 115 void _expect(Object object, String expected) { |
| 93 var encoder = const JsonEncoder.withIndent(' '); | 116 var encoder = const JsonEncoder.withIndent(' '); |
| 94 var prettyOutput = encoder.convert(object); | 117 var prettyOutput = encoder.convert(object); |
| 95 | 118 |
| 96 Expect.equals(expected, prettyOutput); | 119 Expect.equals(expected, prettyOutput); |
| 97 | 120 |
| 98 encoder = const JsonEncoder.withIndent(''); | 121 encoder = const JsonEncoder.withIndent(''); |
| 99 | 122 |
| 100 var flatOutput = encoder.convert(object); | 123 var flatOutput = encoder.convert(object); |
| 101 | 124 |
| 102 var flatExpected = const LineSplitter().convert(expected) | 125 var flatExpected = const LineSplitter() |
| 126 .convert(expected) |
| 103 .map((line) => line.trimLeft()) | 127 .map((line) => line.trimLeft()) |
| 104 .join('\n'); | 128 .join('\n'); |
| 105 | 129 |
| 106 Expect.equals(flatExpected, flatOutput); | 130 Expect.equals(flatExpected, flatOutput); |
| 107 | 131 |
| 108 var compactOutput = JSON.encode(object); | 132 var compactOutput = JSON.encode(object); |
| 109 | 133 |
| 110 encoder = const JsonEncoder.withIndent(null); | 134 encoder = const JsonEncoder.withIndent(null); |
| 111 Expect.equals(compactOutput, encoder.convert(object)); | 135 Expect.equals(compactOutput, encoder.convert(object)); |
| 112 | 136 |
| 113 var prettyDecoded = JSON.decode(prettyOutput); | 137 var prettyDecoded = JSON.decode(prettyOutput); |
| 114 | 138 |
| 115 Expect.equals(compactOutput, JSON.encode(prettyDecoded)); | 139 Expect.equals(compactOutput, JSON.encode(prettyDecoded)); |
| 116 } | 140 } |
| OLD | NEW |