17
17
from liquid .exceptions import FilterArgumentError
18
18
from liquid .exceptions import LiquidSyntaxError
19
19
from liquid .exceptions import LiquidTypeError
20
+ from liquid .exceptions import LiquidWarning
20
21
from liquid .exceptions import TemplateNotFound
21
22
from liquid .exceptions import lookup_warning
22
23
from liquid .extra import IfNotTag
@@ -34,20 +35,20 @@ class Case(NamedTuple):
34
35
expect_render : str = ""
35
36
36
37
@property
37
- def exceptions (self ):
38
+ def exceptions (self ) -> Tuple [ Type [ Error ], ...] :
38
39
if isinstance (self .expect_exception , tuple ):
39
40
return self .expect_exception
40
41
return (self .expect_exception ,)
41
42
42
43
@property
43
- def warnings (self ):
44
+ def warnings (self ) -> Tuple [ Type [ LiquidWarning ], ...] :
44
45
return tuple (lookup_warning (e ) for e in self .exceptions )
45
46
46
47
47
48
class MalformedTemplateTestCase (TestCase ):
48
49
"""Malformed template test case."""
49
50
50
- def setUp (self ):
51
+ def setUp (self ) -> None :
51
52
self .global_context = {
52
53
"product" : {
53
54
"some-tags" : ["hello" , "there" ],
@@ -56,7 +57,7 @@ def setUp(self):
56
57
"tag" : "goodbye" ,
57
58
}
58
59
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 :
60
61
"""Helper method for running lists of `Case`s in each render mode."""
61
62
self ._test_with_env (Environment (tolerance = mode ), test_cases )
62
63
@@ -76,7 +77,7 @@ def _test(self, test_cases: Iterable[Case], mode: Mode = Mode.STRICT):
76
77
env , [case for case in test_cases if "||" not in case .template ]
77
78
)
78
79
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 :
80
81
for case in test_cases :
81
82
with self .subTest (msg = case .description , mode = env .mode ):
82
83
if env .mode == Mode .STRICT :
@@ -104,7 +105,9 @@ def _test_with_env(self, env: Environment, test_cases: Iterable[Case]):
104
105
result = template .render ()
105
106
self .assertEqual (result , case .expect_render )
106
107
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 :
108
111
"""Helper method for testing lists of 'include' or 'render' cases."""
109
112
env = Environment (loader = DictLoader (templates ))
110
113
for case in test_cases :
@@ -133,7 +136,7 @@ def _test_partial(self, test_cases: Iterable[Case], templates: Dict[str, str]):
133
136
result = template .render ()
134
137
self .assertEqual (result , case .expect_render )
135
138
136
- def test_liquid_syntax (self ):
139
+ def test_liquid_syntax (self ) -> None :
137
140
"""Test that we fail early and loud when parsing a malformed template."""
138
141
test_cases = [
139
142
Case (
@@ -390,14 +393,14 @@ def test_liquid_syntax(self):
390
393
self ._test (test_cases , mode = Mode .WARN )
391
394
self ._test (test_cases , mode = Mode .LAX )
392
395
393
- def test_liquid_syntax_from_template_api (self ):
396
+ def test_liquid_syntax_from_template_api (self ) -> None :
394
397
"""Test that syntax errors are raised by default when using the `Template`
395
398
API."""
396
399
with self .assertRaises (LiquidSyntaxError ):
397
400
# Missing colon before filter argument
398
401
Template (r"{{ a | sort foo }}" )
399
402
400
- def test_unrecoverable_syntax_errors (self ):
403
+ def test_unrecoverable_syntax_errors (self ) -> None :
401
404
"""Test that we fail early and loud when parsing a malformed template."""
402
405
test_cases = [
403
406
Case (
@@ -412,23 +415,92 @@ def test_unrecoverable_syntax_errors(self):
412
415
expect_exception = LiquidSyntaxError ,
413
416
expect_msg = "expected '%}', found 'eof', on line 1" ,
414
417
),
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 = [
415
425
Case (
416
426
description = "issue #162" ,
417
427
template = "Hello, \n My name is {{{ name }}" ,
418
428
expect_exception = LiquidSyntaxError ,
419
429
expect_msg = "unexpected '{', on line 2" ,
420
430
),
421
431
Case (
422
- description = "issue #162 " ,
432
+ description = "echo " ,
423
433
template = "Hello, \n My name is {% echo { name %}" ,
424
434
expect_exception = LiquidSyntaxError ,
425
435
expect_msg = "unexpected '{', on line 2" ,
426
436
),
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 \n in { 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
+ ),
427
499
]
428
500
429
501
self ._test (test_cases , mode = Mode .STRICT )
430
502
431
- def test_bad_include (self ):
503
+ def test_bad_include (self ) -> None :
432
504
"""Test that we gracefully handle include errors."""
433
505
test_cases = [
434
506
Case (
@@ -498,7 +570,7 @@ def test_bad_include(self):
498
570
499
571
self ._test_partial (test_cases , templates )
500
572
501
- def test_bad_render (self ):
573
+ def test_bad_render (self ) -> None :
502
574
"""Test that we gracefully handle render errors."""
503
575
test_cases = [
504
576
Case (
@@ -559,7 +631,7 @@ def test_bad_render(self):
559
631
560
632
self ._test_partial (test_cases , templates )
561
633
562
- def test_resume_block (self ):
634
+ def test_resume_block (self ) -> None :
563
635
"""Test that we continue to execute a block after a single statement error."""
564
636
source = (
565
637
r"{% if true %}"
@@ -594,7 +666,7 @@ def test_resume_block(self):
594
666
595
667
self .assertEqual (result , "before error after error" )
596
668
597
- def test_invalid_identifiers (self ):
669
+ def test_invalid_identifiers (self ) -> None :
598
670
"""Test that we gracefully handle invalid identifiers."""
599
671
test_cases = [
600
672
Case (
0 commit comments