Skip to content

Commit 5c2cf1f

Browse files
SilverRainZMiMo v2 Pro
andcommitted
tests: Add smoke test for extra context feature
Test custom extra context registration with @extra_context decorator, load() function in templates, and Template.extra field. Co-Authored-By: MiMo v2 Pro <mimo@xiaomi.com>
1 parent 994f709 commit 5c2cf1f

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from sphinx.application import Sphinx
2+
from sphinxnotes.render import (
3+
extra_context,
4+
ParsingPhaseExtraContext,
5+
GlobalExtraContext,
6+
BaseContextDirective,
7+
Template,
8+
)
9+
10+
11+
@extra_context('custom_parsing')
12+
class CustomParsingExtraContext(ParsingPhaseExtraContext):
13+
def generate(self, directive):
14+
return {'custom_value': 'parsing_test'}
15+
16+
17+
@extra_context('custom_global')
18+
class CustomGlobalExtraContext(GlobalExtraContext):
19+
def generate(self, env):
20+
return {'custom_value': 'global_test'}
21+
22+
23+
class CustomExtraContextDirective(BaseContextDirective):
24+
def current_context(self):
25+
return {}
26+
27+
def current_template(self):
28+
return Template(
29+
"""
30+
{% set _parsing = load('custom_parsing') %}
31+
{% set _global = load('custom_global') %}
32+
Parsing: {{ _parsing.custom_value }}
33+
Global: {{ _global.custom_value }}
34+
""",
35+
extra=['custom_parsing', 'custom_global'],
36+
)
37+
38+
39+
def setup(app: Sphinx):
40+
app.setup_extension('sphinxnotes.render')
41+
app.add_directive('custom-extra', CustomExtraContextDirective)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Extra Context Test
2+
==================
3+
4+
.. custom-extra::

tests/test_smoke.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ def test_strict_data_define_directive_card(app, status, warning):
2626

2727

2828
# -- literalinclude:end:end-to-end-card --
29+
30+
31+
@pytest.mark.sphinx('html', testroot='extra-context')
32+
def test_extra_context_custom_loader(app, status, warning):
33+
app.build()
34+
35+
html = (app.outdir / 'index.html').read_text(encoding='utf-8')
36+
37+
assert 'Parsing: parsing_test' in html
38+
assert 'Global: global_test' in html

0 commit comments

Comments
 (0)