tests: Build version test with CTest

This begins the conversion to CTest and the GLib unit test framework.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-03 08:17:12 -05:00 committed by Anna Schumaker
parent 0e171012ce
commit 2f559b2fb3
8 changed files with 61 additions and 9 deletions

2
.gitignore vendored
View File

@ -18,3 +18,5 @@ CMakeFiles
Makefile
cmake_install.cmake
install_manifest.txt
CTestTestfile.cmake
Testing/

View File

@ -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()

View File

@ -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

11
tests/CMakeLists.txt Normal file
View File

@ -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/)

17
tests/UnitTest.cmake Normal file
View File

@ -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_OBJECTS:${lib}>)
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()

View File

@ -0,0 +1,7 @@
Include(../UnitTest.cmake)
function(core_unit_test name)
unit_test(Core ${name} corelib "${corefiles}")
endfunction()
core_unit_test(Version)

View File

@ -24,7 +24,6 @@ def CoreTest(name):
Export("core_objs", "CoreTest")
res += [ CoreTest("version") ]
res += [ CoreTest("string") ]
res += [ CoreTest("file") ]
res += [ CoreTest("date") ]

View File

@ -2,14 +2,17 @@
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/version.h>
#include <tests/test.h>
#include <glib.h>
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();
}