def test_render_index_filter(self): test_cases = [ RenderCase( description="array of strings", template=r"{{ handles | index: 'foo' }}", expect="0", globals={"handles": ["foo", "bar"]}, partials={}, ), RenderCase( description="array of strings item does not exist", template=r"{{ handles | index: 'baz' }}", expect="", globals={"handles": ["foo", "bar"]}, partials={}, ), ] for case in test_cases: env = Environment() env.add_filter("index", index) env.loader = DictLoader(case.partials) template = env.from_string(case.template, globals=case.globals) with self.subTest(msg=case.description): result = template.render() self.assertEqual(result, case.expect)
def test_render_stylesheet_tag_filter(self): test_cases = [ RenderCase( description="from context variable", template=r"{{ url | stylesheet_tag }}", expect=( '<link href="assets/style.css" rel="stylesheet" ' 'type="text/css" media="all" />' ), globals={"url": "assets/style.css"}, partials={}, ), ] for case in test_cases: env = Environment() env.add_filter("stylesheet_tag", stylesheet_tag) env.loader = DictLoader(case.partials) template = env.from_string(case.template, globals=case.globals) with self.subTest(msg=case.description): result = template.render() self.assertEqual(result, case.expect)
def test_render_script_tag_filter(self): test_cases = [ RenderCase( description="url from context", template=r"{{ url | script_tag }}", expect=( '<script src="assets/assets/app.js" ' 'type="text/javascript"></script>' ), globals={"url": "assets/assets/app.js"}, partials={}, ), ] # Test new style filter implementation for case in test_cases: env = Environment() env.add_filter("script_tag", script_tag) env.loader = DictLoader(case.partials) template = env.from_string(case.template, globals=case.globals) with self.subTest(msg=case.description): result = template.render() self.assertEqual(result, case.expect)