1 /*=============================================================================
2 Copyright (c) 2012 Daniel James
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 =============================================================================*/
11 #include "string_view.hpp"
12 #include <boost/detail/lightweight_test.hpp>
13 #include <boost/range/algorithm/find.hpp>
15 void simple_map_tests()
17 quickbook::string_view
source("First Line\nSecond Line");
18 quickbook::file_ptr fake_file
= new quickbook::file(
19 "(fake file)", source
, 105u);
21 quickbook::string_iterator line1
= fake_file
->source().begin();
22 quickbook::string_iterator line1_end
= boost::find(fake_file
->source(), '\n');
23 quickbook::string_iterator line2
= line1_end
+ 1;
24 quickbook::string_iterator line2_end
= fake_file
->source().end();
26 quickbook::mapped_file_builder builder
;
29 builder
.start(fake_file
);
30 BOOST_TEST(builder
.empty());
31 quickbook::file_ptr f1
= builder
.release();
32 BOOST_TEST(f1
->source().empty());
36 builder
.start(fake_file
);
37 builder
.add(quickbook::string_view(line1
, line2_end
- line1
));
38 quickbook::file_ptr f1
= builder
.release();
39 BOOST_TEST_EQ(f1
->source(), source
);
40 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
41 quickbook::file_position(1,1));
42 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 2),
43 quickbook::file_position(1,3));
44 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line1_end
- line1
)),
45 quickbook::file_position(1,line1_end
- line1
+ 1));
46 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line2
- line1
)),
47 quickbook::file_position(2,1));
48 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
49 fake_file
->position_of(fake_file
->source().end()));
53 builder
.start(fake_file
);
54 builder
.add(quickbook::string_view(line1
, line1_end
- line1
));
55 quickbook::file_ptr f1
= builder
.release();
56 BOOST_TEST_EQ(f1
->source(),
57 quickbook::string_view(source
.begin(), line1_end
- line1
));
58 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
59 quickbook::file_position(1,1));
60 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 2),
61 quickbook::file_position(1,3));
62 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
63 quickbook::file_position(1,line1_end
- line1
+ 1));
67 builder
.start(fake_file
);
68 builder
.add(quickbook::string_view(line2
, line2_end
- line2
));
69 quickbook::file_ptr f1
= builder
.release();
70 BOOST_TEST_EQ(f1
->source(), quickbook::string_view("Second Line"));
71 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
72 quickbook::file_position(2,1));
73 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 2),
74 quickbook::file_position(2,3));
75 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
76 quickbook::file_position(2,line2_end
- line2
+ 1));
80 builder
.start(fake_file
);
81 builder
.add(quickbook::string_view(line2
, line2_end
- line2
));
82 builder
.add(quickbook::string_view(line1_end
, 1));
83 builder
.add(quickbook::string_view(line1
, line1_end
- line1
));
84 quickbook::file_ptr f1
= builder
.release();
85 BOOST_TEST_EQ(f1
->source(),
86 quickbook::string_view("Second Line\nFirst Line"));
88 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
89 quickbook::file_position(2,1));
90 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 2),
91 quickbook::file_position(2,3));
92 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line2_end
- line2
- 1)),
93 quickbook::file_position(2,line2_end
- line2
));
94 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line2_end
- line2
)),
95 quickbook::file_position(1,(line1_end
- line1
+ 1)));
96 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line2_end
- line2
+ 1)),
97 quickbook::file_position(1,1));
98 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
99 quickbook::file_position(1,line1_end
- line1
+ 1));
103 builder
.start(fake_file
);
104 builder
.add(quickbook::string_view(line2
, line2_end
- line2
));
105 builder
.add(quickbook::string_view(line1_end
, 1));
106 builder
.add(quickbook::string_view(line2
, line2_end
- line2
));
107 quickbook::file_ptr f1
= builder
.release();
108 BOOST_TEST_EQ(f1
->source(),
109 quickbook::string_view("Second Line\nSecond Line"));
111 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
112 quickbook::file_position(2,1));
113 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 2),
114 quickbook::file_position(2,3));
115 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line2_end
- line2
- 1)),
116 quickbook::file_position(2,line2_end
- line2
));
117 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line2_end
- line2
)),
118 quickbook::file_position(1,(line1_end
- line1
+ 1)));
119 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + (line2_end
- line2
+ 1)),
120 quickbook::file_position(2,1));
121 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
122 quickbook::file_position(2,line2_end
- line2
+ 1));
127 builder
.start(fake_file
);
128 builder
.add_at_pos("------\n", line1
);
129 builder
.add(quickbook::string_view(line1
, line1_end
- line1
));
130 builder
.add_at_pos("\n------\n", line1_end
);
131 quickbook::file_ptr f1
= builder
.release();
132 BOOST_TEST_EQ(f1
->source(),
133 quickbook::string_view("------\nFirst Line\n------\n"));
135 quickbook::string_iterator newline
= boost::find(f1
->source(), '\n');
137 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
138 quickbook::file_position(1,1));
139 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 2),
140 quickbook::file_position(1,1));
141 BOOST_TEST_EQ(f1
->position_of(newline
),
142 quickbook::file_position(1,1));
143 BOOST_TEST_EQ(f1
->position_of(newline
+ 1),
144 quickbook::file_position(1,1));
145 BOOST_TEST_EQ(f1
->position_of(newline
+ 2),
146 quickbook::file_position(1,2));
147 BOOST_TEST_EQ(f1
->position_of(newline
+ (line1_end
- line1
)),
148 quickbook::file_position(1,line1_end
- line1
));
149 BOOST_TEST_EQ(f1
->position_of(newline
+ (line1_end
- line1
+ 1)),
150 quickbook::file_position(1,line1_end
- line1
+ 1));
151 BOOST_TEST_EQ(f1
->position_of(newline
+ (line1_end
- line1
+ 2)),
152 quickbook::file_position(1,line1_end
- line1
+ 1));
153 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
154 quickbook::file_position(1,line1_end
- line1
+ 1));
158 void indented_map_tests()
160 quickbook::string_view
source(
163 quickbook::file_ptr fake_file
= new quickbook::file(
164 "(fake file)", source
, 105u);
166 quickbook::mapped_file_builder builder
;
169 builder
.start(fake_file
);
170 builder
.unindent_and_add(fake_file
->source());
171 quickbook::file_ptr f1
= builder
.release();
172 BOOST_TEST_EQ(f1
->source(),
173 quickbook::string_view("Code line1\nCode line2\n"));
174 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
175 quickbook::file_position(1,4));
176 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 1),
177 quickbook::file_position(1,5));
178 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 5),
179 quickbook::file_position(1,9));
180 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 10),
181 quickbook::file_position(1,14));
182 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 11),
183 quickbook::file_position(2,4));
184 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
185 quickbook::file_position(3,1));
189 builder
.start(fake_file
);
191 quickbook::mapped_file_builder builder2
;
192 builder2
.start(fake_file
);
193 builder2
.unindent_and_add(fake_file
->source());
194 builder
.add(builder2
);
196 quickbook::file_ptr f1
= builder
.release();
198 BOOST_TEST_EQ(f1
->source(),
199 quickbook::string_view("Code line1\nCode line2\n"));
200 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
201 quickbook::file_position(1,4));
202 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 1),
203 quickbook::file_position(1,5));
204 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 5),
205 quickbook::file_position(1,9));
206 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 10),
207 quickbook::file_position(1,14));
208 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 11),
209 quickbook::file_position(2,4));
210 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
211 quickbook::file_position(3,1));
215 builder
.start(fake_file
);
216 builder
.unindent_and_add(quickbook::string_view(
217 fake_file
->source().begin() + 3,
218 fake_file
->source().end() - (fake_file
->source().begin() + 3)));
219 quickbook::file_ptr f1
= builder
.release();
220 BOOST_TEST_EQ(f1
->source(),
221 quickbook::string_view("Code line1\n Code line2\n"));
222 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
223 quickbook::file_position(1,4));
224 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 1),
225 quickbook::file_position(1,5));
226 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 5),
227 quickbook::file_position(1,9));
228 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 10),
229 quickbook::file_position(1,14));
230 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 11),
231 quickbook::file_position(2,1));
232 BOOST_TEST_EQ(f1
->position_of(f1
->source().end()),
233 quickbook::file_position(3,1));
237 void indented_map_tests2()
239 quickbook::string_view
source(
243 quickbook::file_ptr fake_file
= new quickbook::file(
244 "(fake file)", source
, 105u);
246 quickbook::mapped_file_builder builder
;
249 builder
.start(fake_file
);
250 builder
.unindent_and_add(fake_file
->source());
251 quickbook::file_ptr f1
= builder
.release();
252 BOOST_TEST_EQ(f1
->source(),
253 quickbook::string_view("Code line1\n\nCode line2\n"));
254 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin()),
255 quickbook::file_position(1,4));
256 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 1),
257 quickbook::file_position(1,5));
258 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 5),
259 quickbook::file_position(1,9));
260 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 10),
261 quickbook::file_position(1,14));
262 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 11),
263 quickbook::file_position(2,1));
264 BOOST_TEST_EQ(f1
->position_of(f1
->source().begin() + 12),
265 quickbook::file_position(3,4));
269 void indented_map_leading_blanks_test()
271 quickbook::mapped_file_builder builder
;
274 quickbook::string_view
source("\n\n Code line1\n");
275 quickbook::file_ptr fake_file
= new quickbook::file(
276 "(fake file)", source
, 105u);
277 builder
.start(fake_file
);
278 builder
.unindent_and_add(fake_file
->source());
279 quickbook::file_ptr f1
= builder
.release();
280 BOOST_TEST_EQ(f1
->source(),
281 quickbook::string_view("Code line1\n"));
285 quickbook::string_view
source(" \n \n Code line1\n");
286 quickbook::file_ptr fake_file
= new quickbook::file(
287 "(fake file)", source
, 105u);
288 builder
.start(fake_file
);
289 builder
.unindent_and_add(fake_file
->source());
290 quickbook::file_ptr f1
= builder
.release();
291 BOOST_TEST_EQ(f1
->source(),
292 quickbook::string_view("Code line1\n"));
296 quickbook::string_view
source(" Code line1\n \n Code line2");
297 quickbook::file_ptr fake_file
= new quickbook::file(
298 "(fake file)", source
, 105u);
299 builder
.start(fake_file
);
300 builder
.unindent_and_add(fake_file
->source());
301 quickbook::file_ptr f1
= builder
.release();
302 BOOST_TEST_EQ(f1
->source(),
303 quickbook::string_view("Code line1\n\nCode line2"));
307 void indented_map_trailing_blanks_test()
309 quickbook::mapped_file_builder builder
;
312 quickbook::string_view
source("\n\n Code line1\n ");
313 quickbook::file_ptr fake_file
= new quickbook::file(
314 "(fake file)", source
, 105u);
315 builder
.start(fake_file
);
316 builder
.unindent_and_add(fake_file
->source());
317 quickbook::file_ptr f1
= builder
.release();
318 BOOST_TEST_EQ(f1
->source(),
319 quickbook::string_view("Code line1\n"));
323 quickbook::string_view
source(" \n \n Code line1\n ");
324 quickbook::file_ptr fake_file
= new quickbook::file(
325 "(fake file)", source
, 105u);
326 builder
.start(fake_file
);
327 builder
.unindent_and_add(fake_file
->source());
328 quickbook::file_ptr f1
= builder
.release();
329 BOOST_TEST_EQ(f1
->source(),
330 quickbook::string_view("Code line1\n "));
334 quickbook::string_view
source(" Code line1\n \n Code line2\n ");
335 quickbook::file_ptr fake_file
= new quickbook::file(
336 "(fake file)", source
, 105u);
337 builder
.start(fake_file
);
338 builder
.unindent_and_add(fake_file
->source());
339 quickbook::file_ptr f1
= builder
.release();
340 BOOST_TEST_EQ(f1
->source(),
341 quickbook::string_view("Code line1\n\nCode line2\n"));
346 void indented_map_mixed_test()
348 quickbook::mapped_file_builder builder
;
351 quickbook::string_view
source("\tCode line 1\n Code line 2\n\t Code line 3\n \tCode line 4");
352 quickbook::file_ptr fake_file
= new quickbook::file(
353 "(fake file)", source
, 105u);
354 builder
.start(fake_file
);
355 builder
.unindent_and_add(fake_file
->source());
356 quickbook::file_ptr f1
= builder
.release();
357 BOOST_TEST_EQ(f1
->source(),
358 quickbook::string_view("Code line 1\nCode line 2\n Code line 3\n Code line 4"));
362 quickbook::string_view
source(" Code line 1\n\tCode line 2");
363 quickbook::file_ptr fake_file
= new quickbook::file(
364 "(fake file)", source
, 105u);
365 builder
.start(fake_file
);
366 builder
.unindent_and_add(fake_file
->source());
367 quickbook::file_ptr f1
= builder
.release();
368 BOOST_TEST_EQ(f1
->source(),
369 quickbook::string_view("Code line 1\n Code line 2"));
373 quickbook::string_view
source(" Code line 1\n \tCode line 2");
374 quickbook::file_ptr fake_file
= new quickbook::file(
375 "(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(),
380 quickbook::string_view("Code line 1\n\tCode line 2"));
388 indented_map_tests();
389 indented_map_tests2();
390 indented_map_leading_blanks_test();
391 indented_map_trailing_blanks_test();
392 indented_map_mixed_test();
393 return boost::report_errors();