Debian Codegraph @

tests/test_examples.py

raw
1 """tests that examples provided with Graphviz compile correctly"""
2
3 import os
4 import platform
5 import subprocess
6 import sys
7 from pathlib import Path
8
9 import pytest
10
11 sys.path.append(os.path.dirname(__file__))
12 from gvtest import ( # pylint: disable=wrong-import-position
13 is_static_build,
14 run,
15 run_c,
16 run_raw,
17 which,
18 )
19
20
21 @pytest.mark.parametrize(
22 "src", ["demo.c", "dot.c", "example.c", "neatopack.c", "simple.c"]
23 )
24 @pytest.mark.skipif(
25 is_static_build(),
26 reason="dynamic libraries are unavailable to link against in static builds",
27 )
28 def test_compile_example(src):
29 """try to compile the example"""
30
31 # construct an absolute path to the example
32 filepath = Path(__file__).parent.resolve() / ".." / "dot.demo" / src
33
34 libs = ["cgraph", "gvc"]
35
36 # run the example
37 args = ["-Kneato"] if src in ["demo.c", "dot.c"] else []
38
39 if platform.system() == "Windows":
40 cflags = ["-DGVDLL"]
41 else:
42 cflags = None
43
44 _, _ = run_c(filepath, args, "graph {a -- b}", cflags=cflags, link=libs)
45
46
47 @pytest.mark.parametrize(
48 "src",
49 [
50 "addrings",
51 "attr",
52 "bbox",
53 "bipart",
54 "chkedges",
55 "clustg",
56 "collapse",
57 "cycle",
58 "deghist",
59 "delmulti",
60 "depath",
61 "flatten",
62 "group",
63 "indent",
64 "path",
65 "scale",
66 "span",
67 "treetoclust",
68 "addranks",
69 "anon",
70 "bb",
71 "chkclusters",
72 "cliptree",
73 "col",
74 "color",
75 "dechain",
76 "deledges",
77 "delnodes",
78 "dijkstra",
79 "get-layers-list",
80 "knbhd",
81 "maxdeg",
82 "rotate",
83 "scalexy",
84 "topon",
85 ],
86 )
87 @pytest.mark.skipif(which("gvpr") is None, reason="GVPR not available")
88 def test_gvpr_example(src):
89 """check GVPR can parse the given example"""
90
91 # construct a relative path to the example because gvpr on Windows does not
92 # support absolute paths (#1780)
93 path = Path("cmd/gvpr/lib") / src
94 wd = Path(__file__).parent.parent.resolve()
95
96 # run GVPR with the given script
97 run_raw(["gvpr", "-f", path], stdin=subprocess.DEVNULL, cwd=wd)
98
99
100 @pytest.mark.skipif(which("gvpr") is None, reason="GVPR not available")
101 def test_gvpr_clustg():
102 """check cmd/gvpr/lib/clustg works"""
103
104 # construct a relative path to clustg because gvpr on Windows does not
105 # support absolute paths (#1780)
106 path = Path("cmd/gvpr/lib/clustg")
107 wd = Path(__file__).parent.parent.resolve()
108
109 # some sample input
110 input = "digraph { N1; N2; N1 -> N2; N3; }"
111
112 # run GVPR on this input
113 output = run(["gvpr", "-f", path], input=input, cwd=wd)
114
115 assert (
116 output.strip() == 'strict digraph "clust%1" {\n'
117 "\tnode [_cnt=0];\n"
118 "\tedge [_cnt=0];\n"
119 "\tN1 -> N2\t[_cnt=1];\n"
120 "\tN3;\n"
121 "}"
122 ), "unexpected output"