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 = []; |
10 | 11 |
11 message(path, reason) => path == '' | 12 message(path, reason) => path == '' |
12 ? reason | 13 ? reason |
13 : reason == null ? "path: $path" : "path: $path, $reason"; | 14 : reason == null ? "path: $path" : "path: $path, $reason"; |
14 | 15 |
15 walk(path, expected, actual) { | 16 walk(path, expected, actual) { |
16 if (expected is String || expected is num || expected == null) { | 17 if (expected is String || expected is num || expected == null) { |
17 Expect.equals(expected, actual, message(path, 'not equal')); | 18 expect(actual, equals(expected), reason: message(path, 'not equal')); |
18 return; | 19 return; |
19 } | 20 } |
20 | 21 |
21 // Cycle or DAG? | 22 // Cycle or DAG? |
22 for (int i = 0; i < eItems.length; i++) { | 23 for (int i = 0; i < eItems.length; i++) { |
23 if (expected === eItems[i]) { | 24 if (expected === eItems[i]) { |
24 Expect.identical(aItems[i], actual, | 25 expect(actual, same(aItems[i]), |
25 message(path, 'missing back or side edge')); | 26 reason: message(path, 'missing back or side edge')); |
26 return; | 27 return; |
27 } | 28 } |
28 } | 29 } |
29 for (int i = 0; i < aItems.length; i++) { | 30 for (int i = 0; i < aItems.length; i++) { |
30 if (actual === aItems[i]) { | 31 if (actual === aItems[i]) { |
31 Expect.identical(eItems[i], expected, | 32 expect(expected, same(eItems[i]), |
32 message(path, 'extra back or side edge')); | 33 reason: message(path, 'extra back or side edge')); |
33 return; | 34 return; |
34 } | 35 } |
35 } | 36 } |
36 eItems.add(expected); | 37 eItems.add(expected); |
37 aItems.add(actual); | 38 aItems.add(actual); |
38 | 39 |
39 if (expected is List) { | 40 if (expected is List) { |
40 Expect.isTrue(actual is List, message(path, '$actual is List')); | 41 expect(actual, isList, reason: message(path, '$actual is List')); |
41 Expect.equals(expected.length, actual.length, | 42 expect(actual.length, expected.length, |
42 message(path, 'different list lengths')); | 43 reason: message(path, 'different list lengths')); |
43 for (var i = 0; i < expected.length; i++) { | 44 for (var i = 0; i < expected.length; i++) { |
44 walk('$path[$i]', expected[i], actual[i]); | 45 walk('$path[$i]', expected[i], actual[i]); |
45 } | 46 } |
46 return; | 47 return; |
47 } | 48 } |
48 | 49 |
49 if (expected is Map) { | 50 if (expected is Map) { |
50 Expect.isTrue(actual is Map, message(path, '$actual is Map')); | 51 expect(actual, isMap, reason: message(path, '$actual is Map')); |
51 for (var key in expected.keys) { | 52 for (var key in expected.keys) { |
52 if (!actual.containsKey(key)) { | 53 if (!actual.containsKey(key)) { |
53 Expect.fail(message(path, 'missing key "$key"')); | 54 expect(false, isTrue, reason: message(path, 'missing key "$key"')); |
54 } | 55 } |
55 walk('$path["$key"]', expected[key], actual[key]); | 56 walk('$path["$key"]', expected[key], actual[key]); |
56 } | 57 } |
57 for (var key in actual.keys) { | 58 for (var key in actual.keys) { |
58 if (!expected.containsKey(key)) { | 59 if (!expected.containsKey(key)) { |
59 Expect.fail(message(path, 'extra key "$key"')); | 60 expect(false, isTrue, reason: message(path, 'extra key "$key"')); |
60 } | 61 } |
61 } | 62 } |
62 return; | 63 return; |
63 } | 64 } |
64 | 65 |
65 Expect.fail('Unhandled type: $expected'); | 66 expect(false, isTrue, reason: 'Unhandled type: $expected'); |
66 } | 67 } |
67 | 68 |
68 walk('', expected, actual); | 69 walk('', expected, actual); |
69 } | 70 } |
OLD | NEW |