def _test(self, test_cases): """Helper method for testing lists of test cases.""" for case in test_cases: env = Environment(loader=DictLoader(case.partials)) env.add_tag(IfNotTag) template = env.from_string(case.template, globals=case.globals) with self.subTest(msg=case.description): result = template.render() self.assertEqual(result, case.expect)
class RenderFilterTestCase(unittest.TestCase): def setUp(self): self.env = Environment() def _test(self, filter_: Callable[..., Any], test_cases: Iterable[RenderCase]): for case in test_cases: self.env.loader = DictLoader(case.partials) self.env.add_filter(filter_.name, filter_) template = self.env.from_string(case.template, globals=case.globals) with self.subTest(msg=case.description): if isclass(case.expect) and issubclass(case.expect, Exception): with self.assertRaises(case.expect): template.render() else: result = template.render() self.assertEqual(result, case.expect)
def _test(self, test_cases, lex_func, parse_func): """Utility method for evaluating lists of test cases.""" env = Environment() for case in test_cases: context = Context(env, case.context) with self.subTest(msg=case.description): tokens = TokenStream(lex_func(case.expression)) expr = parse_func(tokens) res = expr.evaluate(context) self.assertEqual(res, case.expect)
def test_golden_if(self): """Test liquid's golden test cases.""" for case in golden_cases: env = Environment( loader=DictLoader(case.partials), tolerance=Mode.STRICT, ) with self.subTest(msg=case.description): if case.error: with self.assertRaises(Error): template = env.from_string(case.template, globals=case.globals) result = template.render() else: template = env.from_string(case.template, globals=case.globals) result = template.render() self.assertEqual(result, case.expect)
def test_render_macro_async(self): """Test that we can render a macro asynchronously.""" env = Environment() env.add_tag(MacroTag) env.add_tag(CallTag) template = env.from_string(r"{% macro 'foo', you: 'World' %}" r"Hello, {{ you }}!" r"{% endmacro %}" r"{% call 'foo' %}" r"{% call 'foo', you: 'you' %}" r"{% call 'nosuchthing' %}") async def coro(): return await template.render_async() result = asyncio.run(coro()) self.assertEqual(result, "Hello, World!Hello, you!")
def test_render_macro_strict_undefined(self): """Test that missing arguments and undefined macros are `Undefined`.""" test_cases = [ RenderCase( description="missing argument", template=(r"{% macro 'func', foo %}" r"{{ foo }}" r"{% endmacro %}" r"{% call 'func' %}"), expect="'foo' is undefined, on line 1", globals={}, partials={}, ), RenderCase( description="undefined macro", template=(r"{% call 'func' %}"), expect="'func' is undefined, on line 1", globals={}, partials={}, ), ] for case in test_cases: env = Environment( loader=DictLoader(case.partials), undefined=StrictUndefined, ) env.add_tag(MacroTag) env.add_tag(CallTag) template = env.from_string(case.template, globals=case.globals) with self.subTest(msg=case.description): with self.assertRaises(UndefinedError) as raised: template.render() self.assertEqual(case.expect, str(raised.exception))
def setUp(self): self.env = Environment()
def setUp(self): self.env = Environment() self.ctx = Context(self.env)
def test_render_macro(self): """Test that we can render `macro` and `call` tags.""" test_cases = [ RenderCase( description="basic macro no call", template=r"{% macro 'func' %}Hello, World!{% endmacro %}", expect="", globals={}, partials={}, ), RenderCase( description="call basic macro", template=(r"{% macro 'func' %}Hello, World!{% endmacro %}" r"{% call 'func' %}"), expect="Hello, World!", globals={}, partials={}, ), RenderCase( description="call basic macro multiple times", template=(r"{% macro 'func' %}Hello, World!{% endmacro %}" r"{% call 'func' %}" r"{% call 'func' %}"), expect="Hello, World!Hello, World!", globals={}, partials={}, ), RenderCase( description="call macro with argument", template=( r"{% macro 'func', you %}Hello, {{ you }}!{% endmacro %}" r"{% call 'func', 'you' %} " r"{% call 'func', 'World' %}"), expect="Hello, you! Hello, World!", globals={}, partials={}, ), RenderCase( description="call macro with default argument", template= (r"{% macro 'func' you: 'brian' %}Hello, {{ you }}!{% endmacro %}" r"{% call 'func' %} " r"{% call 'func' you: 'World' %}"), expect="Hello, brian! Hello, World!", globals={}, partials={}, ), RenderCase( description="boolean literal default argument", template=(r"{% macro 'func' foo: false %}" r"{% if foo %}Hello, World!{% endif %}" r"{% endmacro %}" r"{% call 'func' %}" r"{% call 'func' foo: true %}"), expect="Hello, World!", globals={}, partials={}, ), RenderCase( description="chained default argument from context", template=(r"{% macro 'func' greeting: foo.bar %}" r"{{ greeting }}, World!" r"{% endmacro %}" r"{% call 'func' %}" r"{% call 'func' greeting: 'Goodbye' %}"), expect="Hello, World!Goodbye, World!", globals={"foo": { "bar": "Hello" }}, partials={}, ), RenderCase( description="excess arguments", template=(r"{% macro 'func' %}" r"{{ args | join: '-' }}" r"{% endmacro %}" r"{% call 'func' 1, 2 %}"), expect="1-2", globals={}, partials={}, ), RenderCase( description="excess keyword arguments", template=(r"{% macro 'func' %}" r"{% for arg in kwargs %}" r"{{ arg.0 }} => {{ arg.1 }}, " r"{% endfor %}" r"{% endmacro %}" r"{% call 'func', a: 1, b: 2 %}"), expect="a => 1, b => 2, ", globals={}, partials={}, ), RenderCase( description="missing argument", template=(r"{% macro 'func', foo %}" r"{{ foo }}" r"{% endmacro %}" r"{% call 'func' %}"), expect="", globals={}, partials={}, ), RenderCase( description="undefined macro", template=(r"{% call 'func' %}"), expect="", globals={}, partials={}, ), RenderCase( description="default before positional", template=( r"{% macro 'func' you: 'brian', greeting %}" r"{{ greeting }}, {{ you }}!" r"{% endmacro %}" r"{% call 'func' %} " r"{% call 'func' you: 'World', greeting: 'Goodbye' %}"), expect=", brian! Goodbye, World!", globals={}, partials={}, ), ] for case in test_cases: env = Environment(loader=DictLoader(case.partials)) env.add_tag(MacroTag) env.add_tag(CallTag) template = env.from_string(case.template, globals=case.globals) with self.subTest(msg=case.description): result = template.render() self.assertEqual(result, case.expect)