diff options
| author | 2021-02-03 19:17:50 -0500 | |
|---|---|---|
| committer | 2021-02-03 19:17:50 -0500 | |
| commit | 475d074fd74425efbe783fad08f97f2df0c4909f (patch) | |
| tree | 2acdae53999b3c74b716efa4edb5b40311fa356a /tests/test_template_shortcodes.py | |
| parent | cd502d52787f666fff3254d7d7e7578930c813c2 (diff) | |
| parent | 3a0d66f07b112b6d2bdc2b57bbf717a89a351ce6 (diff) | |
Update upstream source from tag 'upstream/8.1.2'
Update to upstream version '8.1.2'
with Debian dir e5e966a9e6010ef70618dc9a61558fa4db35aceb
Diffstat (limited to 'tests/test_template_shortcodes.py')
| -rw-r--r-- | tests/test_template_shortcodes.py | 110 |
1 files changed, 51 insertions, 59 deletions
diff --git a/tests/test_template_shortcodes.py b/tests/test_template_shortcodes.py index a1d3a91..c6c948d 100644 --- a/tests/test_template_shortcodes.py +++ b/tests/test_template_shortcodes.py @@ -1,50 +1,26 @@ -# -*- coding: utf-8 -*- -# vim: set wrap textwidth=100 """Test template-based shortcodes.""" -from __future__ import unicode_literals - import pytest -from nikola import Nikola - - -class ShortcodeFakeSite(Nikola): - def _get_template_system(self): - if self._template_system is None: - # Load template plugin - self._template_system = self.plugin_manager.getPluginByName( - 'jinja', "TemplateSystem").plugin_object - self._template_system.set_directories('.', 'cache') - self._template_system.set_site(self) - - return self._template_system - - template_system = property(_get_template_system) - -@pytest.fixture(scope="module") -def fakesite(): - s = ShortcodeFakeSite() - s.init_plugins() - s._template_system = None - return s +from nikola import Nikola -def test_mixedargs(fakesite): - TEST_TMPL = """ +def test_mixedargs(site): + test_template = """ arg1: {{ _args[0] }} arg2: {{ _args[1] }} kwarg1: {{ kwarg1 }} kwarg2: {{ kwarg2 }} """ - fakesite.shortcode_registry['test1'] = \ - fakesite._make_renderfunc(TEST_TMPL) - fakesite.shortcode_registry['test2'] = \ - fakesite._make_renderfunc('Something completely different') + site.shortcode_registry["test1"] = site._make_renderfunc(test_template) + site.shortcode_registry["test2"] = site._make_renderfunc( + "Something completely different" + ) - res = fakesite.apply_shortcodes( - '{{% test1 kwarg1=spamm arg1 kwarg2=foo,bar arg2 %}}') + res = site.apply_shortcodes("{{% test1 kwarg1=spamm arg1 kwarg2=foo,bar arg2 %}}")[ + 0 + ] assert res.strip() == """ arg1: arg1 @@ -53,34 +29,50 @@ kwarg1: spamm kwarg2: foo,bar""".strip() -def test_onearg(fakesite): - fakesite.shortcode_registry['test1'] = \ - fakesite._make_renderfunc('arg={{ _args[0] }}') +@pytest.mark.parametrize( + "template, data, expected_result", + [ + # one argument + ("arg={{ _args[0] }}", "{{% test1 onearg %}}", "arg=onearg"), + ("arg={{ _args[0] }}", '{{% test1 "one two" %}}', "arg=one two"), + # keyword arguments + ("foo={{ foo }}", "{{% test1 foo=bar %}}", "foo=bar"), + ("foo={{ foo }}", '{{% test1 foo="bar baz" %}}', "foo=bar baz"), + ("foo={{ foo }}", '{{% test1 foo="bar baz" spamm=ham %}}', "foo=bar baz"), + # data + ( + "data={{ data }}", + "{{% test1 %}}spamm spamm{{% /test1 %}}", + "data=spamm spamm", + ), + ("data={{ data }}", "{{% test1 spamm %}}", "data="), + ("data={{ data }}", "{{% test1 data=dummy %}}", "data="), + ], +) +def test_applying_shortcode(site, template, data, expected_result): + site.shortcode_registry["test1"] = site._make_renderfunc(template) + + assert site.apply_shortcodes(data)[0] == expected_result - assert fakesite.apply_shortcodes('{{% test1 onearg %}}') == 'arg=onearg' - assert fakesite.apply_shortcodes('{{% test1 "one two" %}}') == 'arg=one two' +@pytest.fixture(scope="module") +def site(): + s = ShortcodeFakeSite() + s.init_plugins() + s._template_system = None + return s -def test_kwarg(fakesite): - fakesite.shortcode_registry['test1'] = \ - fakesite._make_renderfunc('foo={{ foo }}') - - res = fakesite.apply_shortcodes('{{% test1 foo=bar %}}') - assert res == 'foo=bar' - res = fakesite.apply_shortcodes('{{% test1 foo="bar baz" %}}') - assert res == 'foo=bar baz' - res = fakesite.apply_shortcodes('{{% test1 foo="bar baz" spamm=ham %}}') - assert res == 'foo=bar baz' +class ShortcodeFakeSite(Nikola): + def _get_template_system(self): + if self._template_system is None: + # Load template plugin + self._template_system = self.plugin_manager.getPluginByName( + "jinja", "TemplateSystem" + ).plugin_object + self._template_system.set_directories(".", "cache") + self._template_system.set_site(self) -def test_data(fakesite): - fakesite.shortcode_registry['test1'] = \ - fakesite._make_renderfunc('data={{ data }}') + return self._template_system - res = fakesite.apply_shortcodes('{{% test1 %}}spamm spamm{{% /test1 %}}') - assert res == 'data=spamm spamm' - res = fakesite.apply_shortcodes('{{% test1 spamm %}}') - assert res == 'data=' - # surprise! - res = fakesite.apply_shortcodes('{{% test1 data=dummy %}}') - assert res == 'data=' + template_system = property(_get_template_system) |
