Browse Source

Add mocks and stubs

Include mocks and stubs implementation in config unit tests.
master
TheoryOfNekomata 2 years ago
parent
commit
498051b454
4 changed files with 112 additions and 15 deletions
  1. +0
    -7
      CMakeLists.txt
  2. +105
    -0
      src/packages/game-test/config/IZ_config.c
  3. +0
    -7
      src/packages/game/config/IZ_config.c
  4. +7
    -1
      src/packages/game/config/IZ_config.h

+ 0
- 7
CMakeLists.txt View File

@@ -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


+ 105
- 0
src/packages/game-test/config/IZ_config.c View File

@@ -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
);
}
}
}

+ 0
- 7
src/packages/game/config/IZ_config.c View File

@@ -1,10 +1,3 @@
#include <stdio.h>
#include <string.h>
#include <minIni.h>
#include <SDL_filesystem.h>
#include <SDL_keyboard.h>

#include "../IZ_action.h"
#include "IZ_config.h"

void IZ_GetConfigPath(char* config_path) {


+ 7
- 1
src/packages/game/config/IZ_config.h View File

@@ -1,8 +1,14 @@
#ifndef IZ_CONFIG_H
#define IZ_CONFIG_H

#include "SDL_keycode.h"
#include <stdio.h>
#include <string.h>
#include <SDL_filesystem.h>
#include <SDL_keyboard.h>
#include <minIni.h>

#include "../IZ_common.h"
#include "../IZ_action.h"

typedef struct {
uint16_t width;


Loading…
Cancel
Save