]> git.proxmox.com Git - rustc.git/blame - vendor/tera/src/renderer/tests/inheritance.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / vendor / tera / src / renderer / tests / inheritance.rs
CommitLineData
136023e0
XL
1use crate::context::Context;
2use crate::tera::Tera;
3
4#[test]
5fn render_simple_inheritance() {
6 let mut tera = Tera::default();
7 tera.add_raw_templates(vec![
8 ("top", "{% block pre %}{% endblock pre %}{% block main %}{% endblock main %}"),
9 ("bottom", "{% extends \"top\" %}{% block main %}MAIN{% endblock %}"),
10 ])
11 .unwrap();
12 let result = tera.render("bottom", &Context::new());
13
14 assert_eq!(result.unwrap(), "MAIN".to_string());
15}
16
17#[test]
18fn render_simple_inheritance_super() {
19 let mut tera = Tera::default();
20 tera.add_raw_templates(vec![
21 ("top", "{% block main %}TOP{% endblock main %}"),
22 ("bottom", "{% extends \"top\" %}{% block main %}{{ super() }}MAIN{% endblock %}"),
23 ])
24 .unwrap();
25 let result = tera.render("bottom", &Context::new());
26
27 assert_eq!(result.unwrap(), "TOPMAIN".to_string());
28}
29
30#[test]
31fn render_multiple_inheritance() {
32 let mut tera = Tera::default();
33 tera.add_raw_templates(vec![
34 ("top", "{% block pre %}{% endblock pre %}{% block main %}{% endblock main %}"),
35 ("mid", "{% extends \"top\" %}{% block pre %}PRE{% endblock pre %}"),
36 ("bottom", "{% extends \"mid\" %}{% block main %}MAIN{% endblock main %}"),
37 ])
38 .unwrap();
39 let result = tera.render("bottom", &Context::new());
40
41 assert_eq!(result.unwrap(), "PREMAIN".to_string());
42}
43
44#[test]
45fn render_multiple_inheritance_with_super() {
46 let mut tera = Tera::default();
47 tera.add_raw_templates(vec![
48 (
49 "grandparent",
50 "{% block hey %}hello{% endblock hey %} {% block ending %}sincerely{% endblock ending %}",
51 ),
52 (
53 "parent",
54 "{% extends \"grandparent\" %}{% block hey %}hi and grandma says {{ super() }}{% endblock hey %}",
55 ),
56 (
57 "child",
58 "{% extends \"parent\" %}{% block hey %}dad says {{ super() }}{% endblock hey %}{% block ending %}{{ super() }} with love{% endblock ending %}",
59 ),
60 ]).unwrap();
61 let result = tera.render("child", &Context::new());
62
63 assert_eq!(
64 result.unwrap(),
65 "dad says hi and grandma says hello sincerely with love".to_string()
66 );
67}
68
69#[test]
70fn render_filter_section_inheritance_no_override() {
71 let mut tera = Tera::default();
72 tera.add_raw_templates(vec![
73 ("top", "{% filter upper %}hello {% block main %}top{% endblock main %}{% endfilter %}"),
74 ("bottom", "{% extends 'top' %}"),
75 ])
76 .unwrap();
77 let result = tera.render("bottom", &Context::new());
78
79 assert_eq!(result.unwrap(), "HELLO TOP".to_string());
80}
81
82#[test]
83fn render_filter_section_inheritance() {
84 let mut tera = Tera::default();
85 tera.add_raw_templates(vec![
86 ("top", "{% filter upper %}hello {% block main %}top{% endblock main %}{% endfilter %}"),
87 ("bottom", "{% extends 'top' %}{% block main %}bottom{% endblock %}"),
88 ])
89 .unwrap();
90 let result = tera.render("bottom", &Context::new());
91
92 assert_eq!(result.unwrap(), "HELLO BOTTOM".to_string());
93}
94
95#[test]
96fn render_super_multiple_inheritance_nested_block() {
97 let mut tera = Tera::default();
98 tera.add_raw_templates(vec![
99 (
100 "grandparent",
101 "{% block hey %}hello{% endblock hey %}",
102 ),
103 (
104 "parent",
105 "{% extends \"grandparent\" %}{% block hey %}hi and grandma says {{ super() }} {% block ending %}sincerely{% endblock ending %}{% endblock hey %}",
106 ),
107 (
108 "child", "{% extends \"parent\" %}{% block hey %}dad says {{ super() }}{% endblock hey %}{% block ending %}{{ super() }} with love{% endblock ending %}",
109 ),
110 ]).unwrap();
111 let result = tera.render("child", &Context::new());
112
113 assert_eq!(
114 result.unwrap(),
115 "dad says hi and grandma says hello sincerely with love".to_string()
116 );
117}
118
119#[test]
120fn render_nested_block_multiple_inheritance_no_super() {
121 let mut tera = Tera::default();
122 tera.add_raw_templates(vec![
123 ("index", "{% block content%}INDEX{% endblock content %}"),
124 (
125 "docs",
126 "{% extends \"index\" %}{% block content%}DOCS{% block more %}MORE{% endblock more %}{% endblock content %}",
127 ),
128 ("page", "{% extends \"docs\" %}{% block more %}PAGE{% endblock more %}"),
129 ]).unwrap();
130
131 let result = tera.render("page", &Context::new());
132
133 assert_eq!(result.unwrap(), "DOCSPAGE".to_string());
134}
135
136#[test]
137fn render_super_in_top_block_errors() {
138 let mut tera = Tera::default();
139 tera.add_raw_templates(vec![("index", "{% block content%}{{super()}}{% endblock content %}")])
140 .unwrap();
141
142 let result = tera.render("index", &Context::new());
143 assert!(result.is_err());
144}
145
146// https://github.com/Keats/tera/issues/215
147#[test]
148fn render_super_in_grandchild_without_redefining_works() {
149 let mut tera = Tera::default();
150 tera.add_raw_templates(vec![
151 ("grandparent", "{% block title %}Title{% endblock %}"),
152 (
153 "parent",
154 "{% extends \"grandparent\" %}{% block title %}{{ super() }} - More{% endblock %}",
155 ),
156 ("child", "{% extends \"parent\" %}"),
157 ])
158 .unwrap();
159
160 let result = tera.render("child", &Context::new());
161 assert_eq!(result.unwrap(), "Title - More".to_string());
162}
163
164#[test]
165fn render_super_in_grandchild_without_redefining_in_parent_works() {
166 let mut tera = Tera::default();
167 tera.add_raw_templates(vec![
168 ("grandparent", "{% block title %}Title{% endblock %}"),
169 ("parent", "{% extends \"grandparent\" %}"),
170 ("child", "{% extends \"parent\" %}{% block title %}{{ super() }} - More{% endblock %}"),
171 ])
172 .unwrap();
173
174 let result = tera.render("child", &Context::new());
175 assert_eq!(result.unwrap(), "Title - More".to_string());
176}