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_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_shortcodes.py')
| -rw-r--r-- | tests/test_shortcodes.py | 256 |
1 files changed, 190 insertions, 66 deletions
diff --git a/tests/test_shortcodes.py b/tests/test_shortcodes.py index 22f603e..8383176 100644 --- a/tests/test_shortcodes.py +++ b/tests/test_shortcodes.py @@ -1,76 +1,200 @@ -# -*- coding: utf-8 -*- -# vim: set wrap textwidth=300 +"""Test shortcodes.""" -u"""Test shortcodes.""" - -from __future__ import unicode_literals import pytest + +import nikola.utils from nikola import shortcodes -from .base import FakeSite, BaseTestCase -import sys -def noargs(site, data='', lang=''): - return "noargs {0} success!".format(data) -def arg(*args, **kwargs): - # don’t clutter the kwargs dict - _ = kwargs.pop('site') - data = kwargs.pop('data') - lang = kwargs.pop('lang') - # TODO hack for Python 2.7 -- remove when possible - if sys.version_info[0] == 2: - args = tuple(i.encode('utf-8') for i in args) - kwargs = {k.encode('utf-8'): v.encode('utf-8') for k, v in kwargs.items()} - return "arg {0}/{1}/{2}".format(args, sorted(kwargs.items()), data) +@pytest.mark.parametrize( + "template, expected_result", + [ + ("test({{% noargs %}})", "test(noargs success!)"), + ( + "test({{% noargs %}}\\hello world/{{% /noargs %}})", + "test(noargs \\hello world/ success!)", + ), + ], +) +def test_noargs(site, template, expected_result): + applied_shortcode = shortcodes.apply_shortcodes(template, site.shortcode_registry)[0] + assert applied_shortcode == expected_result + + +@pytest.mark.parametrize( + "template, expected_result", + [ + ("test({{% arg 1 %}})", "test(arg ('1',)/[]/)"), + ("test({{% arg 1 2aa %}})", "test(arg ('1', '2aa')/[]/)"), + ('test({{% arg "hello world" %}})', "test(arg ('hello world',)/[]/)"), + ("test({{% arg back\\ slash arg2 %}})", "test(arg ('back slash', 'arg2')/[]/)"), + ('test({{% arg "%}}" %}})', "test(arg ('%}}',)/[]/)"), + ], +) +def test_positional_arguments(site, template, expected_result): + applied_shortcode = shortcodes.apply_shortcodes(template, site.shortcode_registry)[0] + assert applied_shortcode == expected_result + + +@pytest.mark.parametrize( + "template, expected_result", + [ + ("test({{% arg 1a=2b %}})", "test(arg ()/[('1a', '2b')]/)"), + ( + 'test({{% arg 1a="2b 3c" 4d=5f %}})', + "test(arg ()/[('1a', '2b 3c'), ('4d', '5f')]/)", + ), + ( + 'test({{% arg 1a="2b 3c" 4d=5f back=slash\\ slash %}})', + "test(arg ()/[('1a', '2b 3c'), ('4d', '5f'), ('back', 'slash slash')]/)", + ), + ], +) +def test_arg_keyword(site, template, expected_result): + applied_shortcode = shortcodes.apply_shortcodes(template, site.shortcode_registry)[0] + assert applied_shortcode == expected_result + + +@pytest.mark.parametrize( + "template, expected_result", + [ + ("test({{% arg 123 %}}Hello!{{% /arg %}})", "test(arg ('123',)/[]/Hello!)"), + ( + "test({{% arg 123 456 foo=bar %}}Hello world!{{% /arg %}})", + "test(arg ('123', '456')/[('foo', 'bar')]/Hello world!)", + ), + ( + 'test({{% arg 123 456 foo=bar baz="quotes rock." %}}Hello test suite!{{% /arg %}})', + "test(arg ('123', '456')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!)", + ), + ( + 'test({{% arg "123 foo" foobar foo=bar baz="quotes rock." %}}Hello test suite!!{{% /arg %}})', + "test(arg ('123 foo', 'foobar')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!!)", + ), + ], +) +def test_data(site, template, expected_result): + applied_shortcode = shortcodes.apply_shortcodes(template, site.shortcode_registry)[0] + assert applied_shortcode == expected_result + + +@pytest.mark.parametrize( + "template, expected_error_pattern", + [ + ( + "{{% start", + "^Shortcode 'start' starting at .* is not terminated correctly with '%}}'!", + ), + ( + "{{% wrong ending %%}", + "^Syntax error in shortcode 'wrong' at .*: expecting whitespace!", + ), + ( + "{{% start %}} {{% /end %}}", + "^Found shortcode ending '{{% /end %}}' which isn't closing a started shortcode", + ), + ('{{% start "asdf %}}', "^Unexpected end of unquoted string"), + ("{{% start =b %}}", "^String starting at .* must be non-empty!"), + ('{{% start "a\\', "^Unexpected end of data while escaping"), + ("{{% start a\\", "^Unexpected end of data while escaping"), + ('{{% start a"b" %}}', "^Unexpected quotation mark in unquoted string"), + ( + '{{% start "a"b %}}', + "^Syntax error in shortcode 'start' at .*: expecting whitespace!", + ), + ("{{% %}}", "^Syntax error: '{{%' must be followed by shortcode name"), + ("{{%", "^Syntax error: '{{%' must be followed by shortcode name"), + ("{{% ", "^Syntax error: '{{%' must be followed by shortcode name"), + ( + "{{% / %}}", + "^Found shortcode ending '{{% / %}}' which isn't closing a started shortcode", + ), + ("{{% / a %}}", "^Syntax error: '{{% /' must be followed by ' %}}'"), + ( + "==> {{% <==", + "^Shortcode '<==' starting at .* is not terminated correctly with '%}}'!", + ), + ], +) +def test_errors(site, template, expected_error_pattern): + with pytest.raises(shortcodes.ParsingError, match=expected_error_pattern): + shortcodes.apply_shortcodes( + template, site.shortcode_registry, raise_exceptions=True + ) + + +@pytest.mark.parametrize( + "input, expected", + [ + ("{{% foo %}}", (u"SC1", {u"SC1": u"{{% foo %}}"})), + ( + "{{% foo %}} bar {{% /foo %}}", + (u"SC1", {u"SC1": u"{{% foo %}} bar {{% /foo %}}"}), + ), + ( + "AAA{{% foo %}} bar {{% /foo %}}BBB", + (u"AAASC1BBB", {u"SC1": u"{{% foo %}} bar {{% /foo %}}"}), + ), + ( + "AAA{{% foo %}} {{% bar %}} {{% /foo %}}BBB", + (u"AAASC1BBB", {u"SC1": u"{{% foo %}} {{% bar %}} {{% /foo %}}"}), + ), + ( + "AAA{{% foo %}} {{% /bar %}} {{% /foo %}}BBB", + (u"AAASC1BBB", {u"SC1": u"{{% foo %}} {{% /bar %}} {{% /foo %}}"}), + ), + ( + "AAA{{% foo %}} {{% bar %}} quux {{% /bar %}} {{% /foo %}}BBB", + ( + u"AAASC1BBB", + {u"SC1": u"{{% foo %}} {{% bar %}} quux {{% /bar %}} {{% /foo %}}"}, + ), + ), + ( + "AAA{{% foo %}} BBB {{% bar %}} quux {{% /bar %}} CCC", + ( + u"AAASC1 BBB SC2 CCC", + {u"SC1": u"{{% foo %}}", u"SC2": u"{{% bar %}} quux {{% /bar %}}"}, + ), + ), + ], +) +def test_extract_shortcodes(input, expected, monkeypatch): + i = iter("SC%d" % i for i in range(1, 100)) + monkeypatch.setattr(shortcodes, "_new_sc_id", i.__next__) + extracted = shortcodes.extract_shortcodes(input) + assert extracted == expected @pytest.fixture(scope="module") -def fakesite(): - s = FakeSite() - s.register_shortcode('noargs', noargs) - s.register_shortcode('arg', arg) +def site(): + s = FakeSiteWithShortcodeRegistry() + s.register_shortcode("noargs", noargs) + s.register_shortcode("arg", arg) return s -def test_noargs(fakesite): - assert shortcodes.apply_shortcodes('test({{% noargs %}})', fakesite.shortcode_registry) == 'test(noargs success!)' - assert shortcodes.apply_shortcodes('test({{% noargs %}}\\hello world/{{% /noargs %}})', fakesite.shortcode_registry) == 'test(noargs \\hello world/ success!)' - -def test_arg_pos(fakesite): - assert shortcodes.apply_shortcodes('test({{% arg 1 %}})', fakesite.shortcode_registry) == "test(arg ('1',)/[]/)" - assert shortcodes.apply_shortcodes('test({{% arg 1 2aa %}})', fakesite.shortcode_registry) == "test(arg ('1', '2aa')/[]/)" - assert shortcodes.apply_shortcodes('test({{% arg "hello world" %}})', fakesite.shortcode_registry) == "test(arg ('hello world',)/[]/)" - assert shortcodes.apply_shortcodes('test({{% arg back\ slash arg2 %}})', fakesite.shortcode_registry) == "test(arg ('back slash', 'arg2')/[]/)" - assert shortcodes.apply_shortcodes('test({{% arg "%}}" %}})', fakesite.shortcode_registry) == "test(arg ('%}}',)/[]/)" - -def test_arg_keyword(fakesite): - assert shortcodes.apply_shortcodes('test({{% arg 1a=2b %}})', fakesite.shortcode_registry) == "test(arg ()/[('1a', '2b')]/)" - assert shortcodes.apply_shortcodes('test({{% arg 1a="2b 3c" 4d=5f %}})', fakesite.shortcode_registry) == "test(arg ()/[('1a', '2b 3c'), ('4d', '5f')]/)" - assert shortcodes.apply_shortcodes('test({{% arg 1a="2b 3c" 4d=5f back=slash\ slash %}})', fakesite.shortcode_registry) == "test(arg ()/[('1a', '2b 3c'), ('4d', '5f'), ('back', 'slash slash')]/)" - -def test_data(fakesite): - assert shortcodes.apply_shortcodes('test({{% arg 123 %}}Hello!{{% /arg %}})', fakesite.shortcode_registry) == "test(arg ('123',)/[]/Hello!)" - assert shortcodes.apply_shortcodes('test({{% arg 123 456 foo=bar %}}Hello world!{{% /arg %}})', fakesite.shortcode_registry) == "test(arg ('123', '456')/[('foo', 'bar')]/Hello world!)" - assert shortcodes.apply_shortcodes('test({{% arg 123 456 foo=bar baz="quotes rock." %}}Hello test suite!{{% /arg %}})', fakesite.shortcode_registry) == "test(arg ('123', '456')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!)" - assert shortcodes.apply_shortcodes('test({{% arg "123 foo" foobar foo=bar baz="quotes rock." %}}Hello test suite!!{{% /arg %}})', fakesite.shortcode_registry) == "test(arg ('123 foo', 'foobar')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!!)" - - -class TestErrors(BaseTestCase): - def setUp(self): - self.fakesite = fakesite() - - def test_errors(self): - self.assertRaisesRegexp(shortcodes.ParsingError, "^Shortcode 'start' starting at .* is not terminated correctly with '%}}'!", shortcodes.apply_shortcodes, '{{% start', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Syntax error in shortcode 'wrong' at .*: expecting whitespace!", shortcodes.apply_shortcodes, '{{% wrong ending %%}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Found shortcode ending '{{% /end %}}' which isn't closing a started shortcode", shortcodes.apply_shortcodes, '{{% start %}} {{% /end %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Unexpected end of unquoted string", shortcodes.apply_shortcodes, '{{% start "asdf %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^String starting at .* must be non-empty!", shortcodes.apply_shortcodes, '{{% start =b %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Unexpected end of data while escaping", shortcodes.apply_shortcodes, '{{% start "a\\', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Unexpected end of data while escaping", shortcodes.apply_shortcodes, '{{% start a\\', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Unexpected quotation mark in unquoted string", shortcodes.apply_shortcodes, '{{% start a"b" %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Syntax error in shortcode 'start' at .*: expecting whitespace!", shortcodes.apply_shortcodes, '{{% start "a"b %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Syntax error: '{{%' must be followed by shortcode name", shortcodes.apply_shortcodes, '{{% %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Syntax error: '{{%' must be followed by shortcode name", shortcodes.apply_shortcodes, '{{%', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Syntax error: '{{%' must be followed by shortcode name", shortcodes.apply_shortcodes, '{{% ', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Found shortcode ending '{{% / %}}' which isn't closing a started shortcode", shortcodes.apply_shortcodes, '{{% / %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Syntax error: '{{% /' must be followed by ' %}}'", shortcodes.apply_shortcodes, '{{% / a %}}', self.fakesite.shortcode_registry, raise_exceptions=True) - self.assertRaisesRegexp(shortcodes.ParsingError, "^Shortcode '<==' starting at .* is not terminated correctly with '%}}'!", shortcodes.apply_shortcodes, '==> {{% <==', self.fakesite.shortcode_registry, raise_exceptions=True) + +class FakeSiteWithShortcodeRegistry: + def __init__(self): + self.shortcode_registry = {} + self.debug = True + + # this code duplicated in nikola/nikola.py + def register_shortcode(self, name, f): + """Register function f to handle shortcode "name".""" + if name in self.shortcode_registry: + nikola.utils.LOGGER.warn("Shortcode name conflict: %s", name) + return + self.shortcode_registry[name] = f + + +def noargs(site, data="", lang=""): + return "noargs {0} success!".format(data) + + +def arg(*args, **kwargs): + # don’t clutter the kwargs dict + kwargs.pop("site") + data = kwargs.pop("data") + kwargs.pop("lang") + return "arg {0}/{1}/{2}".format(args, sorted(kwargs.items()), data) |
