restructured project files and CMakeLists.txt in a way that hopefully makes sense

This commit is contained in:
Gwendolyn 2022-01-06 02:06:45 +01:00
parent ec262ba62f
commit 7b8d48004e
15 changed files with 80 additions and 41 deletions

View file

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

20
examples/CMakeLists.txt Normal file
View file

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

View file

@ -1,4 +1,4 @@
#include "../xtest.h"
#include "xtest.h"
void example_assertions();

View file

@ -1,5 +1,5 @@
#include <stddef.h>
#include "../xtest.h"
#include "xtest.h"
#include "source.h"
void test_assert(void *fixture, void **params) {

View file

@ -1,4 +1,4 @@
#include "../xtest.h"
#include "xtest.h"
#include "source.h"
void test_simple() {

View file

@ -1,5 +1,5 @@
#include <stddef.h>
#include "../xtest.h"
#include "xtest.h"
void fail_assert() {
xtest_assert(0);

View file

@ -1,5 +1,5 @@
#include <stddef.h>
#include "../xtest.h"
#include "xtest.h"
void test_float() {
xtest_assert_float_is(1.0, 1.0, 0);

View file

@ -1,5 +1,5 @@
#include <stddef.h>
#include "../xtest.h"
#include "xtest.h"
void test_1() {}
void test_2() {}

View file

@ -1,5 +1,5 @@
#include <stddef.h>
#include "../xtest.h"
#include "xtest.h"
#include "source.h"
void test_add(void *fixture, void **params) {

View file

@ -1,5 +1,4 @@
#include "../xtest.h"
#include "xtest.h"
#include "source.h"
void no_skip_1() {}

View file

@ -1,6 +1,6 @@
#include <string.h>
#include "source.h"
#include <assert.h>
#include "assert.h"
int ret_1() {
return 1;

View file

@ -6,12 +6,12 @@
#endif
#include <stdio.h>
#include <string.h>
#include <stdint.h>
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)

6
src/CMakeLists.txt Normal file
View file

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

View file

@ -1,8 +1,13 @@
#include <setjmp.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include "xtest.h"
#include <assert.h>
#include <stdlib.h>
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;
}
}