Skip to content

Instantly share code, notes, and snippets.

@pedropapa
Last active September 4, 2023 13:16
Show Gist options
  • Save pedropapa/b5d1726fac9a0972a57cadb0dce3afa4 to your computer and use it in GitHub Desktop.
Save pedropapa/b5d1726fac9a0972a57cadb0dce3afa4 to your computer and use it in GitHub Desktop.
Mock & Spy capabilities for y_testing
#include <YSI_Data\y_hashmap>
#define MAX_MOCK_FUNCTIONS 100
#define MAX_FUNCTION_CALLS 100
#define MAX_FUNCTION_NAME 32
#define MAX_FUNCTION_PARAMS_VALUE 2048
enum MockFunctionCallMap
{
FunctionName[MAX_FUNCTION_NAME],
Calls,
}
static
totalCalls,
params[MAX_FUNCTION_PARAMS_VALUE],
callsParams[MAX_FUNCTION_CALLS][MAX_FUNCTION_PARAMS_VALUE],
callsMockData[MAX_MOCK_FUNCTIONS][MockFunctionCallMap],
HashMap:mapCalls<>;
stock MockInit()
{
HashMap_Init(mapCalls, callsMockData, Calls);
}
stock MockReset(const functionName[])
{
HashMap_Set(mapCalls, functionName, 0);
}
stock MOCK_CALL_COUNT(const function[], count)
{
new calls = HashMap_Get(mapCalls, function);
return calls == count;
}
stock MOCK_CALL(const functionName[], const paramsAsString[])
{
new match = 0;
format(params, sizeof(params), "%s%s", functionName, paramsAsString);
for(new i = 0; i < totalCalls; i++)
{
if(!strcmp(callsParams[i], params))
{
match = 1;
}
}
return match;
}
stock RegisterCall(const functionName[], const paramsAsString[])
{
new calls = HashMap_Get(mapCalls, functionName);
if (calls <= 0) {
calls = 0;
}
calls++;
HashMap_Set(mapCalls, functionName, calls);
format(callsParams[totalCalls], sizeof(callsParams[]), "%s%s", functionName, paramsAsString);
totalCalls++;
}
#include <native_mocks>
// native_mocks.inc
static params[MAX_FUNCTION_PARAMS_VALUE];
stock MOCK_SendClientMessage(playerid, color, const message[])
{
format(params, sizeof(params), "%d %d %s", playerid, color, message);
RegisterCall("SendClientMessage", params);
}
#define _ALS_SendClientMessage
#define SendClientMessage( MOCK_SendClientMessage(
// Include more natives here on-demand...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment