| 1 |
test_that("input validation works", { |
| 2 |
expect_error(render()) |
| 3 |
}) |
| 4 |
|
| 5 |
test_that("templating features work", { |
| 6 |
expect_equal( |
| 7 |
render("Hello {{ name }}!", name = "world"), |
| 8 |
"Hello world!" |
| 9 |
) |
| 10 |
|
| 11 |
expect_equal( |
| 12 |
render("{Hello {{ name }}!}", name = "world"), |
| 13 |
"{Hello world!}" |
| 14 |
) |
| 15 |
}) |
| 16 |
|
| 17 |
test_that("template files work", { |
| 18 |
with_dir_tree(list("foo" = "Hello {{ name }}!"), { |
| 19 |
path_config <- jinjar_config(fs::path_wd()) |
| 20 |
|
| 21 |
expect_equal( |
| 22 |
render(fs::path("foo"), name = "world"), |
| 23 |
"Hello world!" |
| 24 |
) |
| 25 |
expect_equal( |
| 26 |
render(fs::path_wd("foo"), name = "world"), |
| 27 |
"Hello world!" |
| 28 |
) |
| 29 |
expect_equal( |
| 30 |
render(fs::path("foo"), name = "world", .config = path_config), |
| 31 |
"Hello world!" |
| 32 |
) |
| 33 |
expect_equal( |
| 34 |
render(fs::path_wd("foo"), name = "world", .config = path_config), |
| 35 |
"Hello world!" |
| 36 |
) |
| 37 |
expect_error(render(fs::path_home_r("foo"))) |
| 38 |
}) |
| 39 |
}) |
| 40 |
|
| 41 |
test_that("include tag", { |
| 42 |
src <- "Welcome: {% include \"bar\" %}" |
| 43 |
aux <- "Hello {{ name }}!" |
| 44 |
|
| 45 |
with_dir_tree(list( |
| 46 |
"templates/foo" = src, |
| 47 |
"templates/bar" = aux |
| 48 |
), { |
| 49 |
|
| 50 |
expect_error(render(fs::path("foo"), name = "world")) |
| 51 |
expect_snapshot(render( |
| 52 |
fs::path("templates/foo"), |
| 53 |
name = "world", |
| 54 |
.config = jinjar_config(ignore_missing_files = TRUE) |
| 55 |
)) |
| 56 |
expect_snapshot(render( |
| 57 |
fs::path("foo"), |
| 58 |
name = "world", |
| 59 |
.config = jinjar_config(path_loader(fs::path_wd("templates"))) |
| 60 |
)) |
| 61 |
}) |
| 62 |
|
| 63 |
list_config <- jinjar_config(list_loader(list( |
| 64 |
"bar" = aux |
| 65 |
))) |
| 66 |
expect_error(render(src, name = "world")) |
| 67 |
expect_snapshot(render(src, name = "world", .config = list_config)) |
| 68 |
}) |
| 69 |
|
| 70 |
test_that("extends tag", { |
| 71 |
src <- "{% extends \"bar\" %}{% block name %}world{% endblock %}" |
| 72 |
aux <- "Hello {% block name %}{% endblock %}!" |
| 73 |
|
| 74 |
with_dir_tree(list( |
| 75 |
"foo" = src, |
| 76 |
"bar" = aux |
| 77 |
), { |
| 78 |
expect_error(render(fs::path("foo"), name = "world")) |
| 79 |
expect_snapshot(render( |
| 80 |
fs::path("foo"), |
| 81 |
name = "world", |
| 82 |
.config = jinjar_config(ignore_missing_files = TRUE) |
| 83 |
)) |
| 84 |
expect_snapshot(render( |
| 85 |
fs::path("foo"), |
| 86 |
name = "world", |
| 87 |
.config = jinjar_config(path_loader(fs::path_wd())) |
| 88 |
)) |
| 89 |
}) |
| 90 |
|
| 91 |
list_config <- jinjar_config(list_loader(list( |
| 92 |
"bar" = aux |
| 93 |
))) |
| 94 |
expect_error(render(src, name = "world")) |
| 95 |
expect_snapshot(render(src, name = "world", .config = list_config)) |
| 96 |
}) |
| 97 |
|
| 98 |
test_that("escape_html() works", { |
| 99 |
expect_equal( |
| 100 |
render("{{ escape_html(x) }}", x = '&<>"&'), |
| 101 |
"&<>"&" |
| 102 |
) |
| 103 |
}) |
| 104 |
|
| 105 |
test_that("quote_sql() works", { |
| 106 |
expect_equal( |
| 107 |
render("WHERE x = {{ quote_sql(col) }}", col = "world"), |
| 108 |
"WHERE x = 'world'" |
| 109 |
) |
| 110 |
expect_equal( |
| 111 |
render("WHERE x = {{ quote_sql(col) }}", col = "Wayne's World"), |
| 112 |
"WHERE x = 'Wayne''s World'" |
| 113 |
) |
| 114 |
expect_equal( |
| 115 |
render("WHERE x = {{ quote_sql(col) }}", col = 1L), |
| 116 |
"WHERE x = 1" |
| 117 |
) |
| 118 |
expect_equal( |
| 119 |
render("WHERE x = {{ quote_sql(col) }}", col = 2.5), |
| 120 |
"WHERE x = 2.5" |
| 121 |
) |
| 122 |
expect_equal( |
| 123 |
render("WHERE x = {{ quote_sql(col) }}", col = TRUE), |
| 124 |
"WHERE x = TRUE" |
| 125 |
) |
| 126 |
expect_equal( |
| 127 |
render("WHERE x = {{ quote_sql(col) }}", col = FALSE), |
| 128 |
"WHERE x = FALSE" |
| 129 |
) |
| 130 |
expect_equal( |
| 131 |
render("WHERE x = {{ quote_sql(col) }}", col = NA), |
| 132 |
"WHERE x = NULL" |
| 133 |
) |
| 134 |
expect_equal( |
| 135 |
render("WHERE x IN ({{ quote_sql(col) }})", col = c("world", "galaxy")), |
| 136 |
"WHERE x IN ('world', 'galaxy')" |
| 137 |
) |
| 138 |
expect_equal( |
| 139 |
render("WHERE x IN ({{ quote_sql(col) }})", col = c(1, 4, 6)), |
| 140 |
"WHERE x IN (1, 4, 6)" |
| 141 |
) |
| 142 |
}) |
| 143 |
|
| 144 |
cli::test_that_cli("render error", { |
| 145 |
expect_snapshot(render("Hello {{ name }}!"), error = TRUE) |
| 146 |
expect_snapshot(render('{% include "missing.html" %}'), error = TRUE) |
| 147 |
|
| 148 |
expect_snapshot( |
| 149 |
render("{% for x in vec %}{{ x }}{% endfor %}", vec = "world"), |
| 150 |
error = TRUE |
| 151 |
) |
| 152 |
}) |
| 153 |
|
| 154 |
cli::test_that_cli("JSON encoding error", { |
| 155 |
x <- parse_template("Hello {{ name }}!") |
| 156 |
|
| 157 |
expect_snapshot(jinjar:::with_catch_cpp_errors({ |
| 158 |
jinjar:::render_(attr(x, "parsed"), '{"name": "world"]}') |
| 159 |
}), error = TRUE) |
| 160 |
}) |