rustla/parser/tests/
test_block_quotes.rs1use super::*;
8
9#[cfg(test)]
10#[test]
11fn block_quote_01() {
12 let src =
13"
14 This is a paragraph inside a block quote.
15 Indentation determines quotation level.
16
17 This second paragraph is inside a nested block quote,
18 as this has more indentation the the above paragraph.
19
20 This paragraph is again at the first level of quotation.
21
22"
23 .lines()
24 .map(|s| s.to_string())
25 .collect::<Vec<String>>();
26
27 let mut doctree = DocTree::new(PathBuf::from("test"));
28
29 let mut parser = Parser::new(src, doctree, 0, 0, State::Body, 0);
30
31 doctree = parser.parse().unwrap_tree();
32 doctree = doctree.walk_to_root();
33 doctree.print_tree();
34
35 match doctree
36 .shared_child(0).unwrap().shared_data() {
37 TreeNodeType::BlockQuote { .. } => {}
38 _ => panic!(),
39 }
40
41 match doctree
42 .shared_child(0).unwrap()
43 .shared_child(0).unwrap().shared_data() {
44 TreeNodeType::Paragraph { .. } => {}
45 _ => panic!(),
46 }
47
48 match doctree
49 .shared_child(0).unwrap()
50 .shared_child(1).unwrap().shared_data() {
51 TreeNodeType::BlockQuote { .. } => {}
52 _ => panic!(),
53 }
54
55 match doctree
56 .shared_child(0).unwrap()
57 .shared_child(1).unwrap()
58 .shared_child(0).unwrap()
59 .shared_data()
60 {
61 TreeNodeType::Paragraph { .. } => {}
62 _ => panic!(),
63 }
64
65 match doctree
66 .shared_child(0).unwrap()
67 .shared_child(2).unwrap().shared_data() {
68 TreeNodeType::Paragraph { .. } => {}
69 _ => panic!(),
70 }
71}
72
73#[test]
74fn block_quote_02() {
75 let src =
76"
77 This is a paragraph inside a block quote.
78 Indentation determines quotation level.
79 The following attribution ends this block quote
80
81 -- Santtu Söderholm
82
83 This paragraph starts a new block quote at the same level
84 as the previous one (as in it is not nested).
85
86"
87 .lines()
88 .map(|s| s.to_string())
89 .collect::<Vec<String>>();
90
91 let mut doctree = DocTree::new(PathBuf::from("test"));
92
93 let mut parser = Parser::new(src, doctree, 0, 0, State::Body, 0);
94
95 doctree = parser.parse().unwrap_tree();
96 doctree = doctree.walk_to_root();
97 doctree.print_tree();
98
99 match doctree
100 .shared_child(0).unwrap().shared_data() {
101 TreeNodeType::BlockQuote { .. } => {}
102 _ => panic!(),
103 }
104
105 match doctree
106 .shared_child(0).unwrap()
107 .shared_child(0).unwrap().shared_data() {
108 TreeNodeType::Paragraph { .. } => {}
109 _ => panic!(),
110 }
111
112 match doctree
113 .shared_child(0).unwrap()
114 .shared_child(1).unwrap().shared_data() {
115 TreeNodeType::Attribution { raw_text } => {
116 if raw_text.as_str() != "Santtu Söderholm" {
117 panic!()
118 }
119 }
120 _ => panic!(),
121 }
122
123 match doctree
124 .shared_child(1).unwrap().shared_data() {
125 TreeNodeType::BlockQuote { .. } => {}
126 _ => panic!(),
127 }
128
129 match doctree
130 .shared_child(1).unwrap()
131 .shared_child(0).unwrap().shared_data() {
132 TreeNodeType::Paragraph { .. } => {}
133 _ => panic!(),
134 }
135}
136
137#[test]
138fn block_quote_03() {
139 let src =
140"
141 This is a paragraph inside a block quote.
142 Indentation determines quotation level.
143 The below attribution does not end this block quote,
144 as it is indented relative to this block quote level.
145
146 -- Santtu Söderholm inside a nested block quote
147
148 This paragraph continues the first block quote,
149 as the above attribution ends the nested block quote
150 it also ended up triggering.
151
152"
153 .lines()
154 .map(|s| s.to_string())
155 .collect::<Vec<String>>();
156
157 let mut doctree = DocTree::new(PathBuf::from("test"));
158
159 let mut parser = Parser::new(src, doctree, 0, 0, State::Body, 0);
160
161 doctree = parser.parse().unwrap_tree();
162 doctree = doctree.walk_to_root();
163 doctree.print_tree();
164
165 match doctree
166 .shared_child(0).unwrap().shared_data() {
167 TreeNodeType::BlockQuote { .. } => {}
168 _ => panic!(),
169 }
170 match doctree
171 .shared_child(0).unwrap()
172 .shared_child(0).unwrap().shared_data() {
173 TreeNodeType::Paragraph { .. } => {}
174 _ => panic!(),
175 }
176 match doctree
177 .shared_child(0).unwrap()
178 .shared_child(1).unwrap().shared_data() {
179 TreeNodeType::BlockQuote { .. } => {}
180 _ => panic!(),
181 }
182 match doctree
183 .shared_child(0).unwrap()
184 .shared_child(1).unwrap()
185 .shared_child(0).unwrap()
186 .shared_data()
187 {
188 TreeNodeType::Attribution { raw_text } => {
189 if raw_text.as_str() != "Santtu Söderholm inside a nested block quote" {
190 panic!()
191 }
192 }
193 _ => panic!(),
194 }
195 match doctree
196 .shared_child(0).unwrap()
197 .shared_child(2).unwrap().shared_data() {
198 TreeNodeType::Paragraph { .. } => {}
199 _ => panic!(),
200 }
201}
202
203#[test]
204fn block_quote_04() {
205 let src =
206"
207 Below is a multi-line attribution
208
209 -- Santtu Söderholm
210 and company
211
212The attribution will be combined into a single line,
213at least for now.
214"
215 .lines()
216 .map(|s| s.to_string())
217 .collect::<Vec<String>>();
218
219 let mut doctree = DocTree::new(PathBuf::from("test"));
220
221 let mut parser = Parser::new(src, doctree, 0, 0, State::Body, 0);
222
223 doctree = parser.parse().unwrap_tree();
224 doctree = doctree.walk_to_root();
225 doctree.print_tree();
226
227 match doctree
228 .shared_child(0).unwrap().shared_data() {
229 TreeNodeType::BlockQuote { .. } => {}
230 _ => panic!(),
231 }
232
233 match doctree
234 .shared_child(0).unwrap()
235 .shared_child(1).unwrap().shared_data() {
236 TreeNodeType::Attribution { raw_text } => {
237 if raw_text.as_str() != "Santtu Söderholm and company" {
238 panic!()
239 }
240 }
241 _ => panic!(),
242 }
243
244 match doctree
245 .shared_child(1).unwrap().shared_data() {
246 TreeNodeType::Paragraph { .. } => {}
247 _ => panic!(),
248 }
249}
250
251#[test]
252fn block_quote_05() {
253 let src =
254"
255 Below is a multi-line attribution
256
257 -- Santtu Söderholm
258 and company with too little indentation on the second line.
259 This indented block actually ends up inside another less indented
260 block quote as a paragraph.
261
262"
263 .lines()
264 .map(|s| s.to_string())
265 .collect::<Vec<String>>();
266
267 let mut doctree = DocTree::new(PathBuf::from("test"));
268
269 let mut parser = Parser::new(src, doctree, 0, 0, State::Body, 0);
270
271 doctree = parser.parse().unwrap_tree();
272 doctree = doctree.walk_to_root();
273 doctree.print_tree();
274
275 match doctree
276 .shared_child(0).unwrap().shared_data() {
277 TreeNodeType::BlockQuote { .. } => {}
278 _ => panic!(),
279 }
280
281 match doctree
282 .shared_child(0).unwrap()
283 .shared_child(1).unwrap().shared_data() {
284 TreeNodeType::Attribution { raw_text } => assert_eq!(raw_text, "Santtu Söderholm"),
285 _ => panic!(),
286 }
287
288 match doctree
289 .shared_child(1).unwrap().shared_data() {
290 TreeNodeType::BlockQuote { .. } => {}
291 _ => panic!(),
292 }
293
294 match doctree
295 .shared_child(1).unwrap()
296 .shared_child(0).unwrap().shared_data() {
297 TreeNodeType::Paragraph { .. } => {}
298 _ => panic!(),
299 }
300}