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

Unified Diff: client/tests/client/json/json_tests.dart

Issue 8392022: Move JSON unit tests to new test API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Respond to review. Created 9 years, 2 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
« no previous file with comments | « client/tests/client/json/json_test.html ('k') | client/tests/client/json/web_json_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/tests/client/json/json_tests.dart
diff --git a/client/tests/client/json/json_tests.dart b/client/tests/client/json/json_tests.dart
index 294913d69950edc40b54cc60e9bc8b7eed10680d..9d0c1e0bd54802e4b142e49e8217194218c9f4d7 100644
--- a/client/tests/client/json/json_tests.dart
+++ b/client/tests/client/json/json_tests.dart
@@ -5,12 +5,162 @@
#library('json_tests');
#import('dart:json');
-#import('dart:html');
+#import('dart:html'); // TODO(rnystrom): Only needed to tell architecture.py
+ // that this is a web test. Come up with cleaner solution.
#import('../../../testing/unittest/unittest.dart');
-#source('json_test.dart');
-#source('web_json_test.dart');
-
main() {
- WebJsonTest.main();
+ test('Parse', () {
+ // Scalars.
+ expect(JSON.parse(' 5 ')).equals(5);
+ expect(JSON.parse(' -42 ')).equals(-42);
+ expect(JSON.parse(' 3e0 ')).equals(3);
+ expect(JSON.parse(' 3.14 ')).equals(3.14);
+ expect(JSON.parse('true ')).equals(true);
+ expect(JSON.parse(' false')).equals(false);
+ expect(JSON.parse(' null ')).equals(null);
+ expect(JSON.parse('\n\rnull\t')).equals(null);
+ expect(JSON.parse(' "hi there\\" bob" ')).equals('hi there" bob');
+ expect(JSON.parse(' "" ')).equals('');
+
+ // Lists.
+ expectValueEquals([], JSON.parse(' [] '));
+ expectValueEquals([], JSON.parse('[ ]'));
+ expectValueEquals([3, -4.5, true, 'hi', false],
+ JSON.parse(' [3, -4.5, true, "hi", false] '));
+ // Nulls are tricky.
+ expectValueEquals([null], JSON.parse('[null]'));
+ expectValueEquals([3, -4.5, null, true, 'hi', false],
+ JSON.parse(' [3, -4.5, null, true, "hi", false] '));
+ expectValueEquals([[null]], JSON.parse('[[null]]'));
+ expectValueEquals([[3], [], [null], ['hi', true]],
+ JSON.parse(' [ [3], [], [null], ["hi", true]] '));
+
+ // Maps.
+ expectValueEquals({}, JSON.parse(' {} '));
+ expectValueEquals({}, JSON.parse('{ }'));
+
+ expectValueEquals(
+ {'x':3, 'y':-4.5, 'z':'hi', 'u':true, 'v':false},
+ JSON.parse(' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '));
+ expectValueEquals({"x":3, "y":-4.5, "z":'hi'},
+ JSON.parse(' {"x":3, "y": -4.5, "z" : "hi" } '));
+ expectValueEquals({"z":'hi', "x":3, "y":-4.5},
+ JSON.parse(' {"y": -4.5, "z" : "hi" ,"x":3 } '));
+
+ expectValueEquals({' hi bob ':3, '':4.5},
+ JSON.parse('{ " hi bob " :3, "": 4.5}'));
+
+ expectValueEquals({'x':{}}, JSON.parse(' { "x" : { } } '));
+ expectValueEquals({'x':{}}, JSON.parse('{"x":{}}'));
+
+ // Nulls are tricky.
+ expectValueEquals({'w':null}, JSON.parse('{"w":null}'));
+ expectValueEquals({'x':{'w':null}}, JSON.parse('{"x":{"w":null}}'));
+ expectValueEquals(
+ {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false},
+ JSON.parse(' {"x":3, "y": -4.5, "z" : "hi",'
+ + '"w":null, "u" : true, "v": false } '));
+ expectValueEquals(
+ {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true},
+ 'v':null},
+ JSON.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], '
+ + '"z":"hi","w":{"c":null,"d":true}, "v":null}'));
+ });
+
+ test('stringify', () {
+ // Scalars.
+ expect(JSON.stringify(5)).equals('5');
+ expect(JSON.stringify(-42)).equals('-42');
+ // Dart does not guarantee a formatting for doubles,
+ // so reparse and compare to the original.
+ validateRoundTrip(3.14);
+ expect(JSON.stringify(true)).equals('true');
+ expect(JSON.stringify(false)).equals('false');
+ expect(JSON.stringify(null)).equals('null');
+ expect(JSON.stringify(' hi there" bob ')).equals('" hi there\\" bob "');
+ expect(JSON.stringify('')).equals('""');
+
+ // Lists.
+ expect(JSON.stringify([])).equals('[]');
+ expect(JSON.stringify(new List(0))).equals('[]');
+ expect(JSON.stringify(new List(3))).equals('[null,null,null]');
+ validateRoundTrip([3, -4.5, null, true, 'hi', false]);
+ Expect.equals('[[3],[],[null],["hi",true]]',
+ JSON.stringify([[3], [], [null], ['hi', true]]));
+
+ // Maps.
+ expect(JSON.stringify({})).equals('{}');
+ expect(JSON.stringify(new Map())).equals('{}');
+ expect(JSON.stringify({'x':{}})).equals('{"x":{}}');
+ expect(JSON.stringify({'x':{'a':3}})).equals('{"x":{"a":3}}');
+
+ // Dart does not guarantee an order on the keys
+ // of a map literal, so reparse and compare to the original Map.
+ validateRoundTrip(
+ {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false});
+ validateRoundTrip({"x":3, "y":-4.5, "z":'hi'});
+ validateRoundTrip({' hi bob ':3, '':4.5});
+ validateRoundTrip(
+ {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true},
+ 'v':null});
+ });
+
+ test('stringify throws if argument cannot be converted', () {
+ /**
+ * Checks that we get an exception (rather than silently returning null) if
+ * we try to stringify something that cannot be converted to json.
+ */
+ expectThrow(() {
+ JSON.stringify(new TestClass());
+ });
+ });
+}
+
+class TestClass {
+ int x;
+ String y;
+
+ TestClass() : x = 3, y = 'joe' { }
+}
+
+/**
+ * Checks that the argument can be converted to a JSON string and
+ * back, and produce something equivalent to the argument.
+ */
+validateRoundTrip(expected) {
+ expectValueEquals(expected, JSON.parse(JSON.stringify(expected)));
+}
+
+/**
+ * Checks that expected value-equals actual, where value-equals is
+ * the same as == except for Lists and Maps where it's recursive
+ * value-equals.
+ */
+// TODO(chambers): Move into Expect class.
+expectValueEquals(expected, actual, [String message = '']) {
+ if (expected is List && actual is List) {
+ int n = Math.min(expected.length, actual.length);
+ for (int i = 0; i < n; i++) {
+ expectValueEquals(expected[i], actual[i], 'at index ${i} ${message}');
+ }
+ // We check on length at the end in order to provide better error
+ // messages when an unexpected item is inserted in a list.
+ expect(actual.length).equals(expected.length);
+ } else if (expected is Map && actual is Map) {
+ expectIsContained(expected, actual, 'in actual ${message}');
+ expectIsContained(actual, expected, 'in expected ${message}', false);
+ } else {
+ expect(actual).equals(expected);
+ }
+}
+
+expectIsContained(Map expected, Map actual,
+ [String message = '', bool checkValues = true]) {
+ for (final key in expected.getKeys()) {
+ expect(actual.containsKey(key)).equals(true);
+ if (checkValues) {
+ expectValueEquals(expected[key], actual[key],'at key ${key} ${message}');
+ }
+ }
}
« no previous file with comments | « client/tests/client/json/json_test.html ('k') | client/tests/client/json/web_json_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698