OLD | NEW |
1 #library('TestUtils'); | 1 #library('TestUtils'); |
| 2 #import('../../pkg/unittest/unittest.dart'); |
2 | 3 |
3 /** | 4 /** |
4 * Verifies that [actual] has the same graph structure as [expected]. | 5 * Verifies that [actual] has the same graph structure as [expected]. |
5 * Detects cycles and DAG structure in Maps and Lists. | 6 * Detects cycles and DAG structure in Maps and Lists. |
6 */ | 7 */ |
7 verifyGraph(expected, actual) { | 8 verifyGraph(expected, actual) { |
8 var eItems = []; | 9 var eItems = []; |
9 var aItems = []; | 10 var aItems = []; |
| 11 var isMap = new isInstanceOf<Map>('Map'); |
| 12 var isList = new isInstanceOf<List>('List'); |
10 | 13 |
11 message(path, reason) => path == '' | 14 message(path, reason) => path == '' |
12 ? reason | 15 ? reason |
13 : reason == null ? "path: $path" : "path: $path, $reason"; | 16 : reason == null ? "path: $path" : "path: $path, $reason"; |
14 | 17 |
15 walk(path, expected, actual) { | 18 walk(path, expected, actual) { |
16 if (expected is String || expected is num || expected == null) { | 19 if (expected is String || expected is num || expected == null) { |
17 Expect.equals(expected, actual, message(path, 'not equal')); | 20 expect(actual, equals(expected), reason: message(path, 'not equal')); |
18 return; | 21 return; |
19 } | 22 } |
20 | 23 |
21 // Cycle or DAG? | 24 // Cycle or DAG? |
22 for (int i = 0; i < eItems.length; i++) { | 25 for (int i = 0; i < eItems.length; i++) { |
23 if (expected === eItems[i]) { | 26 if (expected === eItems[i]) { |
24 Expect.identical(aItems[i], actual, | 27 expect(actual, same(aItems[i]), |
25 message(path, 'missing back or side edge')); | 28 reason: message(path, 'missing back or side edge')); |
26 return; | 29 return; |
27 } | 30 } |
28 } | 31 } |
29 for (int i = 0; i < aItems.length; i++) { | 32 for (int i = 0; i < aItems.length; i++) { |
30 if (actual === aItems[i]) { | 33 if (actual === aItems[i]) { |
31 Expect.identical(eItems[i], expected, | 34 expect(expected, same(eItems[i]), |
32 message(path, 'extra back or side edge')); | 35 reason: message(path, 'extra back or side edge')); |
33 return; | 36 return; |
34 } | 37 } |
35 } | 38 } |
36 eItems.add(expected); | 39 eItems.add(expected); |
37 aItems.add(actual); | 40 aItems.add(actual); |
38 | 41 |
39 if (expected is List) { | 42 if (expected is List) { |
40 Expect.isTrue(actual is List, message(path, '$actual is List')); | 43 expect(actual, isList, reason: message(path, '$actual is List')); |
41 Expect.equals(expected.length, actual.length, | 44 expect(actual.length, expected.length, |
42 message(path, 'different list lengths')); | 45 reason: message(path, 'different list lengths')); |
43 for (var i = 0; i < expected.length; i++) { | 46 for (var i = 0; i < expected.length; i++) { |
44 walk('$path[$i]', expected[i], actual[i]); | 47 walk('$path[$i]', expected[i], actual[i]); |
45 } | 48 } |
46 return; | 49 return; |
47 } | 50 } |
48 | 51 |
49 if (expected is Map) { | 52 if (expected is Map) { |
50 Expect.isTrue(actual is Map, message(path, '$actual is Map')); | 53 expect(actual, isMap, reason: message(path, '$actual is Map')); |
51 for (var key in expected.keys) { | 54 for (var key in expected.keys) { |
52 if (!actual.containsKey(key)) { | 55 if (!actual.containsKey(key)) { |
53 Expect.fail(message(path, 'missing key "$key"')); | 56 expect(false, isTrue, reason: message(path, 'missing key "$key"')); |
54 } | 57 } |
55 walk('$path["$key"]', expected[key], actual[key]); | 58 walk('$path["$key"]', expected[key], actual[key]); |
56 } | 59 } |
57 for (var key in actual.keys) { | 60 for (var key in actual.keys) { |
58 if (!expected.containsKey(key)) { | 61 if (!expected.containsKey(key)) { |
59 Expect.fail(message(path, 'extra key "$key"')); | 62 expect(false, isTrue, reason: message(path, 'extra key "$key"')); |
60 } | 63 } |
61 } | 64 } |
62 return; | 65 return; |
63 } | 66 } |
64 | 67 |
65 Expect.fail('Unhandled type: $expected'); | 68 expect(false, isTrue, reason: 'Unhandled type: $expected'); |
66 } | 69 } |
67 | 70 |
68 walk('', expected, actual); | 71 walk('', expected, actual); |
69 } | 72 } |
OLD | NEW |