| 1 |
//! Files containing tests for generated code. |
| 2 |
|
| 3 |
use std::fmt; |
| 4 |
use std::path::Path; |
| 5 |
|
| 6 |
use console::style; |
| 7 |
use prettyplease::unparse; |
| 8 |
use similar::{Algorithm, ChangeTag, TextDiffConfig}; |
| 9 |
|
| 10 |
use crate::AnyTemplateArgs; |
| 11 |
use crate::integration::Buffer; |
| 12 |
|
| 13 |
#[track_caller] |
| 14 |
fn build_template(ast: &syn::DeriveInput) -> Result<String, crate::CompileError> { |
| 15 |
let mut buf = Buffer::new(); |
| 16 |
let args = AnyTemplateArgs::new(ast)?; |
| 17 |
crate::build_template(&mut buf, ast, args)?; |
| 18 |
Ok(buf.into_string()) |
| 19 |
} |
| 20 |
|
| 21 |
// This function makes it much easier to compare expected code by adding the wrapping around |
| 22 |
// the code we want to check. |
| 23 |
#[track_caller] |
| 24 |
fn compare(jinja: &str, expected: &str, fields: &[(&str, &str)], size_hint: usize) { |
| 25 |
compare_ex(jinja, expected, fields, size_hint, "") |
| 26 |
} |
| 27 |
|
| 28 |
#[track_caller] |
| 29 |
fn compare_ex( |
| 30 |
jinja: &str, |
| 31 |
expected: &str, |
| 32 |
fields: &[(&str, &str)], |
| 33 |
size_hint: usize, |
| 34 |
prefix: &str, |
| 35 |
) { |
| 36 |
let generated = jinja_to_rust(jinja, fields, prefix).unwrap(); |
| 37 |
|
| 38 |
let expected: proc_macro2::TokenStream = expected.parse().unwrap(); |
| 39 |
let expected: syn::File = syn::parse_quote! { |
| 40 |
impl askama::Template for Foo { |
| 41 |
fn render_into_with_values<AskamaW>( |
| 42 |
&self, |
| 43 |
__askama_writer: &mut AskamaW, |
| 44 |
__askama_values: &dyn askama::Values, |
| 45 |
) -> askama::Result<()> |
| 46 |
where |
| 47 |
AskamaW: askama::helpers::core::fmt::Write + ?askama::helpers::core::marker::Sized, |
| 48 |
{ |
| 49 |
#[allow(unused_imports)] |
| 50 |
use askama::{ |
| 51 |
filters::{AutoEscape as _, WriteWritable as _}, |
| 52 |
helpers::{ResultConverter as _, core::fmt::Write as _}, |
| 53 |
}; |
| 54 |
#expected |
| 55 |
askama::Result::Ok(()) |
| 56 |
} |
| 57 |
const SIZE_HINT: askama::helpers::core::primitive::usize = #size_hint; |
| 58 |
} |
| 59 |
|
| 60 |
/// Implement the [`format!()`][askama::helpers::std::format] trait for [`Foo`] |
| 61 |
/// |
| 62 |
/// Please be aware of the rendering performance notice in the [`Template`][askama::Template] trait. |
| 63 |
impl askama::helpers::core::fmt::Display for Foo { |
| 64 |
#[inline] |
| 65 |
fn fmt(&self, f: &mut askama::helpers::core::fmt::Formatter<'_>) -> askama::helpers::core::fmt::Result { |
| 66 |
askama::Template::render_into(self, f).map_err(|_| askama::helpers::core::fmt::Error) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl askama::filters::FastWritable for Foo { |
| 71 |
#[inline] |
| 72 |
fn write_into<AskamaW>(&self, dest: &mut AskamaW) -> askama::Result<()> |
| 73 |
where |
| 74 |
AskamaW: askama::helpers::core::fmt::Write + ?askama::helpers::core::marker::Sized, |
| 75 |
{ |
| 76 |
askama::Template::render_into(self, dest) |
| 77 |
} |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
let expected = unparse(&expected); |
| 82 |
let generated = unparse(&generated); |
| 83 |
if expected != generated { |
| 84 |
struct Diff<'a>(&'a str, &'a str); |
| 85 |
|
| 86 |
impl fmt::Display for Diff<'_> { |
| 87 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 88 |
let diff = TextDiffConfig::default() |
| 89 |
.algorithm(Algorithm::Patience) |
| 90 |
.diff_lines(self.0, self.1); |
| 91 |
for change in diff.iter_all_changes() { |
| 92 |
let (change, line) = match change.tag() { |
| 93 |
ChangeTag::Equal => ( |
| 94 |
style(" ").dim().bold(), |
| 95 |
style(change.to_string_lossy()).dim(), |
| 96 |
), |
| 97 |
ChangeTag::Delete => ( |
| 98 |
style("-").red().bold(), |
| 99 |
style(change.to_string_lossy()).red(), |
| 100 |
), |
| 101 |
ChangeTag::Insert => ( |
| 102 |
style("+").green().bold(), |
| 103 |
style(change.to_string_lossy()).green(), |
| 104 |
), |
| 105 |
}; |
| 106 |
write!(f, "{change}{line}")?; |
| 107 |
} |
| 108 |
Ok(()) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
panic!( |
| 113 |
"\n\ |
| 114 |
=== Expected ===\n\ |
| 115 |
\n\ |
| 116 |
{expected}\n\ |
| 117 |
\n\ |
| 118 |
=== Generated ===\n\ |
| 119 |
\n\ |
| 120 |
{generated}\n\ |
| 121 |
\n\ |
| 122 |
=== Diff ===\n\ |
| 123 |
\n\ |
| 124 |
{diff}\n\ |
| 125 |
\n\ |
| 126 |
=== FAILURE ===", |
| 127 |
expected = style(&expected).red(), |
| 128 |
generated = style(&generated).green(), |
| 129 |
diff = Diff(&expected, &generated), |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
fn jinja_to_rust(jinja: &str, fields: &[(&str, &str)], prefix: &str) -> syn::Result<syn::File> { |
| 135 |
let jinja = format!( |
| 136 |
r##"#[template(source = {jinja:?}, ext = "txt")] |
| 137 |
{prefix} |
| 138 |
struct Foo {{ {} }}"##, |
| 139 |
fields |
| 140 |
.iter() |
| 141 |
.map(|(name, type_)| format!("{name}: {type_}")) |
| 142 |
.collect::<Vec<_>>() |
| 143 |
.join(","), |
| 144 |
); |
| 145 |
|
| 146 |
let generated = build_template(&syn::parse_str::<syn::DeriveInput>(&jinja).unwrap()).unwrap(); |
| 147 |
let generated = match generated.parse() { |
| 148 |
Ok(generated) => generated, |
| 149 |
Err(err) => panic!( |
| 150 |
"\n\ |
| 151 |
=== Invalid code generated ===\n\ |
| 152 |
\n\ |
| 153 |
{generated}\n\ |
| 154 |
\n\ |
| 155 |
=== Error ===\n\ |
| 156 |
\n\ |
| 157 |
{err}" |
| 158 |
), |
| 159 |
}; |
| 160 |
syn::parse2(generated) |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn check_if_let() { |
| 165 |
// In this test, we ensure that `query` never is `self.query`. |
| 166 |
compare( |
| 167 |
"{% if let Some(query) = s && !query.is_empty() %}{{query}}{% endif %}", |
| 168 |
r"if let Some(query,) = &self.s && !askama::helpers::as_bool(&(query.is_empty())) { |
| 169 |
match ( |
| 170 |
&((&&askama::filters::AutoEscaper::new(&(query), askama::filters::Text)).askama_auto_escape()?), |
| 171 |
) { |
| 172 |
(expr0,) => { |
| 173 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 174 |
} |
| 175 |
} |
| 176 |
}", |
| 177 |
&[], |
| 178 |
3, |
| 179 |
); |
| 180 |
|
| 181 |
// In this test, we ensure that `s` is `self.s` only in the first `if let Some(s) = self.s` |
| 182 |
// condition. |
| 183 |
compare( |
| 184 |
"{% if let Some(s) = s %}{{ s }}{% endif %}", |
| 185 |
r"if let Some(s,) = &self.s { |
| 186 |
match ( |
| 187 |
&((&&askama::filters::AutoEscaper::new(&(s), askama::filters::Text)).askama_auto_escape()?), |
| 188 |
) { |
| 189 |
(expr0,) => { |
| 190 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 191 |
} |
| 192 |
} |
| 193 |
}", |
| 194 |
&[], |
| 195 |
3, |
| 196 |
); |
| 197 |
|
| 198 |
// In this test, we ensure that `s` is `self.s` only in the first `if let Some(s) = self.s` |
| 199 |
// condition. |
| 200 |
compare( |
| 201 |
"{% if let Some(s) = s && !s.is_empty() %}{{s}}{% endif %}", |
| 202 |
r"if let Some(s,) = &self.s && !askama::helpers::as_bool(&(s.is_empty())) { |
| 203 |
match ( |
| 204 |
&((&&askama::filters::AutoEscaper::new(&(s), askama::filters::Text)).askama_auto_escape()?), |
| 205 |
) { |
| 206 |
(expr0,) => { |
| 207 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 208 |
} |
| 209 |
} |
| 210 |
}", |
| 211 |
&[], |
| 212 |
3, |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
// Since this feature is not stable yet, we can't add a "normal" test for it so instead we check |
| 217 |
// the generated code. |
| 218 |
#[test] |
| 219 |
fn check_if_let_chain() { |
| 220 |
// Both `bla` and `blob` variables must exist in this `if`. |
| 221 |
compare( |
| 222 |
"{% if let Some(bla) = y && x && let Some(blob) = y %}{{bla}} {{blob}}{% endif %}", |
| 223 |
r#"if let Some(bla) = &self.y && askama::helpers::as_bool(&(self.x)) && let Some(blob) = &self.y { |
| 224 |
match ( |
| 225 |
&((&&askama::filters::AutoEscaper::new(&(bla), askama::filters::Text)) |
| 226 |
.askama_auto_escape()?), |
| 227 |
&((&&askama::filters::AutoEscaper::new(&(blob), askama::filters::Text)) |
| 228 |
.askama_auto_escape()?), |
| 229 |
) { |
| 230 |
(expr0, expr2) => { |
| 231 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 232 |
__askama_writer.write_str(" ")?; |
| 233 |
(&&&askama::filters::Writable(expr2)).askama_write(__askama_writer, __askama_values)?; |
| 234 |
} |
| 235 |
} |
| 236 |
}"#, |
| 237 |
&[], |
| 238 |
7, |
| 239 |
); |
| 240 |
|
| 241 |
compare( |
| 242 |
r#"{% if let Some(bla) = y |
| 243 |
&& bla == "x" |
| 244 |
&& let Some(blob) = z |
| 245 |
&& blob == "z" %}{{bla}} {{blob}}{% endif %}"#, |
| 246 |
r#"if let Some(bla) = &self.y && askama::helpers::as_bool(&(bla == "x")) |
| 247 |
&& let Some(blob) = &self.z && askama::helpers::as_bool(&(blob == "z")) |
| 248 |
{ |
| 249 |
match ( |
| 250 |
&((&&askama::filters::AutoEscaper::new(&(bla), askama::filters::Text)) |
| 251 |
.askama_auto_escape()?), |
| 252 |
&((&&askama::filters::AutoEscaper::new(&(blob), askama::filters::Text)) |
| 253 |
.askama_auto_escape()?), |
| 254 |
) { |
| 255 |
(expr0, expr2) => { |
| 256 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 257 |
__askama_writer.write_str(" ")?; |
| 258 |
(&&&askama::filters::Writable(expr2)).askama_write(__askama_writer, __askama_values)?; |
| 259 |
} |
| 260 |
} |
| 261 |
}"#, |
| 262 |
&[], |
| 263 |
7, |
| 264 |
); |
| 265 |
|
| 266 |
// Bindings variables with the same name as the bound variable should be declared in the right |
| 267 |
// order. |
| 268 |
compare( |
| 269 |
r#"{% if let Some(y) = y |
| 270 |
&& y == "x" |
| 271 |
&& w |
| 272 |
&& let Some(z) = z |
| 273 |
&& z == "z" %}{{y}} {{z}}{% endif %}"#, |
| 274 |
r#"if let Some(y) = &self.y && self.y == "x" |
| 275 |
&& askama::helpers::as_bool(&(self.w)) && let Some(z) = &self.z |
| 276 |
&& askama::helpers::as_bool(&(z == "z")) |
| 277 |
{ |
| 278 |
match ( |
| 279 |
&((&&askama::filters::AutoEscaper::new(&(y), askama::filters::Text)) |
| 280 |
.askama_auto_escape()?), |
| 281 |
&((&&askama::filters::AutoEscaper::new(&(z), askama::filters::Text)) |
| 282 |
.askama_auto_escape()?), |
| 283 |
) { |
| 284 |
(expr0, expr2) => { |
| 285 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 286 |
__askama_writer.write_str(" ")?; |
| 287 |
(&&&askama::filters::Writable(expr2)).askama_write(__askama_writer, __askama_values)?; |
| 288 |
} |
| 289 |
} |
| 290 |
}"#, |
| 291 |
&[], |
| 292 |
7, |
| 293 |
); |
| 294 |
|
| 295 |
compare( |
| 296 |
r#"{% if w |
| 297 |
&& let Some(y) = y |
| 298 |
&& y == "x" |
| 299 |
&& let Some(z) = z |
| 300 |
&& z == "z" %}{{y}} {{z}}{% endif %}"#, |
| 301 |
r#"if askama::helpers::as_bool(&(self.w)) && let Some(y) = &self.y |
| 302 |
&& self.y == "x" && let Some(z) = &self.z |
| 303 |
&& askama::helpers::as_bool(&(z == "z")) |
| 304 |
{ |
| 305 |
match ( |
| 306 |
&((&&askama::filters::AutoEscaper::new(&(y), askama::filters::Text)) |
| 307 |
.askama_auto_escape()?), |
| 308 |
&((&&askama::filters::AutoEscaper::new(&(z), askama::filters::Text)) |
| 309 |
.askama_auto_escape()?), |
| 310 |
) { |
| 311 |
(expr0, expr2) => { |
| 312 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 313 |
__askama_writer.write_str(" ")?; |
| 314 |
(&&&askama::filters::Writable(expr2)).askama_write(__askama_writer, __askama_values)?; |
| 315 |
} |
| 316 |
} |
| 317 |
}"#, |
| 318 |
&[], |
| 319 |
7, |
| 320 |
); |
| 321 |
} |
| 322 |
|
| 323 |
#[test] |
| 324 |
fn check_includes_only_once() { |
| 325 |
// In this test we make sure that every used template gets referenced exactly once. |
| 326 |
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("templates"); |
| 327 |
let path1 = path.join("include1.html").canonicalize().unwrap(); |
| 328 |
let path2 = path.join("include2.html").canonicalize().unwrap(); |
| 329 |
let path3 = path.join("include3.html").canonicalize().unwrap(); |
| 330 |
compare( |
| 331 |
r#"{% include "include1.html" %}"#, |
| 332 |
&format!( |
| 333 |
r#"const _: &[askama::helpers::core::primitive::u8] = askama::helpers::core::include_bytes!({path1:#?}); |
| 334 |
const _: &[askama::helpers::core::primitive::u8] = askama::helpers::core::include_bytes!({path2:#?}); |
| 335 |
const _: &[askama::helpers::core::primitive::u8] = askama::helpers::core::include_bytes!({path3:#?}); |
| 336 |
__askama_writer.write_str("3333")?;"# |
| 337 |
), |
| 338 |
&[], |
| 339 |
4, |
| 340 |
); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn check_is_defined() { |
| 345 |
// Checks that it removes conditions if we know at compile-time that they always return false. |
| 346 |
// |
| 347 |
// We're forced to add `bla` otherwise `compare` assert fails in weird ways... |
| 348 |
compare( |
| 349 |
"{% if y is defined %}{{query}}{% endif %}bla", |
| 350 |
r#"__askama_writer.write_str("bla")?;"#, |
| 351 |
&[], |
| 352 |
3, |
| 353 |
); |
| 354 |
compare( |
| 355 |
"{% if x is not defined %}{{query}}{% endif %}bla", |
| 356 |
r#"__askama_writer.write_str("bla")?;"#, |
| 357 |
&[("x", "u32")], |
| 358 |
3, |
| 359 |
); |
| 360 |
compare( |
| 361 |
"{% if y is defined && x is not defined %}{{query}}{% endif %}bla", |
| 362 |
r#"__askama_writer.write_str("bla")?;"#, |
| 363 |
&[("x", "u32")], |
| 364 |
3, |
| 365 |
); |
| 366 |
|
| 367 |
// Same with declared variables. |
| 368 |
compare( |
| 369 |
"{% set y = 12 %} |
| 370 |
{%- if y is not defined %}{{query}}{% endif %}bla", |
| 371 |
r#"let y = 12; |
| 372 |
__askama_writer.write_str("bla")?;"#, |
| 373 |
&[], |
| 374 |
3, |
| 375 |
); |
| 376 |
compare( |
| 377 |
"{% set y = 12 %} |
| 378 |
{%- if y is not defined && x is defined %}{{query}}{% endif %}bla", |
| 379 |
r#"let y = 12; |
| 380 |
__askama_writer.write_str("bla")?;"#, |
| 381 |
&[], |
| 382 |
3, |
| 383 |
); |
| 384 |
|
| 385 |
// Checks that if the condition is always `true` at compile-time, then we keep the code but |
| 386 |
// remove the condition. |
| 387 |
compare( |
| 388 |
"{% if y is defined %}bla{% endif %}", |
| 389 |
r#"__askama_writer.write_str("bla")?;"#, |
| 390 |
&[("y", "u32")], |
| 391 |
3, |
| 392 |
); |
| 393 |
compare( |
| 394 |
"{% if x is not defined %}bla{% endif %}", |
| 395 |
r#"__askama_writer.write_str("bla")?;"#, |
| 396 |
&[], |
| 397 |
3, |
| 398 |
); |
| 399 |
// Same with declared variables. |
| 400 |
compare( |
| 401 |
"{% set y = 12 %} |
| 402 |
{%- if y is defined %}bla{% endif %}", |
| 403 |
r#"let y = 12; |
| 404 |
__askama_writer.write_str("bla")?;"#, |
| 405 |
&[], |
| 406 |
3, |
| 407 |
); |
| 408 |
|
| 409 |
// If the always `true` condition is followed by more `else if`/`else`, check that they are |
| 410 |
// removed as well. |
| 411 |
compare( |
| 412 |
"{% if x is defined %}bli |
| 413 |
{%- else if x == 12 %}12{% endif %}bla", |
| 414 |
r#"__askama_writer.write_str("blibla")?;"#, |
| 415 |
&[("x", "u32")], |
| 416 |
6, |
| 417 |
); |
| 418 |
compare( |
| 419 |
"{% if x is defined %}bli |
| 420 |
{%- else if x == 12 %}12 |
| 421 |
{%- else %}nope{% endif %}bla", |
| 422 |
r#"__askama_writer.write_str("blibla")?;"#, |
| 423 |
&[("x", "u32")], |
| 424 |
6, |
| 425 |
); |
| 426 |
// If it's not the first one. |
| 427 |
compare( |
| 428 |
"{% if x == 12 %}bli |
| 429 |
{%- else if x is defined %}12 |
| 430 |
{%- else %}nope{% endif %}", |
| 431 |
r#"if askama::helpers::as_bool(&(self.x == 12)) { |
| 432 |
__askama_writer.write_str("bli")?; |
| 433 |
} else { |
| 434 |
__askama_writer.write_str("12")?; |
| 435 |
}"#, |
| 436 |
&[("x", "u32")], |
| 437 |
5, |
| 438 |
); |
| 439 |
|
| 440 |
// Checking that it doesn't remove the condition if other non-"if (not) defined" checks |
| 441 |
// are present. |
| 442 |
compare( |
| 443 |
"{% if y is defined || x == 12 %}{{x}}{% endif %}", |
| 444 |
r"if askama::helpers::as_bool(&(self.x == 12)) { |
| 445 |
match ( |
| 446 |
&((&&askama::filters::AutoEscaper::new(&(self.x), askama::filters::Text)).askama_auto_escape()?), |
| 447 |
) { |
| 448 |
(expr0,) => { |
| 449 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
", |
| 454 |
&[("x", "u32")], |
| 455 |
3, |
| 456 |
); |
| 457 |
compare( |
| 458 |
"{% if y is defined || x == 12 %}{{x}}{% endif %}", |
| 459 |
r"match ( |
| 460 |
&((&&askama::filters::AutoEscaper::new(&(self.x), askama::filters::Text)).askama_auto_escape()?), |
| 461 |
) { |
| 462 |
(expr0,) => { |
| 463 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 464 |
} |
| 465 |
} |
| 466 |
", |
| 467 |
&[("y", "u32"), ("x", "u32")], |
| 468 |
3, |
| 469 |
); |
| 470 |
compare( |
| 471 |
"{% if y is defined && y == 12 %}{{x}}{% endif %}", |
| 472 |
r"", |
| 473 |
&[], |
| 474 |
0, |
| 475 |
); |
| 476 |
compare( |
| 477 |
"{% if y is defined && y == 12 %}{{y}}{% else %}bli{% endif %}", |
| 478 |
r#"__askama_writer.write_str("bli")?;"#, |
| 479 |
&[], |
| 480 |
3, |
| 481 |
); |
| 482 |
compare( |
| 483 |
"{% if y is defined && y == 12 %}{{y}}{% else %}bli{% endif %}", |
| 484 |
r#" |
| 485 |
if askama::helpers::as_bool(&(self.y == 12)) { |
| 486 |
match ( |
| 487 |
&((&&askama::filters::AutoEscaper::new( |
| 488 |
&(self.y), |
| 489 |
askama::filters::Text, |
| 490 |
)) |
| 491 |
.askama_auto_escape()?), |
| 492 |
) { |
| 493 |
(expr0,) => { |
| 494 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 495 |
} |
| 496 |
} |
| 497 |
} else { |
| 498 |
__askama_writer.write_str("bli")?; |
| 499 |
} |
| 500 |
"#, |
| 501 |
&[("y", "u32")], |
| 502 |
6, |
| 503 |
); |
| 504 |
// Since the first `if` is always `true`, the `else` should not be generated. |
| 505 |
compare( |
| 506 |
"{% if y is defined %}{{y}}{% else %}bli{% endif %}", |
| 507 |
r" |
| 508 |
match ( |
| 509 |
&((&&askama::filters::AutoEscaper::new( |
| 510 |
&(self.y), |
| 511 |
askama::filters::Text, |
| 512 |
)) |
| 513 |
.askama_auto_escape()?), |
| 514 |
) { |
| 515 |
(expr0,) => { |
| 516 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 517 |
} |
| 518 |
} |
| 519 |
", |
| 520 |
&[("y", "u32")], |
| 521 |
3, |
| 522 |
); |
| 523 |
|
| 524 |
// Checking some funny cases. |
| 525 |
|
| 526 |
// This one is a bit useless because you can use `is not defined` but I suppose it's possible |
| 527 |
// to encounter cases like that in the wild so better have a check. |
| 528 |
compare( |
| 529 |
"{% if !(y is defined) %}bla{% endif %}", |
| 530 |
r#"__askama_writer.write_str("bla")?;"#, |
| 531 |
&[], |
| 532 |
3, |
| 533 |
); |
| 534 |
compare( |
| 535 |
"{% if !(y is not defined) %}bli{% endif %}bla", |
| 536 |
r#"__askama_writer.write_str("bla")?;"#, |
| 537 |
&[], |
| 538 |
3, |
| 539 |
); |
| 540 |
compare( |
| 541 |
"{% if !(y is defined) %}bli{% endif %}bla", |
| 542 |
r#"__askama_writer.write_str("bla")?;"#, |
| 543 |
&[("y", "u32")], |
| 544 |
3, |
| 545 |
); |
| 546 |
compare( |
| 547 |
"{% if !(y is not defined) %}bla{% endif %}", |
| 548 |
r#"__askama_writer.write_str("bla")?;"#, |
| 549 |
&[("y", "u32")], |
| 550 |
3, |
| 551 |
); |
| 552 |
|
| 553 |
// Ensure that the `!` is kept . |
| 554 |
compare( |
| 555 |
"{% if y is defined && !y %}bla{% endif %}", |
| 556 |
r#"if !askama::helpers::as_bool(&(self.y)) { |
| 557 |
__askama_writer.write_str("bla")?; |
| 558 |
}"#, |
| 559 |
&[("y", "bool")], |
| 560 |
3, |
| 561 |
); |
| 562 |
compare( |
| 563 |
"{% if y is defined && !(y) %}bla{% endif %}", |
| 564 |
r#"if !(askama::helpers::as_bool(&(self.y))) { |
| 565 |
__askama_writer.write_str("bla")?; |
| 566 |
}"#, |
| 567 |
&[("y", "bool")], |
| 568 |
3, |
| 569 |
); |
| 570 |
compare( |
| 571 |
"{% if y is not defined || !y %}bla{% endif %}", |
| 572 |
r#"if !askama::helpers::as_bool(&(self.y)) { |
| 573 |
__askama_writer.write_str("bla")?; |
| 574 |
}"#, |
| 575 |
&[("y", "bool")], |
| 576 |
3, |
| 577 |
); |
| 578 |
compare( |
| 579 |
"{% if y is not defined || !(y) %}bla{% endif %}", |
| 580 |
r#"if !(askama::helpers::as_bool(&(self.y))) { |
| 581 |
__askama_writer.write_str("bla")?; |
| 582 |
}"#, |
| 583 |
&[("y", "bool")], |
| 584 |
3, |
| 585 |
); |
| 586 |
} |
| 587 |
|
| 588 |
#[test] |
| 589 |
fn check_bool_conditions() { |
| 590 |
// Checks that it removes conditions if we know at compile-time that they always return false. |
| 591 |
// |
| 592 |
// We're forced to add `bla` otherwise `compare` assert fails in weird ways... |
| 593 |
compare( |
| 594 |
"{% if false %}{{query}}{% endif %}bla", |
| 595 |
r#"__askama_writer.write_str("bla")?;"#, |
| 596 |
&[], |
| 597 |
3, |
| 598 |
); |
| 599 |
compare( |
| 600 |
"{% if false && false %}{{query}}{% endif %}bla", |
| 601 |
r#"__askama_writer.write_str("bla")?;"#, |
| 602 |
&[], |
| 603 |
3, |
| 604 |
); |
| 605 |
compare( |
| 606 |
"{% if false && true %}{{query}}{% endif %}bla", |
| 607 |
r#"__askama_writer.write_str("bla")?;"#, |
| 608 |
&[], |
| 609 |
3, |
| 610 |
); |
| 611 |
compare( |
| 612 |
"{% if true && false %}{{query}}{% endif %}bla", |
| 613 |
r#"__askama_writer.write_str("bla")?;"#, |
| 614 |
&[], |
| 615 |
3, |
| 616 |
); |
| 617 |
compare( |
| 618 |
"{% if false || true %}bli{% endif %}bla", |
| 619 |
r#"__askama_writer.write_str("blibla")?;"#, |
| 620 |
&[], |
| 621 |
6, |
| 622 |
); |
| 623 |
compare( |
| 624 |
"{% if true || false %}bli{% endif %}bla", |
| 625 |
r#"__askama_writer.write_str("blibla")?;"#, |
| 626 |
&[], |
| 627 |
6, |
| 628 |
); |
| 629 |
|
| 630 |
compare( |
| 631 |
"{% if true || x == 12 %}{{x}}{% endif %}", |
| 632 |
r"match ( |
| 633 |
&((&&askama::filters::AutoEscaper::new(&(self.x), askama::filters::Text)).askama_auto_escape()?), |
| 634 |
) { |
| 635 |
(expr0,) => { |
| 636 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 637 |
} |
| 638 |
} |
| 639 |
", |
| 640 |
&[("x", "u32")], |
| 641 |
3, |
| 642 |
); |
| 643 |
compare( |
| 644 |
"{% if false || x == 12 %}{{x}}{% endif %}", |
| 645 |
r"if askama::helpers::as_bool(&(self.x == 12)) { |
| 646 |
match ( |
| 647 |
&((&&askama::filters::AutoEscaper::new( |
| 648 |
&(self.x), |
| 649 |
askama::filters::Text, |
| 650 |
)) |
| 651 |
.askama_auto_escape()?), |
| 652 |
) { |
| 653 |
(expr0,) => { |
| 654 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
", |
| 659 |
&[("x", "u32")], |
| 660 |
3, |
| 661 |
); |
| 662 |
|
| 663 |
// Checking that it also works with sub conditions. |
| 664 |
|
| 665 |
// It's important here that the `(true || x == 12)` part remains since it's not first in the |
| 666 |
// condition. |
| 667 |
compare( |
| 668 |
"{% if y == 3 || (true || x == 12) %}{{x}}{% endif %}", |
| 669 |
r"if askama::helpers::as_bool(&(self.y == 3)) || (true) { |
| 670 |
match ( |
| 671 |
&((&&askama::filters::AutoEscaper::new( |
| 672 |
&(self.x), |
| 673 |
askama::filters::Text, |
| 674 |
)) |
| 675 |
.askama_auto_escape()?), |
| 676 |
) { |
| 677 |
(expr0,) => { |
| 678 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
", |
| 683 |
&[], |
| 684 |
3, |
| 685 |
); |
| 686 |
// However in this case, since `(true || x == 12)` is evaluated to `true`, `y == 3` will never |
| 687 |
// be evaluated so the whole code is removed. |
| 688 |
compare( |
| 689 |
"{% if (true || x == 12) || y == 3 %}{{x}}{% endif %}", |
| 690 |
r"match ( |
| 691 |
&((&&askama::filters::AutoEscaper::new( |
| 692 |
&(self.x), |
| 693 |
askama::filters::Text, |
| 694 |
)) |
| 695 |
.askama_auto_escape()?), |
| 696 |
) { |
| 697 |
(expr0,) => { |
| 698 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 699 |
} |
| 700 |
} |
| 701 |
", |
| 702 |
&[], |
| 703 |
3, |
| 704 |
); |
| 705 |
compare( |
| 706 |
"{% if y == 3 || (x == 12 || true) %}{{x}}{% endif %}", |
| 707 |
r" |
| 708 |
if askama::helpers::as_bool(&(self.y == 3)) |
| 709 |
|| (askama::helpers::as_bool(&(self.x == 12)) || true) |
| 710 |
{ |
| 711 |
match ( |
| 712 |
&((&&askama::filters::AutoEscaper::new( |
| 713 |
&(self.x), |
| 714 |
askama::filters::Text, |
| 715 |
)) |
| 716 |
.askama_auto_escape()?), |
| 717 |
) { |
| 718 |
(expr0,) => { |
| 719 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
", |
| 724 |
&[], |
| 725 |
3, |
| 726 |
); |
| 727 |
|
| 728 |
// Some funny cases. |
| 729 |
compare( |
| 730 |
"{% if !(false) %}bla{% endif %}", |
| 731 |
r#"__askama_writer.write_str("bla")?;"#, |
| 732 |
&[], |
| 733 |
3, |
| 734 |
); |
| 735 |
compare( |
| 736 |
"{% if !(true) %}{{query}}{% endif %}bla", |
| 737 |
r#"__askama_writer.write_str("bla")?;"#, |
| 738 |
&[], |
| 739 |
3, |
| 740 |
); |
| 741 |
|
| 742 |
// Complex condition |
| 743 |
compare( |
| 744 |
"{% if (a || !b) && !(c || !d) %}x{% endif %}", |
| 745 |
r#" |
| 746 |
if ( |
| 747 |
askama::helpers::as_bool(&(self.a)) |
| 748 |
|| !askama::helpers::as_bool(&(self.b)) |
| 749 |
) && !( |
| 750 |
askama::helpers::as_bool(&(self.c)) |
| 751 |
|| !askama::helpers::as_bool(&(self.d)) |
| 752 |
) { |
| 753 |
__askama_writer.write_str("x")?; |
| 754 |
}"#, |
| 755 |
&[("a", "i32"), ("b", "i32"), ("c", "i32"), ("d", "i32")], |
| 756 |
1, |
| 757 |
); |
| 758 |
} |
| 759 |
|
| 760 |
#[test] |
| 761 |
fn check_escaping_at_compile_time() { |
| 762 |
compare( |
| 763 |
r#"The card is |
| 764 |
{%- match suit %} |
| 765 |
{%- when Suit::Clubs or Suit::Spades -%} |
| 766 |
{{ " black" }} |
| 767 |
{%- when Suit::Diamonds or Suit::Hearts -%} |
| 768 |
{{ " red" }} |
| 769 |
{%- endmatch %}"#, |
| 770 |
r#"__askama_writer.write_str("The card is")?; |
| 771 |
match &self.suit { |
| 772 |
Suit::Clubs {} | Suit::Spades {} => { |
| 773 |
__askama_writer.write_str(" black")?; |
| 774 |
} |
| 775 |
Suit::Diamonds {} | Suit::Hearts {} => { |
| 776 |
__askama_writer.write_str(" red")?; |
| 777 |
} |
| 778 |
}"#, |
| 779 |
&[("suit", "Suit")], |
| 780 |
16, |
| 781 |
); |
| 782 |
|
| 783 |
compare( |
| 784 |
r#"{{ '\x41' }}{{ '\n' }}{{ '\r' }}{{ '\t' }}{{ '\\' }}{{ '\u{2665}' }}{{ '\'' }}{{ '\"' }}{{ '"' }} |
| 785 |
{{ "\x41\n\r\t\\\u{2665}\'\"'" }}"#, |
| 786 |
r#"__askama_writer.write_str("A |
| 787 |
\r \\♥'\"\" |
| 788 |
A |
| 789 |
\r \\♥'\"'")?;"#, |
| 790 |
&[], |
| 791 |
23, |
| 792 |
); |
| 793 |
|
| 794 |
compare( |
| 795 |
r"{{ 1_2_3_4 }} {{ 4e3 }} {{ false }}", |
| 796 |
r#"__askama_writer.write_str("1234 4000 false")?;"#, |
| 797 |
&[], |
| 798 |
15, |
| 799 |
); |
| 800 |
} |
| 801 |
|
| 802 |
#[cfg(feature = "code-in-doc")] |
| 803 |
#[test] |
| 804 |
fn test_code_in_comment() { |
| 805 |
let ts = r#" |
| 806 |
#[template(ext = "txt", in_doc = true)] |
| 807 |
/// ```askama |
| 808 |
/// Hello world! |
| 809 |
/// ``` |
| 810 |
struct Tmpl; |
| 811 |
"#; |
| 812 |
let ast = syn::parse_str(ts).unwrap(); |
| 813 |
let generated = build_template(&ast).unwrap(); |
| 814 |
assert!(generated.contains("Hello world!")); |
| 815 |
assert!(!generated.contains("compile_error")); |
| 816 |
|
| 817 |
let ts = r#" |
| 818 |
#[template(ext = "txt", in_doc = true)] |
| 819 |
/// ```askama |
| 820 |
/// Hello |
| 821 |
/// world! |
| 822 |
/// ``` |
| 823 |
struct Tmpl; |
| 824 |
"#; |
| 825 |
let ast = syn::parse_str(ts).unwrap(); |
| 826 |
let generated = build_template(&ast).unwrap(); |
| 827 |
assert!(generated.contains("Hello\nworld!")); |
| 828 |
assert!(!generated.contains("compile_error")); |
| 829 |
|
| 830 |
let ts = r#" |
| 831 |
/// ```askama |
| 832 |
/// Hello |
| 833 |
#[template(ext = "txt", in_doc = true)] |
| 834 |
/// world! |
| 835 |
/// ``` |
| 836 |
struct Tmpl; |
| 837 |
"#; |
| 838 |
let ast = syn::parse_str(ts).unwrap(); |
| 839 |
let generated = build_template(&ast).unwrap(); |
| 840 |
assert!(generated.contains("Hello\nworld!")); |
| 841 |
assert!(!generated.contains("compile_error")); |
| 842 |
|
| 843 |
let ts = r#" |
| 844 |
/// This template greets the whole world |
| 845 |
/// |
| 846 |
/// ```askama |
| 847 |
/// Hello |
| 848 |
#[template(ext = "txt", in_doc = true)] |
| 849 |
/// world! |
| 850 |
/// ``` |
| 851 |
/// |
| 852 |
/// Some more text. |
| 853 |
struct Tmpl; |
| 854 |
"#; |
| 855 |
let ast = syn::parse_str(ts).unwrap(); |
| 856 |
let generated = build_template(&ast).unwrap(); |
| 857 |
assert!(generated.contains("Hello\nworld!")); |
| 858 |
assert!(!generated.contains("compile_error")); |
| 859 |
|
| 860 |
let ts = " |
| 861 |
#[template(ext = \"txt\", in_doc = true)] |
| 862 |
#[doc = \"```askama\nHello\nworld!\n```\"] |
| 863 |
struct Tmpl; |
| 864 |
"; |
| 865 |
let ast = syn::parse_str(ts).unwrap(); |
| 866 |
let generated = build_template(&ast).unwrap(); |
| 867 |
assert!(generated.contains("Hello\nworld!")); |
| 868 |
assert!(!generated.contains("compile_error")); |
| 869 |
|
| 870 |
let ts = " |
| 871 |
#[template(ext = \"txt\", in_doc = true)] |
| 872 |
/// ````` |
| 873 |
/// ```askama |
| 874 |
/// {{bla}} |
| 875 |
/// ``` |
| 876 |
/// ````` |
| 877 |
struct BlockOnBlock; |
| 878 |
"; |
| 879 |
let ast = syn::parse_str(ts).unwrap(); |
| 880 |
let err = build_template(&ast).unwrap_err(); |
| 881 |
assert_eq!( |
| 882 |
err.to_string(), |
| 883 |
"when using `in_doc` with the value `true`, the struct's documentation needs a `askama` \ |
| 884 |
code block" |
| 885 |
); |
| 886 |
|
| 887 |
let ts = " |
| 888 |
#[template(ext = \"txt\", in_doc = true)] |
| 889 |
/// ```askama |
| 890 |
/// ````` |
| 891 |
/// {{bla}} |
| 892 |
/// ````` |
| 893 |
/// ``` |
| 894 |
struct BlockOnBlock; |
| 895 |
"; |
| 896 |
let ast = syn::parse_str(ts).unwrap(); |
| 897 |
let generated = build_template(&ast).unwrap(); |
| 898 |
assert!(!generated.contains("compile_error")); |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn test_pluralize() { |
| 903 |
compare( |
| 904 |
r"{{dogs}} dog{{dogs|pluralize}}", |
| 905 |
r#" |
| 906 |
match ( |
| 907 |
&((&&askama::filters::AutoEscaper::new( |
| 908 |
&(self.dogs), |
| 909 |
askama::filters::Text, |
| 910 |
)) |
| 911 |
.askama_auto_escape()?), |
| 912 |
&(askama::filters::pluralize( |
| 913 |
&(self.dogs), |
| 914 |
askama::helpers::Empty, |
| 915 |
askama::filters::Safe("s"), |
| 916 |
)?), |
| 917 |
) { |
| 918 |
(expr0, expr3) => { |
| 919 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 920 |
__askama_writer.write_str(" dog")?; |
| 921 |
(&&&askama::filters::Writable(expr3)).askama_write(__askama_writer, __askama_values)?; |
| 922 |
} |
| 923 |
}"#, |
| 924 |
&[("dogs", "i8")], |
| 925 |
10, |
| 926 |
); |
| 927 |
compare( |
| 928 |
r#"{{dogs}} dog{{dogs|pluralize("go")}}"#, |
| 929 |
r#" |
| 930 |
match ( |
| 931 |
&((&&askama::filters::AutoEscaper::new( |
| 932 |
&(self.dogs), |
| 933 |
askama::filters::Text, |
| 934 |
)) |
| 935 |
.askama_auto_escape()?), |
| 936 |
&(askama::filters::pluralize( |
| 937 |
&(self.dogs), |
| 938 |
askama::filters::Safe("go"), |
| 939 |
askama::filters::Safe("s"), |
| 940 |
)?), |
| 941 |
) { |
| 942 |
(expr0, expr3) => { |
| 943 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 944 |
__askama_writer.write_str(" dog")?; |
| 945 |
(&&&askama::filters::Writable(expr3)).askama_write(__askama_writer, __askama_values)?; |
| 946 |
} |
| 947 |
}"#, |
| 948 |
&[("dogs", "i8")], |
| 949 |
10, |
| 950 |
); |
| 951 |
compare( |
| 952 |
r#"{{mice}} {{mice|pluralize("mouse", "mice")}}"#, |
| 953 |
r#" |
| 954 |
match ( |
| 955 |
&((&&askama::filters::AutoEscaper::new( |
| 956 |
&(self.mice), |
| 957 |
askama::filters::Text, |
| 958 |
)) |
| 959 |
.askama_auto_escape()?), |
| 960 |
&(askama::filters::pluralize( |
| 961 |
&(self.mice), |
| 962 |
askama::filters::Safe("mouse"), |
| 963 |
askama::filters::Safe("mice"), |
| 964 |
)?), |
| 965 |
) { |
| 966 |
(expr0, expr2) => { |
| 967 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 968 |
__askama_writer.write_str(" ")?; |
| 969 |
(&&&askama::filters::Writable(expr2)).askama_write(__askama_writer, __askama_values)?; |
| 970 |
} |
| 971 |
}"#, |
| 972 |
&[("dogs", "i8")], |
| 973 |
7, |
| 974 |
); |
| 975 |
|
| 976 |
compare( |
| 977 |
r"{{count|pluralize(one, count)}}", |
| 978 |
r" |
| 979 |
match ( |
| 980 |
&(askama::filters::pluralize( |
| 981 |
&(self.count), |
| 982 |
(&&askama::filters::AutoEscaper::new( |
| 983 |
&(self.one), |
| 984 |
askama::filters::Text, |
| 985 |
)) |
| 986 |
.askama_auto_escape()?, |
| 987 |
(&&askama::filters::AutoEscaper::new( |
| 988 |
&(self.count), |
| 989 |
askama::filters::Text, |
| 990 |
)) |
| 991 |
.askama_auto_escape()?, |
| 992 |
)?), |
| 993 |
) { |
| 994 |
(expr0,) => { |
| 995 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 996 |
} |
| 997 |
} |
| 998 |
", |
| 999 |
&[("count", "i8"), ("one", "&'static str")], |
| 1000 |
3, |
| 1001 |
); |
| 1002 |
|
| 1003 |
compare( |
| 1004 |
r"{{0|pluralize(sg, pl)}}", |
| 1005 |
r" |
| 1006 |
match ( |
| 1007 |
&((&&askama::filters::AutoEscaper::new(&(self.pl), askama::filters::Text)) |
| 1008 |
.askama_auto_escape()?), |
| 1009 |
) { |
| 1010 |
(expr0,) => { |
| 1011 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
", |
| 1015 |
&[("sg", "&'static str"), ("pl", "&'static str")], |
| 1016 |
3, |
| 1017 |
); |
| 1018 |
compare( |
| 1019 |
r"{{1|pluralize(sg, pl)}}", |
| 1020 |
r" |
| 1021 |
match ( |
| 1022 |
&((&&askama::filters::AutoEscaper::new(&(self.sg), askama::filters::Text)) |
| 1023 |
.askama_auto_escape()?), |
| 1024 |
) { |
| 1025 |
(expr0,) => { |
| 1026 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
", |
| 1030 |
&[("sg", "&'static str"), ("pl", "&'static str")], |
| 1031 |
3, |
| 1032 |
); |
| 1033 |
|
| 1034 |
compare( |
| 1035 |
r#"{{0|pluralize("sg", "pl")}}"#, |
| 1036 |
r#" |
| 1037 |
match (&(askama::filters::Safe("pl")),) { |
| 1038 |
(expr0,) => { |
| 1039 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
"#, |
| 1043 |
&[], |
| 1044 |
3, |
| 1045 |
); |
| 1046 |
compare( |
| 1047 |
r#"{{1|pluralize("sg", "pl")}}"#, |
| 1048 |
r#" |
| 1049 |
match (&(askama::filters::Safe("sg")),) { |
| 1050 |
(expr0,) => { |
| 1051 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
"#, |
| 1055 |
&[], |
| 1056 |
3, |
| 1057 |
); |
| 1058 |
|
| 1059 |
compare( |
| 1060 |
r"{{0|pluralize}}", |
| 1061 |
r#" |
| 1062 |
match (&(askama::filters::Safe("s")),) { |
| 1063 |
(expr0,) => { |
| 1064 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
"#, |
| 1068 |
&[], |
| 1069 |
3, |
| 1070 |
); |
| 1071 |
compare( |
| 1072 |
r"{{1|pluralize}}", |
| 1073 |
r" |
| 1074 |
match (&(askama::helpers::Empty),) { |
| 1075 |
(expr0,) => { |
| 1076 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
", |
| 1080 |
&[], |
| 1081 |
3, |
| 1082 |
); |
| 1083 |
} |
| 1084 |
|
| 1085 |
#[test] |
| 1086 |
fn test_concat() { |
| 1087 |
compare( |
| 1088 |
r#"{{ "<" ~ a ~ "|" ~ b ~ '>' }}"#, |
| 1089 |
r#" |
| 1090 |
__askama_writer.write_str("<")?; |
| 1091 |
match ( |
| 1092 |
&((&&askama::filters::AutoEscaper::new(&(self.a), askama::filters::Text)) |
| 1093 |
.askama_auto_escape()?), |
| 1094 |
&((&&askama::filters::AutoEscaper::new(&(self.b), askama::filters::Text)) |
| 1095 |
.askama_auto_escape()?), |
| 1096 |
) { |
| 1097 |
(expr1, expr3) => { |
| 1098 |
(&&&askama::filters::Writable(expr1)).askama_write(__askama_writer, __askama_values)?; |
| 1099 |
__askama_writer.write_str("|")?; |
| 1100 |
(&&&askama::filters::Writable(expr3)).askama_write(__askama_writer, __askama_values)?; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
__askama_writer.write_str(">")?; |
| 1104 |
"#, |
| 1105 |
&[("a", "&'static str"), ("b", "u32")], |
| 1106 |
9, |
| 1107 |
); |
| 1108 |
|
| 1109 |
compare( |
| 1110 |
r#"{{ ("a=" ~ a ~ " b=" ~ b)|upper }}"#, |
| 1111 |
r#" |
| 1112 |
match ( |
| 1113 |
&((&&askama::filters::AutoEscaper::new( |
| 1114 |
&(askama::filters::upper( |
| 1115 |
&((askama::helpers::Concat( |
| 1116 |
&(askama::helpers::Concat(&("a="), &(self.a))), |
| 1117 |
&(askama::helpers::Concat(&(" b="), &(self.b))), |
| 1118 |
))), |
| 1119 |
)?), |
| 1120 |
askama::filters::Text, |
| 1121 |
)) |
| 1122 |
.askama_auto_escape()?), |
| 1123 |
) { |
| 1124 |
(expr0,) => { |
| 1125 |
(&&&askama::filters::Writable(expr0)).askama_write(__askama_writer, __askama_values)?; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
"#, |
| 1129 |
&[("a", "&'static str"), ("b", "u32")], |
| 1130 |
3, |
| 1131 |
); |
| 1132 |
} |
| 1133 |
|
| 1134 |
#[test] |
| 1135 |
fn extends_with_whitespace_control() { |
| 1136 |
const CONTROL: &[&str] = &["", "\t", "-", "+", "~"]; |
| 1137 |
|
| 1138 |
let expected = jinja_to_rust(r#"front {% extends "a.html" %} back"#, &[], "").unwrap(); |
| 1139 |
let expected = unparse(&expected); |
| 1140 |
for front in CONTROL { |
| 1141 |
for back in CONTROL { |
| 1142 |
let src = format!(r#"front {{%{front} extends "a.html" {back}%}} back"#); |
| 1143 |
let actual = jinja_to_rust(&src, &[], "").unwrap(); |
| 1144 |
let actual = unparse(&actual); |
| 1145 |
assert_eq!(expected, actual, "source: {:?}", src); |
| 1146 |
} |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
#[test] |
| 1151 |
fn test_with_config() { |
| 1152 |
// In this test we make sure that the config path is tracked. |
| 1153 |
compare_ex( |
| 1154 |
r#""#, |
| 1155 |
&format!( |
| 1156 |
"const _: &[askama::helpers::core::primitive::u8] = \ |
| 1157 |
askama::helpers::core::include_bytes!({:#?});", |
| 1158 |
Path::new(env!("CARGO_MANIFEST_DIR")) |
| 1159 |
.join("empty_test_config.toml") |
| 1160 |
.canonicalize() |
| 1161 |
.unwrap(), |
| 1162 |
), |
| 1163 |
&[], |
| 1164 |
0, |
| 1165 |
r#"#[template(config = "empty_test_config.toml")]"#, |
| 1166 |
); |
| 1167 |
} |