File: cmdline_test.c

package info (click to toggle)
boxes 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,800 kB
  • sloc: ansic: 10,566; lex: 517; sh: 503; makefile: 309; yacc: 272; lisp: 78
file content (481 lines) | stat: -rw-r--r-- 11,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * boxes - Command line filter to draw/remove ASCII boxes around text
 * Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License, version 3, as published by the Free Software Foundation.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

/*
 * Unit tests of the 'cmdline' module
 */

#include "config.h"

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include "boxes.h"
#include "cmdline.h"
#include "tools.h"
#include "global_mock.h"
#include "cmdline_test.h"


static opt_t *act(const int num_args, ...)
{
    assert_true(num_args >= 0);

    int argc = 1;
    char **argv = (char **) calloc(argc + 1, sizeof(char *));
    argv[0] = "out/boxes";

    if (num_args > 0) {
        va_list va;
        va_start(va, num_args);
        for (int i=0; i<num_args; i++) {
            char *arg = va_arg(va, char *);
            ++argc;
            argv = (char **) realloc(argv, (argc + 1) * sizeof(char *));
            argv[argc - 1] = arg;
        }
        va_end(va);
    }
    argv[argc] = NULL;

    opt_t *actual = process_commandline(argc, argv);
    BFREE(argv);
    return actual;
}


void test_indentmode_none(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-i", "none");

    assert_non_null(actual);
    assert_int_equal((int) 'n', (int) actual->indentmode);
}


void test_indentmode_invalid_long(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-i", "INVALID");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid indentation mode\n", collect_err[0]);
}


void test_indentmode_invalid_short(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-i", "X");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid indentation mode\n", collect_err[0]);
}


void test_indentmode_box(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-i", "BO");

    assert_non_null(actual);
    assert_int_equal((int) 'b', (int) actual->indentmode);
}


void test_indentmode_text(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-i", "t");

    assert_non_null(actual);
    assert_int_equal((int) 't', (int) actual->indentmode);
}


void test_killblank_true(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-k", "true");

    assert_non_null(actual);
    assert_int_equal(1, actual->killblank);
}


void test_killblank_false(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-k", "false");

    assert_non_null(actual);
    assert_int_equal(0, actual->killblank);
}


void test_killblank_invalid(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-k", "INVALID");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: -k: invalid parameter\n", collect_err[0]);
}


void test_killblank_multiple(void **state)
{
    UNUSED(state);

    opt_t *actual = act(4, "-k", "true", "-k", "false");    // first one wins

    assert_non_null(actual);
    assert_int_equal(1, actual->killblank);
}


void test_killblank_long(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "--kill-blank", "--remove");

    assert_non_null(actual);
    assert_int_equal(1, actual->killblank);

    actual = act(2, "--no-kill-blank", "--remove");

    assert_non_null(actual);
    assert_int_equal(0, actual->killblank);
}


void test_padding_top_bottom(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-p", "t2b10");

    assert_non_null(actual);
    assert_int_equal(2, actual->padding[BTOP]);
    assert_int_equal(10, actual->padding[BBOT]);
}


void test_padding_invalid(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-p", "INVALID");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid padding specification - INVALID\n", collect_err[0]);
}


void test_padding_negative(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-p", "a-1");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid padding specification - a-1\n", collect_err[0]);
}


void test_padding_notset(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-p", "");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid padding specification - \n", collect_err[0]);
}


void test_padding_invalid_value(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-p", "l2rX");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid padding specification - l2rX\n", collect_err[0]);
}


void test_padding_novalue(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-p", "a");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid padding specification - a\n", collect_err[0]);
}


void test_tabstops_zero(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-t", "0");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid tab stop distance -- 0\n", collect_err[0]);
}


void test_tabstops_500(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-t", "500");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid tab stop distance -- 500\n", collect_err[0]);
}


void test_tabstops_4X(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-t", "4X");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid tab handling specification - 4X\n", collect_err[0]);
}


void test_tabstops_4e(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-t", "4e");

    assert_non_null(actual);
    assert_int_equal(4, actual->tabstop);
    assert_int_equal((int) 'e', (int) actual->tabexp);
}


void test_tabstops_4ex(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-t", "4ex");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: invalid tab handling specification - 4ex\n", collect_err[0]);
}


void test_tabstops_7(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-t", "7");

    assert_non_null(actual);
    assert_int_equal(7, actual->tabstop);
    assert_int_equal((int) 'e', (int) actual->tabexp);
}


void test_alignment_invalid_hX(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-a", "hX");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: Illegal text format -- hX\n", collect_err[0]);
}


void test_alignment_invalid_vX(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-a", "vX");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: Illegal text format -- vX\n", collect_err[0]);
}


void test_alignment_invalid_jX(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-a", "jX");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: Illegal text format -- jX\n", collect_err[0]);
}


void test_alignment_notset(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-a", "");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: Illegal text format -- \n", collect_err[0]);
}


void test_alignment_incomplete(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-a", "v");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: Illegal text format -- v\n", collect_err[0]);
}


void test_inputfiles_illegal_third_file(void **state)
{
    UNUSED(state);

    opt_t *actual = act(3, "file1", "file2", "file3_ILLEGAL");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(3, collect_err_size);
    assert_string_equal("boxes: illegal parameter -- file3_ILLEGAL\n", collect_err[0]);
    assert_string_equal("Usage: boxes [options] [infile [outfile]]\n", collect_err[1]);
    assert_string_equal("Try `boxes -h' for more information.\n", collect_err[2]);
}


void test_inputfiles_stdin_stdout(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-", "-");

    assert_non_null(actual);
    assert_ptr_equal(stdin, actual->infile);
    assert_ptr_equal(stdout, actual->outfile);
}


void test_inputfiles_stdin(void **state)
{
    UNUSED(state);

    opt_t *actual = act(1, "-");

    assert_non_null(actual);
    assert_ptr_equal(stdin, actual->infile);
    assert_ptr_equal(stdout, actual->outfile);
}


void test_inputfiles_input_nonexistent(void **state)
{
    UNUSED(state);

    opt_t *actual = act(1, "NON-EXISTENT");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: Can\'t open input file -- NON-EXISTENT\n", collect_err[0]);
}


void test_inputfiles_actual_success(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "../utest/dummy_in.txt", "../out/dummy_out.txt");

    assert_non_null(actual);
    assert_non_null(actual->infile);
    assert_non_null(actual->outfile);
}


void test_command_line_design_empty(void **state)
{
    UNUSED(state);

    opt_t *actual = act(2, "-c", "");

    assert_null(actual);   // invalid option, so we would need to exit with error
    assert_int_equal(1, collect_err_size);
    assert_string_equal("boxes: empty command line design definition\n", collect_err[0]);
}


void test_help(void **state)
{
    UNUSED(state);

    opt_t *actual = act(1, "-h");

    assert_non_null(actual);
    assert_int_equal(1, actual->help);
    assert_int_equal(0, actual->version_requested);
}


void test_version_requested(void **state)
{
    UNUSED(state);

    opt_t *actual = act(1, "-v");

    assert_non_null(actual);
    assert_int_equal(0, actual->help);
    assert_int_equal(1, actual->version_requested);
}


/* vim: set cindent sw=4: */