mirror of
https://github.com/tbamud/tbamud.git
synced 2026-04-30 04:41:51 +02:00
Add Unity-based unit test infrastructure (Phase 1 — 117 tests) (#177)
Introduced new system for unit tests based on the Unity framework. Added tests for some of the simple functions in four different files.
This commit is contained in:
103
tests/CMakeLists.txt
Normal file
103
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,103 @@
|
||||
# tests/CMakeLists.txt
|
||||
# Unity-based unit tests for tbaMUD
|
||||
#
|
||||
# Each test executable is built from:
|
||||
# vendor/unity/unity.c — test framework
|
||||
# test_stubs.c — weak-symbol stubs for all unresolved mud globals
|
||||
# src/<module>.c — the source file(s) under test
|
||||
# test_<name>.c — the test file itself
|
||||
#
|
||||
# Only the source files being tested are compiled — in particular comm.c
|
||||
# (which contains main()) is never included.
|
||||
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
|
||||
enable_testing()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Common settings shared by all test targets
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
set(UNITY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/unity/unity.c)
|
||||
set(STUBS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/test_stubs.c)
|
||||
|
||||
set(MUD_SRCDIR ${CMAKE_SOURCE_DIR}/src)
|
||||
|
||||
# Include paths: mud source dir for conf.h / structs.h etc.,
|
||||
# CMake binary dir for the generated conf.h (cmake builds place it there),
|
||||
# and the Unity header dir.
|
||||
set(TEST_INCLUDES
|
||||
${MUD_SRCDIR}
|
||||
${CMAKE_BINARY_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vendor/unity
|
||||
)
|
||||
|
||||
# Suppress warnings that fire in generated stubs / vendored code and in the
|
||||
# mud sources when compiled outside their normal full-build context.
|
||||
set(TEST_CFLAGS)
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$")
|
||||
list(APPEND TEST_CFLAGS
|
||||
-Wno-unused-parameter
|
||||
-Wno-unused-function
|
||||
-Wno-unused-variable
|
||||
)
|
||||
endif()
|
||||
|
||||
# Helper macro: add_mud_test(name SRC1 [SRC2 …])
|
||||
# Creates an executable, registers it with CTest.
|
||||
macro(add_mud_test TEST_NAME)
|
||||
add_executable(${TEST_NAME}
|
||||
${UNITY_SRC}
|
||||
${STUBS_SRC}
|
||||
${ARGN}
|
||||
)
|
||||
target_include_directories(${TEST_NAME} PRIVATE ${TEST_INCLUDES})
|
||||
target_compile_options(${TEST_NAME} PRIVATE ${TEST_CFLAGS})
|
||||
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
|
||||
endmacro()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# test_utils — covers src/utils.c
|
||||
# ---------------------------------------------------------------------------
|
||||
add_mud_test(test_utils
|
||||
${MUD_SRCDIR}/utils.c
|
||||
${MUD_SRCDIR}/random.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_utils.c
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# test_random — covers src/random.c and rand_number/dice in src/utils.c
|
||||
# ---------------------------------------------------------------------------
|
||||
add_mud_test(test_random
|
||||
${MUD_SRCDIR}/random.c
|
||||
${MUD_SRCDIR}/utils.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_random.c
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# test_interpreter — covers string helpers in src/interpreter.c
|
||||
# ---------------------------------------------------------------------------
|
||||
add_mud_test(test_interpreter
|
||||
${MUD_SRCDIR}/interpreter.c
|
||||
${MUD_SRCDIR}/utils.c
|
||||
${MUD_SRCDIR}/random.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_interpreter.c
|
||||
)
|
||||
|
||||
# crypt() is referenced from interpreter.c (nanny password hashing).
|
||||
# Reuse the crypt library detected by the top-level build when one is needed;
|
||||
# on platforms where crypt() is provided by libc, no extra link library is
|
||||
# required.
|
||||
if(CRYPT_LIBRARY)
|
||||
target_link_libraries(test_interpreter PRIVATE ${CRYPT_LIBRARY})
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# test_class — covers src/class.c
|
||||
# ---------------------------------------------------------------------------
|
||||
add_mud_test(test_class
|
||||
${MUD_SRCDIR}/class.c
|
||||
${MUD_SRCDIR}/utils.c
|
||||
${MUD_SRCDIR}/random.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_class.c
|
||||
)
|
||||
Reference in New Issue
Block a user