-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
test_templates.rs
134 lines (117 loc) · 4.04 KB
/
test_templates.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::collections::BTreeMap;
use std::fs;
use minijinja::{context, Environment, Error, State};
use similar_asserts::assert_eq;
#[test]
fn test_vm() {
let mut refs = Vec::new();
for entry in fs::read_dir("tests/inputs/refs").unwrap() {
let entry = entry.unwrap();
let filename = entry.file_name();
let filename = filename.to_str().unwrap();
if !filename.ends_with(".txt") && !filename.ends_with(".html") {
continue;
}
let source = fs::read_to_string(entry.path()).unwrap();
refs.push((entry.path().clone(), source));
}
insta::glob!("inputs/*", |path| {
if !path.metadata().unwrap().is_file() {
return;
}
let filename = path.file_name().unwrap().to_str().unwrap();
let contents = std::fs::read_to_string(path).unwrap();
let mut iter = contents.splitn(2, "\n---\n");
let mut env = Environment::new();
#[cfg(feature = "debug")]
{
env.set_debug(false);
}
let ctx: serde_json::Value = serde_json::from_str(iter.next().unwrap()).unwrap();
for (path, source) in &refs {
let ref_filename = path.file_name().unwrap().to_str().unwrap();
env.add_template(ref_filename, source).unwrap();
}
let content = iter.next().unwrap();
env.add_template(filename, content).unwrap();
let template = env.get_template(filename).unwrap();
dbg!(&template);
let mut rendered = match template.render(&ctx) {
Ok(rendered) => rendered,
Err(err) => format!("!!!ERROR!!!\n\n{:?}\n", err),
};
rendered.push('\n');
insta::with_settings!({
info => &ctx,
description => content.trim_end(),
omit_expression => true
}, {
insta::assert_snapshot!(&rendered);
});
});
}
#[test]
fn test_custom_filter() {
fn test_filter(_: &State, value: String) -> Result<String, Error> {
Ok(format!("[{}]", value))
}
let mut ctx = BTreeMap::new();
ctx.insert("var", 42);
let mut env = Environment::new();
env.add_filter("test", test_filter);
env.add_template("test", "{{ var|test }}").unwrap();
let tmpl = env.get_template("test").unwrap();
let rv = tmpl.render(&ctx).unwrap();
assert_eq!(rv, "[42]");
}
#[test]
fn test_single() {
let mut env = Environment::new();
env.add_template("simple", "Hello {{ name }}!").unwrap();
let tmpl = env.get_template("simple").unwrap();
let rv = tmpl.render(context!(name => "Peter")).unwrap();
assert_eq!(rv, "Hello Peter!");
}
#[test]
fn test_auto_escaping() {
let mut env = Environment::new();
env.add_template("index.html", "{{ var }}").unwrap();
#[cfg(feature = "json")]
{
env.add_template("index.js", "{{ var }}").unwrap();
}
env.add_template("index.txt", "{{ var }}").unwrap();
// html
let tmpl = env.get_template("index.html").unwrap();
let rv = tmpl.render(context!(var => "<script>")).unwrap();
insta::assert_snapshot!(rv, @"<script>");
// JSON
#[cfg(feature = "json")]
{
use minijinja::value::Value;
let tmpl = env.get_template("index.js").unwrap();
let rv = tmpl.render(context!(var => "foo\"bar'baz")).unwrap();
insta::assert_snapshot!(rv, @r###""foo\"bar'baz""###);
let rv = tmpl
.render(context!(var => [Value::from(true), Value::from("<foo>"), Value::from(())]))
.unwrap();
insta::assert_snapshot!(rv, @r###"[true,"<foo>",null]"###);
}
// Text
let tmpl = env.get_template("index.txt").unwrap();
let rv = tmpl.render(context!(var => "foo\"bar'baz")).unwrap();
insta::assert_snapshot!(rv, @r###"foo"bar'baz"###);
}
#[cfg(feature = "sync")]
#[test]
fn test_loop_changed() {
let rv = minijinja::render!(
r#"
{%- for i in items -%}
{% if loop.changed(i) %}{{ i }}{% endif %}
{%- endfor -%}
"#,
items => vec![1, 1, 1, 2, 3, 4, 4, 5],
);
assert_eq!(rv, "12345");
}