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