forked from dmulholl/shortcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_shortcodes.py
executable file
·199 lines (132 loc) · 5.82 KB
/
test_shortcodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# --------------------------------------------------------------------------
# Unit tests for the shortcodes module. Run using pytest.
# --------------------------------------------------------------------------
import shortcodes
import pytest
# --------------------------------------------------------------------------
# Test handlers.
# --------------------------------------------------------------------------
@shortcodes.register('foo')
def foo_handler(context, content, pargs, kwargs):
return 'bar'
@shortcodes.register('wrap', 'endwrap')
def wrap_handler(context, content, pargs, kwargs):
return '<%s>%s</%s>' % (pargs[0], content, pargs[0])
@shortcodes.register('args')
def args_handler(context, content, pargs, kwargs):
for key, value in sorted(kwargs.items()):
pargs.append(key + ':' + value)
return '|'.join(pargs)
@shortcodes.register('context')
def context_handler(context, content, pargs, kwargs):
return str(context)
@shortcodes.register('divbyzero')
def divbyzero_handler(context, content, pargs, kwargs):
x = 1 / 0
return 'we never make it here'
# --------------------------------------------------------------------------
# Basic shortcode insertion tests.
# --------------------------------------------------------------------------
def test_parse_empty_string():
text = ''
rendered = shortcodes.Parser().parse(text)
assert rendered == ''
def test_parse_string_no_shortcodes():
text = 'foo'
rendered = shortcodes.Parser().parse(text)
assert rendered == 'foo'
def test_parse_single_shortcode():
text = '[% foo %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == 'bar'
def test_parse_single_shortcode_with_text():
text = '..[% foo %]..'
rendered = shortcodes.Parser().parse(text)
assert rendered == '..bar..'
# --------------------------------------------------------------------------
# Test shortcode escaping.
# --------------------------------------------------------------------------
def test_escaped_shortcode():
text = r'\[% foo %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == '[% foo %]'
def test_double_escaped_shortcode():
text = r'\\[% foo %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == r'\[% foo %]'
# --------------------------------------------------------------------------
# Test shortcode arguments.
# --------------------------------------------------------------------------
def test_args_with_double_quoted_strings():
text = '[% args arg1 "arg 2" key1=arg3 key2="arg 4" %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == 'arg1|arg 2|key1:arg3|key2:arg 4'
def test_args_with_single_quoted_strings():
text = "[% args arg1 'arg 2' key1=arg3 key2='arg 4' %]"
rendered = shortcodes.Parser().parse(text)
assert rendered == 'arg1|arg 2|key1:arg3|key2:arg 4'
# --------------------------------------------------------------------------
# Test shortcode nesting.
# --------------------------------------------------------------------------
def test_wrapping_simple_text():
text = '[% wrap div %]foo[% endwrap %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == '<div>foo</div>'
def test_wrapping_shortcode():
text = '[% wrap div %][% foo %][% endwrap %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == '<div>bar</div>'
def test_wrapping_wrapping_shortcode():
text = '[% wrap div %][% wrap p %][% foo %][% endwrap %][% endwrap %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == '<div><p>bar</p></div>'
def test_wrapping_and_text_mix():
text = '[% wrap div %]..[% wrap p %].[% foo %].[% endwrap %]..[% endwrap %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == '<div>..<p>.bar.</p>..</div>'
# --------------------------------------------------------------------------
# Test context object support.
# --------------------------------------------------------------------------
def test_context_object():
text = '[% context %]'
rendered = shortcodes.Parser().parse(text, 101)
assert rendered == '101'
# --------------------------------------------------------------------------
# Test local handler registration.
# --------------------------------------------------------------------------
def test_locally_registered_handler():
text = '[% local %]'
parser = shortcodes.Parser()
parser.register(foo_handler, 'local')
rendered = parser.parse(text)
assert rendered == 'bar'
def test_locally_registered_wrap():
text = '[% localwrap div %]foo[% endlocalwrap %]'
parser = shortcodes.Parser()
parser.register(wrap_handler, 'localwrap', 'endlocalwrap')
rendered = parser.parse(text)
assert rendered == '<div>foo</div>'
# --------------------------------------------------------------------------
# Test raising exceptions.
# --------------------------------------------------------------------------
def test_handler_exception():
text = '[% divbyzero %]'
with pytest.raises(shortcodes.RenderingError) as exinfo:
shortcodes.Parser().parse(text)
assert isinstance(exinfo.value.__cause__, ZeroDivisionError)
def test_invalid_tag_exception():
text = '[% notregistered %]'
with pytest.raises(shortcodes.InvalidTagError):
shortcodes.Parser().parse(text)
def test_unbalanced_tags_exception():
text = '[% wrap %] missing end tag...'
with pytest.raises(shortcodes.NestingError):
shortcodes.Parser().parse(text)
# --------------------------------------------------------------------------
# Test non-ASCII text.
# --------------------------------------------------------------------------
def test_nonascii_args():
text = '[% args pøs0 k€¥="välué" %]'
rendered = shortcodes.Parser().parse(text)
assert rendered == 'pøs0|k€¥:välué'