| 1 |
/* |
| 2 |
* boxes - Command line filter to draw/remove ASCII boxes around text |
| 3 |
* Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public |
| 6 |
* License, version 3, as published by the Free Software Foundation. |
| 7 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 8 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 9 |
* details. |
| 10 |
* You should have received a copy of the GNU General Public License along with this program. |
| 11 |
* If not, see <https://www.gnu.org/licenses/>. |
| 12 |
* |
| 13 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 14 |
*/ |
| 15 |
|
| 16 |
/* |
| 17 |
* Unit tests of the 'cmdline' module |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#include <stdarg.h> |
| 23 |
#include <stddef.h> |
| 24 |
#include <setjmp.h> |
| 25 |
#include <cmocka.h> |
| 26 |
|
| 27 |
#include "boxes.h" |
| 28 |
#include "cmdline.h" |
| 29 |
#include "tools.h" |
| 30 |
#include "global_mock.h" |
| 31 |
#include "cmdline_test.h" |
| 32 |
|
| 33 |
|
| 34 |
static opt_t *act(const int num_args, ...) |
| 35 |
{ |
| 36 |
assert_true(num_args >= 0); |
| 37 |
|
| 38 |
int argc = 1; |
| 39 |
char **argv = (char **) calloc(argc + 1, sizeof(char *)); |
| 40 |
argv[0] = "out/boxes"; |
| 41 |
|
| 42 |
if (num_args > 0) { |
| 43 |
va_list va; |
| 44 |
va_start(va, num_args); |
| 45 |
for (int i=0; i<num_args; i++) { |
| 46 |
char *arg = va_arg(va, char *); |
| 47 |
++argc; |
| 48 |
argv = (char **) realloc(argv, (argc + 1) * sizeof(char *)); |
| 49 |
argv[argc - 1] = arg; |
| 50 |
} |
| 51 |
va_end(va); |
| 52 |
} |
| 53 |
argv[argc] = NULL; |
| 54 |
|
| 55 |
opt_t *actual = process_commandline(argc, argv); |
| 56 |
BFREE(argv); |
| 57 |
return actual; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
void test_indentmode_none(void **state) |
| 62 |
{ |
| 63 |
UNUSED(state); |
| 64 |
|
| 65 |
opt_t *actual = act(2, "-i", "none"); |
| 66 |
|
| 67 |
assert_non_null(actual); |
| 68 |
assert_int_equal((int) 'n', (int) actual->indentmode); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
void test_indentmode_invalid_long(void **state) |
| 73 |
{ |
| 74 |
UNUSED(state); |
| 75 |
|
| 76 |
opt_t *actual = act(2, "-i", "INVALID"); |
| 77 |
|
| 78 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 79 |
assert_int_equal(1, collect_err_size); |
| 80 |
assert_string_equal("boxes: invalid indentation mode\n", collect_err[0]); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
void test_indentmode_invalid_short(void **state) |
| 85 |
{ |
| 86 |
UNUSED(state); |
| 87 |
|
| 88 |
opt_t *actual = act(2, "-i", "X"); |
| 89 |
|
| 90 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 91 |
assert_int_equal(1, collect_err_size); |
| 92 |
assert_string_equal("boxes: invalid indentation mode\n", collect_err[0]); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
void test_indentmode_box(void **state) |
| 97 |
{ |
| 98 |
UNUSED(state); |
| 99 |
|
| 100 |
opt_t *actual = act(2, "-i", "BO"); |
| 101 |
|
| 102 |
assert_non_null(actual); |
| 103 |
assert_int_equal((int) 'b', (int) actual->indentmode); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
void test_indentmode_text(void **state) |
| 108 |
{ |
| 109 |
UNUSED(state); |
| 110 |
|
| 111 |
opt_t *actual = act(2, "-i", "t"); |
| 112 |
|
| 113 |
assert_non_null(actual); |
| 114 |
assert_int_equal((int) 't', (int) actual->indentmode); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
void test_killblank_true(void **state) |
| 119 |
{ |
| 120 |
UNUSED(state); |
| 121 |
|
| 122 |
opt_t *actual = act(2, "-k", "true"); |
| 123 |
|
| 124 |
assert_non_null(actual); |
| 125 |
assert_int_equal(1, actual->killblank); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void test_killblank_false(void **state) |
| 130 |
{ |
| 131 |
UNUSED(state); |
| 132 |
|
| 133 |
opt_t *actual = act(2, "-k", "false"); |
| 134 |
|
| 135 |
assert_non_null(actual); |
| 136 |
assert_int_equal(0, actual->killblank); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
void test_killblank_invalid(void **state) |
| 141 |
{ |
| 142 |
UNUSED(state); |
| 143 |
|
| 144 |
opt_t *actual = act(2, "-k", "INVALID"); |
| 145 |
|
| 146 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 147 |
assert_int_equal(1, collect_err_size); |
| 148 |
assert_string_equal("boxes: -k: invalid parameter\n", collect_err[0]); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
void test_killblank_multiple(void **state) |
| 153 |
{ |
| 154 |
UNUSED(state); |
| 155 |
|
| 156 |
opt_t *actual = act(4, "-k", "true", "-k", "false"); // first one wins |
| 157 |
|
| 158 |
assert_non_null(actual); |
| 159 |
assert_int_equal(1, actual->killblank); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
void test_killblank_long(void **state) |
| 164 |
{ |
| 165 |
UNUSED(state); |
| 166 |
|
| 167 |
opt_t *actual = act(2, "--kill-blank", "--remove"); |
| 168 |
|
| 169 |
assert_non_null(actual); |
| 170 |
assert_int_equal(1, actual->killblank); |
| 171 |
|
| 172 |
actual = act(2, "--no-kill-blank", "--remove"); |
| 173 |
|
| 174 |
assert_non_null(actual); |
| 175 |
assert_int_equal(0, actual->killblank); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
void test_padding_top_bottom(void **state) |
| 180 |
{ |
| 181 |
UNUSED(state); |
| 182 |
|
| 183 |
opt_t *actual = act(2, "-p", "t2b10"); |
| 184 |
|
| 185 |
assert_non_null(actual); |
| 186 |
assert_int_equal(2, actual->padding[BTOP]); |
| 187 |
assert_int_equal(10, actual->padding[BBOT]); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
void test_padding_invalid(void **state) |
| 192 |
{ |
| 193 |
UNUSED(state); |
| 194 |
|
| 195 |
opt_t *actual = act(2, "-p", "INVALID"); |
| 196 |
|
| 197 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 198 |
assert_int_equal(1, collect_err_size); |
| 199 |
assert_string_equal("boxes: invalid padding specification - INVALID\n", collect_err[0]); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
void test_padding_negative(void **state) |
| 204 |
{ |
| 205 |
UNUSED(state); |
| 206 |
|
| 207 |
opt_t *actual = act(2, "-p", "a-1"); |
| 208 |
|
| 209 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 210 |
assert_int_equal(1, collect_err_size); |
| 211 |
assert_string_equal("boxes: invalid padding specification - a-1\n", collect_err[0]); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
void test_padding_notset(void **state) |
| 216 |
{ |
| 217 |
UNUSED(state); |
| 218 |
|
| 219 |
opt_t *actual = act(2, "-p", ""); |
| 220 |
|
| 221 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 222 |
assert_int_equal(1, collect_err_size); |
| 223 |
assert_string_equal("boxes: invalid padding specification - \n", collect_err[0]); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
void test_padding_invalid_value(void **state) |
| 228 |
{ |
| 229 |
UNUSED(state); |
| 230 |
|
| 231 |
opt_t *actual = act(2, "-p", "l2rX"); |
| 232 |
|
| 233 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 234 |
assert_int_equal(1, collect_err_size); |
| 235 |
assert_string_equal("boxes: invalid padding specification - l2rX\n", collect_err[0]); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
void test_padding_novalue(void **state) |
| 240 |
{ |
| 241 |
UNUSED(state); |
| 242 |
|
| 243 |
opt_t *actual = act(2, "-p", "a"); |
| 244 |
|
| 245 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 246 |
assert_int_equal(1, collect_err_size); |
| 247 |
assert_string_equal("boxes: invalid padding specification - a\n", collect_err[0]); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
void test_tabstops_zero(void **state) |
| 252 |
{ |
| 253 |
UNUSED(state); |
| 254 |
|
| 255 |
opt_t *actual = act(2, "-t", "0"); |
| 256 |
|
| 257 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 258 |
assert_int_equal(1, collect_err_size); |
| 259 |
assert_string_equal("boxes: invalid tab stop distance -- 0\n", collect_err[0]); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
void test_tabstops_500(void **state) |
| 264 |
{ |
| 265 |
UNUSED(state); |
| 266 |
|
| 267 |
opt_t *actual = act(2, "-t", "500"); |
| 268 |
|
| 269 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 270 |
assert_int_equal(1, collect_err_size); |
| 271 |
assert_string_equal("boxes: invalid tab stop distance -- 500\n", collect_err[0]); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
void test_tabstops_4X(void **state) |
| 276 |
{ |
| 277 |
UNUSED(state); |
| 278 |
|
| 279 |
opt_t *actual = act(2, "-t", "4X"); |
| 280 |
|
| 281 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 282 |
assert_int_equal(1, collect_err_size); |
| 283 |
assert_string_equal("boxes: invalid tab handling specification - 4X\n", collect_err[0]); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
void test_tabstops_4e(void **state) |
| 288 |
{ |
| 289 |
UNUSED(state); |
| 290 |
|
| 291 |
opt_t *actual = act(2, "-t", "4e"); |
| 292 |
|
| 293 |
assert_non_null(actual); |
| 294 |
assert_int_equal(4, actual->tabstop); |
| 295 |
assert_int_equal((int) 'e', (int) actual->tabexp); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
void test_tabstops_4ex(void **state) |
| 300 |
{ |
| 301 |
UNUSED(state); |
| 302 |
|
| 303 |
opt_t *actual = act(2, "-t", "4ex"); |
| 304 |
|
| 305 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 306 |
assert_int_equal(1, collect_err_size); |
| 307 |
assert_string_equal("boxes: invalid tab handling specification - 4ex\n", collect_err[0]); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
void test_tabstops_7(void **state) |
| 312 |
{ |
| 313 |
UNUSED(state); |
| 314 |
|
| 315 |
opt_t *actual = act(2, "-t", "7"); |
| 316 |
|
| 317 |
assert_non_null(actual); |
| 318 |
assert_int_equal(7, actual->tabstop); |
| 319 |
assert_int_equal((int) 'e', (int) actual->tabexp); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
void test_alignment_invalid_hX(void **state) |
| 324 |
{ |
| 325 |
UNUSED(state); |
| 326 |
|
| 327 |
opt_t *actual = act(2, "-a", "hX"); |
| 328 |
|
| 329 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 330 |
assert_int_equal(1, collect_err_size); |
| 331 |
assert_string_equal("boxes: Illegal text format -- hX\n", collect_err[0]); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
void test_alignment_invalid_vX(void **state) |
| 336 |
{ |
| 337 |
UNUSED(state); |
| 338 |
|
| 339 |
opt_t *actual = act(2, "-a", "vX"); |
| 340 |
|
| 341 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 342 |
assert_int_equal(1, collect_err_size); |
| 343 |
assert_string_equal("boxes: Illegal text format -- vX\n", collect_err[0]); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
void test_alignment_invalid_jX(void **state) |
| 348 |
{ |
| 349 |
UNUSED(state); |
| 350 |
|
| 351 |
opt_t *actual = act(2, "-a", "jX"); |
| 352 |
|
| 353 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 354 |
assert_int_equal(1, collect_err_size); |
| 355 |
assert_string_equal("boxes: Illegal text format -- jX\n", collect_err[0]); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
void test_alignment_notset(void **state) |
| 360 |
{ |
| 361 |
UNUSED(state); |
| 362 |
|
| 363 |
opt_t *actual = act(2, "-a", ""); |
| 364 |
|
| 365 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 366 |
assert_int_equal(1, collect_err_size); |
| 367 |
assert_string_equal("boxes: Illegal text format -- \n", collect_err[0]); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
void test_alignment_incomplete(void **state) |
| 372 |
{ |
| 373 |
UNUSED(state); |
| 374 |
|
| 375 |
opt_t *actual = act(2, "-a", "v"); |
| 376 |
|
| 377 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 378 |
assert_int_equal(1, collect_err_size); |
| 379 |
assert_string_equal("boxes: Illegal text format -- v\n", collect_err[0]); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
void test_inputfiles_illegal_third_file(void **state) |
| 384 |
{ |
| 385 |
UNUSED(state); |
| 386 |
|
| 387 |
opt_t *actual = act(3, "file1", "file2", "file3_ILLEGAL"); |
| 388 |
|
| 389 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 390 |
assert_int_equal(3, collect_err_size); |
| 391 |
assert_string_equal("boxes: illegal parameter -- file3_ILLEGAL\n", collect_err[0]); |
| 392 |
assert_string_equal("Usage: boxes [options] [infile [outfile]]\n", collect_err[1]); |
| 393 |
assert_string_equal("Try `boxes -h' for more information.\n", collect_err[2]); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
void test_inputfiles_stdin_stdout(void **state) |
| 398 |
{ |
| 399 |
UNUSED(state); |
| 400 |
|
| 401 |
opt_t *actual = act(2, "-", "-"); |
| 402 |
|
| 403 |
assert_non_null(actual); |
| 404 |
assert_ptr_equal(stdin, actual->infile); |
| 405 |
assert_ptr_equal(stdout, actual->outfile); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
void test_inputfiles_stdin(void **state) |
| 410 |
{ |
| 411 |
UNUSED(state); |
| 412 |
|
| 413 |
opt_t *actual = act(1, "-"); |
| 414 |
|
| 415 |
assert_non_null(actual); |
| 416 |
assert_ptr_equal(stdin, actual->infile); |
| 417 |
assert_ptr_equal(stdout, actual->outfile); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
void test_inputfiles_input_nonexistent(void **state) |
| 422 |
{ |
| 423 |
UNUSED(state); |
| 424 |
|
| 425 |
opt_t *actual = act(1, "NON-EXISTENT"); |
| 426 |
|
| 427 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 428 |
assert_int_equal(1, collect_err_size); |
| 429 |
assert_string_equal("boxes: Can\'t open input file -- NON-EXISTENT\n", collect_err[0]); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
void test_inputfiles_actual_success(void **state) |
| 434 |
{ |
| 435 |
UNUSED(state); |
| 436 |
|
| 437 |
opt_t *actual = act(2, "../utest/dummy_in.txt", "../out/dummy_out.txt"); |
| 438 |
|
| 439 |
assert_non_null(actual); |
| 440 |
assert_non_null(actual->infile); |
| 441 |
assert_non_null(actual->outfile); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
void test_command_line_design_empty(void **state) |
| 446 |
{ |
| 447 |
UNUSED(state); |
| 448 |
|
| 449 |
opt_t *actual = act(2, "-c", ""); |
| 450 |
|
| 451 |
assert_null(actual); // invalid option, so we would need to exit with error |
| 452 |
assert_int_equal(1, collect_err_size); |
| 453 |
assert_string_equal("boxes: empty command line design definition\n", collect_err[0]); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
void test_help(void **state) |
| 458 |
{ |
| 459 |
UNUSED(state); |
| 460 |
|
| 461 |
opt_t *actual = act(1, "-h"); |
| 462 |
|
| 463 |
assert_non_null(actual); |
| 464 |
assert_int_equal(1, actual->help); |
| 465 |
assert_int_equal(0, actual->version_requested); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
void test_version_requested(void **state) |
| 470 |
{ |
| 471 |
UNUSED(state); |
| 472 |
|
| 473 |
opt_t *actual = act(1, "-v"); |
| 474 |
|
| 475 |
assert_non_null(actual); |
| 476 |
assert_int_equal(0, actual->help); |
| 477 |
assert_int_equal(1, actual->version_requested); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
/* vim: set cindent sw=4: */ |