Skip to content

Commit cfc0883

Browse files
Update tests for lifetimes in filters
1 parent 40353f0 commit cfc0883

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

testing/tests/filters.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,6 @@ fn test_custom_filter_constructs() {
660660
#[test]
661661
fn filter_arguments_mutability() {
662662
mod filters {
663-
664663
// Check mutability is kept for mandatory arguments.
665664
#[askama::filter_fn]
666665
pub fn a(mut value: u32, _: &dyn askama::Values) -> askama::Result<String> {
@@ -691,3 +690,30 @@ fn filter_arguments_mutability() {
691690

692691
assert_eq!(X.render().unwrap(), "2 9 4");
693692
}
693+
694+
// Checks support for lifetimes.
695+
#[test]
696+
fn filter_lifetimes() {
697+
mod filters {
698+
use std::borrow::Cow;
699+
700+
#[askama::filter_fn]
701+
pub fn a<'a: 'b, 'b>(
702+
value: &'a str,
703+
_: &dyn askama::Values,
704+
extra: &'b str,
705+
) -> askama::Result<Cow<'a, str>> {
706+
if extra.is_empty() {
707+
Ok(Cow::Borrowed(value))
708+
} else {
709+
Ok(Cow::Owned(format!("{value}-{extra}")))
710+
}
711+
}
712+
}
713+
714+
#[derive(Template)]
715+
#[template(ext = "txt", source = r#"{{ "a"|a("b") }}"#)]
716+
struct X;
717+
718+
assert_eq!(X.render().unwrap(), "a-b");
719+
}

testing/tests/ui/filter-signature-validation.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ mod missing_required_args {
99
pub fn filter2(_: &dyn askama::Values) -> askama::Result<String> {}
1010
}
1111

12-
mod lifetime_args {
13-
#[askama::filter_fn]
14-
pub fn filter0<'a>(input: usize, _: &dyn askama::Values, arg: &'a ()) -> askama::Result<String> {}
15-
}
16-
1712
mod const_generic_args {
1813
#[askama::filter_fn]
1914
pub fn filter0<const T: bool>(input: usize, _: &dyn askama::Values) -> askama::Result<String> {}

testing/tests/ui/filter-signature-validation.stderr

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,56 @@ error: Filter function missing required environment argument. Example: `fn filte
1616
9 | pub fn filter2(_: &dyn askama::Values) -> askama::Result<String> {}
1717
| ^
1818

19-
error: Lifetime generics are currently not supported for filters
20-
--> tests/ui/filter-signature-validation.rs:14:20
21-
|
22-
14 | pub fn filter0<'a>(input: usize, _: &dyn askama::Values, arg: &'a ()) -> askama::Result<String> {}
23-
| ^^
24-
2519
error: Const generics are currently not supported for filters
26-
--> tests/ui/filter-signature-validation.rs:19:20
20+
--> tests/ui/filter-signature-validation.rs:14:20
2721
|
28-
19 | pub fn filter0<const T: bool>(input: usize, _: &dyn askama::Values) -> askama::Result<String> {}
22+
14 | pub fn filter0<const T: bool>(input: usize, _: &dyn askama::Values) -> askama::Result<String> {}
2923
| ^^^^^
3024

3125
error: Filter functions don't support generic parameter defaults
32-
--> tests/ui/filter-signature-validation.rs:24:24
26+
--> tests/ui/filter-signature-validation.rs:19:24
3327
|
34-
24 | pub fn filter0<T = f64>(input: usize, _: &dyn askama::Values, arg: T) -> askama::Result<String> {}
28+
19 | pub fn filter0<T = f64>(input: usize, _: &dyn askama::Values, arg: T) -> askama::Result<String> {}
3529
| ^^^
3630

3731
error: Filter function is missing return type
38-
--> tests/ui/filter-signature-validation.rs:29:56
32+
--> tests/ui/filter-signature-validation.rs:24:56
3933
|
40-
29 | pub fn filter0(input: usize, _: &dyn askama::Values) {}
34+
24 | pub fn filter0(input: usize, _: &dyn askama::Values) {}
4135
| ^
4236

4337
error: Only conventional function arguments are supported
44-
--> tests/ui/filter-signature-validation.rs:35:58
38+
--> tests/ui/filter-signature-validation.rs:30:58
4539
|
46-
35 | pub fn filter0(input: usize, _: &dyn askama::Values, Wrapper(arg): Wrapper) -> askama::Result<String> {}
40+
30 | pub fn filter0(input: usize, _: &dyn askama::Values, Wrapper(arg): Wrapper) -> askama::Result<String> {}
4741
| ^^^^^^^
4842

4943
error: Impl generics are currently not supported for filters
50-
--> tests/ui/filter-signature-validation.rs:40:63
44+
--> tests/ui/filter-signature-validation.rs:35:63
5145
|
52-
40 | pub fn filter0(input: usize, _: &dyn askama::Values, arg: impl std::fmt::Display) -> askama::Result<String> {}
46+
35 | pub fn filter0(input: usize, _: &dyn askama::Values, arg: impl std::fmt::Display) -> askama::Result<String> {}
5347
| ^^^^
5448

5549
error: All required arguments must appear before any optional ones
56-
--> tests/ui/filter-signature-validation.rs:45:92
50+
--> tests/ui/filter-signature-validation.rs:40:92
5751
|
58-
45 | pub fn filter0(input: usize, _: &dyn askama::Values, #[optional(1337)] opt_arg: usize, req_arg: usize) -> askama::Result<String> {}
52+
40 | pub fn filter0(input: usize, _: &dyn askama::Values, #[optional(1337)] opt_arg: usize, req_arg: usize) -> askama::Result<String> {}
5953
| ^^^^^^^
6054

6155
error: Impl generics are currently not supported for filters
62-
--> tests/ui/filter-signature-validation.rs:50:85
56+
--> tests/ui/filter-signature-validation.rs:45:85
6357
|
64-
50 | pub fn filter0(input: usize, _: &dyn askama::Values, #[optional(1337)] opt_arg: impl std::fmt::Display) -> askama::Result<String> {}
58+
45 | pub fn filter0(input: usize, _: &dyn askama::Values, #[optional(1337)] opt_arg: impl std::fmt::Display) -> askama::Result<String> {}
6559
| ^^^^
6660

6761
error: Optional arguments must not use generic parameters
68-
--> tests/ui/filter-signature-validation.rs:53:99
62+
--> tests/ui/filter-signature-validation.rs:48:99
6963
|
70-
53 | pub fn filter1<T: Copy>(input: usize, _: &dyn askama::Values, #[optional(1337usize)] opt_arg: T) -> askama::Result<String> {}
64+
48 | pub fn filter1<T: Copy>(input: usize, _: &dyn askama::Values, #[optional(1337usize)] opt_arg: T) -> askama::Result<String> {}
7165
| ^
7266

7367
error: Optional arguments must not use generic parameters
74-
--> tests/ui/filter-signature-validation.rs:56:106
68+
--> tests/ui/filter-signature-validation.rs:51:106
7569
|
76-
56 | pub fn filter2<T: Copy>(input: usize, _: &dyn askama::Values, #[optional(1337usize)] opt_arg: Option<T>) -> askama::Result<String> {}
70+
51 | pub fn filter2<T: Copy>(input: usize, _: &dyn askama::Values, #[optional(1337usize)] opt_arg: Option<T>) -> askama::Result<String> {}
7771
| ^

0 commit comments

Comments
 (0)