test_shapes.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. Tests that built-in shape types are emitted correctly.
  3. """
  4. import os.path
  5. import subprocess
  6. import sys
  7. from pathlib import Path
  8. import pytest
  9. # Import helper function to compare graphs from tests/regressions_tests
  10. sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
  11. from regression_test_helpers import ( # pylint: disable=import-error,wrong-import-position
  12. compare_graphs,
  13. )
  14. shapes = [
  15. "box",
  16. "polygon",
  17. "ellipse",
  18. "oval",
  19. "circle",
  20. "point",
  21. "egg",
  22. "triangle",
  23. "none",
  24. "plaintext",
  25. "plain",
  26. "diamond",
  27. "trapezium",
  28. "parallelogram",
  29. "house",
  30. "pentagon",
  31. "hexagon",
  32. "septagon",
  33. "octagon",
  34. "note",
  35. "tab",
  36. "folder",
  37. "box3d",
  38. "component",
  39. "cylinder",
  40. "rect",
  41. "rectangle",
  42. "square",
  43. "star",
  44. "doublecircle",
  45. "doubleoctagon",
  46. "tripleoctagon",
  47. "invtriangle",
  48. "invtrapezium",
  49. "invhouse",
  50. "underline",
  51. "Mdiamond",
  52. "Msquare",
  53. "Mcircle",
  54. # biological circuit shapes
  55. # gene expression symbols
  56. "promoter",
  57. "cds",
  58. "terminator",
  59. "utr",
  60. "insulator",
  61. "ribosite",
  62. "rnastab",
  63. "proteasesite",
  64. "proteinstab",
  65. # dna construction symbols
  66. "primersite",
  67. "restrictionsite",
  68. "fivepoverhang",
  69. "threepoverhang",
  70. "noverhang",
  71. "assembly",
  72. "signature",
  73. "rpromoter",
  74. "larrow",
  75. "rarrow",
  76. "lpromoter",
  77. ]
  78. output_types = ["gv", "svg", "xdot"]
  79. def generate_shape_graph(shape, output_type):
  80. """
  81. Produce a graph of the given shape and output format.
  82. """
  83. if not Path("output").exists():
  84. Path("output").mkdir(parents=True)
  85. output_file = Path("output") / f"{shape}.{output_type}"
  86. input_graph = f'graph G {{ a [label="" shape={shape}] }}'
  87. try:
  88. subprocess.run(
  89. ["dot", f"-T{output_type}", "-o", output_file],
  90. input=input_graph.encode("utf_8"),
  91. check=True,
  92. )
  93. except subprocess.CalledProcessError:
  94. print(f"An error occurred while generating: {output_file}")
  95. sys.exit(1)
  96. if output_type == "svg":
  97. # Remove the number in "Generated by graphviz version <number>"
  98. # to able to compare the output to the reference. This version
  99. # number is different for every Graphviz compilation.
  100. with open(output_file, "rt", encoding="utf-8") as file:
  101. lines = file.readlines()
  102. with open(output_file, "wt", encoding="utf-8") as file:
  103. for line in lines:
  104. if "<!-- Generated by graphviz version " in line:
  105. file.write("<!-- Generated by graphviz version\n")
  106. else:
  107. file.write(line)
  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. os.chdir(Path(__file__).resolve().parent)
  117. generate_shape_graph(shape, output_type)
  118. assert compare_graphs(shape, output_type)