diff --git a/.gitignore b/.gitignore index 6a54ab6c..2045abfc 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ CMakeFiles Makefile cmake_install.cmake install_manifest.txt +CTestTestfile.cmake +Testing/ diff --git a/CMakeLists.txt b/CMakeLists.txt index e4b00acc..a4f21d5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(Ocarina) # Various options for users option(CONFIG_DEBUG "Compile with debugging symbols" OFF) +option(CONFIG_TESTING_VERBOSE "Enable verbose output when running tests" OFF) # Configure settings set(CONFIG_MAJOR 6) @@ -16,8 +17,9 @@ if (CONFIG_RC) set(CONFIG_VERSION "${CONFIG_VERSION}-rc") endif() +set(DEBUG_OPTIONS -Wall -Werror -g -DCONFIG_DEBUG) if (CONFIG_DEBUG) - add_definitions(-Wall -Werror -g -DCONFIG_DEBUG) + add_definitions(${DEBUG_OPTIONS}) set(CONFIG_VERSION "${CONFIG_VERSION}-debug") endif() @@ -65,3 +67,14 @@ install(DIRECTORY share/ DESTINATION /usr/share/) # Configure release target set(CONFIG_RELEASE ocarina-${CONFIG_VERSION}) add_custom_target(release COMMAND git archive -v --prefix=${CONFIG_RELEASE}/ -o ${CONFIG_RELEASE}.tar.gz HEAD) + +# Set up unit tests +set(CMAKE_TEST_COMMAND ctest --output-on-failure) +if (CONFIG_TESTING_VERBOSE) + set(CMAKE_TEST_COMMAND ctest -V) +endif() +if (IS_DIRECTORY tests/) + enable_testing() + add_custom_target(tests COMMAND ${CMAKE_TEST_COMMAND}) + add_subdirectory(tests/) +endif() diff --git a/include/core/version.h b/include/core/version.h index 686dc4b2..2d8c1ed0 100644 --- a/include/core/version.h +++ b/include/core/version.h @@ -5,7 +5,7 @@ #define OCARINA_CORE_VERSION_H #ifdef CONFIG_TESTING -#define OCARINA_NAME "ocarina-test" +#define OCARINA_NAME "ocarina-test/"CONFIG_TESTING_DIR #elif CONFIG_DEBUG #define OCARINA_NAME "ocarina-debug" #else diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..fa889ba1 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,11 @@ + +add_definitions(${DEBUG_OPTIONS} -DCONFIG_TESTING) + +# Handle files that depend on CONFIG_TESTING_DIR +list(REMOVE_ITEM core ${PROJECT_SOURCE_DIR}/core/file.c) +list(REMOVE_ITEM core ${PROJECT_SOURCE_DIR}/core/tags/album.c) +list(APPEND corefiles ${PROJECT_SOURCE_DIR}/core/file.c) +list(APPEND corefiles ${PROJECT_SOURCE_DIR}/core/tags/album.c) + +add_library(corelib OBJECT EXCLUDE_FROM_ALL ${core}) +add_subdirectory(core/) diff --git a/tests/UnitTest.cmake b/tests/UnitTest.cmake new file mode 100644 index 00000000..252a65ce --- /dev/null +++ b/tests/UnitTest.cmake @@ -0,0 +1,17 @@ + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY .) + +file(REMOVE .gitignore) +function(unit_test dir name lib sources) + string(TOLOWER ${name} lower) + string(TOLOWER ${dir}/${name} testname) + string(REPLACE "/" "_" targetname ${testname}) + + add_executable(${targetname} EXCLUDE_FROM_ALL ${lower}.c ${sources} $) + target_compile_definitions(${targetname} PUBLIC -DCONFIG_TESTING_DIR=\"${testname}\") + set_target_properties(${targetname} PROPERTIES OUTPUT_NAME ${lower}) + add_dependencies(tests ${targetname}) + + add_test(NAME ${dir}/${name} COMMAND tests/${testname} --verbose WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) + file(APPEND .gitignore "${lower}\n") +endfunction() diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt new file mode 100644 index 00000000..51f6dbb5 --- /dev/null +++ b/tests/core/CMakeLists.txt @@ -0,0 +1,7 @@ +Include(../UnitTest.cmake) + +function(core_unit_test name) + unit_test(Core ${name} corelib "${corefiles}") +endfunction() + +core_unit_test(Version) diff --git a/tests/core/Sconscript b/tests/core/Sconscript index ed59e212..414c87c5 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -24,7 +24,6 @@ def CoreTest(name): Export("core_objs", "CoreTest") -res += [ CoreTest("version") ] res += [ CoreTest("string") ] res += [ CoreTest("file") ] res += [ CoreTest("date") ] diff --git a/tests/core/version.c b/tests/core/version.c index d8961d51..3da7b1d5 100644 --- a/tests/core/version.c +++ b/tests/core/version.c @@ -2,14 +2,17 @@ * Copyright 2013 (c) Anna Schumaker. */ #include -#include +#include static void test_version() { - test_equal(get_version(), CONFIG_VERSION); - test_equal(OCARINA_NAME, "ocarina-test"); + g_assert_cmpstr(get_version(), ==, CONFIG_VERSION); + g_assert_cmpstr(OCARINA_NAME, ==, "ocarina-test/core/version"); } -DECLARE_UNIT_TESTS( - UNIT_TEST("Version", test_version), -); +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + g_test_add_func("/Core/Version", test_version); + return g_test_run(); +}