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 _testIndentWithNullChar() { |
| 12 var encoder = const JsonEncoder.withIndent('\x00'); |
| 13 var encoded = encoder.convert([[],[[]]]); |
| 14 expect(encoded, "[\n\x00[],\n\x00[\n\x00\x00[]\n\x00]\n]"); |
| 15 } |
| 16 |
| 17 void main() { |
| 18 _testIndentWithNullChar(); |
| 19 |
| 20 _expect(null, 'null'); |
| 21 |
| 22 _expect([[],[[]]], ''' |
| 23 [ |
| 24 [], |
| 25 [ |
| 26 [] |
| 27 ] |
| 28 ]'''); |
| 29 |
| 30 _expect([1, 2, 3, 4], ''' |
| 31 [ |
| 32 1, |
| 33 2, |
| 34 3, |
| 35 4 |
| 36 ]'''); |
| 37 |
| 38 _expect([true, null, 'hello', 42.42], |
| 39 ''' |
| 40 [ |
| 41 true, |
| 42 null, |
| 43 "hello", |
| 44 42.42 |
| 45 ]'''); |
| 46 |
| 47 _expect({"hello": [], "goodbye": {} } , |
| 48 '''{ |
| 49 "hello": [], |
| 50 "goodbye": {} |
| 51 }'''); |
| 52 |
| 53 _expect(["test", 1, 2, 33234.324, true, false, null, { |
| 54 "test1": "test2", |
| 55 "test3": "test4", |
| 56 "grace": 5, |
| 57 "shanna": [0, 1, 2] |
| 58 }, { |
| 59 "lib": "app.dart", |
| 60 "src": ["foo.dart", "bar.dart"] |
| 61 }], |
| 62 '''[ |
| 63 "test", |
| 64 1, |
| 65 2, |
| 66 33234.324, |
| 67 true, |
| 68 false, |
| 69 null, |
| 70 { |
| 71 "test1": "test2", |
| 72 "test3": "test4", |
| 73 "grace": 5, |
| 74 "shanna": [ |
| 75 0, |
| 76 1, |
| 77 2 |
| 78 ] |
| 79 }, |
| 80 { |
| 81 "lib": "app.dart", |
| 82 "src": [ |
| 83 "foo.dart", |
| 84 "bar.dart" |
| 85 ] |
| 86 } |
| 87 ]'''); |
| 88 } |
| 89 |
| 90 void _expect(Object object, String expected) { |
| 91 var encoder = const JsonEncoder.withIndent(' '); |
| 92 var prettyOutput = encoder.convert(object); |
| 93 |
| 94 expect(prettyOutput, expected); |
| 95 |
| 96 encoder = const JsonEncoder.withIndent(''); |
| 97 |
| 98 var flatOutput = encoder.convert(object); |
| 99 |
| 100 var flatExpected = const LineSplitter().convert(expected) |
| 101 .map((line) => line.trimLeft()) |
| 102 .join('\n'); |
| 103 |
| 104 expect(flatOutput, flatExpected); |
| 105 |
| 106 var compactOutput = JSON.encode(object); |
| 107 |
| 108 encoder = const JsonEncoder.withIndent(null); |
| 109 expect(encoder.convert(object), compactOutput); |
| 110 |
| 111 var prettyDecoded = JSON.decode(prettyOutput); |
| 112 |
| 113 expect(JSON.encode(prettyDecoded), compactOutput); |
| 114 } |
OLD | NEW |