Add munit for unit testing.

Main method extracted to separate file to make it easier to test the _rest_ of the code.
munit is added as a submodule by running `cd src; git submodule add https://github.com/nemequ/munit.git` .

Unit testing has so far only been tested on ubuntu.
This commit is contained in:
welcor 2024-05-18 21:21:17 +02:00
parent 455e3a06ea
commit eb8e200a31
11 changed files with 280 additions and 1 deletions

75
src/test/Makefile Normal file
View file

@ -0,0 +1,75 @@
# Using µnit is very simple; just include the header and add the C
# file to your sources. That said, here is a simple Makefile to build
# the example.
CSTD:=99
OPENMP:=n
ASAN:=n
UBSAN:=n
EXTENSION:=
TEST_ENV:=
CFLAGS:=-Wall -Wno-char-subscripts -Wno-unused-but-set-variable
AGGRESSIVE_WARNINGS=n
LIBS:=-lcrypt
#ifeq ($(CC),pgcc)
# CFLAGS+=-c$(CSTD)
#else
# CFLAGS+=-std=c$(CSTD)
#endif
ifeq ($(OPENMP),y)
ifeq ($(CC),pgcc)
CFLAGS+=-mp
else
CFLAGS+=-fopenmp
endif
endif
ifneq ($(SANITIZER),)
CFLAGS+=-fsanitize=$(SANITIZER)
endif
ifneq ($(CC),pgcc)
ifeq ($(EXTRA_WARNINGS),y)
CFLAGS+=-Wall -Wextra -Werror
endif
ifeq ($(ASAN),y)
CFLAGS+=-fsanitize=address
endif
ifeq ($(UBSAN),y)
CFLAGS+=-fsanitize=undefined
endif
endif
MUNIT_FILES := ../munit/munit.h ../munit/munit.c
TEST_FILES := $(ls *.c)
# exclude main.c to avoid having multiple entrypoints.
OBJ_FILES_FROM_MUD_CODE != ls ../*.c | grep -v main.c | sed 's/\.c$$/.o/g' | xargs
OBJ_FILES_FORM_MUNIT:= ../munit/munit.o
OBJ_FILES_FROM_TESTS!= ls *.c | sed 's/\$$.c/.o/g' | xargs
OBJ_FILES := $(OBJ_FILES_FROM_MUD_CODE) $(OBJ_FILES_FORM_MUNIT) $(OBJ_FILES_FROM_TESTS) $(LIBS)
testfile: $(OBJ_FILES)
$(CC) $(CFLAGS) -o testfile $(OBJ_FILES)
test: testfile
$(TEST_ENV) ./testfile
$%.o: %.c
$(CC) $< $(CFLAGS) -c -o $@
clean:
rm -f *.o testfile depend
all: test
default: all
depend:
$(CC) -MM *.c > depend
-include depend

40
src/test/test_handler.c Normal file
View file

@ -0,0 +1,40 @@
#include "test_handler.h"
static void run_single_get_number_test(const char* input_param, const char *name_result, int number_result);
UNIT_TEST(test_get_number)
{
run_single_get_number_test("1.feather", "feather", 1);
run_single_get_number_test("2.feather", "feather", 2);
run_single_get_number_test("1.feat", "feat", 1);
run_single_get_number_test("feather", "feather", 1);
run_single_get_number_test("10.feather", "feather", 10);
return MUNIT_OK;
}
static void run_single_get_number_test(const char* input_param, const char *name_result, int number_result)
{
char *to_free;
char *input = to_free = strdup(input_param);
int number = get_number(&input);
munit_assert_int32(number, ==, number_result);
munit_assert_string_equal(input, name_result);
free(to_free);
}
/* Creating a test suite is pretty simple. First, you'll need an
* array of tests: */
MunitTest handler_c_tests[] = {
{
(char*) "/get_number",
test_get_number,
NULL, // setup callback
NULL, // cleanup callback
MUNIT_TEST_OPTION_NONE,
NULL
},
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

10
src/test/test_handler.h Normal file
View file

@ -0,0 +1,10 @@
#include "testrunner.h"
#ifndef TEST_HANDLER_H
#define TEST_HANDLER_H
extern MunitTest handler_c_tests[];
UNIT_TEST(test_get_number);
#endif

23
src/test/testrunner.c Normal file
View file

@ -0,0 +1,23 @@
#include "testrunner.h"
#include "test_handler.h"
static MunitSuite suites[] = {
{ "/handler.c", handler_c_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE },
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE }
};
static const MunitSuite test_suite = {
(char*) "",
/* The first parameter is the array of test suites. */
NULL,
/* The second an array of suites to trigger from this one */
suites,
MUNIT_SUITE_OPTION_NONE
};
int main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)]) {
/* Finally, we'll actually run our test suite! That second argument
* is the user_data parameter which will be passed either to the
* test or (if provided) the fixture setup function. */
return munit_suite_main(&test_suite, (void*) "µnit", argc, argv);
}

26
src/test/testrunner.h Normal file
View file

@ -0,0 +1,26 @@
#ifndef TESTRUNNER_H
#define TESTRUNNER_H
#include "../conf.h"
#include "../sysdep.h"
#include "../structs.h"
#include "../utils.h"
#include "../comm.h"
#include "../db.h"
#include "../handler.h"
#include "../screen.h"
#include "../interpreter.h"
#include "../spells.h"
#include "../dg_scripts.h"
#include "../act.h"
#include "../class.h"
#include "../fight.h"
#include "../quest.h"
#include "../mud_event.h"
#include "../munit/munit.h"
#define UNIT_TEST(test_name) MunitResult (test_name)(const MunitParameter params[], void* data)
#endif