|
1 | 1 | # Tests are inspired by the test suite of sphinx itself |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
| 4 | +import json |
4 | 5 | import re |
5 | 6 | from typing import TYPE_CHECKING, cast |
6 | 7 |
|
@@ -79,27 +80,59 @@ def test_altairplotdirective(app: Sphinx) -> None: |
79 | 80 | assert result.count(VEGA_JS_URL_DEFAULT) |
80 | 81 | assert SCHEMA_URL in result |
81 | 82 |
|
| 83 | + def extract_embed_values( |
| 84 | + plot_id: int, |
| 85 | + ) -> tuple[dict[str, object], dict[str, object], str | None]: |
| 86 | + div_id = f"index-rst-altair-plot-{plot_id}" |
| 87 | + match = re.search( |
| 88 | + rf'<div id="{div_id}"(?: class="([^"]+)")?>\s*<script>.*?' |
| 89 | + rf"var spec = (\{{.*?\}});\s*" |
| 90 | + rf"var opt = (\{{.*?\}});\s*" |
| 91 | + rf"vegaEmbed\('#{div_id}', spec, opt\)", |
| 92 | + result, |
| 93 | + re.DOTALL, |
| 94 | + ) |
| 95 | + assert match is not None |
| 96 | + class_name = match.group(1) |
| 97 | + spec = json.loads(match.group(2)) |
| 98 | + opt = json.loads(match.group(3)) |
| 99 | + return spec, opt, class_name |
| 100 | + |
| 101 | + for plot_id in (1, 2, 4, 5, 6, 7): |
| 102 | + spec, opt, _ = extract_embed_values(plot_id) |
| 103 | + assert spec["$schema"] == SCHEMA_URL |
| 104 | + assert opt["mode"] == "vega-lite" |
| 105 | + assert opt["renderer"] == "canvas" |
| 106 | + |
82 | 107 | assert 'id="index-rst-altair-source-0"' in result |
83 | 108 | assert '<div id="index-rst-altair-plot-0"' not in result |
84 | 109 |
|
85 | 110 | assert 'id="index-rst-altair-source-1"' in result |
86 | | - assert 'id="index-rst-altair-plot-1"' in result |
87 | | - assert '"actions": {"editor": true, "source": true, "export": true}' in result |
88 | | - |
89 | | - assert '<div id="index-rst-altair-plot-2">' in result |
90 | | - assert '</div><div class="highlight-python notranslate">' in result |
| 111 | + _, plot_1_opt, _ = extract_embed_values(1) |
| 112 | + assert plot_1_opt["actions"] == {"editor": True, "source": True, "export": True} |
| 113 | + |
| 114 | + code_below_section = re.search( |
| 115 | + r'<section id="code-below-plot">.*?</section>', result, re.DOTALL |
| 116 | + ) |
| 117 | + assert code_below_section is not None |
| 118 | + assert re.search( |
| 119 | + r'<div id="index-rst-altair-plot-2">.*?</div><div class="highlight-python notranslate">', |
| 120 | + code_below_section.group(0), |
| 121 | + re.DOTALL, |
| 122 | + ) |
91 | 123 |
|
92 | 124 | assert 'id="index-rst-altair-source-3"' in result |
93 | 125 | assert "Data({" in result |
94 | 126 |
|
95 | | - assert '<div id="index-rst-altair-plot-4"' in result |
| 127 | + extract_embed_values(4) |
96 | 128 | assert 'id="index-rst-altair-source-4"' not in result |
97 | 129 |
|
98 | 130 | assert "Click to show code" in result |
99 | | - assert '<div id="index-rst-altair-plot-5"' in result |
| 131 | + assert re.search(r"<details>.*?Click to show code.*?</details>", result, re.DOTALL) |
| 132 | + extract_embed_values(5) |
100 | 133 |
|
101 | | - assert '<div id="index-rst-altair-plot-6"' in result |
102 | | - assert '"actions": {"editor": true, "source": false, "export": false}' in result |
| 134 | + _, plot_6_opt, _ = extract_embed_values(6) |
| 135 | + assert plot_6_opt["actions"] == {"editor": True, "source": False, "export": False} |
103 | 136 |
|
104 | | - assert result.count('class="test-class"') == 1 |
105 | | - assert '<div id="index-rst-altair-plot-7" class="test-class">' in result |
| 137 | + _, _, plot_7_class = extract_embed_values(7) |
| 138 | + assert plot_7_class == "test-class" |
0 commit comments