summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2021-04-17 01:27:27 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2021-10-28 02:22:57 +0200
commit1f9826faefa2b1a1bddac3f3413a6d6055ee49bf (patch)
treec5c7322b7629211dab22d2f50c83e7263d52db2b
parentd8336885a4c053544fe3491335da0f76afffdfb9 (diff)
Add microunit test framework
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
-rw-r--r--.gitignore1
-rw-r--r--Makefile42
-rw-r--r--README.md22
-rw-r--r--test/controller-test.c89
m---------third_party/munit0
5 files changed, 141 insertions, 13 deletions
diff --git a/.gitignore b/.gitignore
index ff853ac..1f44161 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
*.exe
*.o
+/lib
/build
blast_attack
diff --git a/Makefile b/Makefile
index 6c615f3..2433900 100644
--- a/Makefile
+++ b/Makefile
@@ -1,20 +1,25 @@
-TARGET ?= blast_attack
-SOURCE_DIR ?= ./src
-BUILD_DIR ?= ./build
-
-CFLAGS := -Wall
-OBJS := $(BUILD_DIR)/main.o \
- $(BUILD_DIR)/controller.o
+TARGET ?= blast_attack
+SOURCE_DIR ?= ./src
+TEST_DIR ?= ./test
+LIB_DIR ?= ./lib
+BUILD_DIR ?= ./build
+
+CFLAGS := -Wall
+CFLAGS_TEST := -Isrc -Ithird_party -Llib -lmunit
+SRCS := $(wildcard $(SOURCE_DIR)/*.c)
+SRCS_TEST := $(wildcard $(TEST_DIR)/*.c)
+OBJS := $(patsubst $(SOURCE_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS))
+TESTS := $(patsubst $(TEST_DIR)/%-test.c, $(TEST_DIR)/%, $(SRCS_TEST))
ifeq ($(OS),Windows_NT)
CC := gcc
CFLAGS += -lmingw32 -lSDL2main -lSDL2 \
-IG:\dev\mingw\include\SDL2
else
- CFLAGS += -Wall $(shell pkg-config sdl2 --cflags --libs)
+ CFLAGS += $(shell pkg-config sdl2 --cflags --libs)
endif
-.PHONY: build clean run $(TARGET)
+.PHONY: build clean run test $(TARGET)
all: $(TARGET)
@@ -24,12 +29,27 @@ $(TARGET): $(BUILD_DIR) $(OBJS)
$(BUILD_DIR):
@mkdir -p $@
+$(LIB_DIR):
+ @mkdir -p $@
+
$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
+$(TEST_DIR)/%-test: $(BUILD_DIR)/%.o $(TEST_DIR)/%-test.c
+ $(CC) $(CFLAGS) $(CFLAGS_TEST) $? -o $@
+
+$(TEST_DIR)/%: $(TEST_DIR)/%-test
+ ./$@-test
+
+test: lib/libmunit.so $(BUILD_DIR) $(TESTS)
+
+lib/libmunit.so: $(LIB_DIR) third_party/munit/munit.c
+ $(CC) -Wall -Werror -fpic -c third_party/munit/munit.c -shared -o lib/libmunit.so
+
clean:
- @rm -rf $(OBJS)
+ @rm -rf $(BUILD_DIR)
+ @rm -rf $(LIB_DIR)
@rm $(TARGET)
-run:
+run: all
./$(TARGET)
diff --git a/README.md b/README.md
index 0f4e4bc..99cb855 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,20 @@
-Game
-====
+Black Attack
+------------
+
+## Dependencies
+
+- make
+- SDL2
+- pkg-config
+
+## Run
+
+```shell
+$ make run
+```
+
+## Tests
+
+```shell
+$ make test
+```
diff --git a/test/controller-test.c b/test/controller-test.c
new file mode 100644
index 0000000..fb13000
--- /dev/null
+++ b/test/controller-test.c
@@ -0,0 +1,89 @@
+#define MUNIT_ENABLE_ASSERT_ALIASES
+#include "controller.h"
+#include <munit/munit.h>
+
+static SDL_Event
+an_event(SDL_EventType type, SDL_KeyCode keycode) {
+ SDL_Event event;
+ event.type = type;
+ event.key.keysym.sym = keycode;
+ return event;
+}
+
+static MunitResult
+press_and_release_up(const MunitParameter params[], void* fixture)
+{
+ controller_t ctrl = {0};
+
+ SDL_Event event = an_event(SDL_KEYDOWN, SDLK_UP);
+ controller_update(&ctrl, &event);
+ assert_true(ctrl.up);
+
+ event = an_event(SDL_KEYUP, SDLK_UP);
+ controller_update(&ctrl, &event);
+ assert_false(ctrl.up);
+ return MUNIT_OK;
+}
+
+static MunitResult
+press_and_release_down(const MunitParameter params[], void* fixture)
+{
+ controller_t ctrl = {0};
+
+ SDL_Event event = an_event(SDL_KEYDOWN, SDLK_DOWN);
+ controller_update(&ctrl, &event);
+ assert_true(ctrl.down);
+
+ event = an_event(SDL_KEYUP, SDLK_DOWN);
+ controller_update(&ctrl, &event);
+ assert_false(ctrl.down);
+ return MUNIT_OK;
+}
+
+static MunitResult
+press_and_release_right(const MunitParameter params[], void* fixture)
+{
+ controller_t ctrl = {0};
+
+ SDL_Event event = an_event(SDL_KEYDOWN, SDLK_RIGHT);
+ controller_update(&ctrl, &event);
+ assert_true(ctrl.right);
+
+ event = an_event(SDL_KEYUP, SDLK_RIGHT);
+ controller_update(&ctrl, &event);
+ assert_false(ctrl.right);
+ return MUNIT_OK;
+}
+
+static MunitResult
+press_and_release_left(const MunitParameter params[], void* fixture)
+{
+ controller_t ctrl = {0};
+
+ SDL_Event event = an_event(SDL_KEYDOWN, SDLK_LEFT);
+ controller_update(&ctrl, &event);
+ assert_true(ctrl.left);
+
+ event = an_event(SDL_KEYUP, SDLK_LEFT);
+ controller_update(&ctrl, &event);
+ assert_false(ctrl.left);
+ return MUNIT_OK;
+}
+
+MunitTest tests[] = {
+ { "/press_and_release_up", press_and_release_up, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/press_and_release_down", press_and_release_down, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/press_and_release_right", press_and_release_right, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/press_and_release_left", press_and_release_left, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
+};
+
+static const MunitSuite suite = {
+ "/controller", tests, NULL, 1, MUNIT_TEST_OPTION_NONE
+};
+
+int
+main(int argc, char *argv[])
+{
+ return munit_suite_main(&suite, NULL, argc, argv);
+}
diff --git a/third_party/munit b/third_party/munit
new file mode 160000
+Subproject fbbdf1467eb0d04a6ee465def2e529e4c87f211