Skip to content

Commit dc6445b

Browse files
author
Peter Dolak
committed
Add tests for lstrip whitespace behavior fix
Also did peformance tests for the previous fix and saw no measurable impact.
1 parent 95dd93e commit dc6445b

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

tests/test_lexnparse.py

+92
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,98 @@ def test_lstrip_angle_bracket_compact(self, env):
713713
)
714714
assert tmpl.render(seq=range(5)) == "".join(f"{x}\n" for x in range(5))
715715

716+
def test_lstrip_blocks_outside_with_new_line(self):
717+
env = Environment(lstrip_blocks=True, trim_blocks=False)
718+
tmpl = env.from_string(
719+
" {% if kvs %}(\n"
720+
" {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
721+
" ){% endif %}"
722+
)
723+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
724+
assert out == "(\na=1 b=2 \n )"
725+
726+
def test_lstrip_trim_blocks_outside_with_new_line(self):
727+
env = Environment(lstrip_blocks=True, trim_blocks=True)
728+
tmpl = env.from_string(
729+
" {% if kvs %}(\n"
730+
" {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
731+
" ){% endif %}"
732+
)
733+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
734+
assert out == "(\na=1 b=2 )"
735+
736+
def test_lstrip_blocks_inside_with_new_line(self):
737+
env = Environment(lstrip_blocks=True, trim_blocks=False)
738+
tmpl = env.from_string(
739+
" ({% if kvs %}\n"
740+
" {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
741+
" {% endif %})"
742+
)
743+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
744+
assert out == " (\na=1 b=2 \n)"
745+
746+
def test_lstrip_trim_blocks_inside_with_new_line(self):
747+
env = Environment(lstrip_blocks=True, trim_blocks=True)
748+
tmpl = env.from_string(
749+
" ({% if kvs %}\n"
750+
" {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"
751+
" {% endif %})"
752+
)
753+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
754+
assert out == " (a=1 b=2 )"
755+
756+
def test_lstrip_blocks_without_new_line(self):
757+
env = Environment(lstrip_blocks=True, trim_blocks=False)
758+
tmpl = env.from_string(
759+
" {% if kvs %}"
760+
" {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}"
761+
" {% endif %}"
762+
)
763+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
764+
assert out == " a=1 b=2 "
765+
766+
def test_lstrip_trim_blocks_without_new_line(self):
767+
env = Environment(lstrip_blocks=True, trim_blocks=True)
768+
tmpl = env.from_string(
769+
" {% if kvs %}"
770+
" {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}"
771+
" {% endif %}"
772+
)
773+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
774+
assert out == " a=1 b=2 "
775+
776+
def test_lstrip_blocks_consume_after_without_new_line(self):
777+
env = Environment(lstrip_blocks=True, trim_blocks=False)
778+
tmpl = env.from_string(
779+
" {% if kvs -%}"
780+
" {% for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}"
781+
" {% endif -%}"
782+
)
783+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
784+
assert out == "a=1 b=2 "
785+
786+
def test_lstrip_trim_blocks_consume_before_without_new_line(self):
787+
env = Environment(lstrip_blocks=False, trim_blocks=False)
788+
tmpl = env.from_string(
789+
" {%- if kvs %}"
790+
" {%- for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}"
791+
" {%- endif %}"
792+
)
793+
out = tmpl.render(kvs=[("a", 1), ("b", 2)])
794+
assert out == "a=1 b=2 "
795+
796+
def test_lstrip_trim_blocks_comment(self):
797+
env = Environment(lstrip_blocks=True, trim_blocks=True)
798+
tmpl = env.from_string(" {# 1 space #}\n {# 2 spaces #} {# 4 spaces #}")
799+
out = tmpl.render()
800+
assert out == " " * 4
801+
802+
def test_lstrip_trim_blocks_raw(self):
803+
env = Environment(lstrip_blocks=True, trim_blocks=True)
804+
tmpl = env.from_string("{{x}}\n{%- raw %} {% endraw -%}\n{{ y }}")
805+
out = tmpl.render(x=1, y=2)
806+
assert out == "1 2"
807+
716808
def test_php_syntax_with_manual(self, env):
717809
env = Environment(
718810
"<?", "?>", "<?=", "?>", "<!--", "-->", lstrip_blocks=True, trim_blocks=True

0 commit comments

Comments
 (0)