From 1d6dc5e0d031ba7da7c64e06352e35ee58484f2f Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sun, 6 Oct 2019 08:02:04 +0200 Subject: [PATCH] test: add tests for characters --- test/CMakeLists.txt | 2 +- test/character.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ test/tests.c | 15 ++++++--- test/tests.h | 1 + test/timeline.c | 2 -- 5 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 test/character.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f445bb9..8dcffe5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,7 +2,7 @@ find_package(CMocka) if (CMOCKA_FOUND) set(CMAKE_INSTALL_RPATH "\$ORIGIN/../src") - add_executable(engine-tests tests.c timeline.c) + add_executable(engine-tests tests.c timeline.c character.c) target_link_libraries(engine-tests cmocka libsuperderpy) else(CMOCKA_FOUND) message("CMocka not found; tests disabled.") diff --git a/test/character.c b/test/character.c new file mode 100644 index 0000000..f71090e --- /dev/null +++ b/test/character.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) Sebastian Krzyszkowiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "tests.h" +#include +#include + +// ----------------------------------------- + +static char* anim = "[animation]\nduration=100\nframes=3\nrepeats=0\nfile=dummy\n"; +static char* tmpdir = NULL; + +static int character_setup(void** state) { + char dir[255] = "libsuperderpy-XXXXXX"; + mkdtemp(dir); + chdir(dir); + mkdir("data", 0700); + mkdir("data/sprites", 0700); + mkdir("data/sprites/test", 0700); + FILE* file = fopen("data/sprites/test/animation.ini", "we"); + fputs(anim, file); + fclose(file); + tmpdir = strdup(dir); + return engine_setup(state); +} + +static int character_teardown(void** state) { + unlink("data/sprites/test/animation.ini"); + rmdir("data/sprites/test"); + rmdir("data/sprites"); + rmdir("data"); + chdir(".."); + rmdir(tmpdir); + free(tmpdir); + return engine_teardown(state); +} + +// ----------------------------------------- + +static void character_spritesheet_stops(void** state) { + struct Game* game = *state; + struct Character* character = CreateCharacter(game, "test"); + RegisterSpritesheet(game, character, "animation"); + SelectSpritesheet(game, character, "animation"); + + assert_int_equal(character->pos, 0); + AnimateCharacter(game, character, 0.1, 1.0); + assert_int_equal(character->pos, 1); + AnimateCharacter(game, character, 0.1, 1.0); + assert_int_equal(character->pos, 2); + AnimateCharacter(game, character, 0.1, 1.0); + assert_int_equal(character->pos, 2); + AnimateCharacter(game, character, 0.1, 1.0); + assert_int_equal(character->pos, 2); +} + +int test_character(void) { + const struct CMUnitTest character_tests[] = { + cmocka_unit_test(character_spritesheet_stops), + }; + return cmocka_run_group_tests(character_tests, character_setup, character_teardown); +} diff --git a/test/tests.c b/test/tests.c index 88e8310..a5c1520 100644 --- a/test/tests.c +++ b/test/tests.c @@ -17,18 +17,23 @@ #include "tests.h" +static struct Game* game = NULL; + int engine_setup(void** state) { - char* args[2] = {"", "--debug"}; - *state = libsuperderpy_init(2, args, "test", (struct Params){}); - libsuperderpy_start(*state); + *state = game; return 0; } int engine_teardown(void** state) { - libsuperderpy_destroy(*state); return 0; } int main(int argc, char** argv) { - return test_timeline(); + al_set_app_name("libsuperderpy"); + char* args[2] = {"", "--debug"}; + game = libsuperderpy_init(2, args, "test", (struct Params){}); + libsuperderpy_start(game); + int ret = test_timeline() || test_character(); + libsuperderpy_destroy(game); + return ret; } diff --git a/test/tests.h b/test/tests.h index 64bdaa9..b073961 100644 --- a/test/tests.h +++ b/test/tests.h @@ -33,5 +33,6 @@ int engine_setup(void** state); int engine_teardown(void** state); int test_timeline(void); +int test_character(void); #endif diff --git a/test/timeline.c b/test/timeline.c index e6b7e32..0e42b96 100644 --- a/test/timeline.c +++ b/test/timeline.c @@ -412,8 +412,6 @@ static void timeline_destroyed_while_running(void** state) { } int test_timeline(void) { - al_set_app_name("libsuperderpy"); - const struct CMUnitTest timeline_tests[] = { cmocka_unit_test(timeline_action), cmocka_unit_test(timeline_action_lifecycle),