1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29
30 #include <cstring>
31 #include <iostream>
32
33 #include <atf-c++.hpp>
34
35 #include "env.hpp"
36 #include "ui.hpp"
37
38 // ------------------------------------------------------------------------
39 // Test cases for the free functions.
40 // ------------------------------------------------------------------------
41
42 struct test {
43 const char *tc;
44 const char *tag;
45 bool repeat;
46 size_t col;
47 const char *fmt;
48 const char *result;
49 } tests[] = {
50 //
51 // wo_tag
52 //
53
54 {
55 "wo_tag",
56 "",
57 false,
58 0,
59 "12345",
60 "12345",
61 },
62
63 {
64 "wo_tag",
65 "",
66 false,
67 0,
68 "12345 ",
69 "12345",
70 },
71
72 {
73 "wo_tag",
74 "",
75 false,
76 0,
77 "12345 7890",
78 "12345 7890",
79 },
80
81 {
82 "wo_tag",
83 "",
84 false,
85 0,
86 "12345 789012 45",
87 "12345 789012 45",
88 },
89
90 {
91 "wo_tag",
92 "",
93 false,
94 0,
95 "12345 789012 456",
96 "12345 789012\n456",
97 },
98
99 {
100 "wo_tag",
101 "",
102 false,
103 0,
104 "1234567890123456",
105 "1234567890123456",
106 },
107
108 // TODO(jmmv): Fix the code to pass this test...
109 // {
110 // "wo_tag",
111 // "",
112 // false,
113 // 0,
114 // " 2345678901234567",
115 // "\n2345678901234567",
116 // },
117
118 {
119 "wo_tag",
120 "",
121 false,
122 0,
123 "12345 789012345 78",
124 "12345 789012345\n78",
125 },
126
127 //
128 // wo_tag_col
129 //
130
131 {
132 "wo_tag_col",
133 "",
134 false,
135 10,
136 "12345",
137 " 12345",
138 },
139
140 {
141 "wo_tag_col",
142 "",
143 false,
144 10,
145 "12345 7890",
146 " 12345\n"
147 " 7890",
148 },
149
150 {
151 "wo_tag_col",
152 "",
153 false,
154 10,
155 "1 3 5 7 9",
156 " 1 3 5\n"
157 " 7 9",
158 },
159
160 //
161 // w_tag_no_repeat
162 //
163
164 {
165 "w_tag_no_repeat",
166 "1234: ",
167 false,
168 0,
169 "789012345",
170 "1234: 789012345",
171 },
172
173 {
174 "w_tag_no_repeat",
175 "1234: ",
176 false,
177 0,
178 "789 1234 56789",
179 "1234: 789 1234\n"
180 " 56789",
181 },
182
183 {
184 "w_tag_no_repeat",
185 "1234: ",
186 false,
187 0,
188 "789012345",
189 "1234: 789012345",
190 },
191
192 {
193 "w_tag_no_repeat",
194 "1234: ",
195 false,
196 0,
197 "789012345 7890",
198 "1234: 789012345\n"
199 " 7890",
200 },
201
202 //
203 // w_tag_repeat
204 //
205
206 {
207 "w_tag_repeat",
208 "1234: ",
209 true,
210 0,
211 "789012345",
212 "1234: 789012345",
213 },
214
215 {
216 "w_tag_repeat",
217 "1234: ",
218 true,
219 0,
220 "789 1234 56789",
221 "1234: 789 1234\n"
222 "1234: 56789",
223 },
224
225 {
226 "w_tag_repeat",
227 "1234: ",
228 true,
229 0,
230 "789012345",
231 "1234: 789012345",
232 },
233
234 {
235 "w_tag_no_repeat",
236 "1234: ",
237 true,
238 0,
239 "789012345 7890",
240 "1234: 789012345\n"
241 "1234: 7890",
242 },
243
244 //
245 // w_tag_col
246 //
247
248 {
249 "w_tag_col",
250 "1234:",
251 false,
252 10,
253 "1 3 5",
254 "1234: 1 3 5",
255 },
256
257 {
258 "w_tag_col",
259 "1234:",
260 false,
261 10,
262 "1 3 5 7 9",
263 "1234: 1 3 5\n"
264 " 7 9",
265 },
266
267 {
268 "w_tag_col",
269 "1234:",
270 true,
271 10,
272 "1 3 5 7 9",
273 "1234: 1 3 5\n"
274 "1234: 7 9",
275 },
276
277 //
278 // paragraphs
279 //
280
281 {
282 "paragraphs",
283 "",
284 false,
285 0,
286 "1 3 5\n\n",
287 "1 3 5"
288 },
289
290 {
291 "paragraphs",
292 "",
293 false,
294 0,
295 "1 3 5\n2 4 6",
296 "1 3 5\n\n2 4 6"
297 },
298
299 {
300 "paragraphs",
301 "",
302 false,
303 0,
304 "1234 6789 123456\n2 4 6",
305 "1234 6789\n123456\n\n2 4 6"
306 },
307
308 {
309 "paragraphs",
310 "12: ",
311 false,
312 0,
313 "56789 123456\n2 4 6",
314 "12: 56789\n 123456\n\n 2 4 6"
315 },
316
317 {
318 "paragraphs",
319 "12: ",
320 true,
321 0,
322 "56789 123456\n2 4 6",
323 "12: 56789\n12: 123456\n12: \n12: 2 4 6"
324 },
325
326 {
327 "paragraphs",
328 "12:",
329 false,
330 4,
331 "56789 123456\n2 4 6",
332 "12: 56789\n 123456\n\n 2 4 6"
333 },
334
335 {
336 "paragraphs",
337 "12:",
338 true,
339 4,
340 "56789 123456\n2 4 6",
341 "12: 56789\n12: 123456\n12:\n12: 2 4 6"
342 },
343
344 //
345 // end
346 //
347
348 {
349 NULL,
350 NULL,
351 false,
352 0,
353 NULL,
354 NULL,
355 },
356 };
357
358 static
359 void
run_tests(const char * tc)360 run_tests(const char *tc)
361 {
362 struct test *t;
363
364 std::cout << "Running tests for " << tc << "\n";
365
366 tools::env::set("COLUMNS", "15");
367
368 for (t = &tests[0]; t->tc != NULL; t++) {
369 if (std::strcmp(t->tc, tc) == 0) {
370 std::cout << "\n";
371 std::cout << "Testing with tag '" << t->tag << "', '"
372 << (t->repeat ? "repeat" : "no repeat") << "', col "
373 << t->col << "\n";
374 std::cout << "Input: >>>" << t->fmt << "<<<\n";
375 std::cout << "Expected output: >>>" << t->result << "<<<\n";
376
377 std::string result = tools::ui::format_text_with_tag(t->fmt, t->tag,
378 t->repeat, t->col);
379 std::cout << "Output : >>>" << result << "<<<\n";
380 ATF_REQUIRE_EQ(t->result, result);
381 }
382 }
383 }
384
385 ATF_TEST_CASE(wo_tag);
ATF_TEST_CASE_HEAD(wo_tag)386 ATF_TEST_CASE_HEAD(wo_tag)
387 {
388 set_md_var("descr", "Checks formatting without tags");
389 }
ATF_TEST_CASE_BODY(wo_tag)390 ATF_TEST_CASE_BODY(wo_tag)
391 {
392 run_tests("wo_tag");
393 }
394
395 ATF_TEST_CASE(wo_tag_col);
ATF_TEST_CASE_HEAD(wo_tag_col)396 ATF_TEST_CASE_HEAD(wo_tag_col)
397 {
398 set_md_var("descr", "Checks formatting without tags and with a non-zero "
399 "starting column");
400 }
ATF_TEST_CASE_BODY(wo_tag_col)401 ATF_TEST_CASE_BODY(wo_tag_col)
402 {
403 run_tests("wo_tag_col");
404 }
405
406 ATF_TEST_CASE(w_tag_no_repeat);
ATF_TEST_CASE_HEAD(w_tag_no_repeat)407 ATF_TEST_CASE_HEAD(w_tag_no_repeat)
408 {
409 set_md_var("descr", "Checks formatting with a tag");
410 }
ATF_TEST_CASE_BODY(w_tag_no_repeat)411 ATF_TEST_CASE_BODY(w_tag_no_repeat)
412 {
413 run_tests("w_tag_no_repeat");
414 }
415
416 ATF_TEST_CASE(w_tag_repeat);
ATF_TEST_CASE_HEAD(w_tag_repeat)417 ATF_TEST_CASE_HEAD(w_tag_repeat)
418 {
419 set_md_var("descr", "Checks formatting with a tag and repeating it on "
420 "each line");
421 }
ATF_TEST_CASE_BODY(w_tag_repeat)422 ATF_TEST_CASE_BODY(w_tag_repeat)
423 {
424 run_tests("w_tag_repeat");
425 }
426
427 ATF_TEST_CASE(w_tag_col);
ATF_TEST_CASE_HEAD(w_tag_col)428 ATF_TEST_CASE_HEAD(w_tag_col)
429 {
430 set_md_var("descr", "Checks formatting with a tag and starting at a "
431 "column greater than its length");
432 }
ATF_TEST_CASE_BODY(w_tag_col)433 ATF_TEST_CASE_BODY(w_tag_col)
434 {
435 run_tests("w_tag_col");
436 }
437
438 ATF_TEST_CASE(paragraphs);
ATF_TEST_CASE_HEAD(paragraphs)439 ATF_TEST_CASE_HEAD(paragraphs)
440 {
441 set_md_var("descr", "Checks formatting a string that contains multiple "
442 "paragraphs");
443 }
ATF_TEST_CASE_BODY(paragraphs)444 ATF_TEST_CASE_BODY(paragraphs)
445 {
446 run_tests("paragraphs");
447 }
448
449 // ------------------------------------------------------------------------
450 // Main.
451 // ------------------------------------------------------------------------
452
ATF_INIT_TEST_CASES(tcs)453 ATF_INIT_TEST_CASES(tcs)
454 {
455 // Add the test cases for the free functions.
456 ATF_ADD_TEST_CASE(tcs, wo_tag);
457 ATF_ADD_TEST_CASE(tcs, wo_tag_col);
458 ATF_ADD_TEST_CASE(tcs, w_tag_no_repeat);
459 ATF_ADD_TEST_CASE(tcs, w_tag_repeat);
460 ATF_ADD_TEST_CASE(tcs, w_tag_col);
461 ATF_ADD_TEST_CASE(tcs, paragraphs);
462 }
463