|
8 | 8 | from mypy.build import BuildManager, BuildSourceSet, State, order_ascc, sorted_components |
9 | 9 | from mypy.errors import Errors |
10 | 10 | from mypy.fscache import FileSystemCache |
11 | | -from mypy.graph_utils import strongly_connected_components, topsort |
| 11 | +from mypy.graph_utils import strongly_connected_components, topsort, topsort2 |
12 | 12 | from mypy.modulefinder import SearchPaths |
13 | 13 | from mypy.options import Options |
14 | 14 | from mypy.plugin import Plugin |
|
18 | 18 |
|
19 | 19 |
|
20 | 20 | class GraphSuite(Suite): |
| 21 | + def test_topsort_empty(self) -> None: |
| 22 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = {} |
| 23 | + assert_equal(list(topsort2(data)), []) |
| 24 | + |
21 | 25 | def test_topsort(self) -> None: |
22 | | - a = frozenset({"A"}) |
23 | | - b = frozenset({"B"}) |
24 | | - c = frozenset({"C"}) |
25 | | - d = frozenset({"D"}) |
26 | | - data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: {b, c}, b: {d}, c: {d}} |
27 | | - res = list(topsort(data)) |
28 | | - assert_equal(res, [{d}, {b, c}, {a}]) |
| 26 | + for topsort_func in [topsort, topsort2]: |
| 27 | + a = frozenset({"A"}) |
| 28 | + b = frozenset({"B"}) |
| 29 | + c = frozenset({"C"}) |
| 30 | + d = frozenset({"D"}) |
| 31 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: {b, c}, b: {d}, c: {d}} |
| 32 | + res = list(topsort_func(data)) |
| 33 | + assert_equal(res, [{d}, {b, c}, {a}]) |
| 34 | + |
| 35 | + def test_topsort_orphan(self) -> None: |
| 36 | + for topsort_func in [topsort, topsort2]: |
| 37 | + a = frozenset({"A"}) |
| 38 | + b = frozenset({"B"}) |
| 39 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: {b}} |
| 40 | + res = list(topsort_func(data)) |
| 41 | + assert_equal(res, [{b}, {a}]) |
| 42 | + |
| 43 | + def test_topsort_independent(self) -> None: |
| 44 | + for topsort_func in [topsort, topsort2]: |
| 45 | + a = frozenset({"A"}) |
| 46 | + b = frozenset({"B"}) |
| 47 | + c = frozenset({"C"}) |
| 48 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: set(), b: set(), c: set()} |
| 49 | + res = list(topsort_func(data)) |
| 50 | + assert_equal(res, [{a, b, c}]) |
| 51 | + |
| 52 | + def test_topsort_linear_chain(self) -> None: |
| 53 | + for topsort_func in [topsort, topsort2]: |
| 54 | + a = frozenset({"A"}) |
| 55 | + b = frozenset({"B"}) |
| 56 | + c = frozenset({"C"}) |
| 57 | + d = frozenset({"D"}) |
| 58 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = { |
| 59 | + a: {b}, |
| 60 | + b: {c}, |
| 61 | + c: {d}, |
| 62 | + d: set(), |
| 63 | + } |
| 64 | + res = list(topsort_func(data)) |
| 65 | + assert_equal(res, [{d}, {c}, {b}, {a}]) |
| 66 | + |
| 67 | + def test_topsort_self_dependency(self) -> None: |
| 68 | + for topsort_func in [topsort, topsort2]: |
| 69 | + a = frozenset({"A"}) |
| 70 | + b = frozenset({"B"}) |
| 71 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: {a, b}, b: set()} |
| 72 | + res = list(topsort_func(data)) |
| 73 | + assert_equal(res, [{b}, {a}]) |
| 74 | + |
| 75 | + def test_topsort_orphan_diamond(self) -> None: |
| 76 | + for topsort_func in [topsort, topsort2]: |
| 77 | + a = frozenset({"A"}) |
| 78 | + b = frozenset({"B"}) |
| 79 | + c = frozenset({"C"}) |
| 80 | + # B and C are orphans -- they appear only in values, not as keys. |
| 81 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: {b, c}} |
| 82 | + res = list(topsort_func(data)) |
| 83 | + assert_equal(res, [{b, c}, {a}]) |
| 84 | + |
| 85 | + def test_topsort_cycle(self) -> None: |
| 86 | + for topsort_func in [topsort, topsort2]: |
| 87 | + a = frozenset({"A"}) |
| 88 | + b = frozenset({"B"}) |
| 89 | + data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: {b}, b: {a}} |
| 90 | + with self.assertRaises(AssertionError): |
| 91 | + list(topsort_func(data)) |
29 | 92 |
|
30 | 93 | def test_scc(self) -> None: |
31 | 94 | vertices = {"A", "B", "C", "D"} |
|
0 commit comments