From 901f7aef483996ee6ff0fc5bd52c1d00e865cafd Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Thu, 16 Feb 2023 15:45:08 +0800 Subject: [PATCH] Update input tests Ensure input test implementation is aligned to current input API. --- CMakeLists.txt | 9 ++ .../dependencies/SDL2/SDL_joystick.mock.h | 13 +- .../src/packages/config/IZ_config.mock.h | 15 ++ .../src/packages/stdinc/IZ_string.mock.h | 9 ++ src/packages/config/README.md | 10 ++ src/packages/game/input/IZ_joystick.c | 12 +- src/packages/game/input/IZ_keyboard.c | 5 + src/packages/game/input/IZ_midi.c | 5 + src/packages/game/input/input.test.c | 139 ++++-------------- src/packages/server/db/IZ_repo.c | 5 + 10 files changed, 100 insertions(+), 122 deletions(-) create mode 100644 __mocks__/src/packages/config/IZ_config.mock.h create mode 100644 src/packages/config/README.md diff --git a/CMakeLists.txt b/CMakeLists.txt index 1035eef..9529184 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -225,7 +225,16 @@ add_executable( src/packages/stdinc/IZ_string.h __mocks__/src/packages/stdinc/IZ_string.mock.h + src/packages/stdinc/IZ_stdlib.h + __mocks__/src/packages/stdinc/IZ_stdlib.mock.h + src/packages/game/input/input.test.c + __mocks__/src/packages/config/IZ_config.mock.h) + +target_link_libraries( + game-test-input + SDL2main + SDL2 ) add_executable( diff --git a/__mocks__/dependencies/SDL2/SDL_joystick.mock.h b/__mocks__/dependencies/SDL2/SDL_joystick.mock.h index 7dd5658..bc2ec10 100644 --- a/__mocks__/dependencies/SDL2/SDL_joystick.mock.h +++ b/__mocks__/dependencies/SDL2/SDL_joystick.mock.h @@ -2,15 +2,14 @@ #define SDL_JOYSTICK_MOCK_H #include +#include #include "../../../src/packages/common/IZ_common.h" -typedef struct _SDL_Joystick {} SDL_Joystick; - #define MOCK_OPEN_JOYSTICKS 1 mock(SDL_JoystickOpen) SDL_Joystick* SDL_JoystickOpen(i32 device_index) { - static SDL_Joystick joystick; - mock_return(SDL_JoystickOpen) &joystick; + static void* joystick = (void*) 1; + mock_return(SDL_JoystickOpen) (SDL_Joystick*) &joystick; } mock(SDL_NumJoysticks) i32 SDL_NumJoysticks(void) { @@ -25,4 +24,10 @@ mock(SDL_JoystickClose) void SDL_JoystickClose(SDL_Joystick* _joystick) { mock_return(SDL_JoystickClose); } +mock(SDL_JoystickGetGUID) SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick* joystick) { + mock_return(SDL_JoystickGetGUID) (SDL_JoystickGUID) { + .data = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, } + }; +} + #endif diff --git a/__mocks__/src/packages/config/IZ_config.mock.h b/__mocks__/src/packages/config/IZ_config.mock.h new file mode 100644 index 0000000..cd3d073 --- /dev/null +++ b/__mocks__/src/packages/config/IZ_config.mock.h @@ -0,0 +1,15 @@ +#ifndef IZ_CONFIG_MOCK_H +#define IZ_CONFIG_MOCK_H + +#include +#include "../../../src/packages/config/IZ_config.h" + +mock(IZ_ConfigInitialize) IZ_ConfigInitializeResult IZ_ConfigInitialize(IZ_ConfigItem[], const char*, u8, const char*[]) { + mock_return(IZ_ConfigInitialize) IZ_CONFIG_INITIALIZE_RESULT_OK; +} + +mock(IZ_ConfigSave) IZ_ConfigSaveResult IZ_ConfigSave(IZ_ConfigItem[], const char*) { + mock_return(IZ_ConfigSave) 0; +} + +#endif diff --git a/__mocks__/src/packages/stdinc/IZ_string.mock.h b/__mocks__/src/packages/stdinc/IZ_string.mock.h index 47eb80f..1de2c04 100644 --- a/__mocks__/src/packages/stdinc/IZ_string.mock.h +++ b/__mocks__/src/packages/stdinc/IZ_string.mock.h @@ -2,9 +2,18 @@ #define IZ_STRING_MOCK_C #include +#include "../../../src/packages/compat/IZ_windows.h" mock(IZ_memset) void* IZ_memset(void* dst, int c, size_t len) { mock_return(IZ_memset) 0; } +mock(IZ_memcpy) errno_t IZ_memcpy(void* const dest, const rsize_t dest_size, const void* const source, const rsize_t source_size) { + mock_return(IZ_memcpy) 0; +} + +mock(IZ_strlwr) errno_t IZ_strlwr(char* dest, const char* str, rsize_t str_size) { + mock_return(IZ_strlwr) 0; +} + #endif diff --git a/src/packages/config/README.md b/src/packages/config/README.md new file mode 100644 index 0000000..67ad110 --- /dev/null +++ b/src/packages/config/README.md @@ -0,0 +1,10 @@ +## Procedure + +Subsystem calls its initialize method. + +1. Subsystem copies hardcoded default values to its in-memory state container. +2. Subsystem defines the available config items it has, and how it is connected to various config sources (i.e. specify its section and key in the config file, as well as supplying command line options connected to this config item). +3. Subsystem binds the config items with their respective sections and keys in the config file (i.e. correctly point the values to the respective internal state container of the app). +4. Subsystem retrieves the config file values as well as command line arguments (higher priority) supplied to the app upon invocation. +5. Subsystem checks if there are some volatile state data to retrieve and syncs it with the state. +6. Subsystem saves the state to the config file. diff --git a/src/packages/game/input/IZ_joystick.c b/src/packages/game/input/IZ_joystick.c index 5f308b8..6075d2f 100644 --- a/src/packages/game/input/IZ_joystick.c +++ b/src/packages/game/input/IZ_joystick.c @@ -302,16 +302,16 @@ IZ_ProcedureResult IZ_JoystickInitialize(IZ_JoystickState(* state)[IZ_PLAYERS], IZ_memcpy(&(*state)[player_index].config.guid, sizeof(SDL_GUID), &joystick_guid, sizeof(SDL_GUID)); //(*state)[player_index].config.guid = joystick_guid; - printf("[INPUT:JOYSTICK] Initialize event from GUID: "); - for (u8 zz = 0; zz < 16; zz += 1) { - printf("%02x", (*state)[player_index].config.guid.data[zz]); - } - printf("\n"); +// printf("[INPUT:JOYSTICK] Initialize event from GUID: "); +// for (u8 zz = 0; zz < 16; zz += 1) { +// printf("%02x", (*state)[player_index].config.guid.data[zz]); +// } +// printf("\n"); } // Post config (after joystick GUIDs have been queried), this is unique to joysticks since they can be plugged in any // time. - IZ_ConfigSaveResult post_config_save_result = IZ_ConfigSave(joystick_config_items, config_path); + IZ_ConfigSaveResult post_config_save_result = IZ_JoystickSaveConfig(state, config_path); if (post_config_save_result < 0) { return -3; } diff --git a/src/packages/game/input/IZ_keyboard.c b/src/packages/game/input/IZ_keyboard.c index dcca2cf..8a5c867 100644 --- a/src/packages/game/input/IZ_keyboard.c +++ b/src/packages/game/input/IZ_keyboard.c @@ -92,5 +92,10 @@ IZ_ProcedureResult IZ_KeyboardInitialize(IZ_KeyboardState(* state)[IZ_PLAYERS], return -2; } + IZ_ConfigSaveResult post_config_save_result = IZ_KeyboardSaveConfig(state, config_path); + if (post_config_save_result < 0) { + return -3; + } + return 0; } diff --git a/src/packages/game/input/IZ_midi.c b/src/packages/game/input/IZ_midi.c index 4850a48..7a41f5f 100644 --- a/src/packages/game/input/IZ_midi.c +++ b/src/packages/game/input/IZ_midi.c @@ -181,6 +181,11 @@ IZ_ProcedureResult IZ_MIDIInputInitialize(IZ_MIDIInputState(* state)[IZ_PLAYERS] ); } + IZ_ConfigSaveResult post_config_save_result = IZ_MIDIInputSaveConfig(state, config_path); + if (post_config_save_result < 0) { + return -3; + } + return 0; } diff --git a/src/packages/game/input/input.test.c b/src/packages/game/input/input.test.c index da70c02..de2074a 100644 --- a/src/packages/game/input/input.test.c +++ b/src/packages/game/input/input.test.c @@ -5,6 +5,8 @@ #include "../../../../__mocks__/dependencies/minIni/minIni.mock.h" #include "../../../../__mocks__/dependencies/portmidi/portmidi.mock.h" #include "../../../../__mocks__/src/packages/stdinc/IZ_string.mock.h" +#include "../../../../__mocks__/src/packages/stdinc/IZ_stdlib.mock.h" +#include "../../../../__mocks__/src/packages/config/IZ_config.mock.h" #include "IZ_keyboard.h" #include "IZ_joystick.h" #include "IZ_midi.h" @@ -35,11 +37,11 @@ spec("input") { } after_each() { - mock_reset(ini_putl); + mock_reset(IZ_ConfigSave); } after_each() { - mock_reset(ini_getl); + mock_reset(IZ_ConfigInitialize); } it("sets initial state") { @@ -50,29 +52,15 @@ spec("input") { } it("calls load method") { - mock_set_expected_calls(ini_getl, ((IZ_CONTROLS - 4) + 2) * IZ_PLAYERS); - IZ_JoystickInitialize(&state, IZ_CONFIG_GAME_PATH, 0, NULL); - check( - mock_get_expected_calls(ini_getl) == mock_get_actual_calls(ini_getl), - "Call count mismatch for ini_getl() (expected %u, received %u).", - mock_get_expected_calls(ini_getl), - mock_get_actual_calls(ini_getl) - ); + check(mock_is_called(IZ_ConfigInitialize), "Config load function not called."); } it("calls save method") { - mock_set_expected_calls(ini_putl, ((IZ_CONTROLS - 4) + 2) * IZ_PLAYERS); - IZ_JoystickInitialize(&state, IZ_CONFIG_GAME_PATH, 0, NULL); - check( - mock_get_expected_calls(ini_putl) == mock_get_actual_calls(ini_putl), - "Call count mismatch for ini_putl() (expected %u, received %u).", - mock_get_expected_calls(ini_putl), - mock_get_actual_calls(ini_putl) - ); + check(mock_is_called(IZ_ConfigSave), "Config save function not called."); } it("opens device handles") { @@ -339,7 +327,7 @@ spec("input") { static IZ_JoystickState state[IZ_PLAYERS]; after_each() { - mock_reset(ini_putl); + mock_reset(IZ_ConfigSave); } before_each() { @@ -351,26 +339,19 @@ spec("input") { } it("calls save method") { - mock_set_expected_calls(ini_putl, ((IZ_CONTROLS - 4) + 2) * IZ_PLAYERS); - IZ_JoystickSaveConfig(&state, IZ_CONFIG_GAME_PATH); - check( - mock_get_expected_calls(ini_putl) == mock_get_actual_calls(ini_putl), - "Call count mismatch for ini_putl() (expected %u, received %u).", - mock_get_expected_calls(ini_putl), - mock_get_actual_calls(ini_putl) - ); + check(mock_is_called(IZ_ConfigSave), "Config save function not called."); } } describe("Teardown") { - static SDL_Joystick device; + static void* device = (void*) 1; static IZ_JoystickState state[IZ_PLAYERS] = {}; before_each() { for (u8 p = 0; p < IZ_PLAYERS; p += 1) { - state[p].device = &device; + state[p].device = device; } } @@ -402,11 +383,11 @@ spec("input") { // } after_each() { - mock_reset(ini_gets); + mock_reset(IZ_ConfigInitialize); } after_each() { - mock_reset(ini_puts); + mock_reset(IZ_ConfigSave); } before_each() { @@ -424,29 +405,15 @@ spec("input") { } it("calls load method") { - mock_set_expected_calls(ini_gets, IZ_CONTROLS * IZ_PLAYERS); - IZ_KeyboardInitialize(&state, IZ_CONFIG_GAME_PATH, 0, NULL); - check( - mock_get_expected_calls(ini_gets) == mock_get_actual_calls(ini_gets), - "Call count mismatch for ini_gets() (expected %u, received %u).", - mock_get_expected_calls(ini_gets), - mock_get_actual_calls(ini_gets) - ); + check(mock_is_called(IZ_ConfigInitialize), "Config load function not called."); } it("calls save method") { - mock_set_expected_calls(ini_puts, IZ_CONTROLS * IZ_PLAYERS); - IZ_KeyboardInitialize(&state, IZ_CONFIG_GAME_PATH, 0, NULL); - check( - mock_get_expected_calls(ini_puts) == mock_get_actual_calls(ini_puts), - "Call count mismatch for ini_puts() (expected %u, received %u).", - mock_get_expected_calls(ini_puts), - mock_get_actual_calls(ini_puts) - ); + check(mock_is_called(IZ_ConfigSave), "Config save function not called."); } } @@ -492,7 +459,7 @@ spec("input") { static IZ_KeyboardState state[IZ_PLAYERS] = {}; after_each() { - mock_reset(ini_puts); + mock_reset(IZ_ConfigSave); } before_each() { @@ -504,16 +471,9 @@ spec("input") { } it("calls save method") { - mock_set_expected_calls(ini_puts, IZ_CONTROLS * IZ_PLAYERS); - - IZ_KeyboardSaveConfig(IZ_CONFIG_GAME_PATH, &state); + IZ_KeyboardSaveConfig(&state, IZ_CONFIG_GAME_PATH); - check( - mock_get_expected_calls(ini_puts) == mock_get_actual_calls(ini_puts), - "Call count mismatch for ini_puts() (expected %u, received %u).", - mock_get_expected_calls(ini_puts), - mock_get_actual_calls(ini_puts) - ); + check(mock_is_called(IZ_ConfigSave), "Config save function not called."); } } } @@ -535,19 +495,19 @@ spec("input") { } after_each() { - mock_reset(ini_puts); + mock_reset(IZ_ConfigSave); } after_each() { - mock_reset(ini_gets); + mock_reset(IZ_ConfigInitialize); } after_each() { - mock_reset(ini_putl); + mock_reset(IZ_ConfigSave); } after_each() { - mock_reset(ini_getl); + mock_reset(IZ_ConfigInitialize); } it("sets initial state") { @@ -558,45 +518,15 @@ spec("input") { } it("calls load method") { - mock_set_expected_calls(ini_gets, IZ_CONTROLS * IZ_PLAYERS); - mock_set_expected_calls(ini_getl, 2 * IZ_PLAYERS); - IZ_MIDIInputInitialize(&state, IZ_CONFIG_GAME_PATH, 0, NULL); - check( - mock_get_expected_calls(ini_gets) == mock_get_actual_calls(ini_gets), - "Call count mismatch for ini_gets() (expected %u, received %u).", - mock_get_expected_calls(ini_gets), - mock_get_actual_calls(ini_gets) - ); - - check( - mock_get_expected_calls(ini_getl) == mock_get_actual_calls(ini_getl), - "Call count mismatch for ini_getl() (expected %u, received %u).", - mock_get_expected_calls(ini_getl), - mock_get_actual_calls(ini_getl) - ); + check(mock_is_called(IZ_ConfigInitialize), "Config load function not called."); } it("calls save method") { - mock_set_expected_calls(ini_puts, IZ_CONTROLS * IZ_PLAYERS); - mock_set_expected_calls(ini_putl, 2 * IZ_PLAYERS); - IZ_MIDIInputInitialize(&state, IZ_CONFIG_GAME_PATH, 0, NULL); - check( - mock_get_expected_calls(ini_puts) == mock_get_actual_calls(ini_puts), - "Call count mismatch for ini_puts() (expected %u, received %u).", - mock_get_expected_calls(ini_puts), - mock_get_actual_calls(ini_puts) - ); - - check( - mock_get_expected_calls(ini_putl) == mock_get_actual_calls(ini_putl), - "Call count mismatch for ini_putl() (expected %u, received %u).", - mock_get_expected_calls(ini_putl), - mock_get_actual_calls(ini_putl) - ); + check(mock_is_called(IZ_ConfigSave), "Config save function not called."); } it("opens device handles") { @@ -617,32 +547,17 @@ spec("input") { static IZ_MIDIInputState state[IZ_PLAYERS]; after_each() { - mock_reset(ini_puts); + mock_reset(IZ_ConfigSave); } after_each() { - mock_reset(ini_putl); + mock_reset(IZ_ConfigSave); } it("calls save method") { - mock_set_expected_calls(ini_puts, IZ_CONTROLS * IZ_PLAYERS); - mock_set_expected_calls(ini_putl, 2 * IZ_PLAYERS); + IZ_MIDIInputSaveConfig(&state, IZ_CONFIG_GAME_PATH); - IZ_MIDIInputSaveConfig(IZ_CONFIG_GAME_PATH, &state); - - check( - mock_get_expected_calls(ini_puts) == mock_get_actual_calls(ini_puts), - "Call count mismatch for ini_puts() (expected %u, received %u).", - mock_get_expected_calls(ini_puts), - mock_get_actual_calls(ini_puts) - ); - - check( - mock_get_expected_calls(ini_putl) == mock_get_actual_calls(ini_putl), - "Call count mismatch for ini_putl() (expected %u, received %u).", - mock_get_expected_calls(ini_putl), - mock_get_actual_calls(ini_putl) - ); + check(mock_is_called(IZ_ConfigSave), "Config save function not called."); } } diff --git a/src/packages/server/db/IZ_repo.c b/src/packages/server/db/IZ_repo.c index 3f3a615..df6fc3c 100644 --- a/src/packages/server/db/IZ_repo.c +++ b/src/packages/server/db/IZ_repo.c @@ -46,6 +46,11 @@ IZ_ProcedureResult IZ_RepoInitialize(IZ_RepoState* state, const char* config_pat IZ_strcat(config_path_dir, 128, "/"); IZ_strcat(config_path_dir, 128, state->config.path); sqlite3_open_v2(config_path_dir, &state->db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); + + IZ_ConfigSaveResult post_config_save_result = IZ_RepoSaveConfig(state, config_path); + if (post_config_save_result < 0) { + return -3; + } return 0; }