Add test for rest pattern

This commit is contained in:
Guillaume Gomez 2024-07-30 22:00:25 +02:00
parent 2771533594
commit c860a22e85
3 changed files with 141 additions and 73 deletions

View File

@ -1,78 +1,26 @@
use rinja::Template;
#[test]
fn a() {
#[derive(Template)]
#[template(source = "{% if let (a, ..) = abc %}-{{a}}-{% endif %}", ext = "txt")]
struct Tmpl {
abc: (u32, u32, u32),
}
assert_eq!(Tmpl { abc: (1, 2, 3) }.to_string(), "-1-");
}
#[derive(Template)]
#[template(
source = r#"
{%- if let [1, 2, who @ .., 4] = [1, 2, 3, 4] -%}
111> {{"{:?}"|format(who)}}
{%- endif -%}
{%- if let [who @ .., 4] = [1, 2, 3, 4] -%}
222> {{"{:?}"|format(who)}}
{%- endif -%}
{%- if let [1, who @ ..] = [1, 2, 3, 4] -%}
333> {{"{:?}"|format(who)}}
{%- endif -%}
"#,
ext = "txt"
)]
struct Rest;
#[test]
fn ab() {
#[derive(Template)]
#[template(
source = "{% if let (a, b, ..) = abc %}-{{a}}{{b}}-{% endif %}",
ext = "txt"
)]
struct Tmpl {
abc: (u32, u32, u32),
}
assert_eq!(Tmpl { abc: (1, 2, 3) }.to_string(), "-12-");
}
#[test]
fn abc() {
#[derive(Template)]
#[template(
source = "{% if let (a, b, c, ..) = abc %}-{{a}}{{b}}{{c}}-{% endif %}",
ext = "txt"
)]
struct Tmpl1 {
abc: (u32, u32, u32),
}
assert_eq!(Tmpl1 { abc: (1, 2, 3) }.to_string(), "-123-");
assert_eq!(Tmpl2 { abc: (1, 2, 3) }.to_string(), "-123-");
#[derive(Template)]
#[template(
source = "{% if let (a, b, c, ..) = abc %}-{{a}}{{b}}{{c}}-{% endif %}",
ext = "txt"
)]
struct Tmpl2 {
abc: (u32, u32, u32),
}
assert_eq!(Tmpl2 { abc: (1, 2, 3) }.to_string(), "-123-");
}
#[test]
fn bc() {
#[derive(Template)]
#[template(
source = "{% if let (.., b, c) = abc %}-{{b}}{{c}}-{% endif %}",
ext = "txt"
)]
struct Tmpl {
abc: (u32, u32, u32),
}
assert_eq!(Tmpl { abc: (1, 2, 3) }.to_string(), "-23-");
}
#[test]
fn c() {
#[derive(Template)]
#[template(source = "{% if let (.., c) = abc %}-{{c}}-{% endif %}", ext = "txt")]
struct Tmpl {
abc: (u32, u32, u32),
}
assert_eq!(Tmpl { abc: (1, 2, 3) }.to_string(), "-3-");
fn test_rest() {
assert_eq!(
Rest.render().unwrap(),
"111> [3]222> [1, 2, 3]333> [2, 3, 4]"
);
}

View File

@ -0,0 +1,60 @@
use rinja::Template;
// Checking that we can't use `..` more than once.
#[derive(Template)]
#[template(
source = r#"
{%- if let [1, 2, who @ .., 4, ..] = [1, 2, 3, 4] -%}
{%- endif -%}
"#,
ext = "txt"
)]
struct Err1;
#[derive(Template)]
#[template(
source = r#"
{%- if let (.., 1, 2, .., 4) = (1, 2, 3, 4) -%}
{%- endif -%}
"#,
ext = "txt"
)]
struct Err2;
// This code doesn't make sense but the goal is to ensure that you can't
// use `..` in a struct more than once.
#[derive(Template)]
#[template(
source = r#"
{%- if let Cake { .., a, .. } = [1, 2, 3, 4] -%}
{%- endif -%}
"#,
ext = "txt"
)]
struct Err3;
// Now checking we can't use `@ ..` in tuples and structs.
#[derive(Template)]
#[template(
source = r#"
{%- if let (1, 2, who @ .., 4) = (1, 2, 3, 4) -%}
{%- endif -%}
"#,
ext = "txt"
)]
struct Err4;
// This code doesn't make sense but the goal is to ensure that you can't
// use `@ ..` in a struct so here we go.
#[derive(Template)]
#[template(
source = r#"
{%- if let Cake { a, who @ .. } = [1, 2, 3, 4] -%}
{%- endif -%}
"#,
ext = "txt"
)]
struct Err5;
fn main() {
}

View File

@ -0,0 +1,60 @@
error: `..` can only be used once per array pattern
--> <source attribute>:2:31
"..] = [1, 2, 3, 4] -%}\n{%- endif -%}\n"
--> tests/ui/rest_pattern.rs:6:14
|
6 | source = r#"
| ______________^
7 | | {%- if let [1, 2, who @ .., 4, ..] = [1, 2, 3, 4] -%}
8 | | {%- endif -%}
9 | | "#,
| |__^
error: `..` can only be used once per tuple pattern
--> <source attribute>:2:22
".., 4) = (1, 2, 3, 4) -%}\n{%- endif -%}\n"
--> tests/ui/rest_pattern.rs:16:14
|
16 | source = r#"
| ______________^
17 | | {%- if let (.., 1, 2, .., 4) = (1, 2, 3, 4) -%}
18 | | {%- endif -%}
19 | | "#,
| |__^
error: unexpected `,` character after `..`
note that in a named struct, `..` must come last to ignore other members
--> <source attribute>:2:20
", a, .. } = [1, 2, 3, 4] -%}\n{%- endif -%}\n"
--> tests/ui/rest_pattern.rs:28:14
|
28 | source = r#"
| ______________^
29 | | {%- if let Cake { .., a, .. } = [1, 2, 3, 4] -%}
30 | | {%- endif -%}
31 | | "#,
| |__^
error: `@ ..` cannot be used in tuple
--> <source attribute>:2:18
"who @ .., 4) = (1, 2, 3, 4) -%}\n{%- endif -%}\n"
--> tests/ui/rest_pattern.rs:39:14
|
39 | source = r#"
| ______________^
40 | | {%- if let (1, 2, who @ .., 4) = (1, 2, 3, 4) -%}
41 | | {%- endif -%}
42 | | "#,
| |__^
error: `@ ..` cannot be used in struct
--> <source attribute>:2:21
"who @ .. } = [1, 2, 3, 4] -%}\n{%- endif -%}\n"
--> tests/ui/rest_pattern.rs:51:14
|
51 | source = r#"
| ______________^
52 | | {%- if let Cake { a, who @ .. } = [1, 2, 3, 4] -%}
53 | | {%- endif -%}
54 | | "#,
| |__^