Skip to content

Commit

Permalink
test: Add tests for all library types
Browse files Browse the repository at this point in the history
With the new CMake script, we can generate four types of library:
shared, static, original source, and amalgamation source. We add tests
to make sure all library types can work correctly.
  • Loading branch information
surreylabs committed Apr 2, 2019
1 parent 53de1bd commit 432a160
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 deletions.
50 changes: 18 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ project(${ARGTABLE3_PROJECT_NAME})

option(ARGTABLE3_ENABLE_CONAN "Enable Conan dependency manager" OFF)
option(ARGTABLE3_ENABLE_TESTS "Enable unit tests" ON)
option(ARGTABLE3_USE_AMALGAMATION "Use the amalgamation distribution" OFF)
option(ARGTABLE3_BUILD_STATIC_EXAMPLES "Build examples with the static argtable3 library" OFF)

get_filename_component(VERSION_TAG_PATH "version.tag" ABSOLUTE)
Expand All @@ -58,7 +57,6 @@ set(PROJECT_VERSION_TWEAK 0)
set(ARGTABLE3_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.${PROJECT_VERSION_TWEAK})
set(ARGTABLE3_FULL_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.${PROJECT_VERSION_BUILD})


if(ARGTABLE3_ENABLE_CONAN AND EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_basic_setup(TARGETS)
Expand All @@ -71,36 +69,24 @@ if(ARGTABLE3_ENABLE_TESTS)
enable_testing()
endif()

if(ARGTABLE3_USE_AMALGAMATION)
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/dist/argtable3.c")
execute_process(
COMMAND "${CMAKE_SOURCE_DIR}/tools/build" dist
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/tools"
)
endif()

set(ARGTABLE3_SRC_FILES
${CMAKE_SOURCE_DIR}/dist/argtable3.c
)
else()
set(ARGTABLE3_SRC_FILES
${CMAKE_SOURCE_DIR}/src/arg_cmd.c
${CMAKE_SOURCE_DIR}/src/arg_date.c
${CMAKE_SOURCE_DIR}/src/arg_dbl.c
${CMAKE_SOURCE_DIR}/src/arg_dstr.c
${CMAKE_SOURCE_DIR}/src/arg_end.c
${CMAKE_SOURCE_DIR}/src/arg_file.c
${CMAKE_SOURCE_DIR}/src/arg_hashtable.c
${CMAKE_SOURCE_DIR}/src/arg_int.c
${CMAKE_SOURCE_DIR}/src/arg_lit.c
${CMAKE_SOURCE_DIR}/src/arg_rem.c
${CMAKE_SOURCE_DIR}/src/arg_rex.c
${CMAKE_SOURCE_DIR}/src/arg_str.c
${CMAKE_SOURCE_DIR}/src/arg_utils.c
${CMAKE_SOURCE_DIR}/src/argtable3.c
${CMAKE_SOURCE_DIR}/src/getopt_long.c
)
endif()
set(ARGTABLE3_AMALGAMATION_SRC_FILE ${PROJECT_SOURCE_DIR}/dist/argtable3.c)
set(ARGTABLE3_SRC_FILES
${PROJECT_SOURCE_DIR}/src/arg_cmd.c
${PROJECT_SOURCE_DIR}/src/arg_date.c
${PROJECT_SOURCE_DIR}/src/arg_dbl.c
${PROJECT_SOURCE_DIR}/src/arg_dstr.c
${PROJECT_SOURCE_DIR}/src/arg_end.c
${PROJECT_SOURCE_DIR}/src/arg_file.c
${PROJECT_SOURCE_DIR}/src/arg_hashtable.c
${PROJECT_SOURCE_DIR}/src/arg_int.c
${PROJECT_SOURCE_DIR}/src/arg_lit.c
${PROJECT_SOURCE_DIR}/src/arg_rem.c
${PROJECT_SOURCE_DIR}/src/arg_rex.c
${PROJECT_SOURCE_DIR}/src/arg_str.c
${PROJECT_SOURCE_DIR}/src/arg_utils.c
${PROJECT_SOURCE_DIR}/src/argtable3.c
${PROJECT_SOURCE_DIR}/src/getopt_long.c
)

# Platform specific settings for installation
if(UNIX)
Expand Down
8 changes: 3 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################


# Get all the .c files in the examples directory
file(GLOB EXAMPLES_SOURCES RELATIVE ${CMAKE_SOURCE_DIR}/examples *.c)
file(GLOB EXAMPLES_SOURCES RELATIVE ${PROJECT_SOURCE_DIR}/examples *.c)

if(NOT ARGTABLE3_BUILD_STATIC_EXAMPLES)
add_definitions(-Dargtable3_IMPORTS)
Expand All @@ -42,8 +40,8 @@ endif()

foreach(examples_src ${EXAMPLES_SOURCES})
string(REPLACE ".c" "" examplename ${examples_src})
add_executable(${examplename} ${CMAKE_SOURCE_DIR}/examples/${examples_src})
target_include_directories(${examplename} PRIVATE ${CMAKE_SOURCE_DIR}/src)
add_executable(${examplename} ${PROJECT_SOURCE_DIR}/examples/${examples_src})
target_include_directories(${examplename} PRIVATE ${PROJECT_SOURCE_DIR}/src)
if(ARGTABLE3_BUILD_STATIC_EXAMPLES)
target_link_libraries(${examplename} argtable3_static ${ARGTABLE3_EXTRA_LIBS})
else()
Expand Down
50 changes: 43 additions & 7 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################

set(TEST_SRC_FILES
set(TEST_PUBLIC_SRC_FILES
testall.c
testarglit.c
testargstr.c
Expand All @@ -37,17 +37,53 @@ set(TEST_SRC_FILES
testargdbl.c
testargfile.c
testargrex.c
testarghashtable.c
testargdstr.c
testargcmd.c
CuTest.c
${ARGTABLE3_SRC_FILES}
)

add_executable(testall ${TEST_SRC_FILES})
target_include_directories(testall PRIVATE ${CMAKE_SOURCE_DIR}/src)
set(TEST_SRC_FILES
${TEST_PUBLIC_SRC_FILES}
testarghashtable.c
)

if(UNIX)
target_link_libraries(testall m)
set(ARGTABLE3_EXTRA_LIBS m)
endif()

add_test(NAME testall COMMAND "$<TARGET_FILE:testall>")
add_executable(test_shared ${TEST_PUBLIC_SRC_FILES})
target_compile_definitions(test_shared PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY)
target_include_directories(test_shared PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(test_shared argtable3 ${ARGTABLE3_EXTRA_LIBS})
add_custom_command(TARGET test_shared POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE:argtable3>"
"$<TARGET_FILE_DIR:test_shared>"
)

add_executable(test_static ${TEST_SRC_FILES})
target_compile_definitions(test_static PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY)
target_include_directories(test_static PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(test_static argtable3_static ${ARGTABLE3_EXTRA_LIBS})

add_executable(test_src ${TEST_SRC_FILES} ${ARGTABLE3_SRC_FILES})
target_include_directories(test_src PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(test_src ${ARGTABLE3_EXTRA_LIBS})

add_custom_command(OUTPUT ${ARGTABLE3_AMALGAMATION_SRC_FILE}
COMMAND "${PROJECT_SOURCE_DIR}/tools/build" dist
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools"
)

add_executable(test_amalgamation ${TEST_SRC_FILES} ${ARGTABLE3_AMALGAMATION_SRC_FILE})
target_include_directories(test_amalgamation PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(test_amalgamation ${ARGTABLE3_EXTRA_LIBS})
add_custom_command(TARGET test_amalgamation PRE_BUILD
COMMAND "${PROJECT_SOURCE_DIR}/tools/build" dist
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools"
)

add_test(NAME test_shared COMMAND "$<TARGET_FILE:test_shared>")
add_test(NAME test_static COMMAND "$<TARGET_FILE:test_static>")
add_test(NAME test_src COMMAND "$<TARGET_FILE:test_src>")
add_test(NAME test_amalgamation COMMAND "$<TARGET_FILE:test_amalgamation>")
9 changes: 7 additions & 2 deletions tests/testall.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ CuSuite* get_argdate_testsuite();
CuSuite* get_argdbl_testsuite();
CuSuite* get_argfile_testsuite();
CuSuite* get_argrex_testsuite();
CuSuite* get_arghashtable_testsuite();
CuSuite* get_argdstr_testsuite();
CuSuite* get_argcmd_testsuite();

#ifndef ARGTABLE3_TEST_PUBLIC_ONLY
CuSuite* get_arghashtable_testsuite();
#endif

void RunAllTests(void) {
CuString* output = CuStringNew();
CuSuite* suite = CuSuiteNew();
Expand All @@ -54,9 +57,11 @@ void RunAllTests(void) {
CuSuiteAddSuite(suite, get_argdbl_testsuite());
CuSuiteAddSuite(suite, get_argfile_testsuite());
CuSuiteAddSuite(suite, get_argrex_testsuite());
CuSuiteAddSuite(suite, get_arghashtable_testsuite());
CuSuiteAddSuite(suite, get_argdstr_testsuite());
CuSuiteAddSuite(suite, get_argcmd_testsuite());
#ifndef ARGTABLE3_TEST_PUBLIC_ONLY
CuSuiteAddSuite(suite, get_arghashtable_testsuite());
#endif

CuSuiteRun(suite);
CuSuiteSummary(suite, output);
Expand Down

0 comments on commit 432a160

Please sign in to comment.