瀏覽代碼

Define mock macros

Use macros for syntactic sugar.
master
TheoryOfNekomata 2 年之前
父節點
當前提交
6d6c35d557
共有 1 個檔案被更改,包括 32 行新增34 行删除
  1. +32
    -34
      src/packages/game-test/config/IZ_config.c

+ 32
- 34
src/packages/game-test/config/IZ_config.c 查看文件

@@ -1,26 +1,27 @@
#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 "";
#define mock(X) static uint8_t calls_##X = 0;

#define mock_return(X) calls_##X += 1; return

#define mock_reset(X) calls_##X = 0

#define mock_calls(X) ((uint8_t) (calls_##X))

mock(SDL_GetBasePath) char* SDL_GetBasePath() {
mock_return(SDL_GetBasePath) "";
}

static uint8_t calls_SDL_GetKeyName = 0;
const char* SDL_GetKeyName(SDL_KeyCode code) {
calls_SDL_GetKeyName += 1;
return "";
mock(SDL_GetKeyName) const char* SDL_GetKeyName (SDL_KeyCode code) {
mock_return(SDL_GetKeyName) "";
}

static uint8_t calls_SDL_GetKeyFromName = 0;
SDL_KeyCode SDL_GetKeyFromName(const char* name) {
calls_SDL_GetKeyFromName += 1;
return 0;
mock(SDL_GetKeyFromName) SDL_KeyCode SDL_GetKeyFromName(const char* name) {
mock_return(SDL_GetKeyFromName) 0;
}

static uint8_t calls_ini_gets = 0;
int ini_gets(
mock(ini_gets) int ini_gets (
const char *Section,
const char *Key,
const char *DefValue,
@@ -28,19 +29,16 @@ int ini_gets(
int BufferSize,
const char *Filename
) {
calls_ini_gets += 1;
return 0;
mock_return(ini_gets) 0;
}

static uint8_t calls_ini_getl = 0;
long ini_getl(
mock(ini_getl) long ini_getl(
const TCHAR *Section,
const TCHAR *Key,
long DefValue,
const TCHAR *Filename
) {
calls_ini_getl += 1;
return DefValue;
mock_return(ini_getl) DefValue;
}

spec("config") {
@@ -48,30 +46,30 @@ spec("config") {
static IZ_Config config;

after_each() {
calls_ini_getl = 0;
mock_reset(ini_getl);
}

after_each() {
calls_ini_gets = 0;
mock_reset(ini_gets);
}

after_each() {
calls_SDL_GetKeyFromName = 0;
mock_reset(SDL_GetKeyFromName);
}

after_each() {
calls_SDL_GetKeyName = 0;
mock_reset(SDL_GetKeyName);
}

after_each() {
calls_SDL_GetBasePath = 0;
mock_reset(SDL_GetBasePath);
}

it("should load default config values") {
IZ_LoadConfig(&config);

check(
calls_SDL_GetBasePath > 0,
mock_calls(SDL_GetBasePath) > 0,
"SDL_GetBasePath() not called."
);

@@ -80,10 +78,10 @@ spec("config") {
+ 1 // input params
+ (12 * PLAYERS); // joystick controls
check(
calls_ini_getl == expected_calls_ini_getl,
mock_calls(ini_getl) == expected_calls_ini_getl,
"Call count mismatch for ini_getl() (expected %u, received %u).",
expected_calls_ini_getl,
calls_ini_getl
mock_calls(ini_getl)
);

check(
@@ -107,28 +105,28 @@ spec("config") {
static const int expected_calls_ini_gets =
(16 * PLAYERS); // keyboard controls
check(
calls_ini_gets == expected_calls_ini_gets,
mock_calls(ini_gets) == expected_calls_ini_gets,
"Call count mismatch for ini_gets() (expected %u, received %u).",
expected_calls_ini_gets,
calls_ini_gets
mock_calls(ini_gets)
);

static const int expected_calls_SDL_GetKeyFromName =
(16 * PLAYERS); // keyboard controls
check(
calls_SDL_GetKeyFromName == expected_calls_SDL_GetKeyFromName,
mock_calls(SDL_GetKeyFromName) == expected_calls_SDL_GetKeyFromName,
"Call count mismatch for SDL_GetKeyFromName() (expected %u, received %u).",
expected_calls_SDL_GetKeyFromName,
calls_SDL_GetKeyFromName
mock_calls(SDL_GetKeyFromName)
);

static const int expected_calls_SDL_GetKeyName =
(16 * PLAYERS); // keyboard controls
check(
calls_SDL_GetKeyName == expected_calls_SDL_GetKeyName,
mock_calls(SDL_GetKeyName) == expected_calls_SDL_GetKeyName,
"Call count mismatch for SDL_GetKeyName() (expected %u, received %u).",
expected_calls_SDL_GetKeyName,
calls_SDL_GetKeyName
mock_calls(SDL_GetKeyName)
);
}
}


Loading…
取消
儲存