|
1
|
|
|
# coding: utf8 |
|
2
|
|
|
|
|
3
|
|
|
# Copyright 2013-2017 Vincent Jacques <[email protected]> |
|
4
|
|
|
|
|
5
|
|
|
from __future__ import division, absolute_import, print_function |
|
6
|
|
|
|
|
7
|
|
|
import re |
|
8
|
|
|
import textwrap |
|
9
|
|
|
import unittest |
|
10
|
|
|
|
|
11
|
|
|
from ActionTree import * |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class GraphTestCase(unittest.TestCase): |
|
15
|
|
|
def test_internal_graph_not_returned(self): |
|
16
|
|
|
g = DependencyGraph(Action("a")) |
|
17
|
|
|
g1 = g.get_graphviz_graph() |
|
18
|
|
|
self.assertEqual(g1.format, "pdf") |
|
19
|
|
|
g1.format = "png" |
|
20
|
|
|
self.assertEqual(g.get_graphviz_graph().format, "pdf") |
|
21
|
|
|
|
|
22
|
|
|
spaces = re.compile(r"\s+") |
|
23
|
|
|
|
|
24
|
|
|
def normalize(self, g): |
|
25
|
|
|
return self.spaces.sub(" ", g).strip() |
|
26
|
|
|
|
|
27
|
|
|
def assertGraphEqual(self, g, expected): |
|
28
|
|
|
self.assertEqual( |
|
29
|
|
|
self.normalize(g.source), |
|
30
|
|
|
self.normalize(expected), |
|
31
|
|
|
) |
|
32
|
|
|
|
|
33
|
|
|
def test_single_action(self): |
|
34
|
|
|
a = Action("a") |
|
35
|
|
|
|
|
36
|
|
|
self.assertGraphEqual( |
|
37
|
|
|
DependencyGraph(a).get_graphviz_graph(), |
|
38
|
|
|
""" |
|
39
|
|
|
digraph action { |
|
40
|
|
|
node [shape=box] |
|
41
|
|
|
0 [label=a] |
|
42
|
|
|
} |
|
43
|
|
|
""" |
|
44
|
|
|
) |
|
45
|
|
|
|
|
46
|
|
|
def test_dependency(self): |
|
47
|
|
|
b = Action("b") |
|
48
|
|
|
a = Action("a") |
|
49
|
|
|
a.add_dependency(b) |
|
50
|
|
|
|
|
51
|
|
|
self.assertGraphEqual( |
|
52
|
|
|
DependencyGraph(a).get_graphviz_graph(), |
|
53
|
|
|
""" |
|
54
|
|
|
digraph action { |
|
55
|
|
|
node [shape=box] |
|
56
|
|
|
0 [label=b] |
|
57
|
|
|
1 [label=a] |
|
58
|
|
|
1 -> 0 |
|
59
|
|
|
} |
|
60
|
|
|
""" |
|
61
|
|
|
) |
|
62
|
|
|
|
|
63
|
|
|
def test_add_dependency_after_constructing_graph(self): |
|
64
|
|
|
a = Action("a") |
|
65
|
|
|
g = DependencyGraph(a) |
|
66
|
|
|
a.add_dependency(Action("b")) |
|
67
|
|
|
|
|
68
|
|
|
self.assertGraphEqual( |
|
69
|
|
|
g.get_graphviz_graph(), |
|
70
|
|
|
""" |
|
71
|
|
|
digraph action { |
|
72
|
|
|
node [shape=box] |
|
73
|
|
|
0 [label=a] |
|
74
|
|
|
} |
|
75
|
|
|
""" |
|
76
|
|
|
) |
|
77
|
|
|
|
|
78
|
|
|
def test_diamond(self): |
|
79
|
|
|
for i in range(10): |
|
80
|
|
|
a = Action("a") |
|
81
|
|
|
b = Action("b") |
|
82
|
|
|
b.add_dependency(a) |
|
83
|
|
|
c = Action("c") |
|
84
|
|
|
c.add_dependency(a) |
|
85
|
|
|
d = Action("d") |
|
86
|
|
|
d.add_dependency(c) |
|
87
|
|
|
d.add_dependency(b) |
|
88
|
|
|
|
|
89
|
|
|
self.assertIn( |
|
90
|
|
|
self.normalize(DependencyGraph(d).get_graphviz_graph().source), |
|
91
|
|
|
[ |
|
92
|
|
|
self.normalize( |
|
93
|
|
|
""" |
|
94
|
|
|
digraph action { |
|
95
|
|
|
node [shape=box] |
|
96
|
|
|
0 [label=a] |
|
97
|
|
|
1 [label=b] |
|
98
|
|
|
1 -> 0 |
|
99
|
|
|
2 [label=c] |
|
100
|
|
|
2 -> 0 |
|
101
|
|
|
3 [label=d] |
|
102
|
|
|
3 -> 1 |
|
103
|
|
|
3 -> 2 |
|
104
|
|
|
} |
|
105
|
|
|
""" |
|
106
|
|
|
), |
|
107
|
|
|
self.normalize( |
|
108
|
|
|
""" |
|
109
|
|
|
digraph action { |
|
110
|
|
|
node [shape=box] |
|
111
|
|
|
0 [label=a] |
|
112
|
|
|
1 [label=c] |
|
113
|
|
|
1 -> 0 |
|
114
|
|
|
2 [label=b] |
|
115
|
|
|
2 -> 0 |
|
116
|
|
|
3 [label=d] |
|
117
|
|
|
3 -> 1 |
|
118
|
|
|
3 -> 2 |
|
119
|
|
|
} |
|
120
|
|
|
""" |
|
121
|
|
|
), |
|
122
|
|
|
] |
|
123
|
|
|
) |
|
124
|
|
|
|
|
125
|
|
|
def test_weird_string_label(self): |
|
126
|
|
|
a = Action("spaces and; semi=columns") |
|
127
|
|
|
|
|
128
|
|
|
self.assertGraphEqual( |
|
129
|
|
|
DependencyGraph(a).get_graphviz_graph(), |
|
130
|
|
|
""" |
|
131
|
|
|
digraph action { |
|
132
|
|
|
node [shape=box] |
|
133
|
|
|
0 [label="spaces and; semi=columns"] |
|
134
|
|
|
} |
|
135
|
|
|
""" |
|
136
|
|
|
) |
|
137
|
|
|
|
|
138
|
|
|
def test_None_label(self): |
|
139
|
|
|
a = Action(None) |
|
140
|
|
|
|
|
141
|
|
|
self.assertGraphEqual( |
|
142
|
|
|
DependencyGraph(a).get_graphviz_graph(), |
|
143
|
|
|
""" |
|
144
|
|
|
digraph action { |
|
145
|
|
|
node [shape=box] |
|
146
|
|
|
0 [shape=point] |
|
147
|
|
|
} |
|
148
|
|
|
""" |
|
149
|
|
|
) |
|
150
|
|
|
|
|
151
|
|
|
def test_None_label_twice(self): |
|
152
|
|
|
a = Action(None) |
|
153
|
|
|
b = Action(None) |
|
154
|
|
|
a.add_dependency(b) |
|
155
|
|
|
|
|
156
|
|
|
self.assertGraphEqual( |
|
157
|
|
|
DependencyGraph(a).get_graphviz_graph(), |
|
158
|
|
|
""" |
|
159
|
|
|
digraph action { |
|
160
|
|
|
node [shape=box] |
|
161
|
|
|
0 [shape=point] |
|
162
|
|
|
1 [shape=point] |
|
163
|
|
|
1 -> 0 |
|
164
|
|
|
} |
|
165
|
|
|
""" |
|
166
|
|
|
) |
|
167
|
|
|
|