Skip to content

Commit

Permalink
Add test for rest pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jul 30, 2024
1 parent eeb08fc commit cb7ac70
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 73 deletions.
94 changes: 21 additions & 73 deletions testing/tests/rest_pattern.rs
Original file line number Diff line number Diff line change
@@ -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-");
}

#[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-");
}
#[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 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]"
);
}
60 changes: 60 additions & 0 deletions testing/tests/ui/rest_pattern.rs
Original file line number Diff line number Diff line change
@@ -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() {
}
50 changes: 50 additions & 0 deletions testing/tests/ui/rest_pattern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
error: `..` can only be used once per array pattern
failed to parse template source at row 2, column 31 near:
"..] = [1, 2, 3, 4] -%}\n{%- endif -%}\n"
--> tests/ui/rest_pattern.rs:4:10
|
4 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `..` can only be used once per tuple pattern
failed to parse template source at row 2, column 22 near:
".., 4) = (1, 2, 3, 4) -%}\n{%- endif -%}\n"
--> tests/ui/rest_pattern.rs:14:10
|
14 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unexpected `,` character after `..`
note that in a named struct, `..` must come last to ignore other members
failed to parse template source at row 2, column 20 near:
", a, .. } = [1, 2, 3, 4] -%}\n{%- endif -"...
--> tests/ui/rest_pattern.rs:26:10
|
26 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `@ ..` cannot be used in tuple
failed to parse template source at row 2, column 18 near:
"who @ .., 4) = (1, 2, 3, 4) -%}\n{%- endi"...
--> tests/ui/rest_pattern.rs:37:10
|
37 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `@ ..` cannot be used in struct
failed to parse template source at row 2, column 21 near:
"who @ .. } = [1, 2, 3, 4] -%}\n{%- endif "...
--> tests/ui/rest_pattern.rs:49:10
|
49 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit cb7ac70

Please sign in to comment.