/
parser_test.rb
309 lines (257 loc) · 7.21 KB
/
parser_test.rb
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require File.expand_path '../test_helper', __FILE__
context 'Parser' do
setup do
@graph1 = AsciiDag.parse <<'EOF'
A---B---C topic
/
D---E---F---G master
EOF
@graph2 = AsciiDag.parse <<'EOF'
B'---C' topic
/
D---E---A'---F master
EOF
@graph3 = AsciiDag.parse <<'EOF'
o---o---o---o---o master
| \
| o'--o'--o' topic
\
o---o---o---o---o next
EOF
@graph4 = AsciiDag.parse <<'EOF'
A---B---C topic
/ \
D---E---F---G---H master
EOF
@graph5 = AsciiDag.parse <<'EOF'
E-------F
\ \
G---H---I---J
\
L--M
EOF
@graph6 = AsciiDag.parse <<'EOF'
o--o--o <-- Branch A
/
o--o--o <-- master
\
o--o--o <-- Branch B
EOF
@graph7 = AsciiDag.parse <<'EOF'
o--o--O--o--o--o <-- origin
\ \
t--t--t--m <-- their branch:
EOF
@graph8 = AsciiDag.parse <<'EOF'
G-Y-G-W-W-W-X-X-X-X
\ /
W-W-B
/
Y---G-W---W
\ / \
Y-Y X-X-X-X
EOF
@graph9 = AsciiDag.parse <<'EOF'
H'--I'--J' topicB
/
| E---F---G topicA
|/
A---B---C---D master
EOF
@graph10 = AsciiDag.parse <<'EOF'
1 2 3
A-B-C
\6 7 8
F-G-H
1 2/
D---E
EOF
@graph11 = AsciiDag.parse <<'EOF'
P---o---o---M---x---x---W---x
\ /
A---B---C----------------D---E <-- fixed-up topic branch
EOF
@graph12 = AsciiDag.parse <<'EOF'
o---o---o---B
/
---o---1---o---o---o---A
EOF
@graph13 = AsciiDag.parse <<'EOF'
o---o---o---o---C
/
/ o---o---o---B
/ /
---2---1---o---o---o---A
EOF
@graph14 = AsciiDag.parse <<'EOF'
"master"
o---o
\ "topic"
o---o---o---o---o---o
EOF
@graph15 = AsciiDag.parse <<'EOF'
"topicB"
o---o---o---o---o---* (pretend merge)
/ /
/o---o---o----------'
|/ "topicA"
o---o"master"
\ "topic"
o---o---o---o---o---o
EOF
@graph16 = AsciiDag.parse <<'EOF'
---Z---o---X--...---o---A---o---o---Y*--...---o---B*--D*
EOF
@graph17 = AsciiDag.parse <<'EOF'
r1---r2---r3 remotes/git-svn
\
A---B master
EOF
end
test 'parses nodes' do
a = find_node @graph1, 'A'
e = find_node @graph1, 'E'
assert_equal [6, 2], a.position
assert_equal [e], a.parents
b = find_node @graph1, 'B'
assert_equal [10, 2], b.position
assert_equal [a], b.parents
d = find_node @graph1, 'D'
assert_equal [0, 0], d.position
assert_equal [], d.parents
end
test 'parses nodes with prime names' do
assert_equal [15, 2], find_node(@graph2, "B'").position
assert_equal [20, 2], find_node(@graph2, "C'").position
assert_equal [8, 0], find_node(@graph2, "A'").position
assert_equal [13, 0], find_node(@graph2, 'F').position
end
test 'does not count edges as nodes' do
['-', '/', '|', '\\'].each do |char|
(1..4).each do |i|
graph_name = "@graph#{i}"
graph = self.instance_variable_get(graph_name)
node = find_node(graph, char)
assert_nil node, "Found unexpected node #{node.inspect} in #{graph_name}"
end
end
end
test 'handles multiple nodes with the same label' do
os = find_all_nodes @graph3, 'o'
assert_equal 10, os.length
o_primes = find_all_nodes @graph3, "o'"
assert_equal 3, o_primes.length
origin = os[5]
assert_equal [0, 4], origin.position
assert_equal [], origin.parents
assert_equal [origin], os[6].parents
assert_equal [os[6]], os[0].parents
assert_equal [os[9]], o_primes[0].parents
assert_equal [o_primes[0]], o_primes[1].parents
end
test 'merge commits have multiple parents' do
c = find_node @graph4, 'C'
g = find_node @graph4, 'G'
h = find_node @graph4, 'H'
assert_equal [c, g], h.parents
end
test 'nodes get unique IDs' do
ids = {}
@graph3.nodes.each do |node|
assert !ids.has_key?(node.id), "Found duplicate IDs: #{node.inspect}, #{ids[node.id].inspect}"
ids[node.id] = node
end
end
test 'branch labels are separate from nodes' do
master = @graph1.branch_labels.find { |l| l.label == 'master' }
assert master
assert_nil find_node(@graph1, 'master')
end
test 'treats tabs as 8 spaces' do
l = find_node @graph5, 'L'
j = find_node @graph5, 'J'
assert_equal [j], l.parents
end
test 'recognizes branch label arrows' do
assert_nil find_node(@graph6, '<')
branch_a = find_branch_label @graph6, 'Branch A'
assert_not_nil branch_a
assert_equal [17, 4], branch_a.position
master = find_branch_label @graph6, 'master'
assert_not_nil master
assert_equal [9, 2], master.position
branch_b = find_branch_label @graph6, 'Branch B'
assert_not_nil branch_b
assert_equal [17, 0], branch_b.position
assert_nil find_node(@graph7, '<')
their_branch = find_branch_label @graph7, 'their branch:'
assert_not_nil their_branch
assert_equal [20, 0], their_branch.position
end
test 'should replace apostrophes with primes' do
a = find_node @graph2, "A'"
assert_equal "A′", a.dot_label
end
test 'should only follow edges in allowed directions' do
xs = find_all_nodes @graph8, 'X'
ws = find_all_nodes @graph8, 'W'
x = xs[4]
w = ws[6]
assert_equal [w], x.parents
w2 = ws[2]
w3 = ws[3]
assert_equal [w2], w3.parents
end
test 'should follow pipe underneath slash' do
h = find_node @graph9, "H'"
d = find_node @graph9, 'D'
assert_equal [d], h.parents
end
test 'should treat disconnected numbers as labels' do
six = find_branch_label @graph10, '6'
assert_not_nil six
seven = find_node @graph10, '7'
assert_nil seven
end
test 'should treat numbers with parents as nodes' do
one = find_node @graph12, '1'
assert_not_nil one
os = find_all_nodes @graph12, 'o'
assert_equal 7, os.length
assert_equal [one], os[1].parents
assert_equal [os[0]], one.parents
end
test 'should treat numbers that are parents as nodes' do
two = find_node @graph13, '2'
assert_not_nil two
end
test 'allows hyphens in arrowed branch labels' do
label = find_branch_label @graph11, 'fixed-up topic branch'
assert_not_nil label
end
test 'should remove quotes from branch labels' do
label = find_branch_label @graph14, 'master'
assert_equal 'master', label.label
assert_equal 'master', label.dot_label
end
test 'should not merge nodes with adjacent quoted branch labels' do
os = find_all_nodes @graph15, 'o'
o = os[7]
assert_equal [12, 2], o.position
assert_equal 'o', o.label
assert_equal [os[6]], o.parents
master = find_branch_label @graph15, 'master'
assert_not_nil master
end
test 'should allow asterisks in node names' do
b = find_node @graph16, 'B*'
d = find_node @graph16, 'D*'
assert_equal [b], d.parents
end
test 'should number-suffixed node names' do
r1 = find_node @graph17, 'r1'
r2 = find_node @graph17, 'r2'
assert_equal [], r1.parents
assert_equal [r1], r2.parents
end
end