Skip to content

Commit

Permalink
Add basic shortcode tests
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Dec 23, 2015
1 parent ae8e24c commit bdbb9a8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions nikola/shortcodes.py
Expand Up @@ -107,6 +107,7 @@ class SCParser(HTMLParser):

# Because shortcode attributes are HTML-like, we are abusing the HTML parser.
# TODO replace with self-contained parser
# FIXME should be able to take quoted positional arguments!

def parse_sc(self, data):
"""Parse shortcode arguments into a tuple."""
Expand Down
46 changes: 46 additions & 0 deletions tests/test_shortcodes.py
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# vim: set wrap

u"""Test shortcodes."""

from __future__ import unicode_literals
import pytest
from nikola import shortcodes
from .base import FakeSite

def noargs(site, data=':'):
return "noargs {0} success!".format(data)

def arg(*args, **kwargs):
# don’t clutter the kwargs dict
_ = kwargs.pop('site')
data = kwargs.pop('data', None)
return "arg {0}/{1}/{2}".format(args, sorted(kwargs.items()), data)


@pytest.fixture(scope="module")
def fakesite():
s = FakeSite()
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',)/[]/None)"
assert shortcodes.apply_shortcodes('test({{% arg 1 2aa %}})', fakesite.shortcode_registry) == "test(arg ('1', '2aa')/[]/None)"
# TODO: currently unsupported!
# assert shortcodes.apply_shortcodes('test({{% arg "hello world" %}})', fakesite.shortcode_registry) == "test(arg ('hello world')/[]/None)"

def test_arg_keyword(fakesite):
assert shortcodes.apply_shortcodes('test({{% arg 1a=2b %}})', fakesite.shortcode_registry) == "test(arg ()/[('1a', '2b')]/None)"
assert shortcodes.apply_shortcodes('test({{% arg 1a="2b 3c" 4d=5f %}})', fakesite.shortcode_registry) == "test(arg ()/[('1a', '2b 3c'), ('4d', '5f')]/None)"

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!!)"

0 comments on commit bdbb9a8

Please sign in to comment.