| 1 |
""" |
| 2 |
JinjaX |
| 3 |
Copyright (c) Juan-Pablo Scaletti <[email protected]> |
| 4 |
""" |
| 5 |
import jinja2 |
| 6 |
import pytest |
| 7 |
from markupsafe import Markup |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 11 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 12 |
def test_render_prefixed(catalog, folder, folder_t, autoescape, undefined): |
| 13 |
"""Components mounted with a prefix should be able to import other components |
| 14 |
from the same folder without specifying the prefix. |
| 15 |
""" |
| 16 |
catalog.jinja_env.autoescape = autoescape |
| 17 |
catalog.jinja_env.undefined = undefined |
| 18 |
catalog.add_folder(folder_t, prefix="ui") |
| 19 |
|
| 20 |
(folder / "Test.jinja").write_text("<ui:Title />") |
| 21 |
|
| 22 |
(folder_t / "Title.jinja").write_text("prefix") |
| 23 |
|
| 24 |
html = catalog.render("Test") |
| 25 |
assert html == Markup("prefix") |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 29 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 30 |
def test_prefix_namespace(catalog, folder, folder_t, autoescape, undefined): |
| 31 |
"""Components mounted with a prefix should be able to import other components |
| 32 |
from the same folder without specifying the prefix. |
| 33 |
""" |
| 34 |
catalog.jinja_env.autoescape = autoescape |
| 35 |
catalog.jinja_env.undefined = undefined |
| 36 |
catalog.add_folder(folder_t, prefix="ui") |
| 37 |
|
| 38 |
(folder / "Title.jinja").write_text("parent") |
| 39 |
|
| 40 |
(folder_t / "Title.jinja").write_text("prefix") |
| 41 |
(folder_t / "Alert.jinja").write_text("<Title />") |
| 42 |
|
| 43 |
html = catalog.render("ui:Alert") |
| 44 |
assert html == Markup("prefix") |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 48 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 49 |
def test_prefix_namespace_sub(catalog, folder, folder_t, autoescape, undefined): |
| 50 |
"""Components mounted with a prefix should be able to import other components |
| 51 |
from the same folder without specifying the prefix, even if those components |
| 52 |
are in a subfolder. |
| 53 |
""" |
| 54 |
catalog.jinja_env.autoescape = autoescape |
| 55 |
catalog.jinja_env.undefined = undefined |
| 56 |
catalog.add_folder(folder_t, prefix="ui") |
| 57 |
|
| 58 |
|
| 59 |
(folder / "Title.jinja").write_text("parent") |
| 60 |
(folder / "sub").mkdir() |
| 61 |
(folder / "sub" / "Title.jinja").write_text("sub/parent") |
| 62 |
|
| 63 |
(folder_t / "Title.jinja").write_text("sub") |
| 64 |
(folder_t / "sub").mkdir() |
| 65 |
(folder_t / "sub" / "Title.jinja").write_text("sub/prefix") |
| 66 |
(folder_t / "Alert.jinja").write_text("<sub.Title />") |
| 67 |
|
| 68 |
html = catalog.render("ui:Alert") |
| 69 |
assert html == Markup("sub/prefix") |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 73 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 74 |
def test_prefix_fallback(catalog, folder, folder_t, autoescape, undefined): |
| 75 |
"""If a component is not found in the folder with the prefix, it should |
| 76 |
fallback to the no-prefix folders. |
| 77 |
""" |
| 78 |
catalog.jinja_env.autoescape = autoescape |
| 79 |
catalog.jinja_env.undefined = undefined |
| 80 |
catalog.add_folder(folder_t, prefix="ui") |
| 81 |
|
| 82 |
(folder / "Title.jinja").write_text("parent") |
| 83 |
(folder_t / "Alert.jinja").write_text("<Title />") |
| 84 |
|
| 85 |
html = catalog.render("ui:Alert") |
| 86 |
assert html == Markup("parent") |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 90 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 91 |
def test_prefix_namespace_assets(catalog, folder, folder_t, autoescape, undefined): |
| 92 |
"""Components import without specifying the prefix should also be |
| 93 |
able to auto-import their assets. |
| 94 |
""" |
| 95 |
catalog.jinja_env.autoescape = autoescape |
| 96 |
catalog.jinja_env.undefined = undefined |
| 97 |
catalog.add_folder(folder_t, prefix="ui") |
| 98 |
|
| 99 |
(folder_t / "Title.jinja").write_text("prefix") |
| 100 |
(folder_t / "Title.css").touch() |
| 101 |
(folder_t / "Layout.jinja").write_text(""" |
| 102 |
{{ catalog.render_assets() }} |
| 103 |
{{ content }} |
| 104 |
""") |
| 105 |
(folder_t / "Alert.jinja").write_text("<Layout><Title /></Layout>") |
| 106 |
|
| 107 |
html = catalog.render("ui:Alert") |
| 108 |
assert html == Markup(""" |
| 109 |
<link rel="stylesheet" href="/static/components/ui/Title.css"> |
| 110 |
prefix |
| 111 |
""".strip()) |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 115 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 116 |
def test_allow_dots_in_prefixes(catalog, folder, folder_t, autoescape, undefined): |
| 117 |
catalog.jinja_env.autoescape = autoescape |
| 118 |
catalog.jinja_env.undefined = undefined |
| 119 |
catalog.add_folder(folder_t, prefix="ui.widgets") |
| 120 |
|
| 121 |
(folder_t / "Title.jinja").write_text("prefix") |
| 122 |
(folder / "Test.jinja").write_text("<ui.widgets:Title />") |
| 123 |
|
| 124 |
html = catalog.render("Test") |
| 125 |
assert html == Markup("prefix") |