add_executable(${PROJECT_NAME}
main.c
- rename.c
- config.h)
+ command_validate.c
+ rename.c)
install(TARGETS ${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
--- /dev/null
+#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
--- /dev/null
+#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
#include <string.h>
#include <stdbool.h>
#include "rename.h"
+#include "command_validate.h"
#include "config.h"
glob_t _get_files(const char* dir_path)
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];
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)
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
--- /dev/null
+#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
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),
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
--- /dev/null
+#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