aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests
diff options
context:
space:
mode:
authorLibravatar Guillaume Gomez <guillaume1.gomez@gmail.com>2023-11-17 14:40:12 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-11-28 11:36:09 +0100
commit28e26751cef5ca5e3b0a0e6e8c8aadff92dc615b (patch)
tree3a7adf6392bf95cc849095974d669171576e95d5 /testing/tests
parentb3020ee8bf979037e4191558ee7f1131b5c82de7 (diff)
downloadaskama-28e26751cef5ca5e3b0a0e6e8c8aadff92dc615b.tar.gz
askama-28e26751cef5ca5e3b0a0e6e8c8aadff92dc615b.tar.bz2
askama-28e26751cef5ca5e3b0a0e6e8c8aadff92dc615b.zip
Add tests for named arguments in macro calls
Diffstat (limited to 'testing/tests')
-rw-r--r--testing/tests/macro.rs28
-rw-r--r--testing/tests/ui/macro_named_argument.rs45
-rw-r--r--testing/tests/ui/macro_named_argument.stderr44
3 files changed, 117 insertions, 0 deletions
diff --git a/testing/tests/macro.rs b/testing/tests/macro.rs
index faada37..0088f36 100644
--- a/testing/tests/macro.rs
+++ b/testing/tests/macro.rs
@@ -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, param2=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
+3 2
+"
+ );
+}
diff --git a/testing/tests/ui/macro_named_argument.rs b/testing/tests/ui/macro_named_argument.rs
new file mode 100644
index 0000000..fe8a194
--- /dev/null
+++ b/testing/tests/ui/macro_named_argument.rs
@@ -0,0 +1,45 @@
+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;
+
+// Ensures that named arguments can't be used for arguments before them.
+#[derive(Template)]
+#[template(source = "{%- macro thrice(param1, param2) -%}
+{{ param1 }} {{ param2 }}
+{%- endmacro -%}
+{%- call thrice(3, param1=2) -%}", ext = "html")]
+struct InvalidNamedArg5;
+
+fn main() {
+}
diff --git a/testing/tests/ui/macro_named_argument.stderr b/testing/tests/ui/macro_named_argument.stderr
new file mode 100644
index 0000000..b346f2d
--- /dev/null
+++ b/testing/tests/ui/macro_named_argument.stderr
@@ -0,0 +1,44 @@
+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)
+
+error: cannot have unnamed argument (`param2`) after named argument in macro "thrice"
+ --> tests/ui/macro_named_argument.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)