File size: 7,933 Bytes
4cef980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
310
311
312
313
314
315
316
317
318
from textwrap import dedent

import pytest

import networkx as nx


def test_directed_tree_str():
    # Create a directed forest with labels
    graph = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph)
    for node in graph.nodes:
        graph.nodes[node]["label"] = "node_" + chr(ord("a") + node)

    node_target = dedent(
        """
        ╙── 0
            β”œβ”€β•Ό 1
            β”‚Β Β  β”œβ”€β•Ό 3
            β”‚Β Β  └─╼ 4
            └─╼ 2
                β”œβ”€β•Ό 5
                └─╼ 6
        """
    ).strip()

    label_target = dedent(
        """
        ╙── node_a
            β”œβ”€β•Ό node_b
            β”‚Β Β  β”œβ”€β•Ό node_d
            β”‚Β Β  └─╼ node_e
            └─╼ node_c
                β”œβ”€β•Ό node_f
                └─╼ node_g
        """
    ).strip()

    # Basic node case
    ret = nx.forest_str(graph, with_labels=False)
    print(ret)
    assert ret == node_target

    # Basic label case
    ret = nx.forest_str(graph, with_labels=True)
    print(ret)
    assert ret == label_target

    # Custom write function case
    lines = []
    ret = nx.forest_str(graph, write=lines.append, with_labels=False)
    assert ret is None
    assert lines == node_target.split("\n")

    # Smoke test to ensure passing the print function works. To properly test
    # this case we would need to capture stdout. (for potential reference
    # implementation see :class:`ubelt.util_stream.CaptureStdout`)
    ret = nx.forest_str(graph, write=print)
    assert ret is None


def test_empty_graph():
    assert nx.forest_str(nx.DiGraph()) == "β•™"
    assert nx.forest_str(nx.Graph()) == "β•™"


def test_directed_multi_tree_forest():
    tree1 = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph)
    tree2 = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph)
    forest = nx.disjoint_union_all([tree1, tree2])
    ret = nx.forest_str(forest)
    print(ret)

    target = dedent(
        """
        β•Ÿβ”€β”€ 0
        β•ŽΒ Β  β”œβ”€β•Ό 1
        β•ŽΒ Β  β”‚Β Β  β”œβ”€β•Ό 3
        β•ŽΒ Β  β”‚Β Β  └─╼ 4
        β•ŽΒ Β  └─╼ 2
        β•ŽΒ Β      β”œβ”€β•Ό 5
        β•ŽΒ Β      └─╼ 6
        ╙── 7
            β”œβ”€β•Ό 8
            β”‚Β Β  β”œβ”€β•Ό 10
            β”‚Β Β  └─╼ 11
            └─╼ 9
                β”œβ”€β•Ό 12
                └─╼ 13
        """
    ).strip()
    assert ret == target

    tree3 = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph)
    forest = nx.disjoint_union_all([tree1, tree2, tree3])
    ret = nx.forest_str(forest, sources=[0, 14, 7])
    print(ret)

    target = dedent(
        """
        β•Ÿβ”€β”€ 0
        β•ŽΒ Β  β”œβ”€β•Ό 1
        β•ŽΒ Β  β”‚Β Β  β”œβ”€β•Ό 3
        β•ŽΒ Β  β”‚Β Β  └─╼ 4
        β•ŽΒ Β  └─╼ 2
        β•ŽΒ Β      β”œβ”€β•Ό 5
        β•ŽΒ Β      └─╼ 6
        β•Ÿβ”€β”€ 14
        β•ŽΒ Β  β”œβ”€β•Ό 15
        β•ŽΒ Β  β”‚Β Β  β”œβ”€β•Ό 17
        β•ŽΒ Β  β”‚Β Β  └─╼ 18
        β•ŽΒ Β  └─╼ 16
        β•ŽΒ Β      β”œβ”€β•Ό 19
        β•ŽΒ Β      └─╼ 20
        ╙── 7
            β”œβ”€β•Ό 8
            β”‚Β Β  β”œβ”€β•Ό 10
            β”‚Β Β  └─╼ 11
            └─╼ 9
                β”œβ”€β•Ό 12
                └─╼ 13
        """
    ).strip()
    assert ret == target

    ret = nx.forest_str(forest, sources=[0, 14, 7], ascii_only=True)
    print(ret)

    target = dedent(
        """
        +-- 0
        :Β Β  |-> 1
        :Β Β  |Β Β  |-> 3
        :Β Β  |Β Β  L-> 4
        :Β Β  L-> 2
        :Β Β      |-> 5
        :Β Β      L-> 6
        +-- 14
        :Β Β  |-> 15
        :Β Β  |Β Β  |-> 17
        :Β Β  |Β Β  L-> 18
        :Β Β  L-> 16
        :Β Β      |-> 19
        :Β Β      L-> 20
        +-- 7
            |-> 8
            |Β Β  |-> 10
            |Β Β  L-> 11
            L-> 9
                |-> 12
                L-> 13
        """
    ).strip()
    assert ret == target


