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

Unified Diff: tests/lib/convert/json_pretty_test.dart

Issue 224093007: dart:convert: support indented output w/ JsonEncoder (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: using string for indent Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
« sdk/lib/convert/json.dart ('K') | « sdk/lib/convert/json.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/convert/json_pretty_test.dart
diff --git a/tests/lib/convert/json_pretty_test.dart b/tests/lib/convert/json_pretty_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d82c0af90b85256e97250e5f016d7b96865652ff
--- /dev/null
+++ b/tests/lib/convert/json_pretty_test.dart
@@ -0,0 +1,98 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library json_pretty_test;
+
+import 'dart:convert';
+
+import 'package:matcher/matcher.dart';
+
+void main() {
+ _expect(null, 'null');
+
+ _expect([1, 2, 3, 4], '''
+[
+ 1,
+ 2,
+ 3,
+ 4
+]''');
+
+ _expect([true, null, 'hello', 42.42],
+ '''
+[
+ true,
+ null,
+ "hello",
+ 42.42
+]''');
+
+ _expect({"hello": [], "goodbye": {} } ,
+'''{
+ "hello": [],
+ "goodbye": {}
+}''');
+
+ _expect(["test", 1, 2, 33234.324, true, false, null, {
+ "test1": "test2",
+ "test3": "test4",
+ "grace": 5,
+ "shanna": [0, 1, 2]
+ }, {
+ "lib": "app.dart",
+ "src": ["foo.dart", "bar.dart"]
+ }],
+ '''[
+ "test",
+ 1,
+ 2,
+ 33234.324,
+ true,
+ false,
+ null,
+ {
+ "test1": "test2",
+ "test3": "test4",
+ "grace": 5,
+ "shanna": [
+ 0,
+ 1,
+ 2
+ ]
+ },
+ {
+ "lib": "app.dart",
+ "src": [
+ "foo.dart",
+ "bar.dart"
+ ]
+ }
+]''');
+}
+
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
+void _expect(Object object, String expected) {
+ var encoder = const JsonEncoder.withIndent(' ');
+ var prettyOutput = encoder.convert(object);
+
+ expect(prettyOutput, expected);
+
+ encoder = const JsonEncoder.withIndent('');
+
+ var flatOutput = encoder.convert(object);
+
+ var flatExpected = const LineSplitter().convert(expected)
+ .map((line) => line.trimLeft())
+ .join('\n');
+
+ expect(flatOutput, flatExpected);
+
+ var compactOutput = JSON.encode(object);
+
+ encoder = const JsonEncoder.withIndent(null);
+ expect(encoder.convert(object), compactOutput);
+
+ var prettyDecoded = JSON.decode(prettyOutput);
+
+ expect(JSON.encode(prettyDecoded), compactOutput);
+}
« sdk/lib/convert/json.dart ('K') | « sdk/lib/convert/json.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698