From 498051b45459de77876eceed3e5d755d72831235 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Tue, 17 May 2022 11:58:33 +0800 Subject: [PATCH] Add mocks and stubs Include mocks and stubs implementation in config unit tests. --- CMakeLists.txt | 7 -- src/packages/game-test/config/IZ_config.c | 105 ++++++++++++++++++++++ src/packages/game/config/IZ_config.c | 7 -- src/packages/game/config/IZ_config.h | 8 +- 4 files changed, 112 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f347a50..e4efedb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,18 +43,11 @@ target_link_libraries( add_executable( game-test - dependencies/minIni/dev/minIni.h - dependencies/minIni/dev/minIni.c src/packages/game/config/IZ_config.h src/packages/game/config/IZ_config.c dependencies/bdd-for-c/bdd-for-c.h src/packages/game-test/config/IZ_config.c ) -target_link_libraries( - game-test - SDL2main - SDL2 -) if (WIN32) add_custom_command(TARGET game POST_BUILD diff --git a/src/packages/game-test/config/IZ_config.c b/src/packages/game-test/config/IZ_config.c index 45026b3..d38038f 100644 --- a/src/packages/game-test/config/IZ_config.c +++ b/src/packages/game-test/config/IZ_config.c @@ -1,13 +1,91 @@ #include "bdd-for-c.h" #include "../../game/config/IZ_config.h" +static uint8_t calls_SDL_GetBasePath = 0; +char* SDL_GetBasePath() { + calls_SDL_GetBasePath += 1; + return ""; +} + +static uint8_t calls_SDL_GetKeyName = 0; +const char* SDL_GetKeyName(SDL_KeyCode code) { + calls_SDL_GetKeyName += 1; + return ""; +} + +static uint8_t calls_SDL_GetKeyFromName = 0; +SDL_KeyCode SDL_GetKeyFromName(const char* name) { + calls_SDL_GetKeyFromName += 1; + return 0; +} + +static uint8_t calls_ini_gets = 0; +int ini_gets( + const char *Section, + const char *Key, + const char *DefValue, + char *Buffer, + int BufferSize, + const char *Filename +) { + calls_ini_gets += 1; + return 0; +} + +static uint8_t calls_ini_getl = 0; +long ini_getl( + const TCHAR *Section, + const TCHAR *Key, + long DefValue, + const TCHAR *Filename +) { + calls_ini_getl += 1; + return DefValue; +} + spec("config") { describe("LoadConfig") { static IZ_Config config; + after_each() { + calls_ini_getl = 0; + } + + after_each() { + calls_ini_gets = 0; + } + + after_each() { + calls_SDL_GetKeyFromName = 0; + } + + after_each() { + calls_SDL_GetKeyName = 0; + } + + after_each() { + calls_SDL_GetBasePath = 0; + } + it("should load default config values") { IZ_LoadConfig(&config); + check( + calls_SDL_GetBasePath > 0, + "SDL_GetBasePath() not called." + ); + + static const int expected_calls_ini_getl = + 3 // video params + + 1 // input params + + (12 * PLAYERS); // joystick controls + check( + calls_ini_getl == expected_calls_ini_getl, + "Call count mismatch for ini_getl() (expected %u, received %u).", + expected_calls_ini_getl, + calls_ini_getl + ); + check( config.video.width == 640, "Default value for Video.Width is not loaded." @@ -25,6 +103,33 @@ spec("config") { config.input.gamepad_axis_threshold == 8000, "Default value for Input.GamepadAxisThreshold is not loaded." ); + + static const int expected_calls_ini_gets = + (16 * PLAYERS); // keyboard controls + check( + calls_ini_gets == expected_calls_ini_gets, + "Call count mismatch for ini_gets() (expected %u, received %u).", + expected_calls_ini_gets, + calls_ini_gets + ); + + static const int expected_calls_SDL_GetKeyFromName = + (16 * PLAYERS); // keyboard controls + check( + calls_SDL_GetKeyFromName == expected_calls_SDL_GetKeyFromName, + "Call count mismatch for SDL_GetKeyFromName() (expected %u, received %u).", + expected_calls_SDL_GetKeyFromName, + calls_SDL_GetKeyFromName + ); + + static const int expected_calls_SDL_GetKeyName = + (16 * PLAYERS); // keyboard controls + check( + calls_SDL_GetKeyName == expected_calls_SDL_GetKeyName, + "Call count mismatch for SDL_GetKeyName() (expected %u, received %u).", + expected_calls_SDL_GetKeyName, + calls_SDL_GetKeyName + ); } } } diff --git a/src/packages/game/config/IZ_config.c b/src/packages/game/config/IZ_config.c index 0d92f66..06683e7 100644 --- a/src/packages/game/config/IZ_config.c +++ b/src/packages/game/config/IZ_config.c @@ -1,10 +1,3 @@ -#include -#include -#include -#include -#include - -#include "../IZ_action.h" #include "IZ_config.h" void IZ_GetConfigPath(char* config_path) { diff --git a/src/packages/game/config/IZ_config.h b/src/packages/game/config/IZ_config.h index aa07b51..5b170e7 100644 --- a/src/packages/game/config/IZ_config.h +++ b/src/packages/game/config/IZ_config.h @@ -1,8 +1,14 @@ #ifndef IZ_CONFIG_H #define IZ_CONFIG_H -#include "SDL_keycode.h" +#include +#include +#include +#include +#include + #include "../IZ_common.h" +#include "../IZ_action.h" typedef struct { uint16_t width;