| 1 |
""" |
| 2 |
Graphviz miscellaneous test cases |
| 3 |
""" |
| 4 |
|
| 5 |
import itertools |
| 6 |
import json |
| 7 |
import os |
| 8 |
import sys |
| 9 |
from pathlib import Path |
| 10 |
from typing import Union |
| 11 |
|
| 12 |
sys.path.append(os.path.dirname(__file__)) |
| 13 |
from gvtest import ROOT, compile_c, dot, run # pylint: disable=wrong-import-position |
| 14 |
|
| 15 |
|
| 16 |
def test_json_node_order(): |
| 17 |
""" |
| 18 |
test that nodes appear in JSON output in the same order as they were input |
| 19 |
""" |
| 20 |
|
| 21 |
# a simple graph with some nodes |
| 22 |
input = ( |
| 23 |
"digraph G {\n" |
| 24 |
" ordering=out;\n" |
| 25 |
' {rank=same;"1";"2"};\n' |
| 26 |
' "1"->"2";\n' |
| 27 |
' {rank=same;"4";"5";};\n' |
| 28 |
' "4"->"5";\n' |
| 29 |
' "7"->"5";\n' |
| 30 |
' "7"->"4";\n' |
| 31 |
' "6"->"1";\n' |
| 32 |
' "3"->"6";\n' |
| 33 |
' "6"->"4";\n' |
| 34 |
' "3"->"8";\n' |
| 35 |
"}" |
| 36 |
) |
| 37 |
|
| 38 |
# turn it into JSON |
| 39 |
output = dot("json", source=input) |
| 40 |
|
| 41 |
# parse this into a data structure we can inspect |
| 42 |
data = json.loads(output) |
| 43 |
|
| 44 |
# extract the nodes, filtering out subgraphs |
| 45 |
nodes = [n["name"] for n in data["objects"] if "nodes" not in n] |
| 46 |
|
| 47 |
# the nodes should appear in the order in which they were seen in the input |
| 48 |
assert nodes == ["1", "2", "4", "5", "7", "6", "3", "8"] |
| 49 |
|
| 50 |
|
| 51 |
def test_json_edge_order(): |
| 52 |
""" |
| 53 |
test that edges appear in JSON output in the same order as they were input |
| 54 |
""" |
| 55 |
|
| 56 |
# a simple graph with some edges |
| 57 |
input = ( |
| 58 |
"digraph G {\n" |
| 59 |
" ordering=out;\n" |
| 60 |
' {rank=same;"1";"2"};\n' |
| 61 |
' "1"->"2";\n' |
| 62 |
' {rank=same;"4";"5";};\n' |
| 63 |
' "4"->"5";\n' |
| 64 |
' "7"->"5";\n' |
| 65 |
' "7"->"4";\n' |
| 66 |
' "6"->"1";\n' |
| 67 |
' "3"->"6";\n' |
| 68 |
' "6"->"4";\n' |
| 69 |
' "3"->"8";\n' |
| 70 |
"}" |
| 71 |
) |
| 72 |
|
| 73 |
# turn it into JSON |
| 74 |
output = dot("json", source=input) |
| 75 |
|
| 76 |
# parse this into a data structure we can inspect |
| 77 |
data = json.loads(output) |
| 78 |
|
| 79 |
# the edges should appear in the order in which they were seen in the input |
| 80 |
expected = [ |
| 81 |
("1", "2"), |
| 82 |
("4", "5"), |
| 83 |
("7", "5"), |
| 84 |
("7", "4"), |
| 85 |
("6", "1"), |
| 86 |
("3", "6"), |
| 87 |
("6", "4"), |
| 88 |
("3", "8"), |
| 89 |
] |
| 90 |
edges = [ |
| 91 |
(data["objects"][e["tail"]]["name"], data["objects"][e["head"]]["name"]) |
| 92 |
for e in data["edges"] |
| 93 |
] |
| 94 |
assert edges == expected |
| 95 |
|
| 96 |
|
| 97 |
def test_xml_escape(tmp_path: Path): |
| 98 |
""" |
| 99 |
Check the functionality of ../lib/util/xml.c:gv_xml_escape. |
| 100 |
""" |
| 101 |
|
| 102 |
# locate our test program |
| 103 |
xml_c = Path(__file__).parent / "../lib/util/xml.c" |
| 104 |
assert xml_c.exists(), "missing xml.c" |
| 105 |
|
| 106 |
# compile the stub to something we can run |
| 107 |
xml_exe = tmp_path / "xml.exe" |
| 108 |
lib = ROOT / "lib" |
| 109 |
cflags = ["-DTEST_XML", f"-I{lib}"] |
| 110 |
compile_c(xml_c, cflags, dst=xml_exe) |
| 111 |
|
| 112 |
def escape( |
| 113 |
dash: bool, nbsp: bool, raw: bool, utf8: bool, s: Union[bytes, str] |
| 114 |
) -> str: |
| 115 |
|
| 116 |
source = tmp_path / "input" |
| 117 |
if isinstance(s, bytes): |
| 118 |
source.write_bytes(s) |
| 119 |
else: |
| 120 |
source.write_text(s, encoding="utf-8") |
| 121 |
|
| 122 |
destination = tmp_path / "output" |
| 123 |
|
| 124 |
args = [xml_exe] |
| 125 |
if dash: |
| 126 |
args += ["--dash"] |
| 127 |
if nbsp: |
| 128 |
args += ["--nbsp"] |
| 129 |
if raw: |
| 130 |
args += ["--raw"] |
| 131 |
if utf8: |
| 132 |
args += ["--utf8"] |
| 133 |
args += [source, destination] |
| 134 |
|
| 135 |
run(args) |
| 136 |
|
| 137 |
return destination.read_text(encoding="utf-8") |
| 138 |
|
| 139 |
for dash, nbsp, raw, utf8 in itertools.product((False, True), repeat=4): |
| 140 |
# something basic with nothing escapable |
| 141 |
plain = "the quick brown fox" |
| 142 |
plain_escaped = escape(dash, nbsp, raw, utf8, plain) |
| 143 |
assert plain == plain_escaped, "text incorrectly modified" |
| 144 |
|
| 145 |
# basic tag escaping |
| 146 |
tag = "template <typename T> void foo(T t);" |
| 147 |
tag_escaped = escape(dash, nbsp, raw, utf8, tag) |
| 148 |
assert ( |
| 149 |
tag_escaped == "template <typename T> void foo(T t);" |
| 150 |
), "incorrect < or > escaping" |
| 151 |
|
| 152 |
# something with an embedded escape |
| 153 |
embedded = "salt & pepper" |
| 154 |
embedded_escaped = escape(dash, nbsp, raw, utf8, embedded) |
| 155 |
if raw: |
| 156 |
assert embedded_escaped == "salt &amp; pepper", "missing & escape" |
| 157 |
else: |
| 158 |
assert embedded_escaped == embedded, "text incorrectly modified" |
| 159 |
|
| 160 |
# hyphen escaping |
| 161 |
hyphen = "UTF-8" |
| 162 |
hyphen_escaped = escape(dash, nbsp, raw, utf8, hyphen) |
| 163 |
if dash: |
| 164 |
assert hyphen_escaped == "UTF-8", "incorrect dash escape" |
| 165 |
else: |
| 166 |
assert hyphen_escaped == hyphen, "text incorrectly modified" |
| 167 |
|
| 168 |
# line endings |
| 169 |
nl = b"the quick\nbrown\rfox" |
| 170 |
nl_escaped = escape(dash, nbsp, raw, utf8, nl) |
| 171 |
if raw: |
| 172 |
assert ( |
| 173 |
nl_escaped == "the quick brown fox" |
| 174 |
), "incorrect new line escape" |
| 175 |
else: |
| 176 |
# allow benign modification of the \r |
| 177 |
assert nl_escaped in ( |
| 178 |
"the quick\nbrown\rfox", |
| 179 |
"the quick\nbrown\nfox", |
| 180 |
), "text incorrectly modified" |
| 181 |
|
| 182 |
# non-breaking space escaping |
| 183 |
two = "the quick brown fox" |
| 184 |
two_escaped = escape(dash, nbsp, raw, utf8, two) |
| 185 |
if nbsp: |
| 186 |
assert two_escaped == "the quick  brown fox", "incorrect nbsp escape" |
| 187 |
else: |
| 188 |
assert two_escaped == two, "text incorrectly modified" |
| 189 |
|
| 190 |
# cases from table in https://en.wikipedia.org/wiki/UTF-8 |
| 191 |
for c, expected in ( |
| 192 |
("$", "$"), |
| 193 |
("¢", "¢"), |
| 194 |
("ह", "ह"), |
| 195 |
("€", "€"), |
| 196 |
("한", "한"), |
| 197 |
("𐍈", "𐍈"), |
| 198 |
): |
| 199 |
unescaped = f"character |{c}|" |
| 200 |
escaped = escape(dash, nbsp, raw, utf8, unescaped) |
| 201 |
if utf8: |
| 202 |
assert escaped == f"character |{expected}|", "bad UTF-8 escaping" |
| 203 |
else: |
| 204 |
assert escaped == unescaped, "bad UTF-8 passthrough" |