def test_qreeSpecialTokenError(): "Test that special token `__qree` is disallowed." badTplList = [ # 1, 2, 3: "__qree", "{{: data.__qree :}}", "{{= data['__qree'] =}}", # 4: """ @= import json; @= data.update({"__qree": "foobar"}); {{: json.dumps(data) :}} """, ] assert len(badTplList) == 4 for badTpl in badTplList: with pytest.raises(SyntaxError) as e: qree.renderStr(badTpl, data={}) assert type(e.value) is SyntaxError assert "special token" in e.value.args[0] # Next: goodTpl = """ @= import json; @= data.update({"foo": "bar"}); {{: json.dumps(data) :}} """ expected = """ {"foo": "bar"} """ assert qree.renderStr(goodTpl, data={}) == expected
def test_basic_substitution(): tpl = "Hello, {{: data + '!' :}}" assert qree.renderStr(tpl, "World") == 'Hello, World!' assert qree.renderStr(tpl, "> World") == 'Hello, > World!' tpl2 = "Hello, {{= data + '!' =}}" assert qree.renderStr(tpl2, "World") == 'Hello, World!' assert qree.renderStr(tpl2, "> World") == 'Hello, > World!'
def test_substitution_tag_error(): "Test unclosed/nested substitution tags raise SyntaxError." badTplList = [ "Hello, {{: data + '!' ]]", # Unclosed "Hello, {{= {{: data + '!' :}} =}}", # Nesting banned "Hello, {{: {{= data + '!' =}} :}}", """ @= import qree; {{: qree.renderStr('{{= data.title() =}}', data=data) :}} """, ] for badTpl in badTplList: with pytest.raises(SyntaxError) as e: qree.renderStr(badTpl, "World") assert type(e.value) is SyntaxError assert "Tag-mismatch" in e.value.args[0]
def test_indent_tag_error(): badTplList = [ # 1, 2 : "@{", "@}", # -------------------------- # 3: """ @= if True: @{ Unclosed indent. # """, # -------------------------- # 4: """ @= if True: # Unopened indent. @} """, # -------------------------- # 5: """ @= if True: @{ if True: @{ Unclosed inner indent. # @} """, # -------------------------- # 6: """ @= if True: @{ Non-comment line along w/ open-indent. @} """, # -------------------------- # 7: """ @= if True: @{ @} Non-comment line along w/ close-indent. """, ] assert len(badTplList) == 7 for badTpl in badTplList: with pytest.raises(IndentationError): print(qree.renderStr(badTpl, data=None))
def test_basic_indent(): tpl = """ @= for n in range(1, data+1): @{ {{: n :}} is {{: 'EVEN' if n % 2 == 0 else 'ODD' :}} @} """ expected = """ 1 is ODD 2 is EVEN 3 is ODD 4 is EVEN """ assert qree.renderStr(tpl, data=4) == expected
def test_nested_indent(): tpl = """ @= for n in range(1, data+1): @{ @= if n % 15 == 0: @{ # Comment w/ indent-open. FizzBuzz! @} @= elif n % 3 == 0: @{ Fizz! @} # Comment w/ indent-close. @= elif n % 5 == 0: @{ Buzz! @} @= else: @{ {{: n :}} @} @} """ expected = """ 1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 FizzBuzz! """ assert qree.renderStr(tpl, data=15) == expected
def test_identity(): assert qree.renderStr("plain") == "plain" assert qree.renderStr('foo\nbar\nbaz') == 'foo\nbar\nbaz'
def test_pyQuotesOk(): "Test that single-quotes _within_ py-code are OK." qree.renderStr("{{: data['foo'] :}}", {"foo": "bar"}) == "bar" qree.renderStr("{{: data['''foo'''] :}}", {"foo": "bar"}) == "bar"
def test_nonPyQuotesOk(): "Test that single-quotes _outside_ py-code are OK." qree.renderStr("Trip ''' quotes") == "Trip ''' quotes" qree.renderStr("'{{: data :}}'", None) == "'None'"