1 /*=============================================================================
2 Copyright (c) 2012 Daniel James
3
4 Use, modification and distribution is subject to the Boost Software
5 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/range/algorithm/find.hpp>
11 #include "files.hpp"
12 #include "fwd.hpp"
13 #include "string_view.hpp"
14
simple_map_tests()15 void simple_map_tests()
16 {
17 quickbook::string_view source("First Line\nSecond Line");
18 quickbook::file_ptr fake_file =
19 new quickbook::file("(fake file)", source, 105u);
20
21 quickbook::string_iterator line1 = fake_file->source().begin();
22 quickbook::string_iterator line1_end =
23 boost::find(fake_file->source(), '\n');
24 quickbook::string_iterator line2 = line1_end + 1;
25 quickbook::string_iterator line2_end = fake_file->source().end();
26
27 quickbook::mapped_file_builder builder;
28
29 { // Empty test
30 builder.start(fake_file);
31 BOOST_TEST(builder.empty());
32 quickbook::file_ptr f1 = builder.release();
33 BOOST_TEST(f1->source().empty());
34 }
35
36 { // Add full text
37 builder.start(fake_file);
38 builder.add(quickbook::string_view(line1, line2_end - line1));
39 quickbook::file_ptr f1 = builder.release();
40 BOOST_TEST_EQ(f1->source(), source);
41 BOOST_TEST_EQ(
42 f1->position_of(f1->source().begin()),
43 quickbook::file_position(1, 1));
44 BOOST_TEST_EQ(
45 f1->position_of(f1->source().begin() + 2),
46 quickbook::file_position(1, 3));
47 BOOST_TEST_EQ(
48 f1->position_of(f1->source().begin() + (line1_end - line1)),
49 quickbook::file_position(1, line1_end - line1 + 1));
50 BOOST_TEST_EQ(
51 f1->position_of(f1->source().begin() + (line2 - line1)),
52 quickbook::file_position(2, 1));
53 BOOST_TEST_EQ(
54 f1->position_of(f1->source().end()),
55 fake_file->position_of(fake_file->source().end()));
56 }
57
58 { // Add first line
59 builder.start(fake_file);
60 builder.add(quickbook::string_view(line1, line1_end - line1));
61 quickbook::file_ptr f1 = builder.release();
62 BOOST_TEST_EQ(
63 f1->source(),
64 quickbook::string_view(source.begin(), line1_end - line1));
65 BOOST_TEST_EQ(
66 f1->position_of(f1->source().begin()),
67 quickbook::file_position(1, 1));
68 BOOST_TEST_EQ(
69 f1->position_of(f1->source().begin() + 2),
70 quickbook::file_position(1, 3));
71 BOOST_TEST_EQ(
72 f1->position_of(f1->source().end()),
73 quickbook::file_position(1, line1_end - line1 + 1));
74 }
75
76 { // Add second line
77 builder.start(fake_file);
78 builder.add(quickbook::string_view(line2, line2_end - line2));
79 quickbook::file_ptr f1 = builder.release();
80 BOOST_TEST_EQ(f1->source(), quickbook::string_view("Second Line"));
81 BOOST_TEST_EQ(
82 f1->position_of(f1->source().begin()),
83 quickbook::file_position(2, 1));
84 BOOST_TEST_EQ(
85 f1->position_of(f1->source().begin() + 2),
86 quickbook::file_position(2, 3));
87 BOOST_TEST_EQ(
88 f1->position_of(f1->source().end()),
89 quickbook::file_position(2, line2_end - line2 + 1));
90 }
91
92 { // Out of order
93 builder.start(fake_file);
94 builder.add(quickbook::string_view(line2, line2_end - line2));
95 builder.add(quickbook::string_view(line1_end, 1));
96 builder.add(quickbook::string_view(line1, line1_end - line1));
97 quickbook::file_ptr f1 = builder.release();
98 BOOST_TEST_EQ(
99 f1->source(), quickbook::string_view("Second Line\nFirst Line"));
100
101 BOOST_TEST_EQ(
102 f1->position_of(f1->source().begin()),
103 quickbook::file_position(2, 1));
104 BOOST_TEST_EQ(
105 f1->position_of(f1->source().begin() + 2),
106 quickbook::file_position(2, 3));
107 BOOST_TEST_EQ(
108 f1->position_of(f1->source().begin() + (line2_end - line2 - 1)),
109 quickbook::file_position(2, line2_end - line2));
110 BOOST_TEST_EQ(
111 f1->position_of(f1->source().begin() + (line2_end - line2)),
112 quickbook::file_position(1, (line1_end - line1 + 1)));
113 BOOST_TEST_EQ(
114 f1->position_of(f1->source().begin() + (line2_end - line2 + 1)),
115 quickbook::file_position(1, 1));
116 BOOST_TEST_EQ(
117 f1->position_of(f1->source().end()),
118 quickbook::file_position(1, line1_end - line1 + 1));
119 }
120
121 { // Repeated text
122 builder.start(fake_file);
123 builder.add(quickbook::string_view(line2, line2_end - line2));
124 builder.add(quickbook::string_view(line1_end, 1));
125 builder.add(quickbook::string_view(line2, line2_end - line2));
126 quickbook::file_ptr f1 = builder.release();
127 BOOST_TEST_EQ(
128 f1->source(), quickbook::string_view("Second Line\nSecond Line"));
129
130 BOOST_TEST_EQ(
131 f1->position_of(f1->source().begin()),
132 quickbook::file_position(2, 1));
133 BOOST_TEST_EQ(
134 f1->position_of(f1->source().begin() + 2),
135 quickbook::file_position(2, 3));
136 BOOST_TEST_EQ(
137 f1->position_of(f1->source().begin() + (line2_end - line2 - 1)),
138 quickbook::file_position(2, line2_end - line2));
139 BOOST_TEST_EQ(
140 f1->position_of(f1->source().begin() + (line2_end - line2)),
141 quickbook::file_position(1, (line1_end - line1 + 1)));
142 BOOST_TEST_EQ(
143 f1->position_of(f1->source().begin() + (line2_end - line2 + 1)),
144 quickbook::file_position(2, 1));
145 BOOST_TEST_EQ(
146 f1->position_of(f1->source().end()),
147 quickbook::file_position(2, line2_end - line2 + 1));
148 }
149
150 { // Generated text
151 builder.start(fake_file);
152 builder.add_at_pos("------\n", line1);
153 builder.add(quickbook::string_view(line1, line1_end - line1));
154 builder.add_at_pos("\n------\n", line1_end);
155 quickbook::file_ptr f1 = builder.release();
156 BOOST_TEST_EQ(
157 f1->source(),
158 quickbook::string_view("------\nFirst Line\n------\n"));
159
160 quickbook::string_iterator newline = boost::find(f1->source(), '\n');
161
162 BOOST_TEST_EQ(
163 f1->position_of(f1->source().begin()),
164 quickbook::file_position(1, 1));
165 BOOST_TEST_EQ(
166 f1->position_of(f1->source().begin() + 2),
167 quickbook::file_position(1, 1));
168 BOOST_TEST_EQ(f1->position_of(newline), quickbook::file_position(1, 1));
169 BOOST_TEST_EQ(
170 f1->position_of(newline + 1), quickbook::file_position(1, 1));
171 BOOST_TEST_EQ(
172 f1->position_of(newline + 2), quickbook::file_position(1, 2));
173 BOOST_TEST_EQ(
174 f1->position_of(newline + (line1_end - line1)),
175 quickbook::file_position(1, line1_end - line1));
176 BOOST_TEST_EQ(
177 f1->position_of(newline + (line1_end - line1 + 1)),
178 quickbook::file_position(1, line1_end - line1 + 1));
179 BOOST_TEST_EQ(
180 f1->position_of(newline + (line1_end - line1 + 2)),
181 quickbook::file_position(1, line1_end - line1 + 1));
182 BOOST_TEST_EQ(
183 f1->position_of(f1->source().end()),
184 quickbook::file_position(1, line1_end - line1 + 1));
185 }
186 }
187
indented_map_tests()188 void indented_map_tests()
189 {
190 quickbook::string_view source(" Code line1\n"
191 " Code line2\n");
192 quickbook::file_ptr fake_file =
193 new quickbook::file("(fake file)", source, 105u);
194
195 quickbook::mapped_file_builder builder;
196
197 {
198 builder.start(fake_file);
199 builder.unindent_and_add(fake_file->source());
200 quickbook::file_ptr f1 = builder.release();
201 BOOST_TEST_EQ(
202 f1->source(), quickbook::string_view("Code line1\nCode line2\n"));
203 BOOST_TEST_EQ(
204 f1->position_of(f1->source().begin()),
205 quickbook::file_position(1, 4));
206 BOOST_TEST_EQ(
207 f1->position_of(f1->source().begin() + 1),
208 quickbook::file_position(1, 5));
209 BOOST_TEST_EQ(
210 f1->position_of(f1->source().begin() + 5),
211 quickbook::file_position(1, 9));
212 BOOST_TEST_EQ(
213 f1->position_of(f1->source().begin() + 10),
214 quickbook::file_position(1, 14));
215 BOOST_TEST_EQ(
216 f1->position_of(f1->source().begin() + 11),
217 quickbook::file_position(2, 4));
218 BOOST_TEST_EQ(
219 f1->position_of(f1->source().end()),
220 quickbook::file_position(3, 1));
221 }
222
223 {
224 builder.start(fake_file);
225 {
226 quickbook::mapped_file_builder builder2;
227 builder2.start(fake_file);
228 builder2.unindent_and_add(fake_file->source());
229 builder.add(builder2);
230 }
231 quickbook::file_ptr f1 = builder.release();
232
233 BOOST_TEST_EQ(
234 f1->source(), quickbook::string_view("Code line1\nCode line2\n"));
235 BOOST_TEST_EQ(
236 f1->position_of(f1->source().begin()),
237 quickbook::file_position(1, 4));
238 BOOST_TEST_EQ(
239 f1->position_of(f1->source().begin() + 1),
240 quickbook::file_position(1, 5));
241 BOOST_TEST_EQ(
242 f1->position_of(f1->source().begin() + 5),
243 quickbook::file_position(1, 9));
244 BOOST_TEST_EQ(
245 f1->position_of(f1->source().begin() + 10),
246 quickbook::file_position(1, 14));
247 BOOST_TEST_EQ(
248 f1->position_of(f1->source().begin() + 11),
249 quickbook::file_position(2, 4));
250 BOOST_TEST_EQ(
251 f1->position_of(f1->source().end()),
252 quickbook::file_position(3, 1));
253 }
254
255 {
256 builder.start(fake_file);
257 builder.unindent_and_add(quickbook::string_view(
258 fake_file->source().begin() + 3,
259 fake_file->source().end() - (fake_file->source().begin() + 3)));
260 quickbook::file_ptr f1 = builder.release();
261 BOOST_TEST_EQ(
262 f1->source(),
263 quickbook::string_view("Code line1\n Code line2\n"));
264 BOOST_TEST_EQ(
265 f1->position_of(f1->source().begin()),
266 quickbook::file_position(1, 4));
267 BOOST_TEST_EQ(
268 f1->position_of(f1->source().begin() + 1),
269 quickbook::file_position(1, 5));
270 BOOST_TEST_EQ(
271 f1->position_of(f1->source().begin() + 5),
272 quickbook::file_position(1, 9));
273 BOOST_TEST_EQ(
274 f1->position_of(f1->source().begin() + 10),
275 quickbook::file_position(1, 14));
276 BOOST_TEST_EQ(
277 f1->position_of(f1->source().begin() + 11),
278 quickbook::file_position(2, 1));
279 BOOST_TEST_EQ(
280 f1->position_of(f1->source().end()),
281 quickbook::file_position(3, 1));
282 }
283 }
284
indented_map_tests2()285 void indented_map_tests2()
286 {
287 quickbook::string_view source(" Code line1\n"
288 "\n"
289 " Code line2\n");
290 quickbook::file_ptr fake_file =
291 new quickbook::file("(fake file)", source, 105u);
292
293 quickbook::mapped_file_builder builder;
294
295 {
296 builder.start(fake_file);
297 builder.unindent_and_add(fake_file->source());
298 quickbook::file_ptr f1 = builder.release();
299 BOOST_TEST_EQ(
300 f1->source(), quickbook::string_view("Code line1\n\nCode line2\n"));
301 BOOST_TEST_EQ(
302 f1->position_of(f1->source().begin()),
303 quickbook::file_position(1, 4));
304 BOOST_TEST_EQ(
305 f1->position_of(f1->source().begin() + 1),
306 quickbook::file_position(1, 5));
307 BOOST_TEST_EQ(
308 f1->position_of(f1->source().begin() + 5),
309 quickbook::file_position(1, 9));
310 BOOST_TEST_EQ(
311 f1->position_of(f1->source().begin() + 10),
312 quickbook::file_position(1, 14));
313 BOOST_TEST_EQ(
314 f1->position_of(f1->source().begin() + 11),
315 quickbook::file_position(2, 1));
316 BOOST_TEST_EQ(
317 f1->position_of(f1->source().begin() + 12),
318 quickbook::file_position(3, 4));
319 }
320 }
321
indented_map_leading_blanks_test()322 void indented_map_leading_blanks_test()
323 {
324 quickbook::mapped_file_builder builder;
325
326 {
327 quickbook::string_view source("\n\n Code line1\n");
328 quickbook::file_ptr fake_file =
329 new quickbook::file("(fake file)", source, 105u);
330 builder.start(fake_file);
331 builder.unindent_and_add(fake_file->source());
332 quickbook::file_ptr f1 = builder.release();
333 BOOST_TEST_EQ(f1->source(), quickbook::string_view("Code line1\n"));
334 }
335
336 {
337 quickbook::string_view source(" \n \n Code line1\n");
338 quickbook::file_ptr fake_file =
339 new quickbook::file("(fake file)", source, 105u);
340 builder.start(fake_file);
341 builder.unindent_and_add(fake_file->source());
342 quickbook::file_ptr f1 = builder.release();
343 BOOST_TEST_EQ(f1->source(), quickbook::string_view("Code line1\n"));
344 }
345
346 {
347 quickbook::string_view source(" Code line1\n \n Code line2");
348 quickbook::file_ptr fake_file =
349 new quickbook::file("(fake file)", source, 105u);
350 builder.start(fake_file);
351 builder.unindent_and_add(fake_file->source());
352 quickbook::file_ptr f1 = builder.release();
353 BOOST_TEST_EQ(
354 f1->source(), quickbook::string_view("Code line1\n\nCode line2"));
355 }
356 }
357
indented_map_trailing_blanks_test()358 void indented_map_trailing_blanks_test()
359 {
360 quickbook::mapped_file_builder builder;
361
362 {
363 quickbook::string_view source("\n\n Code line1\n ");
364 quickbook::file_ptr fake_file =
365 new quickbook::file("(fake file)", source, 105u);
366 builder.start(fake_file);
367 builder.unindent_and_add(fake_file->source());
368 quickbook::file_ptr f1 = builder.release();
369 BOOST_TEST_EQ(f1->source(), quickbook::string_view("Code line1\n"));
370 }
371
372 {
373 quickbook::string_view source(" \n \n Code line1\n ");
374 quickbook::file_ptr fake_file =
375 new quickbook::file("(fake file)", source, 105u);
376 builder.start(fake_file);
377 builder.unindent_and_add(fake_file->source());
378 quickbook::file_ptr f1 = builder.release();
379 BOOST_TEST_EQ(f1->source(), quickbook::string_view("Code line1\n "));
380 }
381
382 {
383 quickbook::string_view source(" Code line1\n \n Code line2\n ");
384 quickbook::file_ptr fake_file =
385 new quickbook::file("(fake file)", source, 105u);
386 builder.start(fake_file);
387 builder.unindent_and_add(fake_file->source());
388 quickbook::file_ptr f1 = builder.release();
389 BOOST_TEST_EQ(
390 f1->source(), quickbook::string_view("Code line1\n\nCode line2\n"));
391 }
392 }
393
indented_map_mixed_test()394 void indented_map_mixed_test()
395 {
396 quickbook::mapped_file_builder builder;
397
398 {
399 quickbook::string_view source("\tCode line 1\n Code line 2\n\t "
400 "Code line 3\n \tCode line 4");
401 quickbook::file_ptr fake_file =
402 new quickbook::file("(fake file)", source, 105u);
403 builder.start(fake_file);
404 builder.unindent_and_add(fake_file->source());
405 quickbook::file_ptr f1 = builder.release();
406 BOOST_TEST_EQ(
407 f1->source(),
408 quickbook::string_view(
409 "Code line 1\nCode line 2\n Code line 3\n Code line 4"));
410 }
411
412 {
413 quickbook::string_view source(" Code line 1\n\tCode line 2");
414 quickbook::file_ptr fake_file =
415 new quickbook::file("(fake file)", source, 105u);
416 builder.start(fake_file);
417 builder.unindent_and_add(fake_file->source());
418 quickbook::file_ptr f1 = builder.release();
419 BOOST_TEST_EQ(
420 f1->source(), quickbook::string_view("Code line 1\n Code line 2"));
421 }
422
423 {
424 quickbook::string_view source(" Code line 1\n \tCode line 2");
425 quickbook::file_ptr fake_file =
426 new quickbook::file("(fake file)", source, 105u);
427 builder.start(fake_file);
428 builder.unindent_and_add(fake_file->source());
429 quickbook::file_ptr f1 = builder.release();
430 BOOST_TEST_EQ(
431 f1->source(), quickbook::string_view("Code line 1\n\tCode line 2"));
432 }
433 }
434
main()435 int main()
436 {
437 simple_map_tests();
438 indented_map_tests();
439 indented_map_tests2();
440 indented_map_leading_blanks_test();
441 indented_map_trailing_blanks_test();
442 indented_map_mixed_test();
443 return boost::report_errors();
444 }
445