mirror of
https://github.com/ascii-boxes/boxes.git
synced 2025-02-12 07:40:16 +01:00
Add unit tests for indentation mode cmdline parsing
This commit is contained in:
parent
5ea2f5b2b8
commit
0e2b57c97d
@ -303,7 +303,7 @@ static int indentation_mode(opt_t *result, char *optarg)
|
||||
result->indentmode = 'n';
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "%s: invalid indentation mode\n", PROJECT);
|
||||
bx_fprintf(stderr, "%s: invalid indentation mode\n", PROJECT);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@ -611,7 +611,17 @@ static void print_debug_info(opt_t *result)
|
||||
|
||||
opt_t *process_commandline(int argc, char *argv[])
|
||||
{
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "argc = %d\n", argc);
|
||||
fprintf(stderr, "argv = [");
|
||||
for(int i=0; i<=argc; i++) {
|
||||
fprintf(stderr, "%s%s", argv[i], i < argc ? ", " : "");
|
||||
}
|
||||
fprintf(stderr, "]\n");
|
||||
#endif
|
||||
|
||||
opt_t *result = create_new_opt();
|
||||
optind = 1; /* ensure that getopt() will process all arguments, even in unit test situations */
|
||||
|
||||
/* Intercept '--help' and '-?' cases first, as they are not supported by getopt() */
|
||||
if (argc >= 2 && argv[1] != NULL && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)) {
|
||||
|
@ -24,7 +24,7 @@ SRC_DIR = ../src
|
||||
UTEST_DIR = ../utest
|
||||
VPATH = $(SRC_DIR):$(SRC_DIR)/misc:$(UTEST_DIR)
|
||||
|
||||
UTEST_NORM = global_mock.c tools_test.c regulex_test.o main.o
|
||||
UTEST_NORM = global_mock.c cmdline_test.c tools_test.c regulex_test.o main.o
|
||||
MOCKS = bx_fprintf
|
||||
|
||||
.PHONY: check_dir flags_unix flags_win32 flags_ utest
|
||||
@ -72,6 +72,7 @@ unittest.exe: $(UTEST_OBJ) | check_dir
|
||||
|
||||
|
||||
global_mock.o: global_mock.c global_mock.h boxes.h unicode.h config.h | check_dir
|
||||
cmdline_test.o: cmdline_test.c cmdline_test.h global_mock.h boxes.h cmdline.h config.h | check_dir
|
||||
tools_test.o: tools_test.c tools_test.h tools.h config.h | check_dir
|
||||
regulex_test.o: regulex_test.c regulex_test.h global_mock.h regulex.h config.h | check_dir
|
||||
main.o: main.c global_mock.h tools_test.h regulex_test.h config.h | check_dir
|
||||
|
121
utest/cmdline_test.c
Normal file
121
utest/cmdline_test.c
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* boxes - Command line filter to draw/remove ASCII boxes around text
|
||||
* Copyright (c) 1999-2021 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 2, 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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);
|
||||
char *arg;
|
||||
while ((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)
|
||||
{
|
||||
(void) state; /* unused */
|
||||
|
||||
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)
|
||||
{
|
||||
(void) state; /* unused */
|
||||
|
||||
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)
|
||||
{
|
||||
(void) state; /* unused */
|
||||
|
||||
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)
|
||||
{
|
||||
(void) state; /* unused */
|
||||
|
||||
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)
|
||||
{
|
||||
(void) state; /* unused */
|
||||
|
||||
opt_t *actual = act(2, "-i", "t");
|
||||
|
||||
assert_non_null(actual);
|
||||
assert_int_equal((int) 't', (int) actual->indentmode);
|
||||
}
|
||||
|
||||
|
||||
/*EOF*/ /* vim: set cindent sw=4: */
|
38
utest/cmdline_test.h
Normal file
38
utest/cmdline_test.h
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* boxes - Command line filter to draw/remove ASCII boxes around text
|
||||
* Copyright (c) 1999-2021 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 2, 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, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*/
|
||||
|
||||
/*
|
||||
* Unit tests of the 'cmdline' module
|
||||
*/
|
||||
|
||||
#ifndef CMDLINE_TEST_H
|
||||
#define CMDLINE_TEST_H
|
||||
|
||||
|
||||
void test_indentmode_none(void **state);
|
||||
void test_indentmode_invalid_long(void **state);
|
||||
void test_indentmode_invalid_short(void **state);
|
||||
void test_indentmode_box(void **state);
|
||||
void test_indentmode_text(void **state);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/*EOF*/ /* vim: set cindent sw=4: */
|
22
utest/main.c
22
utest/main.c
@ -29,6 +29,7 @@
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "global_mock.h"
|
||||
#include "cmdline_test.h"
|
||||
#include "tools_test.h"
|
||||
#include "regulex_test.h"
|
||||
|
||||
@ -46,11 +47,12 @@ int main(void)
|
||||
{
|
||||
setup_mocks();
|
||||
|
||||
const struct CMUnitTest tools_tests[] = {
|
||||
cmocka_unit_test(test_strisyes_true),
|
||||
cmocka_unit_test(test_strisyes_false),
|
||||
cmocka_unit_test(test_strisno_true),
|
||||
cmocka_unit_test(test_strisno_false)
|
||||
const struct CMUnitTest cmdline_tests[] = {
|
||||
cmocka_unit_test_setup(test_indentmode_none, beforeTest),
|
||||
cmocka_unit_test_setup(test_indentmode_invalid_long, beforeTest),
|
||||
cmocka_unit_test_setup(test_indentmode_invalid_short, beforeTest),
|
||||
cmocka_unit_test_setup(test_indentmode_box, beforeTest),
|
||||
cmocka_unit_test_setup(test_indentmode_text, beforeTest)
|
||||
};
|
||||
|
||||
const struct CMUnitTest regulex_tests[] = {
|
||||
@ -61,9 +63,17 @@ int main(void)
|
||||
cmocka_unit_test_setup(test_regex_replace_error, beforeTest)
|
||||
};
|
||||
|
||||
const struct CMUnitTest tools_tests[] = {
|
||||
cmocka_unit_test(test_strisyes_true),
|
||||
cmocka_unit_test(test_strisyes_false),
|
||||
cmocka_unit_test(test_strisno_true),
|
||||
cmocka_unit_test(test_strisno_false)
|
||||
};
|
||||
|
||||
int num_failed = 0;
|
||||
num_failed += cmocka_run_group_tests(tools_tests, NULL, NULL);
|
||||
num_failed += cmocka_run_group_tests(cmdline_tests, NULL, NULL);
|
||||
num_failed += cmocka_run_group_tests(regulex_tests, NULL, NULL);
|
||||
num_failed += cmocka_run_group_tests(tools_tests, NULL, NULL);
|
||||
|
||||
teardown();
|
||||
return num_failed;
|
||||
|
Loading…
Reference in New Issue
Block a user