Skip to content

Commit 381fd14

Browse files
committed
Add more tests
1 parent ebb8e52 commit 381fd14

File tree

1 file changed

+86
-14
lines changed

1 file changed

+86
-14
lines changed

tests/test_malformed.py

+86-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from liquid.exceptions import FilterArgumentError
1818
from liquid.exceptions import LiquidSyntaxError
1919
from liquid.exceptions import LiquidTypeError
20+
from liquid.exceptions import LiquidWarning
2021
from liquid.exceptions import TemplateNotFound
2122
from liquid.exceptions import lookup_warning
2223
from liquid.extra import IfNotTag
@@ -34,20 +35,20 @@ class Case(NamedTuple):
3435
expect_render: str = ""
3536

3637
@property
37-
def exceptions(self):
38+
def exceptions(self) -> Tuple[Type[Error], ...]:
3839
if isinstance(self.expect_exception, tuple):
3940
return self.expect_exception
4041
return (self.expect_exception,)
4142

4243
@property
43-
def warnings(self):
44+
def warnings(self) -> Tuple[Type[LiquidWarning], ...]:
4445
return tuple(lookup_warning(e) for e in self.exceptions)
4546

4647

4748
class MalformedTemplateTestCase(TestCase):
4849
"""Malformed template test case."""
4950

50-
def setUp(self):
51+
def setUp(self) -> None:
5152
self.global_context = {
5253
"product": {
5354
"some-tags": ["hello", "there"],
@@ -56,7 +57,7 @@ def setUp(self):
5657
"tag": "goodbye",
5758
}
5859

59-
def _test(self, test_cases: Iterable[Case], mode: Mode = Mode.STRICT):
60+
def _test(self, test_cases: Iterable[Case], mode: Mode = Mode.STRICT) -> None:
6061
"""Helper method for running lists of `Case`s in each render mode."""
6162
self._test_with_env(Environment(tolerance=mode), test_cases)
6263

@@ -76,7 +77,7 @@ def _test(self, test_cases: Iterable[Case], mode: Mode = Mode.STRICT):
7677
env, [case for case in test_cases if "||" not in case.template]
7778
)
7879

79-
def _test_with_env(self, env: Environment, test_cases: Iterable[Case]):
80+
def _test_with_env(self, env: Environment, test_cases: Iterable[Case]) -> None:
8081
for case in test_cases:
8182
with self.subTest(msg=case.description, mode=env.mode):
8283
if env.mode == Mode.STRICT:
@@ -104,7 +105,9 @@ def _test_with_env(self, env: Environment, test_cases: Iterable[Case]):
104105
result = template.render()
105106
self.assertEqual(result, case.expect_render)
106107

107-
def _test_partial(self, test_cases: Iterable[Case], templates: Dict[str, str]):
108+
def _test_partial(
109+
self, test_cases: Iterable[Case], templates: Dict[str, str]
110+
) -> None:
108111
"""Helper method for testing lists of 'include' or 'render' cases."""
109112
env = Environment(loader=DictLoader(templates))
110113
for case in test_cases:
@@ -133,7 +136,7 @@ def _test_partial(self, test_cases: Iterable[Case], templates: Dict[str, str]):
133136
result = template.render()
134137
self.assertEqual(result, case.expect_render)
135138

