def test_parser_parse_for(liqparser): liqparser.stream = LiquidStream.from_string('for a in b') with pytest.raises(LiquidSyntaxError) as ex: liqparser.parse_statement('{%') assert "Expecting a closing tag for '{%'" in str(ex.value) liqparser.stream = LiquidStream.from_string('for a in b %} aa') liqparser.parse_statement('{%')
def test_include_extends_stacks(HERE): with pytest.raises(LiquidSyntaxError) as exc: liquid = Liquid(""" {{%- mode compact %}} {{% extends {0}/templates/extends.liq %}} {{% block b2 %}} {{% include "{0}/templates/include4.liq" x %}} {{{{x}}}} {{% endblock %}} """.format(HERE), liquid_loglevel='debug') stream = LiquidStream.from_string(str(exc.value)) _, tag = stream.until( ["'for' node expects format: 'for var1, var2 in expr'"]) assert bool(tag) _, tag = stream.until(["File <LIQUID TEMPLATE SOURCE>"]) assert bool(tag) _, tag = stream.until(["> 5."]) assert bool(tag) _, tag = stream.until(["File"]) assert bool(tag) _, tag = stream.until(["> 1. {% include include3.liq %}"]) assert bool(tag) _, tag = stream.until(["File"]) assert bool(tag) _, tag = stream.until(["> 9."], wraps=[]) assert bool(tag)
def test_include_extends_render_stacks(HERE): liquid = Liquid(""" {{%- mode compact %}} {{% extends {0}/templates/extends.liq %}} {{% block b2 %}} {{% include "{0}/templates/include1.liq" x %}} {{{{x}}}} {{% endblock %}} """.format(HERE), liquid_loglevel='debug') # see if I have render stacks correct: with pytest.raises(LiquidRenderError) as exc: liquid.render(x={}) # can't do {} * 10 stream = LiquidStream.from_string(str(exc.value)) _, tag = stream.until( ["unsupported operand type(s) for *: 'dict' and 'int'"], wraps=[], quotes=[]) assert bool(tag) _, tag = stream.until(["Template call stacks:"], wraps=[], quotes=[]) assert bool(tag) _, tag = stream.until(["File <LIQUID TEMPLATE SOURCE>"]) assert bool(tag) _, tag = stream.until(["> 5. {% include"]) assert bool(tag) _, tag = stream.until(["templates/include1.liq"], wraps=[], quotes=[]) assert bool(tag) _, tag = stream.until(["> 1. {% assign"], wraps=[], quotes=[]) assert bool(tag) _, tag = stream.until(["Compiled source"], wraps=[], quotes=[]) assert bool(tag) _, tag = stream.until(["> 33."], wraps=[], quotes=[]) assert bool(tag)
def test_parser_parse_mode(liqparser): liqparser.stream = LiquidStream.from_string( 'config mode=loose, loglevel=debug -%}') liqparser.parse_statement('{%') assert liqparser.config.mode == 'loose' assert logging.getLevelName( logging.getLogger(LIQUID_LOGGER_NAME).level) == 'DEBUG'
def liqparser(): precode = LiquidCode() code = LiquidCode() stream = LiquidStream.from_string("abcde") return LiquidParser(stream=stream, shared_code=precode, code=code, filename='', prev=None, config=LiquidConfig(mode='compact', loglevel='detail', include='', extends=None))
def test_single_extends(debug, HERE): with pytest.raises(LiquidSyntaxError) as exc: liq = Liquid("""1 2 3 4 5 6 7 {{% extends {}/templates/parent3.liq %}} 9 10 11 12 13 14 """.format(HERE), liquid_loglevel='debug') #print(exc.value) stream = LiquidStream.from_string(str(exc.value)) # Unmatched end tag: 'endfor', expect 'endif' # Template call stacks: # ---------------------------------------------- # File <LIQUID TEMPLATE SOURCE> # > 8. {% extends /.../liquidpy/tests/templates/parent3.liq %} # File /.../liquidpy/tests/templates/parent3.liq # 10. {% if true %} # 11. 1 # 12. {% # 13. # 14. # > 15. endfor %} # 16. {% endcapture %} _, tag = stream.until(["Unclosed tag '{%'"]) assert bool(tag) _, tag = stream.until(["Template call stacks:"]) assert bool(tag) _, tag = stream.until(["File <LIQUID TEMPLATE SOURCE>"]) assert bool(tag) _, tag = stream.until(["> 8. {% extends"]) assert bool(tag) _, tag = stream.until(["liquidpy/tests/templates/parent3.liq"]) assert bool(tag) _, tag = stream.until(["9. {% capture x %}"]) assert bool(tag) _, tag = stream.until(["> 12. {%"]) assert bool(tag)
def test_parser_init(): precode = LiquidCode() code = LiquidCode() stream = LiquidStream.from_string("abcde") lp = LiquidParser(stream=stream, shared_code=precode, code=code, prev=None, config=None, filename='') assert lp.stream is stream assert lp.config is None assert lp.context.stacks == [] assert lp.shared_code is precode assert lp.code is code assert lp.endtag is None
def test_multi_extends(debug, HERE): with pytest.raises(LiquidSyntaxError) as exc: Liquid("{{% extends {}/templates/parent4.liq %}}".format(HERE), liquid_loglevel='debug') # Statement "for" expects format: "for var1, var2 in expr" # # Template call stacks: # ---------------------------------------------- # File <LIQUID TEMPLATE SOURCE> # > 1. {% extends /.../liquidpy/tests/templates/parent4.liq %} # File /.../liquidpy/tests/templates/parent4.liq # > 6. {% include include3.liq %} # File /.../liquidpy/tests/templates/include3.liq # 4. 4. comment # 5. 5. {% endcomment %} # 6. 6. {% for multi # 7. 7. line, just # 8. 8. to check if the # > 9. 9. line number is correct %} # 10. 10. {{ "I have" + # 11. 11. "some other lines" }} # 12. 12. {% endfor %} stream = LiquidStream.from_string(str(exc.value)) _, tag = stream.until( ["'for' node expects format: 'for var1, var2 in expr'"]) assert bool(tag) _, tag = stream.until(['Template call stacks:']) assert bool(tag) _, tag = stream.until(['File <LIQUID TEMPLATE SOURCE>']) assert bool(tag) _, tag = stream.until(['> 1. {% extends']) assert bool(tag) _, tag = stream.until(['liquidpy/tests/templates/parent4.liq']) assert bool(tag) _, tag = stream.until(['> 6. {% include include3.liq %}']) assert bool(tag) _, tag = stream.until(['liquidpy/tests/templates/include3.liq']) assert bool(tag) _, tag = stream.until(['4. 4. comment']) assert bool(tag) _, tag = stream.until(['> 9. 9. line number is correct %}']) assert bool(tag) _, tag = stream.until(['12. 12. {% endfor %}']) assert bool(tag)
def test_syntax_error_multi_include(HERE): with pytest.raises(LiquidSyntaxError) as exc: Liquid("{{% include {}/templates/include4.liq %}}".format(HERE), liquid_loglevel='debug') stream = LiquidStream.from_string(str(exc.value)) _, tag = stream.until( ["'for' node expects format: 'for var1, var2 in expr'"]) assert bool(tag) _, tag = stream.until(['Template call stacks:']) assert bool(tag) _, tag = stream.until(['File <LIQUID TEMPLATE SOURCE>']) assert bool(tag) _, tag = stream.until(['> 1. {% include']) assert bool(tag) _, tag = stream.until(['liquidpy/tests/templates/include4.liq']) assert bool(tag) _, tag = stream.until(['> 1. {% include include3.liq %}']) assert bool(tag) _, tag = stream.until(['liquidpy/tests/templates/include3.liq']) assert bool(tag) _, tag = stream.until(['4. 4. comment']) assert bool(tag) _, tag = stream.until(['5. 5. {% endcomment %}']) assert bool(tag) _, tag = stream.until(['6. 6. {% for multi']) assert bool(tag) _, tag = stream.until(['7. 7. line, just']) assert bool(tag) _, tag = stream.until(['8. 8. to check if the']) assert bool(tag) _, tag = stream.until(['> 9. 9. line number is correct %}']) assert bool(tag) _, tag = stream.until(['10. 10. {{ "I have" +']) assert bool(tag) _, tag = stream.until(['11. 11. "some other lines" }}']) assert bool(tag) _, tag = stream.until(['12. 12. {% endfor %}']) assert bool(tag)
def test_syntax_error_single_include(HERE): with pytest.raises(LiquidSyntaxError) as exc: Liquid("""1 2 3 4 5 6 7 {{% include {}/templates/include3.liq %}} 9 10 11 12 13 14 """.format(HERE), liquid_loglevel='debug') # liquid.exceptions.LiquidSyntaxError: Statement "for" expects format: "for var1, var2 in expr" # # Template call stacks: # ---------------------------------------------- # File <LIQUID TEMPLATE SOURCE> # > 8. {% include /.../liquidpy/tests/templates/include3.liq %} # File /.../liquidpy/tests/templates/include3.liq # 4. 4. comment # 5. 5. {% endcomment %} # 6. 6. {% for multi # 7. 7. line, just # 8. 8. to check if the # > 9. 9. line number is correct %} # 10. 10. {{ "I have" + # 11. 11. "some other lines" }} # 12. 12. {% endfor %} stream = LiquidStream.from_string(str(exc.value)) _, tag = stream.until( ["'for' node expects format: 'for var1, var2 in expr'"]) assert bool(tag) _, tag = stream.until(['Template call stacks:']) assert bool(tag) _, tag = stream.until(['> 8. {% include']) assert bool(tag) _, tag = stream.until(['liquidpy/tests/templates/include3.liq']) assert bool(tag) _, tag = stream.until(['4. 4. comment']) assert bool(tag) _, tag = stream.until(['5. 5. {% endcomment %}']) assert bool(tag) _, tag = stream.until(['6. 6. {% for multi']) assert bool(tag) _, tag = stream.until(['7. 7. line, just']) assert bool(tag) _, tag = stream.until(['8. 8. to check if the']) assert bool(tag) _, tag = stream.until(['> 9. 9. line number is correct %}']) assert bool(tag) _, tag = stream.until(['10. 10. {{ "I have" +']) assert bool(tag) _, tag = stream.until(['11. 11. "some other lines" }}']) assert bool(tag) _, tag = stream.until(['12. 12. {% endfor %}']) assert bool(tag)