From 6ee37650982033ad16e39f5c108cd33f6a0933d7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 27 Jun 2024 22:04:44 +0200 Subject: [PATCH] Add regression test for `..` pattern in structs --- testing/tests/let_destructoring.rs | 26 ++++++++++++ .../tests/ui/let_destructuring_has_rest.rs | 40 +++++++++++++++++++ .../ui/let_destructuring_has_rest.stderr | 26 ++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 testing/tests/ui/let_destructuring_has_rest.rs create mode 100644 testing/tests/ui/let_destructuring_has_rest.stderr diff --git a/testing/tests/let_destructoring.rs b/testing/tests/let_destructoring.rs index 5d433028..aaa84839 100644 --- a/testing/tests/let_destructoring.rs +++ b/testing/tests/let_destructoring.rs @@ -119,3 +119,29 @@ fn test_let_destruct_with_path_and_with_keyword() { }; assert_eq!(t.render().unwrap(), "hello"); } + +#[derive(Template)] +#[template( + source = " +{%- if let RestPattern2 { a, b } = x -%}hello {{ a }}{%- endif -%} +{%- if let RestPattern2 { a, b, } = x -%}hello {{ b }}{%- endif -%} +{%- if let RestPattern2 { a, .. } = x -%}hello {{ a }}{%- endif -%} +", + ext = "html" +)] +struct RestPattern { + x: RestPattern2, +} + +struct RestPattern2 { + a: u32, + b: u32, +} + +#[test] +fn test_has_rest_pattern() { + let t = RestPattern { + x: RestPattern2 { a: 0, b: 1 }, + }; + assert_eq!(t.render().unwrap(), "hello 0hello 1hello 0"); +} diff --git a/testing/tests/ui/let_destructuring_has_rest.rs b/testing/tests/ui/let_destructuring_has_rest.rs new file mode 100644 index 00000000..4301bd24 --- /dev/null +++ b/testing/tests/ui/let_destructuring_has_rest.rs @@ -0,0 +1,40 @@ +use rinja::Template; + +struct X { + a: u32, + b: u32, +} + +#[derive(Template)] +#[template(source = " +{%- if let X { a, .. } = x -%}hello {{ a }}{%- endif -%} +", ext = "html")] +struct T1 { + x: X, +} + +#[derive(Template)] +#[template(source = " +{%- if let X { a, .., } = x -%}hello {{ a }}{%- endif -%} +", ext = "html")] +struct T2 { + x: X, +} + +#[derive(Template)] +#[template(source = " +{%- if let X { a .. } = x -%}hello {{ a }}{%- endif -%} +", ext = "html")] +struct T3 { + x: X, +} + +#[derive(Template)] +#[template(source = " +{%- if let X { .. } = x -%}hello {{ a }}{%- endif -%} +", ext = "html")] +struct T4 { + x: X, +} + +fn main() {} diff --git a/testing/tests/ui/let_destructuring_has_rest.stderr b/testing/tests/ui/let_destructuring_has_rest.stderr new file mode 100644 index 00000000..fd22deb5 --- /dev/null +++ b/testing/tests/ui/let_destructuring_has_rest.stderr @@ -0,0 +1,26 @@ +error: unexpected `,` character after `..` + failed to parse template source at row 2, column 20 near: + ", } = x -%}hello {{ a }}{%- endif -%}\n" + --> tests/ui/let_destructuring_has_rest.rs:16:10 + | +16 | #[derive(Template)] + | ^^^^^^^^ + | + = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: failed to parse template source at row 2, column 17 near: + ".. } = x -%}hello {{ a }}{%- endif -%}\n" + --> tests/ui/let_destructuring_has_rest.rs:24:10 + | +24 | #[derive(Template)] + | ^^^^^^^^ + | + = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0609]: no field `a` on type `&T4` + --> tests/ui/let_destructuring_has_rest.rs:32:10 + | +32 | #[derive(Template)] + | ^^^^^^^^ unknown field + | + = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)