136-
def test_liquid_syntax(self):
139+
def test_liquid_syntax(self) -> None:
137140
"""Test that we fail early and loud when parsing a malformed template."""
138141
test_cases = [
139142
Case(
@@ -390,14 +393,14 @@ def test_liquid_syntax(self):
390393
self._test(test_cases, mode=Mode.WARN)
391394
self._test(test_cases, mode=Mode.LAX)
392395

393-
def test_liquid_syntax_from_template_api(self):
396+
def test_liquid_syntax_from_template_api(self) -> None:
394397
"""Test that syntax errors are raised by default when using the `Template`
395398
API."""
396399
with self.assertRaises(LiquidSyntaxError):
397400
# Missing colon before filter argument
398401
Template(r"{{ a | sort foo }}")
399402

400-
def test_unrecoverable_syntax_errors(self):
403+
def test_unrecoverable_syntax_errors(self) -> None:
401404
"""Test that we fail early and loud when parsing a malformed template."""
402405
test_cases = [
403406
Case(
@@ -412,23 +415,92 @@ def test_unrecoverable_syntax_errors(self):
412415
expect_exception=LiquidSyntaxError,
413416
expect_msg="expected '%}', found 'eof', on line 1",
414417
),
418+
]
419+
420+
self._test(test_cases, mode=Mode.STRICT)
421+
422+
def test_error_line_numbers(self) -> None:
423+
"""Test that we get the correct line number in error messages."""
424+
test_cases = [
415425
Case(
416426
description="issue #162",
417427
template="Hello, \nMy name is {{{ name }}",
418428
expect_exception=LiquidSyntaxError,
419429
expect_msg="unexpected '{', on line 2",
420430
),
421431
Case(
422-
description="issue #162",
432+
description="echo",
423433
template="Hello, \nMy name is {% echo { name %}",
424434
expect_exception=LiquidSyntaxError,
425435
expect_msg="unexpected '{', on line 2",
426436
),
437+
Case(
438+
description="assign",
439+
template="Hello, \n\n {% assign x = { name %}",
440+
expect_exception=LiquidSyntaxError,
441+
expect_msg="unexpected '{', on line 3",
442+
),
443+
Case(
444+
description="decrement",
445+
template="Hello, \n\n {% decrement { x %}",
446+
expect_exception=LiquidSyntaxError,
447+
expect_msg="unexpected '{', on line 3",
448+
),
449+
Case(
450+
description="increment",
451+
template="Hello, \n\n {% increment { x %}",
452+
expect_exception=LiquidSyntaxError,
453+
expect_msg="unexpected '{', on line 3",
454+
),
455+
Case(
456+
description="for",
457+
template="Hello, \n {% for x \nin { y %}{% endfor %}",
458+
expect_exception=LiquidSyntaxError,
459+
expect_msg="unexpected '{', on line 3",
460+
),
461+
Case(
462+
description="if",
463+
template="Hello, \n {% if x == { y %}{% endif %}",
464+
expect_exception=LiquidSyntaxError,
465+
expect_msg="unexpected '{', on line 2",
466+
),
467+
Case(
468+
description="elsif",
469+
template="Hello, \n {% if x == y %}\n{% elsif z < { %}{% endif %}",
470+
expect_exception=LiquidSyntaxError,
471+
expect_msg="unexpected '{', on line 3",
472+
),
473+
Case(
474+
description="render",
475+
template="Hello, \n {% render 'foo' x: { %}",
476+
expect_exception=LiquidSyntaxError,
477+
expect_msg="unexpected '{', on line 2",
478+
),
479+
Case(
480+
description="tablerow",
481+
template="Hello, \n {% tablerow x in { y %}{% endtablerow %}",
482+
expect_exception=LiquidSyntaxError,
483+
expect_msg="unexpected '{', on line 2",
484+
),
485+
Case(
486+
description="unless",
487+
template="Hello, \n {% unless x == { y %}{% endunless %}",
488+
expect_exception=LiquidSyntaxError,
489+
expect_msg="unexpected '{', on line 2",
490+
),
491+
Case(
492+
description="unless elsif",
493+
template=(
494+
"Hello, \n {% unless x == y %}\n{% elsif z < { %}{% endunless %}"
495+
),
496+
expect_exception=LiquidSyntaxError,
497+
expect_msg="unexpected '{', on line 3",
498+
),
427499
]
428500

429501
self._test(test_cases, mode=Mode.STRICT)
430502

431-
def test_bad_include(self):
503+
def test_bad_include(self) -> None:
432504
"""Test that we gracefully handle include errors."""
433505
test_cases = [
434506
Case(
@@ -498,7 +570,7 @@ def test_bad_include(self):
498570

499571
self._test_partial(test_cases, templates)
500572

501-
def test_bad_render(self):
573+
def test_bad_render(self) -> None:
502574
"""Test that we gracefully handle render errors."""
503575
test_cases = [
504576
Case(
@@ -559,7 +631,7 @@ def test_bad_render(self):
559631

560632
self._test_partial(test_cases, templates)
561633

562-
def test_resume_block(self):
634+
def test_resume_block(self) -> None:
563635
"""Test that we continue to execute a block after a single statement error."""
564636
source = (
565637
r"{% if true %}"
@@ -594,7 +666,7 @@ def test_resume_block(self):
594666

595667
self.assertEqual(result, "before error after error")
596668

597-
def test_invalid_identifiers(self):
669+
def test_invalid_identifiers(self) -> None:
598670
"""Test that we gracefully handle invalid identifiers."""
599671
test_cases = [
600672
Case(

0 commit comments

Comments
 (0)