def test_signals_are_emitted(app, context): """Signal is emitted when templates are generated""" with app.test_request_context(): with captured_templates(app) as templates: render_template("test.html", context) assert templates.__len__() == 1 assert templates[0][0].filename == "test.html" assert templates[0][1]["name"] == "Rudolf"
def test_blueprint_templates(app, context): """Templates can be loaded from blueprint packages""" with app.test_request_context(): rendered = render_template("test_blueprint/blueprint-template.txt", context) assert rendered == "Hello blueprint Rudolf\n" rendered = render_template("test_blueprint/prefix-blueprint-template.txt", context) assert rendered == "Hello blueprint Rudolf\n" rendered = render_template("prefix-blueprint-template.txt", context) assert rendered == "Hello no-prefix-blueprint Rudolf\n"
def test_filters_per_render(app): """Filters can be applied per rendering""" with app.test_request_context(): def prepend_title(template): return template | Transformer('head/title').append(' - Flask-Genshi') rendered = render_template('filter.html', filter=prepend_title) expected = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' '"http://www.w3.org/TR/html4/strict.dtd">\n' '<html><head><title>Hi! - Flask-Genshi</title></head></html>') assert rendered == expected
def test_applies_method_filters(app): """Method filters are applied for generated and rendered templates""" with app.test_request_context(): genshi = app.extensions['genshi'] @genshi.filter('html') def prepend_title(template): return template | Transformer('head/title').prepend('Flask-Genshi - ') rendered = render_template('filter.html') expected = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' '"http://www.w3.org/TR/html4/strict.dtd">\n' '<html><head><title>Flask-Genshi - Hi!</title></head></html>') assert rendered == expected
def test_does_translations(app): """Callback interface is able to inject Translator filter""" with app.test_request_context(): genshi = app.extensions['genshi'] @genshi.template_parsed def callback(template): Translator(lambda s: s.upper()).setup(template) rendered = render_template('i18n.html') expected = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' '"http://www.w3.org/TR/html4/strict.dtd">\n' '<p>HELLO!</p>') assert rendered == expected
def test_filters_per_render(app): """Filters can be applied per rendering""" with app.test_request_context(): def prepend_title(template): return template | Transformer("head/title").append( " - Flask-Genshi") rendered = render_template("filter.html", filter=prepend_title) # Remove leading indentation, for cleaner multi-line string expected = cleandoc(""" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Hi! - Flask-Genshi</title></head></html> """) assert rendered == expected
def test_provides_jinja_tests_and_filters(app): """Jinja tests and filters should be provided as context dictionaries.""" with app.test_request_context(): rendered = render_template("jinja_tests_and_filters.html") # Remove leading indentation, for cleaner multi-line string expected_data = cleandoc(""" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <p class="odd"> HELLO WORLD Hello... foo bar FooBar </p> """) assert rendered == expected_data
def test_provides_jinja_tests_and_filters(app): """Jinja tests and filters should be provided as context dictionaries.""" with app.test_request_context(): rendered = render_template('jinja_tests_and_filters.html') expected_data = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' '"http://www.w3.org/TR/html4/strict.dtd">\n' '<p class="odd">\n' ' HELLO WORLD\n' ' <span class="even">\n' ' hello world\n' ' </span>\n' ' Hello...\n' ' foo bar\n' ' FooBar\n' '</p>') assert rendered == expected_data
def test_applies_method_filters(app): """Method filters are applied for generated and rendered templates""" with app.test_request_context(): genshi = app.extensions["genshi"] @genshi.filter("html") def prepend_title(template): return template | Transformer("head/title").prepend( "Flask-Genshi - ") rendered = render_template("filter.html") # Remove leading indentation, for cleaner multi-line string expected = cleandoc(""" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><title>Flask-Genshi - Hi!</title></head></html> """) assert rendered == expected
def test_does_translations(app): """Callback interface is able to inject Translator filter""" with app.test_request_context(): genshi = app.extensions["genshi"] @genshi.template_parsed def callback(template): Translator(lambda s: s.upper()).setup(template) rendered = render_template("i18n.html") # Remove leading indentation, for cleaner multi-line string expected = cleandoc(""" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <p>HELLO!</p> """) assert rendered == expected
def test_works_with_flatland(app): """Filters can take the context and support flatland""" with app.test_request_context(): genshi = app.extensions["genshi"] @genshi.template_parsed def callback(template): flatland_setup(template) context = dict(form=FlatlandForm({"username": "******"})) rendered = render_template("flatland.html", context) # Remove leading indentation, for cleaner multi-line string expected = cleandoc(""" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <input type="text" name="username" value="dag"> """) assert rendered == expected