]> git.proxmox.com Git - rustc.git/blob - vendor/tera/src/parser/tests/whitespace.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / vendor / tera / src / parser / tests / whitespace.rs
1 use crate::parser::ast::*;
2 use crate::parser::remove_whitespace;
3 use std::collections::HashMap;
4
5 #[test]
6 fn do_nothing_if_unneeded() {
7 let ast = vec![Node::Text("hey ".to_string())];
8 assert_eq!(remove_whitespace(ast.clone(), None), ast);
9 }
10
11 #[test]
12 fn remove_previous_ws_if_single_opening_tag_requires_it() {
13 let ws = WS { left: true, right: false };
14 let ast = vec![
15 Node::Text("hey ".to_string()),
16 Node::ImportMacro(ws, "hey ".to_string(), "ho".to_string()),
17 ];
18
19 assert_eq!(
20 remove_whitespace(ast.clone(), None),
21 vec![
22 Node::Text("hey".to_string()), // it removed the trailing space
23 Node::ImportMacro(ws, "hey ".to_string(), "ho".to_string()),
24 ]
25 );
26 }
27
28 #[test]
29 fn remove_next_ws_if_single_opening_tag_requires_it() {
30 let ws = WS { left: true, right: true };
31 let ast = vec![
32 Node::ImportMacro(ws, "hey ".to_string(), "ho".to_string()),
33 Node::Text(" hey".to_string()),
34 ];
35
36 assert_eq!(
37 remove_whitespace(ast.clone(), None),
38 vec![
39 Node::ImportMacro(ws, "hey ".to_string(), "ho".to_string()),
40 Node::Text("hey".to_string()), // it removed the leading space
41 ]
42 );
43 }
44
45 #[test]
46 fn handle_ws_both_sides_for_raw_tag() {
47 let start_ws = WS { left: true, right: false };
48 let end_ws = WS { left: true, right: true };
49 let ast =
50 vec![Node::Raw(start_ws, " hey ".to_string(), end_ws), Node::Text(" hey".to_string())];
51
52 assert_eq!(
53 remove_whitespace(ast.clone(), None),
54 vec![
55 // it removed only the space at the end
56 Node::Raw(start_ws, " hey".to_string(), end_ws),
57 Node::Text("hey".to_string()),
58 ]
59 );
60 }
61
62 #[test]
63 fn handle_ws_both_sides_for_macro_definitions() {
64 let start_ws = WS { left: true, right: true };
65 let end_ws = WS { left: true, right: true };
66 let ast = vec![Node::MacroDefinition(
67 start_ws,
68 MacroDefinition {
69 name: "something".to_string(),
70 args: HashMap::new(),
71 body: vec![
72 Node::Text("\n ".to_string()),
73 Node::Text("hey".to_string()),
74 Node::Text(" ".to_string()),
75 ],
76 },
77 end_ws,
78 )];
79
80 assert_eq!(
81 remove_whitespace(ast.clone(), None),
82 vec![Node::MacroDefinition(
83 start_ws,
84 MacroDefinition {
85 name: "something".to_string(),
86 args: HashMap::new(),
87 body: vec![Node::Text("hey".to_string())],
88 },
89 end_ws,
90 ),]
91 );
92 }
93
94 #[test]
95 fn handle_ws_both_sides_for_forloop_tag_and_remove_empty_node() {
96 let start_ws = WS { left: true, right: true };
97 let end_ws = WS { left: true, right: true };
98 let ast = vec![
99 Node::Forloop(
100 start_ws,
101 Forloop {
102 key: None,
103 value: "item".to_string(),
104 container: Expr::new(ExprVal::Int(1)),
105 // not valid but we don't care about it here
106 body: vec![Node::Text(" ".to_string()), Node::Text("hey ".to_string())],
107 empty_body: None,
108 },
109 end_ws,
110 ),
111 Node::Text(" hey".to_string()),
112 ];
113
114 assert_eq!(
115 remove_whitespace(ast.clone(), None),
116 vec![
117 Node::Forloop(
118 start_ws,
119 Forloop {
120 key: None,
121 value: "item".to_string(),
122 container: Expr::new(ExprVal::Int(1)),
123 // not valid but we don't care about it here
124 body: vec![Node::Text("hey".to_string())],
125 empty_body: None,
126 },
127 end_ws,
128 ),
129 Node::Text("hey".to_string()),
130 ]
131 );
132 }
133
134 #[test]
135 fn handle_ws_for_if_nodes() {
136 let end_ws = WS { left: false, right: true };
137 let ast = vec![
138 Node::Text("C ".to_string()),
139 Node::If(
140 If {
141 conditions: vec![
142 (
143 WS { left: true, right: true },
144 Expr::new(ExprVal::Int(1)),
145 vec![Node::Text(" a ".to_string())],
146 ),
147 (
148 WS { left: true, right: false },
149 Expr::new(ExprVal::Int(1)),
150 vec![Node::Text(" a ".to_string())],
151 ),
152 (
153 WS { left: true, right: true },
154 Expr::new(ExprVal::Int(1)),
155 vec![Node::Text(" a ".to_string())],
156 ),
157 ],
158 otherwise: None,
159 },
160 end_ws,
161 ),
162 Node::Text(" hey".to_string()),
163 ];
164
165 assert_eq!(
166 remove_whitespace(ast.clone(), None),
167 vec![
168 Node::Text("C".to_string()),
169 Node::If(
170 If {
171 conditions: vec![
172 (
173 WS { left: true, right: true },
174 Expr::new(ExprVal::Int(1)),
175 vec![Node::Text("a".to_string())],
176 ),
177 (
178 WS { left: true, right: false },
179 Expr::new(ExprVal::Int(1)),
180 vec![Node::Text(" a".to_string())],
181 ),
182 (
183 WS { left: true, right: true },
184 Expr::new(ExprVal::Int(1)),
185 vec![Node::Text("a ".to_string())],
186 ),
187 ],
188 otherwise: None,
189 },
190 end_ws,
191 ),
192 Node::Text("hey".to_string()),
193 ]
194 );
195 }
196
197 #[test]
198 fn handle_ws_for_if_nodes_with_else() {
199 let end_ws = WS { left: true, right: true };
200 let ast = vec![
201 Node::Text("C ".to_string()),
202 Node::If(
203 If {
204 conditions: vec![
205 (
206 WS { left: true, right: true },
207 Expr::new(ExprVal::Int(1)),
208 vec![Node::Text(" a ".to_string())],
209 ),
210 (
211 WS { left: true, right: false },
212 Expr::new(ExprVal::Int(1)),
213 vec![Node::Text(" a ".to_string())],
214 ),
215 (
216 WS { left: true, right: true },
217 Expr::new(ExprVal::Int(1)),
218 vec![Node::Text(" a ".to_string())],
219 ),
220 ],
221 otherwise: Some((
222 WS { left: true, right: true },
223 vec![Node::Text(" a ".to_string())],
224 )),
225 },
226 end_ws,
227 ),
228 Node::Text(" hey".to_string()),
229 ];
230
231 assert_eq!(
232 remove_whitespace(ast.clone(), None),
233 vec![
234 Node::Text("C".to_string()),
235 Node::If(
236 If {
237 conditions: vec![
238 (
239 WS { left: true, right: true },
240 Expr::new(ExprVal::Int(1)),
241 vec![Node::Text("a".to_string())],
242 ),
243 (
244 WS { left: true, right: false },
245 Expr::new(ExprVal::Int(1)),
246 vec![Node::Text(" a".to_string())],
247 ),
248 (
249 WS { left: true, right: true },
250 Expr::new(ExprVal::Int(1)),
251 vec![Node::Text("a".to_string())],
252 ),
253 ],
254 otherwise: Some((
255 WS { left: true, right: true },
256 vec![Node::Text("a".to_string())],
257 )),
258 },
259 end_ws,
260 ),
261 Node::Text("hey".to_string()),
262 ]
263 );
264 }