forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestgraph.py
More file actions
85 lines (76 loc) · 3.09 KB
/
testgraph.py
File metadata and controls
85 lines (76 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Test cases for graph processing code in build.py."""
import sys
from typing import AbstractSet, Dict, Set, List
from mypy.test.helpers import assert_equal, Suite
from mypy.build import BuildManager, State, BuildSourceSet
from mypy.modulefinder import SearchPaths
from mypy.build import topsort, strongly_connected_components, sorted_components, order_ascc
from mypy.version import __version__
from mypy.options import Options
from mypy.report import Reports
from mypy.plugin import Plugin
from mypy.errors import Errors
from mypy.fscache import FileSystemCache
class GraphSuite(Suite):
def test_topsort(self) -> None:
a = frozenset({'A'})
b = frozenset({'B'})
c = frozenset({'C'})
d = frozenset({'D'})
data = {a: {b, c}, b: {d}, c: {d}} # type: Dict[AbstractSet[str], Set[AbstractSet[str]]]
res = list(topsort(data))
assert_equal(res, [{d}, {b, c}, {a}])
def test_scc(self) -> None:
vertices = {'A', 'B', 'C', 'D'}
edges = {'A': ['B', 'C'],
'B': ['C'],
'C': ['B', 'D'],
'D': []} # type: Dict[str, List[str]]
sccs = set(frozenset(x) for x in strongly_connected_components(vertices, edges))
assert_equal(sccs,
{frozenset({'A'}),
frozenset({'B', 'C'}),
frozenset({'D'})})
def _make_manager(self) -> BuildManager:
errors = Errors()
options = Options()
fscache = FileSystemCache()
search_paths = SearchPaths((), (), (), ())
manager = BuildManager(
data_dir='',
search_paths=search_paths,
ignore_prefix='',
source_set=BuildSourceSet([]),
reports=Reports('', {}),
options=options,
version_id=__version__,
plugin=Plugin(options),
plugins_snapshot={},
errors=errors,
flush_errors=lambda msgs, serious: None,
fscache=fscache,
stdout=sys.stdout,
stderr=sys.stderr,
)
return manager
def test_sorted_components(self) -> None:
manager = self._make_manager()
graph = {'a': State('a', None, 'import b, c', manager),
'd': State('d', None, 'pass', manager),
'b': State('b', None, 'import c', manager),
'c': State('c', None, 'import b, d', manager),
}
res = sorted_components(graph)
assert_equal(res, [frozenset({'d'}), frozenset({'c', 'b'}), frozenset({'a'})])
def test_order_ascc(self) -> None:
manager = self._make_manager()
graph = {'a': State('a', None, 'import b, c', manager),
'd': State('d', None, 'def f(): import a', manager),
'b': State('b', None, 'import c', manager),
'c': State('c', None, 'import b, d', manager),
}
res = sorted_components(graph)
assert_equal(res, [frozenset({'a', 'd', 'c', 'b'})])
ascc = res[0]
scc = order_ascc(graph, ascc)
assert_equal(scc, ['d', 'c', 'b', 'a'])