Skip to content

Commit

Permalink
Add tests for named arguments in macro calls
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 24, 2023
1 parent 7daa1cb commit ebfd0cd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
28 changes: 28 additions & 0 deletions testing/tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,31 @@ fn test_macro_self_arg() {
let t = MacroSelfArgTemplate { s: "foo" };
assert_eq!(t.render().unwrap(), "foo");
}

#[derive(Template)]
#[template(
source = "{%- macro thrice(param1, param2) -%}
{{ param1 }} {{ param2 }}
{% endmacro -%}
{%- call thrice(param1=2, param2=3) -%}
{%- call thrice(param2=3, param1=2) -%}
{%- call thrice(3, param1=2) -%}
",
ext = "html"
)]
struct MacroNamedArg;

#[test]
// We check that it's always the correct values passed to the
// expected argument.
fn test_named_argument() {
assert_eq!(
MacroNamedArg.render().unwrap(),
"\
2 3
2 3
2 3
"
);
}
37 changes: 37 additions & 0 deletions testing/tests/ui/macro_named_argument.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use askama::Template;

#[derive(Template)]
#[template(source = "{%- macro thrice(param1, param2) -%}
{{ param1 }} {{ param2 }}
{%- endmacro -%}
{%- call thrice(param1=2, param3=3) -%}", ext = "html")]
struct InvalidNamedArg;

#[derive(Template)]
#[template(source = "{%- macro thrice(param1, param2) -%}
{{ param1 }} {{ param2 }}
{%- endmacro -%}
{%- call thrice(param1=2, param1=3) -%}", ext = "html")]
struct InvalidNamedArg2;

// Ensures that filters can't have named arguments.
#[derive(Template)]
#[template(source = "{%- macro thrice(param1, param2) -%}
{{ param1 }} {{ param2 }}
{%- endmacro -%}
{%- call thrice(3, param1=2) | filter(param1=12) -%}", ext = "html")]
struct InvalidNamedArg3;

// Ensures that named arguments can only be passed last.
#[derive(Template)]
#[template(source = "{%- macro thrice(param1, param2) -%}
{{ param1 }} {{ param2 }}
{%- endmacro -%}
{%- call thrice(param1=2, 3) -%}", ext = "html")]
struct InvalidNamedArg4;

fn main() {
}
36 changes: 36 additions & 0 deletions testing/tests/ui/macro_named_argument.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error: no argument named `param3` in macro "thrice"
--> tests/ui/macro_named_argument.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)

error: named argument `param1` was passed more than once
problems parsing template source at row 5, column 15 near:
"(param1=2, param1=3) -%}"
--> tests/ui/macro_named_argument.rs:11:10
|
11 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: problems parsing template source at row 5, column 29 near:
"| filter(param1=12) -%}"
--> tests/ui/macro_named_argument.rs:20:10
|
20 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

error: named arguments must always be passed last
problems parsing template source at row 4, column 15 near:
"(param1=2, 3) -%}"
--> tests/ui/macro_named_argument.rs:29:10
|
29 | #[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 ebfd0cd

Please sign in to comment.