Mock test for keyboardinput and fixed the found issues

This commit is contained in:
Linus Probert 2018-05-18 13:49:31 +02:00
parent 2feeb5cbcf
commit 138cba1400
3 changed files with 53 additions and 3 deletions

View File

@ -219,6 +219,8 @@ IF (CMOCKA_FOUND)
add_executable(test_keyboardinput test/test_keyboardinput src/keyboardinput src/keyboard) add_executable(test_keyboardinput test/test_keyboardinput src/keyboardinput src/keyboard)
target_link_libraries(test_keyboardinput ${CMOCKA_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(test_keyboardinput ${CMOCKA_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
set_target_properties(test_keyboardinput PROPERTIES
LINK_FLAGS "-Wl,--wrap,keyboard_direction_press -Wl,--wrap,keyboard_press")
add_test(test_keyboardinput test_keyboardinput) add_test(test_keyboardinput test_keyboardinput)
ENDIF (CMOCKA_FOUND ) ENDIF (CMOCKA_FOUND )

View File

@ -43,7 +43,7 @@ keyboardinput_handle_event(KeyboardInput *input, SDL_Event *event)
} }
for (int i = SDLK_0; i <= SDLK_9; ++i) { for (int i = SDLK_0; i <= SDLK_9; ++i) {
if (keyboard_press(i, event)) if (keyboard_press(i, event))
input->currentState |= (1 << i); input->currentState |= (KEY_NUM0 << (i - SDLK_0));
} }
if (keyboard_direction_release(UP, event)) { if (keyboard_direction_release(UP, event)) {
@ -59,7 +59,7 @@ keyboardinput_handle_event(KeyboardInput *input, SDL_Event *event)
} }
for (int i = SDLK_0; i <= SDLK_9; ++i) { for (int i = SDLK_0; i <= SDLK_9; ++i) {
if (keyboard_release(i, event)) if (keyboard_release(i, event))
input->currentState &= ~(1 << i); input->currentState &= ~(KEY_NUM0 << (i - SDLK_0));
} }
} }

View File

@ -22,13 +22,58 @@
#include <setjmp.h> #include <setjmp.h>
#include <cmocka.h> #include <cmocka.h>
#include "../src/keyboardinput.h" #include "../src/keyboardinput.h"
#include "../src/keyboard.h"
static KeyboardInput input = { 0, 0 };
bool __wrap_keyboard_direction_press(Direction, SDL_Event*);
bool __wrap_keyboard_press(Uint64, SDL_Event*);
bool
__wrap_keyboard_direction_press(Direction d, SDL_Event *event)
{
(void) d;
(void) event;
return mock_type(bool);
}
bool
__wrap_keyboard_press(Uint64 key, SDL_Event *event)
{
(void) key;
(void) event;
return mock_type(bool);
}
static void
test_event_parse(void **state)
{
(void) state;
KeyboardInput input = { 0, 0 };
will_return(__wrap_keyboard_direction_press, true); // KEY_UP
will_return(__wrap_keyboard_press, true); // NUM0
will_return(__wrap_keyboard_press, false); // NUM1
will_return(__wrap_keyboard_press, false); // NUM2
will_return(__wrap_keyboard_press, false); // NUM3
will_return(__wrap_keyboard_press, false); // NUM4
will_return(__wrap_keyboard_press, false); // NUM5
will_return(__wrap_keyboard_press, false); // NUM6
will_return(__wrap_keyboard_press, false); // NUM7
will_return(__wrap_keyboard_press, false); // NUM8
will_return(__wrap_keyboard_press, false); // NUM9
SDL_Event event;
keyboardinput_handle_event(&input, &event);
assert_true(key_is_pressed(&input, KEY_UP));
assert_true(key_is_pressed(&input, KEY_NUM0));
}
static void static void
test_keypress(void **state) test_keypress(void **state)
{ {
(void) state; (void) state;
KeyboardInput input = { 0, 0 };
input.lastState = 0; input.lastState = 0;
input.currentState = KEY_UP; input.currentState = KEY_UP;
assert_true(key_is_pressed(&input, KEY_UP)); assert_true(key_is_pressed(&input, KEY_UP));
@ -38,6 +83,7 @@ static void
test_keyrelease(void **state) test_keyrelease(void **state)
{ {
(void) state; (void) state;
KeyboardInput input = { 0, 0 };
input.lastState = KEY_UP; input.lastState = KEY_UP;
input.currentState = 0; input.currentState = 0;
assert_true(key_is_released(&input, KEY_UP)); assert_true(key_is_released(&input, KEY_UP));
@ -47,6 +93,7 @@ static void
test_keydown(void **state) test_keydown(void **state)
{ {
(void) state; (void) state;
KeyboardInput input = { 0, 0 };
input.currentState = KEY_UP; input.currentState = KEY_UP;
assert_true(key_is_down(&input, KEY_UP)); assert_true(key_is_down(&input, KEY_UP));
} }
@ -58,6 +105,7 @@ int main(void)
cmocka_unit_test(test_keypress), cmocka_unit_test(test_keypress),
cmocka_unit_test(test_keyrelease), cmocka_unit_test(test_keyrelease),
cmocka_unit_test(test_keydown), cmocka_unit_test(test_keydown),
cmocka_unit_test(test_event_parse),
}; };
return cmocka_run_group_tests(tests, NULL, NULL); return cmocka_run_group_tests(tests, NULL, NULL);