test: add tests for characters

This commit is contained in:
Sebastian Krzyszkowiak 2019-10-06 08:02:04 +02:00
parent b8d44792f1
commit 1d6dc5e0d0
No known key found for this signature in database
GPG key ID: E8F235CF3BDBC3FF
5 changed files with 88 additions and 8 deletions

View file

@ -2,7 +2,7 @@ find_package(CMocka)
if (CMOCKA_FOUND) if (CMOCKA_FOUND)
set(CMAKE_INSTALL_RPATH "\$ORIGIN/../src") 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) target_link_libraries(engine-tests cmocka libsuperderpy)
else(CMOCKA_FOUND) else(CMOCKA_FOUND)
message("CMocka not found; tests disabled.") message("CMocka not found; tests disabled.")

76
test/character.c Normal file
View file

@ -0,0 +1,76 @@
/*
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "tests.h"
#include <stdlib.h>
#include <sys/stat.h>
// -----------------------------------------
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);
}

View file

@ -17,18 +17,23 @@
#include "tests.h" #include "tests.h"
static struct Game* game = NULL;
int engine_setup(void** state) { int engine_setup(void** state) {
char* args[2] = {"", "--debug"}; *state = game;
*state = libsuperderpy_init(2, args, "test", (struct Params){});
libsuperderpy_start(*state);
return 0; return 0;
} }
int engine_teardown(void** state) { int engine_teardown(void** state) {
libsuperderpy_destroy(*state);
return 0; return 0;
} }
int main(int argc, char** argv) { 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;
} }

View file

@ -33,5 +33,6 @@
int engine_setup(void** state); int engine_setup(void** state);
int engine_teardown(void** state); int engine_teardown(void** state);
int test_timeline(void); int test_timeline(void);
int test_character(void);
#endif #endif

View file

@ -412,8 +412,6 @@ static void timeline_destroyed_while_running(void** state) {
} }
int test_timeline(void) { int test_timeline(void) {
al_set_app_name("libsuperderpy");
const struct CMUnitTest timeline_tests[] = { const struct CMUnitTest timeline_tests[] = {
cmocka_unit_test(timeline_action), cmocka_unit_test(timeline_action),
cmocka_unit_test(timeline_action_lifecycle), cmocka_unit_test(timeline_action_lifecycle),