diff --git a/CMakeLists.txt b/CMakeLists.txt index 764cb75..f6b7edf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,23 +1,29 @@ cmake_minimum_required(VERSION 3.21) -project(xtest C) +project( + xtest + VERSION 0.1 + DESCRIPTION "A test framework for C" + LANGUAGES C +) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED TRUE) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror") - -add_compile_definitions(XTEST) - -include_directories(BEFORE ${CMAKE_SOURCE_DIR}/assert) -add_executable(example-all xtest.c examples/all.c examples/parameterized.c examples/assertions.c examples/expect_assertions.c examples/fail.c examples/groups.c examples/float.c examples/source.c examples/source.h examples/skip.c) -target_compile_definitions(example-all PRIVATE XTEST_ALL_EXAMPLES) +add_subdirectory(src) -add_executable(example-assertions xtest.c examples/assertions.c examples/source.c examples/source.h) -add_executable(example-expect_assertions xtest.c examples/expect_assertions.c examples/source.c examples/source.h) -add_executable(example-fail xtest.c examples/fail.c examples/source.c examples/source.h) -add_executable(example-float xtest.c examples/float.c examples/source.c examples/source.h) -add_executable(example-groups xtest.c examples/groups.c examples/source.c examples/source.h) -add_executable(example-parameterized xtest.c examples/parameterized.c examples/source.c examples/source.h) -add_executable(example-skip xtest.c examples/skip.c examples/source.c examples/source.h) +set(XTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + +function(add_xtest_executable TARGET) + add_executable(${TARGET} ${ARGN}) + target_include_directories(${TARGET} AFTER PRIVATE "${XTEST_SOURCE_DIR}/include/xtest") + target_include_directories(${TARGET} SYSTEM BEFORE PRIVATE "${XTEST_SOURCE_DIR}/include/xtest-assert") + target_link_libraries(${TARGET} xtest) +endfunction() + + +option(XTEST_BUILD_EXAMPLES "build the xtest examples" OFF) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + add_subdirectory(examples) +endif() \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..5024ef4 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,20 @@ +function(xtest_define_example NAME) + set(TARGET_NAME "example-${NAME}") + add_xtest_executable(${TARGET_NAME} source.c ${NAME}.c ${ARGN}) + if (NAME STREQUAL "all") + target_compile_definitions(${TARGET_NAME} PRIVATE XTEST_ALL_EXAMPLES) + endif () + target_compile_options(${TARGET_NAME} PRIVATE -Wall -pedantic) +endfunction() + + +if (XTEST_BUILD_EXAMPLES) + xtest_define_example(all assertions.c expect_assertions.c fail.c float.c groups.c parameterized.c skip.c) + xtest_define_example(assertions) + xtest_define_example(expect_assertions) + xtest_define_example(fail) + xtest_define_example(float) + xtest_define_example(groups) + xtest_define_example(parameterized) + xtest_define_example(skip) +endif () \ No newline at end of file diff --git a/examples/all.c b/examples/all.c index 642bf9b..174f4bf 100644 --- a/examples/all.c +++ b/examples/all.c @@ -1,4 +1,4 @@ -#include "../xtest.h" +#include "xtest.h" void example_assertions(); diff --git a/examples/assertions.c b/examples/assertions.c index 59b18a5..e1b3e7c 100644 --- a/examples/assertions.c +++ b/examples/assertions.c @@ -1,5 +1,5 @@ #include -#include "../xtest.h" +#include "xtest.h" #include "source.h" void test_assert(void *fixture, void **params) { diff --git a/examples/expect_assertions.c b/examples/expect_assertions.c index 210dbd7..3821540 100644 --- a/examples/expect_assertions.c +++ b/examples/expect_assertions.c @@ -1,4 +1,4 @@ -#include "../xtest.h" +#include "xtest.h" #include "source.h" void test_simple() { diff --git a/examples/fail.c b/examples/fail.c index 29679d8..6ba48ee 100644 --- a/examples/fail.c +++ b/examples/fail.c @@ -1,5 +1,5 @@ #include -#include "../xtest.h" +#include "xtest.h" void fail_assert() { xtest_assert(0); diff --git a/examples/float.c b/examples/float.c index ed773f1..f23fde1 100644 --- a/examples/float.c +++ b/examples/float.c @@ -1,5 +1,5 @@ #include -#include "../xtest.h" +#include "xtest.h" void test_float() { xtest_assert_float_is(1.0, 1.0, 0); diff --git a/examples/groups.c b/examples/groups.c index 6b427ff..45af64e 100644 --- a/examples/groups.c +++ b/examples/groups.c @@ -1,5 +1,5 @@ #include -#include "../xtest.h" +#include "xtest.h" void test_1() {} void test_2() {} diff --git a/examples/parameterized.c b/examples/parameterized.c index 189b4c2..3533dad 100644 --- a/examples/parameterized.c +++ b/examples/parameterized.c @@ -1,5 +1,5 @@ #include -#include "../xtest.h" +#include "xtest.h" #include "source.h" void test_add(void *fixture, void **params) { diff --git a/examples/skip.c b/examples/skip.c index 0f97a27..2f6849d 100644 --- a/examples/skip.c +++ b/examples/skip.c @@ -1,5 +1,4 @@ - -#include "../xtest.h" +#include "xtest.h" #include "source.h" void no_skip_1() {} diff --git a/examples/source.c b/examples/source.c index 3fe006d..36801d0 100644 --- a/examples/source.c +++ b/examples/source.c @@ -1,6 +1,6 @@ #include #include "source.h" -#include +#include "assert.h" int ret_1() { return 1; diff --git a/assert/assert.h b/include/xtest-assert/assert.h similarity index 100% rename from assert/assert.h rename to include/xtest-assert/assert.h diff --git a/xtest.h b/include/xtest/xtest.h similarity index 93% rename from xtest.h rename to include/xtest/xtest.h index f132fd1..dd013e4 100644 --- a/xtest.h +++ b/include/xtest/xtest.h @@ -6,12 +6,12 @@ #endif -#include #include +#include -typedef void (*xtest_test_fn)(void * fixture, void ** params); -typedef void (*xtest_setup_fn)(void ** fixture); -typedef void (*xtest_teardown_fn)(void * fixture); +typedef void (*xtest_test_fn)(void *fixture, void **params); +typedef void (*xtest_setup_fn)(void **fixture); +typedef void (*xtest_teardown_fn)(void *fixture); typedef struct xtest_param_s xtest_param; @@ -42,15 +42,17 @@ enum xtest_type { xtest_type_other, }; -void xtest_init(int argc, char**argv); +void xtest_init(int argc, char **argv); void xtest_fail_assert(const char *expression, const char *file, int line, void *expected, void *actual, _Bool invert, enum xtest_type type); void -xtest_assert_float(double expected, double actual, int precision, _Bool invert, const char *expression, const char *file, +xtest_assert_float(double expected, double actual, int precision, _Bool invert, const char *expression, + const char *file, int line); -void xtest_assert_mem(const char *expected, const char *actual, size_t length, _Bool invert, const char *expression, const char *file, +void xtest_assert_mem(const char *expected, const char *actual, size_t length, _Bool invert, const char *expression, + const char *file, int line); void xtest_internal_run(xtest_test_fn fn, const char *name, xtest_param *params, xtest_setup_fn setup, @@ -63,6 +65,7 @@ void xtest_skip(const char *reason); void xtest_expect_assertion_failure(); int xtest_complete(); + #define xtest_run(fn) xtest_internal_run(fn, #fn, (void*)0, (void*)0, (void*)0) #define xtest_run_parameterized(fn, params) xtest_internal_run(fn, #fn, params, (void*)0, (void*)0) #define xtest_run_with_fixture(fn, setup, teardown) xtest_internal_run(fn, #fn, (void*)0, setup, teardown) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..b0ca476 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ + +add_library(xtest xtest.c) +target_compile_definitions(xtest PRIVATE XTEST) +target_compile_options(xtest PRIVATE -Wall -pedantic) +target_include_directories(xtest PRIVATE ../extern/pcg ../include/xtest) + diff --git a/xtest.c b/src/xtest.c similarity index 96% rename from xtest.c rename to src/xtest.c index 032f27f..482287a 100644 --- a/xtest.c +++ b/src/xtest.c @@ -1,8 +1,13 @@ #include #include +#include +#include +#include + #include "xtest.h" #include -#include + + struct xtest_assert_info { const char *file; @@ -106,16 +111,15 @@ void xtest_init(int argc, char **argv) { int list_flag = 0; int help_flag = 0; const char *format = NULL; - const char *progname = argv[0]; struct option long_options[] = { - {"list", no_argument, &list_flag, 1}, // list the tests instead of running them - {"help", no_argument, &help_flag, 1}, // list the tests instead of running them - {"match", required_argument, 0, 'm'}, // only run tests that match the provided filter - {"output-format", required_argument, 0, 'f'}, // select output format - {0, 0, 0, 0} + {"list", no_argument, &list_flag, 1}, // list the tests instead of running them + {"help", no_argument, &help_flag, 1}, // list the tests instead of running them + {"match", required_argument, 0, 'm'}, // only run tests that match the provided filter + {"output-format", required_argument, 0, 'f'}, // select output format + {0, 0, 0, 0} }; while (1) { int option_index = 0; @@ -407,7 +411,8 @@ void xtest_fail_assert(const char *expression, const char *file, int line, void } void -xtest_assert_float(double expected, double actual, int precision, _Bool invert, const char *expression, const char *file, +xtest_assert_float(double expected, double actual, int precision, _Bool invert, const char *expression, + const char *file, int line) { double epsilon = 1.0; @@ -474,4 +479,4 @@ void xtest_internal_end_group() { void xtest_expect_assertion_failure() { expecting_assertion = 1; -} \ No newline at end of file +}