mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-27 13:00:57 +00:00
154 lines
2.9 KiB
Rust
154 lines
2.9 KiB
Rust
#![allow(clippy::useless_let_if_seq)]
|
|
|
|
use rinja::Template;
|
|
|
|
#[derive(Template)]
|
|
#[template(source = "{% let v = s %}{{ v }}", ext = "txt")]
|
|
struct LetTemplate<'a> {
|
|
s: &'a str,
|
|
}
|
|
|
|
#[test]
|
|
fn test_let() {
|
|
let t = LetTemplate { s: "foo" };
|
|
assert_eq!(t.render().unwrap(), "foo");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "let.html")]
|
|
struct LetTupleTemplate<'a> {
|
|
s: &'a str,
|
|
t: (&'a str, &'a str),
|
|
}
|
|
|
|
#[test]
|
|
fn test_let_tuple() {
|
|
let t = LetTupleTemplate {
|
|
s: "foo",
|
|
t: ("bar", "bazz"),
|
|
};
|
|
assert_eq!(t.render().unwrap(), "foo\nbarbazz");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "let-decl.html")]
|
|
struct LetDeclTemplate<'a> {
|
|
cond: bool,
|
|
s: &'a str,
|
|
}
|
|
|
|
#[test]
|
|
fn test_let_decl() {
|
|
let t = LetDeclTemplate {
|
|
cond: false,
|
|
s: "bar",
|
|
};
|
|
assert_eq!(t.render().unwrap(), "bar");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "let-shadow.html")]
|
|
struct LetShadowTemplate {
|
|
cond: bool,
|
|
}
|
|
|
|
impl LetShadowTemplate {
|
|
fn tuple() -> (i32, i32) {
|
|
(4, 5)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_let_shadow() {
|
|
let t = LetShadowTemplate { cond: true };
|
|
assert_eq!(t.render().unwrap(), "22-1-33-11-22");
|
|
|
|
let t = LetShadowTemplate { cond: false };
|
|
assert_eq!(t.render().unwrap(), "222-1-333-4-5-11-222");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(source = "{% for v in self.0 %}{{ v }}{% endfor %}", ext = "txt")]
|
|
struct SelfIterTemplate(Vec<usize>);
|
|
|
|
#[test]
|
|
fn test_self_iter() {
|
|
let t = SelfIterTemplate(vec![1, 2, 3]);
|
|
assert_eq!(t.render().unwrap(), "123");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(
|
|
source = "{% if true %}{% let t = a.unwrap() %}{{ t }}{% endif %}",
|
|
ext = "txt"
|
|
)]
|
|
struct IfLet {
|
|
a: Option<&'static str>,
|
|
}
|
|
|
|
#[test]
|
|
fn test_if_let() {
|
|
let t = IfLet { a: Some("foo") };
|
|
assert_eq!(t.render().unwrap(), "foo");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "let-destruct-tuple.html")]
|
|
struct LetDestructTupleTemplate {
|
|
abcd: (char, ((char, char), char)),
|
|
}
|
|
|
|
#[test]
|
|
fn test_destruct_tuple() {
|
|
let t = LetDestructTupleTemplate {
|
|
abcd: ('w', (('x', 'y'), 'z')),
|
|
};
|
|
assert_eq!(t.render().unwrap(), "wxyz\nwz\nw");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(
|
|
source = "{% let x = 1 %}{% for x in x..=x %}{{ x }}{% endfor %}",
|
|
ext = "txt"
|
|
)]
|
|
struct DeclRange;
|
|
|
|
#[test]
|
|
fn test_decl_range() {
|
|
let t = DeclRange;
|
|
assert_eq!(t.render().unwrap(), "1");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(
|
|
source = "{% let x %}{% let x = 1 %}{% for x in x..=x %}{{ x }}{% endfor %}",
|
|
ext = "txt"
|
|
)]
|
|
struct DeclAssignRange;
|
|
|
|
#[test]
|
|
fn test_decl_assign_range() {
|
|
let t = DeclAssignRange;
|
|
assert_eq!(t.render().unwrap(), "1");
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(
|
|
source = "
|
|
{%- set t = title -%}
|
|
{{t}}/{{title -}}
|
|
",
|
|
ext = "txt"
|
|
)]
|
|
struct DoNotMoveFields {
|
|
title: String,
|
|
}
|
|
|
|
#[test]
|
|
fn test_not_moving_fields_in_var() {
|
|
let x = DoNotMoveFields {
|
|
title: "a".to_string(),
|
|
};
|
|
assert_eq!(x.render().unwrap(), "a/a");
|
|
}
|