1 use crate::parser
::ast
::*;
2 use crate::parser
::remove_whitespace
;
3 use std
::collections
::HashMap
;
6 fn do_nothing_if_unneeded() {
7 let ast
= vec
![Node
::Text("hey ".to_string())];
8 assert_eq
!(remove_whitespace(ast
.clone(), None
), ast
);
12 fn remove_previous_ws_if_single_opening_tag_requires_it() {
13 let ws
= WS { left: true, right: false }
;
15 Node
::Text("hey ".to_string()),
16 Node
::ImportMacro(ws
, "hey ".to_string(), "ho".to_string()),
20 remove_whitespace(ast
.clone(), None
),
22 Node
::Text("hey".to_string()), // it removed the trailing space
23 Node
::ImportMacro(ws
, "hey ".to_string(), "ho".to_string()),
29 fn remove_next_ws_if_single_opening_tag_requires_it() {
30 let ws
= WS { left: true, right: true }
;
32 Node
::ImportMacro(ws
, "hey ".to_string(), "ho".to_string()),
33 Node
::Text(" hey".to_string()),
37 remove_whitespace(ast
.clone(), None
),
39 Node
::ImportMacro(ws
, "hey ".to_string(), "ho".to_string()),
40 Node
::Text("hey".to_string()), // it removed the leading space
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 }
;
50 vec
![Node
::Raw(start_ws
, " hey ".to_string(), end_ws
), Node
::Text(" hey".to_string())];
53 remove_whitespace(ast
.clone(), None
),
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()),
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(
69 name
: "something".to_string(),
72 Node
::Text("\n ".to_string()),
73 Node
::Text("hey".to_string()),
74 Node
::Text(" ".to_string()),
81 remove_whitespace(ast
.clone(), None
),
82 vec
![Node
::MacroDefinition(
85 name
: "something".to_string(),
87 body
: vec
![Node
::Text("hey".to_string())],
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 }
;
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())],
111 Node
::Text(" hey".to_string()),
115 remove_whitespace(ast
.clone(), 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())],
129 Node
::Text("hey".to_string()),
135 fn handle_ws_for_if_nodes() {
136 let end_ws
= WS { left: false, right: true }
;
138 Node
::Text("C ".to_string()),
143 WS { left: true, right: true }
,
144 Expr
::new(ExprVal
::Int(1)),
145 vec
![Node
::Text(" a ".to_string())],
148 WS { left: true, right: false }
,
149 Expr
::new(ExprVal
::Int(1)),
150 vec
![Node
::Text(" a ".to_string())],
153 WS { left: true, right: true }
,
154 Expr
::new(ExprVal
::Int(1)),
155 vec
![Node
::Text(" a ".to_string())],
162 Node
::Text(" hey".to_string()),
166 remove_whitespace(ast
.clone(), None
),
168 Node
::Text("C".to_string()),
173 WS { left: true, right: true }
,
174 Expr
::new(ExprVal
::Int(1)),
175 vec
![Node
::Text("a".to_string())],
178 WS { left: true, right: false }
,
179 Expr
::new(ExprVal
::Int(1)),
180 vec
![Node
::Text(" a".to_string())],
183 WS { left: true, right: true }
,
184 Expr
::new(ExprVal
::Int(1)),
185 vec
![Node
::Text("a ".to_string())],
192 Node
::Text("hey".to_string()),
198 fn handle_ws_for_if_nodes_with_else() {
199 let end_ws
= WS { left: true, right: true }
;
201 Node
::Text("C ".to_string()),
206 WS { left: true, right: true }
,
207 Expr
::new(ExprVal
::Int(1)),
208 vec
![Node
::Text(" a ".to_string())],
211 WS { left: true, right: false }
,
212 Expr
::new(ExprVal
::Int(1)),
213 vec
![Node
::Text(" a ".to_string())],
216 WS { left: true, right: true }
,
217 Expr
::new(ExprVal
::Int(1)),
218 vec
![Node
::Text(" a ".to_string())],
222 WS { left: true, right: true }
,
223 vec
![Node
::Text(" a ".to_string())],
228 Node
::Text(" hey".to_string()),
232 remove_whitespace(ast
.clone(), None
),
234 Node
::Text("C".to_string()),
239 WS { left: true, right: true }
,
240 Expr
::new(ExprVal
::Int(1)),
241 vec
![Node
::Text("a".to_string())],
244 WS { left: true, right: false }
,
245 Expr
::new(ExprVal
::Int(1)),
246 vec
![Node
::Text(" a".to_string())],
249 WS { left: true, right: true }
,
250 Expr
::new(ExprVal
::Int(1)),
251 vec
![Node
::Text("a".to_string())],
255 WS { left: true, right: true }
,
256 vec
![Node
::Text("a".to_string())],
261 Node
::Text("hey".to_string()),