| 1 |
""" |
| 2 |
Tests that built-in shape types are emitted correctly. |
| 3 |
""" |
| 4 |
|
| 5 |
import os.path |
| 6 |
import re |
| 7 |
import sys |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
sys.path.append(os.path.join(os.path.dirname(__file__), "../../../tests")) |
| 13 |
from gvtest import dot # pylint: disable=wrong-import-position |
| 14 |
|
| 15 |
shapes = [ |
| 16 |
"box", |
| 17 |
"polygon", |
| 18 |
"ellipse", |
| 19 |
"oval", |
| 20 |
"circle", |
| 21 |
"point", |
| 22 |
"egg", |
| 23 |
"triangle", |
| 24 |
"none", |
| 25 |
"plaintext", |
| 26 |
"plain", |
| 27 |
"diamond", |
| 28 |
"trapezium", |
| 29 |
"parallelogram", |
| 30 |
"house", |
| 31 |
"pentagon", |
| 32 |
"hexagon", |
| 33 |
"septagon", |
| 34 |
"octagon", |
| 35 |
"note", |
| 36 |
"tab", |
| 37 |
"folder", |
| 38 |
"box3d", |
| 39 |
"component", |
| 40 |
"cylinder", |
| 41 |
"rect", |
| 42 |
"rectangle", |
| 43 |
"square", |
| 44 |
"star", |
| 45 |
"doublecircle", |
| 46 |
"doubleoctagon", |
| 47 |
"tripleoctagon", |
| 48 |
"invtriangle", |
| 49 |
"invtrapezium", |
| 50 |
"invhouse", |
| 51 |
"underline", |
| 52 |
"Mdiamond", |
| 53 |
"Msquare", |
| 54 |
"Mcircle", |
| 55 |
# biological circuit shapes |
| 56 |
# gene expression symbols |
| 57 |
"promoter", |
| 58 |
"cds", |
| 59 |
"terminator", |
| 60 |
"utr", |
| 61 |
"insulator", |
| 62 |
"ribosite", |
| 63 |
"rnastab", |
| 64 |
"proteasesite", |
| 65 |
"proteinstab", |
| 66 |
# dna construction symbols |
| 67 |
"primersite", |
| 68 |
"restrictionsite", |
| 69 |
"fivepoverhang", |
| 70 |
"threepoverhang", |
| 71 |
"noverhang", |
| 72 |
"assembly", |
| 73 |
"signature", |
| 74 |
"rpromoter", |
| 75 |
"larrow", |
| 76 |
"rarrow", |
| 77 |
"lpromoter", |
| 78 |
] |
| 79 |
|
| 80 |
output_types = ["gv", "svg", "xdot"] |
| 81 |
|
| 82 |
|
| 83 |
def generate_shape_graph(shape, output_type) -> str: |
| 84 |
""" |
| 85 |
Produce a graph of the given shape and output format. |
| 86 |
|
| 87 |
Returns: |
| 88 |
A graph in the given output format. |
| 89 |
""" |
| 90 |
|
| 91 |
input_graph = f'graph G {{ a [label="" shape={shape}] }}' |
| 92 |
output = dot(output_type, source=input_graph) |
| 93 |
|
| 94 |
if output_type == "svg": |
| 95 |
# Remove the number in "Generated by graphviz version <number>" |
| 96 |
# to able to compare the output to the reference. This version |
| 97 |
# number is different for every Graphviz compilation. |
| 98 |
output = re.sub( |
| 99 |
r"<!-- Generated by graphviz version .*$", |
| 100 |
"<!-- Generated by graphviz version", |
| 101 |
output, |
| 102 |
flags=re.MULTILINE, |
| 103 |
) |
| 104 |
|
| 105 |
return output |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize( |
| 109 |
"shape,output_type", |
| 110 |
[(shape, output_type) for shape in shapes for output_type in output_types], |
| 111 |
) |
| 112 |
def test_shape(shape, output_type): |
| 113 |
""" |
| 114 |
Check a shape corresponds to its reference. |
| 115 |
""" |
| 116 |
my_dir = Path(__file__).resolve().parent |
| 117 |
output = generate_shape_graph(shape, output_type) |
| 118 |
|
| 119 |
ref = my_dir / f"reference/{shape}.{output_type}" |
| 120 |
reference = ref.read_text(encoding="utf-8") |
| 121 |
|
| 122 |
assert reference == output |