def test_parse_single_if_statement(): sc = Scanner('#<if_statement>', "{% if somevar %}Foo!{% endif %}") p = Parser(sc) s = p.parse_statement() assert (isinstance(s, IfStatement)) assert (len(s.cases) == 1) case0 = s.cases[0] assert (isinstance(case0.test, VarRefExpression)) assert (case0.test.name == 'somevar') assert (len(case0.cons) == 1) assert (isinstance(case0.cons[0], TextStatement)) assert (case0.cons[0].text == 'Foo!')
def test_parse_for_in_statement(): sc = Scanner('#<for_in_statement>', "{% for i in range(1, 10) %}Foo!{% endfor %}") p = Parser(sc) s = p.parse_statement() assert (isinstance(s, ForInStatement)) assert (isinstance(s.pattern, VarPattern)) assert (s.pattern.name == 'i') assert (isinstance(s.expression, AppExpression)) assert (isinstance(s.expression.operator, VarRefExpression)) assert (s.expression.operator.name == 'range') assert (len(s.expression.operands) == 2) assert (isinstance(s.expression.operands[0], ConstExpression)) assert (s.expression.operands[0].value == 1) assert (isinstance(s.expression.operands[1], ConstExpression)) assert (s.expression.operands[1].value == 10) assert (len(s.body) == 1) assert (isinstance(s.body[0], TextStatement)) assert (s.body[0].text == 'Foo!')
def test_parse_fail_parse_close_delimiter(): sc1 = Scanner('#<wrong_for_endif>', "{% for i in range(1, 10) %}Foo!{% endif %}") p1 = Parser(sc1) with pytest.raises(ParseError): p1.parse_statement() sc2 = Scanner('#<wrong_for_for>', "{% for i in range(1, 20) %}Foo!{% for %}") p2 = Parser(sc2) with pytest.raises(ParseError): p2.parse_statement() sc3 = Scanner('#<wrong_if_endfor>', "{% if somevmar %}Foo!{% endfor %}") p3 = Parser(sc3) with pytest.raises(ParseError): p3.parse_statement()