Inital commit

This commit is contained in:
2026-05-09 20:32:55 +02:00
commit bee8424782
21 changed files with 818 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
find_package(Unity REQUIRED)
find_package(CMock REQUIRED)
set(MOCK_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/mocks")
file(MAKE_DIRECTORY "${MOCK_GEN_DIR}")
# Generate a CMock mock from a header and attach it to a target.
# Usage: cmock_generate_mock(<target> <absolute-path-to-header>)
# CMock names generated files Mock<Name>.h/.c (capital M, no separator)
function(cmock_generate_mock target header)
if (NOT RUBY_EXECUTABLE)
message(FATAL_ERROR "Ruby is required for CMock generation")
endif()
get_filename_component(name "${header}" NAME_WE)
get_filename_component(header_dir "${header}" DIRECTORY)
set(mock_src "${MOCK_GEN_DIR}/Mock${name}.c")
set(mock_hdr "${MOCK_GEN_DIR}/Mock${name}.h")
add_custom_command(
OUTPUT "${mock_src}" "${mock_hdr}"
COMMAND "${RUBY_EXECUTABLE}" "${CMOCK_SCRIPT}"
"--mock_path=${MOCK_GEN_DIR}"
"${header}"
DEPENDS "${header}"
COMMENT "CMock: generating Mock${name}"
VERBATIM
)
target_sources("${target}" PRIVATE "${mock_src}")
# MOCK_GEN_DIR for the generated header; header_dir so the generated
# #include "logger.h" resolves without the ctdd/ prefix
target_include_directories("${target}" PRIVATE "${MOCK_GEN_DIR}" "${header_dir}")
endfunction()
# str tests — pure functions, no mock needed
add_executable(test_str test_str.c)
target_include_directories(test_str PRIVATE "${CMAKE_SOURCE_DIR}")
target_link_libraries(test_str PRIVATE ctdd_str Unity::Unity)
target_compile_features(test_str PRIVATE c_std_23)
add_test(NAME test_str COMMAND test_str)
# report tests — CMock-generated mock for log_message
add_executable(test_report test_report.c)
target_include_directories(test_report PRIVATE "${CMAKE_SOURCE_DIR}")
target_link_libraries(test_report PRIVATE ctdd_report Unity::Unity CMock::CMock)
target_compile_features(test_report PRIVATE c_std_23)
cmock_generate_mock(test_report "${CMAKE_SOURCE_DIR}/ctdd/logger.h")
add_test(NAME test_report COMMAND test_report)
+35
View File
@@ -0,0 +1,35 @@
#include "unity.h"
#include "ctdd/report.h"
#include "Mocklogger.h"
void setUp(void) { Mocklogger_Init(); }
void tearDown(void) { Mocklogger_Verify(); Mocklogger_Destroy(); }
void test_report_formats_label_and_value(void) {
log_message_Expect("count: 42");
report_value("count", 42);
}
void test_report_negative_value(void) {
log_message_Expect("score: -5");
report_value("score", -5);
}
void test_report_zero(void) {
log_message_Expect("total: 0");
report_value("total", 0);
}
void test_report_calls_log_exactly_once(void) {
log_message_Expect("x: 1");
report_value("x", 1);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_report_formats_label_and_value);
RUN_TEST(test_report_negative_value);
RUN_TEST(test_report_zero);
RUN_TEST(test_report_calls_log_exactly_once);
return UNITY_END();
}
+76
View File
@@ -0,0 +1,76 @@
#include "unity.h"
#include "ctdd/str.h"
void setUp(void) {}
void tearDown(void) {}
void test_starts_with_match(void) {
TEST_ASSERT_TRUE(str_starts_with("hello world", "hello"));
}
void test_starts_with_no_match(void) {
TEST_ASSERT_FALSE(str_starts_with("hello world", "world"));
}
void test_starts_with_empty_prefix(void) {
TEST_ASSERT_TRUE(str_starts_with("hello", ""));
}
void test_ends_with_match(void) {
TEST_ASSERT_TRUE(str_ends_with("hello world", "world"));
}
void test_ends_with_no_match(void) {
TEST_ASSERT_FALSE(str_ends_with("hello world", "hello"));
}
void test_ends_with_full_string(void) {
TEST_ASSERT_TRUE(str_ends_with("tdd", "tdd"));
}
void test_count_multiple(void) {
TEST_ASSERT_EQUAL_INT(3, str_count("banana", 'a'));
}
void test_count_none(void) {
TEST_ASSERT_EQUAL_INT(0, str_count("hello", 'z'));
}
void test_count_single(void) {
TEST_ASSERT_EQUAL_INT(1, str_count("ctdd", 'c'));
}
void test_upper_lowercase(void) {
char dst[16];
str_upper(dst, "hello", sizeof(dst));
TEST_ASSERT_EQUAL_STRING("HELLO", dst);
}
void test_upper_mixed(void) {
char dst[16];
str_upper(dst, "Hello World", sizeof(dst));
TEST_ASSERT_EQUAL_STRING("HELLO WORLD", dst);
}
void test_upper_already_upper(void) {
char dst[16];
str_upper(dst, "TDD", sizeof(dst));
TEST_ASSERT_EQUAL_STRING("TDD", dst);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_starts_with_match);
RUN_TEST(test_starts_with_no_match);
RUN_TEST(test_starts_with_empty_prefix);
RUN_TEST(test_ends_with_match);
RUN_TEST(test_ends_with_no_match);
RUN_TEST(test_ends_with_full_string);
RUN_TEST(test_count_multiple);
RUN_TEST(test_count_none);
RUN_TEST(test_count_single);
RUN_TEST(test_upper_lowercase);
RUN_TEST(test_upper_mixed);
RUN_TEST(test_upper_already_upper);
return UNITY_END();
}