def test_undirected_multi_tree_forest():
    tree1 = nx.balanced_tree(r=2, h=2, create_using=nx.Graph)
    tree2 = nx.balanced_tree(r=2, h=2, create_using=nx.Graph)
    tree2 = nx.relabel_nodes(tree2, {n: n + len(tree1) for n in tree2.nodes})
    forest = nx.union(tree1, tree2)
    ret = nx.forest_str(forest, sources=[0, 7])
    print(ret)

    target = dedent(
        """
        β•Ÿβ”€β”€ 0
        β•ŽΒ Β  β”œβ”€β”€ 1
        β•ŽΒ Β  β”‚Β Β  β”œβ”€β”€ 3
        β•ŽΒ Β  β”‚Β Β  └── 4
        β•ŽΒ Β  └── 2
        β•ŽΒ Β      β”œβ”€β”€ 5
        β•ŽΒ Β      └── 6
        ╙── 7
            β”œβ”€β”€ 8
            β”‚Β Β  β”œβ”€β”€ 10
            β”‚Β Β  └── 11
            └── 9
                β”œβ”€β”€ 12
                └── 13
        """
    ).strip()
    assert ret == target

    ret = nx.forest_str(forest, sources=[0, 7], ascii_only=True)
    print(ret)

    target = dedent(
        """
        +-- 0
        :Β Β  |-- 1
        :Β Β  |Β Β  |-- 3
        :Β Β  |Β Β  L-- 4
        :Β Β  L-- 2
        :Β Β      |-- 5
        :Β Β      L-- 6
        +-- 7
            |-- 8
            |Β Β  |-- 10
            |Β Β  L-- 11
            L-- 9
                |-- 12
                L-- 13
        """
    ).strip()
    assert ret == target


def test_undirected_tree_str():
    # Create a directed forest with labels
    graph = nx.balanced_tree(r=2, h=2, create_using=nx.Graph)

    # arbitrary starting point
    nx.forest_str(graph)

    node_target0 = dedent(
        """
        ╙── 0
            β”œβ”€β”€ 1
            β”‚Β Β  β”œβ”€β”€ 3
            β”‚Β Β  └── 4
            └── 2
                β”œβ”€β”€ 5
                └── 6
        """
    ).strip()

    # defined starting point
    ret = nx.forest_str(graph, sources=[0])
    print(ret)
    assert ret == node_target0

    # defined starting point
    node_target2 = dedent(
        """
        ╙── 2
            β”œβ”€β”€ 0
            β”‚Β Β  └── 1
            β”‚Β Β      β”œβ”€β”€ 3
            β”‚Β Β      └── 4
            β”œβ”€β”€ 5
            └── 6
        """
    ).strip()
    ret = nx.forest_str(graph, sources=[2])
    print(ret)
    assert ret == node_target2


def test_forest_str_errors():
    ugraph = nx.complete_graph(3, create_using=nx.Graph)

    with pytest.raises(nx.NetworkXNotImplemented):
        nx.forest_str(ugraph)

    dgraph = nx.complete_graph(3, create_using=nx.DiGraph)

    with pytest.raises(nx.NetworkXNotImplemented):
        nx.forest_str(dgraph)


def test_overspecified_sources():
    """
    When sources are directly specified, we wont be able to determine when we
    are in the last component, so there will always be a trailing, leftmost
    pipe.
    """
    graph = nx.disjoint_union_all(
        [
            nx.balanced_tree(r=2, h=1, create_using=nx.DiGraph),
            nx.balanced_tree(r=1, h=2, create_using=nx.DiGraph),
            nx.balanced_tree(r=2, h=1, create_using=nx.DiGraph),
        ]
    )

    # defined starting point
    target1 = dedent(
        """
        β•Ÿβ”€β”€ 0
        β•ŽΒ Β  β”œβ”€β•Ό 1
        β•ŽΒ Β  └─╼ 2
        β•Ÿβ”€β”€ 3
        β•ŽΒ Β  └─╼ 4
        β•ŽΒ Β      └─╼ 5
        β•Ÿβ”€β”€ 6
        β•ŽΒ Β  β”œβ”€β•Ό 7
        β•ŽΒ Β  └─╼ 8
        """
    ).strip()

    target2 = dedent(
        """
        β•Ÿβ”€β”€ 0
        β•ŽΒ Β  β”œβ”€β•Ό 1
        β•ŽΒ Β  └─╼ 2
        β•Ÿβ”€β”€ 3
        β•ŽΒ Β  └─╼ 4
        β•ŽΒ Β      └─╼ 5
        ╙── 6
            β”œβ”€β•Ό 7
            └─╼ 8
        """
    ).strip()

    lines = []
    nx.forest_str(graph, write=lines.append, sources=graph.nodes)
    got1 = chr(10).join(lines)
    print("got1: ")
    print(got1)

    lines = []
    nx.forest_str(graph, write=lines.append)
    got2 = chr(10).join(lines)
    print("got2: ")
    print(got2)

    assert got1 == target1
    assert got2 == target2