async def test_static_node(self): template = Template("{% static 'js/app.js' %}") self.app.template_engine.add_template(template, ['test']) self.app.template_engine.compile_templates() self.assertEqual( '/static/js/app.js', await self.app.template_engine.render('test') )
def test_for_node_with_if_else_condition(self): """ :return: """ tp = TemplateParser() content = """ {% for x in range(0, 10)%} {% if x == 0 %} {{ x }} {% else %} - {% endif %} {% endfor %} """.replace( "\n", "" ).replace( " ", "" ) parsed = tp.parse(Template(content=content)) expected_types = deque([ForNode, IfNode, EvalNode, ElseNode, TextNode]) generated_nodes = parsed.flat_view(parsed.ast) while expected_types: expected_type = expected_types.popleft() current_node = next(generated_nodes) self.assertIsInstance(current_node, expected_type)
async def test_url_for_inside_for_node(self): template = Template('{% for x in range(0, 10)%}{% url "home" %}{% endfor %}') self.app.template_engine.add_template(template, ['test']) self.app.template_engine.compile_templates() self.assertEqual( self.app.url_for('home') * 10, await self.app.template_engine.render('test') )
def test_for_node(self): """ :return: """ tp = TemplateParser() parsed = tp.parse(Template(content="{% for x in range(0, 10)%}{{x}}{%endfor %}")) expected_types = deque([ForNode, EvalNode]) generated_nodes = parsed.flat_view(parsed.ast) while expected_types: expected_type = expected_types.popleft() current_node = next(generated_nodes) self.assertIsInstance(current_node, expected_type)
async def test_simple_exception_expects_correct_line_in_stack(self): """ :return: """ content = """ {% for x in range(0, 10)%} {{ x.non_existent_call() }} {% endfor %} """ self.template_engine.add_template(Template(content=content), names=['test']) self.template_engine.compile_templates(verbose=False) try: await self.template_engine.render('test') except TemplateRenderError as error: self.assertEqual(error.template_line, '{{ x.non_existent_call() }}') self.assertIsInstance(error.original_exception, AttributeError)
async def test_empty_template_expects_empty_string(self): template = Template('') self.app.template_engine.add_template(template, ['test']) self.app.template_engine.compile_templates(verbose=False) self.assertEqual('', await self.app.template_engine.render('test'))
async def test_empty_template_expects_empty_string(self): template = Template("") self.engine.add_template(template, ["test"]) self.engine.compile_templates(verbose=False) self.assertEqual("", await self.engine.render("test"))
async def test_render_for_with_a_single_variable(self): template = Template("{% for a in [(1, 2)] %} {{ a }} {% endfor %}") self.engine.add_template(template, ["test"]) self.engine.compile_templates(verbose=False) self.assertEqual(" (1, 2) ", await self.engine.render("test"))
async def test_render_for_with_with_unpacked_variables(self): template = Template( "{% for a, b in [(1, 2)] %} {{ a }} + {{ b }} {% endfor %}") self.engine.add_template(template, ["test"]) self.engine.compile_templates(verbose=False) self.assertEqual(" 1 + 2 ", await self.engine.render("test"))