]> gitweb.hhaalo.de Git - mv-none-space.git/commitdiff
add tests for command validation
authorBastian Dehn <hhaalo@arcor.de>
Sun, 10 May 2026 07:43:36 +0000 (09:43 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 10 May 2026 07:46:13 +0000 (09:46 +0200)
src/CMakeLists.txt
src/command_validate.c [new file with mode: 0644]
src/command_validate.h [new file with mode: 0644]
src/main.c
tests/CMakeLists.txt
tests/command_validate_tests.c [new file with mode: 0644]
tests/rename_tests.c
tests/tests.c [new file with mode: 0644]

index 051cd5d99b25f78b21c1a712739c7fde59b45f79..d8cbad2056128bc834e87cefc801458bb6f2ab7a 100644 (file)
@@ -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 (file)
index 0000000..c8b4de8
--- /dev/null
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <string.h>
+#include "command_validate.h"
+#include "config.h"
+
+bool validate_number_of_args(int argc)
+{
+       if (argc > 1 && argc < 4)
+               return true;
+
+       printf("ERROR: %s <dir> [<command>]\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 (file)
index 0000000..f2f0d96
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef COMMAND_VALIDATE_H
+#define COMMAND_VALIDATE_H
+#include <stdbool.h>
+
+bool validate_number_of_args(int argc);
+bool validate_command(const char* cmd);
+
+#endif
\ No newline at end of file
index ac0e6ab1aa788053a1bec91de8147c62400f2e19..2831b38844cd96fb0920e37eda4c30aaf5557a36 100644 (file)
@@ -5,6 +5,7 @@
 #include <string.h>
 #include <stdbool.h>
 #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 <dir> [<command>]\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)
index 3c7d39479c90ad76cef44e38a7f19c703c84c6b2..6886e721dbf8b8709a99e6c203dba8a6117f176a 100644 (file)
@@ -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 (file)
index 0000000..c0d75c5
--- /dev/null
@@ -0,0 +1,75 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#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
index 611cbb58bcd26cd9efe26a33ed208515b992d251..3f3b39a7186a78414d5d60a286d5754f392a0cea 100644 (file)
@@ -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 (file)
index 0000000..15e2113
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stddef.h>
+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