From c860a22e854089ffb047b621860dc452c9eb8f62 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 30 Jul 2024 22:00:25 +0200 Subject: [PATCH] Add test for rest pattern --- testing/tests/rest_pattern.rs | 94 +++++++--------------------- testing/tests/ui/rest_pattern.rs | 60 ++++++++++++++++++ testing/tests/ui/rest_pattern.stderr | 60 ++++++++++++++++++ 3 files changed, 141 insertions(+), 73 deletions(-) create mode 100644 testing/tests/ui/rest_pattern.rs create mode 100644 testing/tests/ui/rest_pattern.stderr diff --git a/testing/tests/rest_pattern.rs b/testing/tests/rest_pattern.rs index 28e4d933..2d9d071d 100644 --- a/testing/tests/rest_pattern.rs +++ b/testing/tests/rest_pattern.rs @@ -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]" + ); } diff --git a/testing/tests/ui/rest_pattern.rs b/testing/tests/ui/rest_pattern.rs new file mode 100644 index 00000000..027699c7 --- /dev/null +++ b/testing/tests/ui/rest_pattern.rs @@ -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() { +} diff --git a/testing/tests/ui/rest_pattern.stderr b/testing/tests/ui/rest_pattern.stderr new file mode 100644 index 00000000..c384da5d --- /dev/null +++ b/testing/tests/ui/rest_pattern.stderr @@ -0,0 +1,60 @@ +error: `..` can only be used once per array pattern + --> :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 + --> :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 + --> :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 + --> :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 + --> :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 | | "#, + | |__^