Add regression test for * and & in expressions

This commit is contained in:
Guillaume Gomez 2024-07-01 20:54:43 +02:00
parent e9fc4a6db3
commit e5a3e1c763
3 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,62 @@
use rinja::Template;
#[derive(Template)]
#[template(
source = r#"
{%- if *title == "something" -%}
something1
{%- elif title == &"another" -%}
another2
{%- elif &*title == &&*"yep" -%}
yep3
{%- else -%}
{{title}}
{%- endif -%}
"#,
ext = "html"
)]
struct RefDeref {
title: &'static &'static str,
}
#[test]
fn test_ref_deref() {
let x = RefDeref {
title: &"something",
};
assert_eq!(x.render().unwrap(), "something1");
let x = RefDeref { title: &"another" };
assert_eq!(x.render().unwrap(), "another2");
let x = RefDeref { title: &"yep" };
assert_eq!(x.render().unwrap(), "yep3");
let x = RefDeref { title: &"bla" };
assert_eq!(x.render().unwrap(), "bla");
}
#[derive(Template)]
#[template(
source = r#"
{%- let x = *title -%}
{%- if x == "another" -%}
another2
{%- else -%}
{{x}}
{%- endif -%}
"#,
ext = "html"
)]
struct RefDerefAssignment {
title: &'static &'static str,
}
#[test]
fn test_ref_deref_assign() {
let x = RefDerefAssignment { title: &"another" };
assert_eq!(x.render().unwrap(), "another2");
let x = RefDerefAssignment { title: &"two" };
assert_eq!(x.render().unwrap(), "two");
}

View File

@ -0,0 +1,8 @@
use rinja::Template;
#[derive(Template)]
#[template(source = "{% let *x = 2 %}", ext = "html")]
struct WrongRefDeref;
fn main() {
}

View File

@ -0,0 +1,8 @@
error: failed to parse template source at row 1, column 7 near:
"*x = 2 %}"
--> tests/ui/ref_deref.rs:3:10
|
3 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)