mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-29 05:51:32 +00:00
84 lines
2.1 KiB
Rust
84 lines
2.1 KiB
Rust
use rinja::Template;
|
|
|
|
#[test]
|
|
fn test_int_parser() {
|
|
#[derive(Template)]
|
|
#[template(source = "{% let v = self.parse()? %}{{s}}={{v}}", ext = "txt")]
|
|
struct IntParserTemplate<'a> {
|
|
s: &'a str,
|
|
}
|
|
|
|
impl IntParserTemplate<'_> {
|
|
fn parse(&self) -> Result<i32, std::num::ParseIntError> {
|
|
self.s.parse()
|
|
}
|
|
}
|
|
|
|
let template = IntParserTemplate { s: "💯" };
|
|
assert!(matches!(template.render(), Err(rinja::Error::Custom(_))));
|
|
assert_eq!(
|
|
format!("{}", &template.render().unwrap_err()),
|
|
"invalid digit found in string"
|
|
);
|
|
|
|
let template = IntParserTemplate { s: "100" };
|
|
assert_eq!(template.render().unwrap(), "100=100");
|
|
}
|
|
|
|
#[test]
|
|
fn fail_fmt() {
|
|
#[derive(Template)]
|
|
#[template(source = "{{ value()? }}", ext = "txt")]
|
|
struct FailFmt {
|
|
inner: Option<&'static str>,
|
|
}
|
|
|
|
impl FailFmt {
|
|
fn value(&self) -> Result<&'static str, std::fmt::Error> {
|
|
if let Some(inner) = self.inner {
|
|
Ok(inner)
|
|
} else {
|
|
Err(std::fmt::Error)
|
|
}
|
|
}
|
|
}
|
|
|
|
let template = FailFmt { inner: None };
|
|
assert!(matches!(template.render(), Err(rinja::Error::Custom(_))));
|
|
assert_eq!(
|
|
format!("{}", &template.render().unwrap_err()),
|
|
format!("{}", std::fmt::Error)
|
|
);
|
|
|
|
let template = FailFmt {
|
|
inner: Some("hello world"),
|
|
};
|
|
assert_eq!(template.render().unwrap(), "hello world");
|
|
}
|
|
|
|
#[test]
|
|
fn fail_str() {
|
|
#[derive(Template)]
|
|
#[template(source = "{{ value()? }}", ext = "txt")]
|
|
struct FailStr {
|
|
value: bool,
|
|
}
|
|
|
|
impl FailStr {
|
|
fn value(&self) -> Result<&'static str, &'static str> {
|
|
if !self.value {
|
|
Err("FAIL")
|
|
} else {
|
|
Ok("hello world")
|
|
}
|
|
}
|
|
}
|
|
|
|
let template = FailStr { value: false };
|
|
assert!(matches!(template.render(), Err(rinja::Error::Custom(_))));
|
|
assert_eq!(format!("{}", &template.render().unwrap_err()), "FAIL");
|
|
|
|
let template = FailStr { value: true };
|
|
assert_eq!(template.render().unwrap(), "hello world");
|
|
}
|