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