From 32d9ff77900aa1396f8f522655acded7bc51d94c Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sun, 10 May 2026 09:43:36 +0200 Subject: [PATCH] add tests for command validation --- src/CMakeLists.txt | 4 +- src/command_validate.c | 28 +++++++++++++ src/command_validate.h | 8 ++++ src/main.c | 29 ++----------- tests/CMakeLists.txt | 12 ++++-- tests/command_validate_tests.c | 75 ++++++++++++++++++++++++++++++++++ tests/rename_tests.c | 6 +-- tests/tests.c | 21 ++++++++++ 8 files changed, 148 insertions(+), 35 deletions(-) create mode 100644 src/command_validate.c create mode 100644 src/command_validate.h create mode 100644 tests/command_validate_tests.c create mode 100644 tests/tests.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 051cd5d..d8cbad2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,8 +5,8 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) add_executable(${PROJECT_NAME} main.c - rename.c - config.h) + command_validate.c + rename.c) install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) diff --git a/src/command_validate.c b/src/command_validate.c new file mode 100644 index 0000000..c8b4de8 --- /dev/null +++ b/src/command_validate.c @@ -0,0 +1,28 @@ +#include +#include +#include "command_validate.h" +#include "config.h" + +bool validate_number_of_args(int argc) +{ + if (argc > 1 && argc < 4) + return true; + + printf("ERROR: %s []\n", PROJECT_NAME); + return false; +} + +bool validate_command(const char* cmd) +{ + if (strcmp(cmd, "lower") == 0) + return true; + + if (strcmp(cmd, "point") == 0) + return true; + + if (strcmp(cmd, "revert") == 0) + return true; + + printf("ERROR: Unknown command\n"); + return false; +} \ No newline at end of file diff --git a/src/command_validate.h b/src/command_validate.h new file mode 100644 index 0000000..f2f0d96 --- /dev/null +++ b/src/command_validate.h @@ -0,0 +1,8 @@ +#ifndef COMMAND_VALIDATE_H +#define COMMAND_VALIDATE_H +#include + +bool validate_number_of_args(int argc); +bool validate_command(const char* cmd); + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index ac0e6ab..2831b38 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include #include #include "rename.h" +#include "command_validate.h" #include "config.h" glob_t _get_files(const char* dir_path) @@ -60,35 +61,11 @@ int _rename_files(const char* dir_path, rename_ptr renamefunc) return EXIT_SUCCESS; } -bool _validate_number_of_args(int argc) -{ - if (argc > 1 && argc < 4) - return true; - - printf("ERROR: %s []\n", PROJECT_NAME); - return false; -} - -bool _validate_command(const char* cmd) -{ - if (strcmp(cmd, "lower") == 0) - return true; - - if (strcmp(cmd, "point") == 0) - return true; - - if (strcmp(cmd, "revert") == 0) - return true; - - printf("ERROR: Unknown command\n"); - return false; -} - int main(int argc, char* argv[]) { printf("Version %s\n", VERSION); - if (!_validate_number_of_args(argc)) + if (!validate_number_of_args(argc)) return EXIT_FAILURE; const char* dir = argv[1]; @@ -96,7 +73,7 @@ int main(int argc, char* argv[]) return _rename_files(dir, rename_string); const char* cmd = argv[2]; - if (!_validate_command(cmd)) + if (!validate_command(cmd)) return EXIT_FAILURE; if (strcmp(cmd, "lower") == 0) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3c7d394..6886e72 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,15 +3,19 @@ cmake_minimum_required(VERSION 3.25.1) find_library(CMOCKA cmocka REQUIRED) find_program(CPPCHECK cppcheck REQUIRED) -add_executable(rename_tests +include_directories(${CMAKE_BINARY_DIR}/src) +add_executable(tests + tests.c + command_validate_tests.c rename_tests.c + ../src/command_validate.c ../src/rename.c) -target_link_libraries(rename_tests +target_link_libraries(tests ${CMOCKA}) add_custom_target(run_tests - ALL ./rename_tests - DEPENDS rename_tests) + ALL ./tests + DEPENDS tests) add_custom_command(TARGET run_tests POST_BUILD diff --git a/tests/command_validate_tests.c b/tests/command_validate_tests.c new file mode 100644 index 0000000..c0d75c5 --- /dev/null +++ b/tests/command_validate_tests.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include "../src/command_validate.h" + +void validate_number_of_args_ok_test(int argc) +{ + bool valid = validate_number_of_args(argc); + + assert_true(valid); +} + +void validate_number_of_args_ok_tests() +{ + for (int i = 2; i < 4; i++) { + validate_number_of_args_ok_test(i); + } +} + +void validate_number_of_args_fail_test(int argc) +{ + bool valid = validate_number_of_args(argc); + + assert_false(valid); +} + +void validate_number_of_args_fail_tests() +{ + validate_number_of_args_fail_test(0); + validate_number_of_args_fail_test(1); + validate_number_of_args_fail_test(4); + validate_number_of_args_fail_test(5); +} + +void validate_command_ok_test(const char* cmd) +{ + bool valid = validate_command(cmd); + + assert_true(valid); +} + +void validate_command_ok_tests() +{ + validate_command_ok_test("lower"); + validate_command_ok_test("point"); + validate_command_ok_test("revert"); +} + +void validate_command_fail_test(const char* cmd) +{ + bool valid = validate_command(cmd); + + assert_false(valid); +} + +void validate_command_fail_tests() +{ + validate_command_fail_test(""); + validate_command_fail_test("unknown"); + validate_command_fail_test("bla"); +} + +int run_command_validate_tests() +{ + const struct CMUnitTest command_validate_tests[] = { + cmocka_unit_test(validate_number_of_args_ok_tests), + cmocka_unit_test(validate_number_of_args_fail_tests), + cmocka_unit_test(validate_command_ok_tests), + cmocka_unit_test(validate_command_fail_tests) + }; + + return cmocka_run_group_tests(command_validate_tests, NULL, NULL); +} \ No newline at end of file diff --git a/tests/rename_tests.c b/tests/rename_tests.c index 611cbb5..3f3b39a 100644 --- a/tests/rename_tests.c +++ b/tests/rename_tests.c @@ -213,9 +213,9 @@ void replace_string_test(void** result) assert_string_equal(*result, "Hello World!"); } -int main() +int run_rename_tests() { - const struct CMUnitTest tests[] = { + const struct CMUnitTest rename_tests[] = { cmocka_unit_test_teardown(strdup_test, teardown), cmocka_unit_test_teardown(rename_spaces_test, teardown), cmocka_unit_test_teardown(rename_ae_test, teardown), @@ -240,5 +240,5 @@ int main() cmocka_unit_test_teardown(replace_string_test, teardown) }; - return cmocka_run_group_tests(tests, NULL, NULL); + return cmocka_run_group_tests(rename_tests, NULL, NULL); } \ No newline at end of file diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..15e2113 --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,21 @@ +#include +typedef int (*runtestfn)(); + +int run_command_validate_tests(); +int run_rename_tests(); + +int main() +{ + const runtestfn testgroups[] = { + run_command_validate_tests, + run_rename_tests + }; + + size_t count = sizeof(testgroups) / sizeof(runtestfn); + for (size_t i = 0; i < count; i++) { + if (testgroups[i]()) + return 1; + } + + return 0; +} \ No newline at end of file -- 2.47.3