Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 | |
| 7 import 'package:front_end/src/async_dependency_walker.dart'; | |
| 8 import 'package:test/test.dart'; | |
| 9 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| 10 | |
| 11 main() { | |
| 12 defineReflectiveSuite(() { | |
| 13 defineReflectiveTests(AsyncDependencyWalkerTest); | |
| 14 }); | |
| 15 } | |
| 16 | |
| 17 @reflectiveTest | |
| 18 class AsyncDependencyWalkerTest { | |
| 19 final nodes = <String, TestNode>{}; | |
| 20 | |
| 21 Future checkGraph( | |
| 22 Map<String, List<String>> graph, | |
| 23 String startingNodeName, | |
| 24 List<List<String>> expectedEvaluations, | |
| 25 List<bool> expectedSccFlags) async { | |
| 26 makeGraph(graph); | |
| 27 var walker = await walk(startingNodeName); | |
| 28 expect(walker._evaluations, expectedEvaluations.map((x) => x.toSet())); | |
| 29 expect(walker._sccFlags, expectedSccFlags); | |
| 30 } | |
| 31 | |
| 32 TestNode getNode(String name) => | |
| 33 nodes.putIfAbsent(name, () => new TestNode(name)); | |
| 34 | |
| 35 void makeGraph(Map<String, List<String>> graph) { | |
| 36 graph.forEach((name, deps) { | |
| 37 var node = getNode(name); | |
| 38 for (var dep in deps) { | |
| 39 node._dependencies.add(getNode(dep)); | |
| 40 } | |
| 41 }); | |
| 42 } | |
| 43 | |
| 44 Future test_complex_graph() async { | |
|
scheglov
2016/12/06 19:24:14
For tests I usually don't specify the Future retur
Paul Berry
2016/12/06 20:28:45
No benefit I'm aware of. I went ahead and removed
| |
| 45 await checkGraph( | |
| 46 { | |
| 47 'a': ['b', 'c'], | |
| 48 'b': ['c', 'd'], | |
| 49 'c': [], | |
| 50 'd': ['c', 'e'], | |
| 51 'e': ['b', 'f'], | |
| 52 'f': ['c', 'd'] | |
| 53 }, | |
| 54 'a', | |
| 55 [ | |
| 56 ['c'], | |
| 57 ['b', 'd', 'e', 'f'], | |
| 58 ['a'] | |
| 59 ], | |
| 60 [false, true, false]); | |
| 61 } | |
| 62 | |
| 63 Future test_diamond() async { | |
| 64 await checkGraph( | |
| 65 { | |
| 66 'a': ['b', 'c'], | |
| 67 'b': ['d'], | |
| 68 'c': ['d'], | |
| 69 'd': [] | |
| 70 }, | |
| 71 'a', | |
| 72 [ | |
| 73 ['d'], | |
| 74 ['b'], | |
| 75 ['c'], | |
| 76 ['a'] | |
| 77 ], | |
| 78 [false, false, false, false]); | |
| 79 } | |
| 80 | |
| 81 Future test_singleNode() async { | |
| 82 await checkGraph( | |
| 83 {'a': []}, | |
| 84 'a', | |
| 85 [ | |
| 86 ['a'] | |
| 87 ], | |
| 88 [false]); | |
| 89 } | |
| 90 | |
| 91 Future test_singleNodeWithTrivialCycle() async { | |
| 92 await checkGraph( | |
| 93 { | |
| 94 'a': ['a'] | |
| 95 }, | |
| 96 'a', | |
| 97 [ | |
| 98 ['a'] | |
| 99 ], | |
| 100 [true]); | |
| 101 } | |
| 102 | |
| 103 Future test_threeNodesWithCircularDependency() async { | |
| 104 await checkGraph( | |
| 105 { | |
| 106 'a': ['b'], | |
| 107 'b': ['c'], | |
| 108 'c': ['a'], | |
| 109 }, | |
| 110 'a', | |
| 111 [ | |
| 112 ['a', 'b', 'c'] | |
| 113 ], | |
| 114 [true]); | |
| 115 } | |
| 116 | |
| 117 Future test_twoBacklinksEarlierFirst() async { | |
| 118 // Test a graph A->B->C->D, where D points back to B and then C. | |
| 119 await checkGraph( | |
| 120 { | |
| 121 'a': ['b'], | |
| 122 'b': ['c'], | |
| 123 'c': ['d'], | |
| 124 'd': ['b', 'c'] | |
| 125 }, | |
| 126 'a', | |
| 127 [ | |
| 128 ['b', 'c', 'd'], | |
| 129 ['a'] | |
| 130 ], | |
| 131 [true, false]); | |
| 132 } | |
| 133 | |
| 134 Future test_twoBacklinksLaterFirst() async { | |
| 135 // Test a graph A->B->C->D, where D points back to C and then B. | |
| 136 await checkGraph( | |
| 137 { | |
| 138 'a': ['b'], | |
| 139 'b': ['c'], | |
| 140 'c': ['d'], | |
| 141 'd': ['c', 'b'] | |
| 142 }, | |
| 143 'a', | |
| 144 [ | |
| 145 ['b', 'c', 'd'], | |
| 146 ['a'] | |
| 147 ], | |
| 148 [true, false]); | |
| 149 } | |
| 150 | |
| 151 Future test_twoNodesWithCircularDependency() async { | |
| 152 await checkGraph( | |
| 153 { | |
| 154 'a': ['b'], | |
| 155 'b': ['a'] | |
| 156 }, | |
| 157 'a', | |
| 158 [ | |
| 159 ['a', 'b'] | |
| 160 ], | |
| 161 [true]); | |
| 162 } | |
| 163 | |
| 164 Future test_twoNodesWithSimpleDependency() async { | |
| 165 await checkGraph( | |
| 166 { | |
| 167 'a': ['b'], | |
| 168 'b': [] | |
| 169 }, | |
| 170 'a', | |
| 171 [ | |
| 172 ['b'], | |
| 173 ['a'] | |
| 174 ], | |
| 175 [false, false]); | |
| 176 } | |
| 177 | |
| 178 Future<TestWalker> walk(String startingNodeName) async { | |
| 179 var testWalker = new TestWalker(); | |
| 180 await testWalker.walk(getNode(startingNodeName)); | |
| 181 return testWalker; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 class TestNode extends Node<TestNode> { | |
| 186 final String _name; | |
| 187 | |
| 188 bool _computeDependenciesCalled = false; | |
| 189 | |
| 190 final _dependencies = <TestNode>[]; | |
| 191 | |
| 192 TestNode(this._name); | |
| 193 | |
| 194 @override | |
| 195 Future<List<TestNode>> computeDependencies() async { | |
| 196 expect(_computeDependenciesCalled, false); | |
| 197 _computeDependenciesCalled = true; | |
| 198 return _dependencies; | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 class TestWalker extends AsyncDependencyWalker<TestNode> { | |
| 203 final _evaluations = <Set<String>>[]; | |
| 204 final _sccFlags = <bool>[]; | |
| 205 | |
| 206 @override | |
| 207 Future evaluate(TestNode v) async { | |
| 208 _evaluations.add([v._name].toSet()); | |
| 209 _sccFlags.add(false); | |
| 210 } | |
| 211 | |
| 212 @override | |
| 213 Future evaluateScc(List<TestNode> scc) async { | |
| 214 var sccNames = scc.map((node) => node._name).toSet(); | |
| 215 // Make sure there were no duplicates | |
| 216 expect(sccNames.length, scc.length); | |
| 217 _evaluations.add(sccNames); | |
| 218 _sccFlags.add(true); | |
| 219 } | |
| 220 } | |
| OLD | NEW |