sourcehook/test/testbail.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Dec 23 11:58:11 2005 +0000 (2005-12-23)
changeset 162 a95db73792e5
parent 133 ffe46578a1ae
child 163 377bdd31c28d
permissions -rw-r--r--
One bugfix, addition of META_RETURN_(VALUE_)NEWPARAMS (implemented using recalls), tests now support compiling against an older version of sourcehook.h (through the sourcehook_test.h file) while main.cpp which now also acts as a CSourceHookImpl factory compiles against the new version.
     1 // TESTBAIL
     2 // This test used to be a test for a bug BAIL found.
     3 // That bug is now fixed so I've granted BAIL the pleasure of being a test for
     4 // the correct priority ordering of hook managers based on their version.
     5 
     6 /*
     7 	THE PROBLEM:
     8 		Old hook funcs don't work right when you combine override returns and recalls.
     9 	THE SOLUTION:
    10 		Always use a new hook func when possible. For this, hook funcs have to be able to say
    11 			" HELLO I'M NEW! "
    12 
    13 	This file tests that functionality.
    14 
    15 	How it works:
    16 		testbail.cpp compiles with old version of sourcehook.h.
    17 		It sets everything up, adds a hook on a function
    18 		Then testbail2.cpp which has the new version adds a hook on the same function and 
    19 		does a recall and overrides the value in it.
    20 */
    21 
    22 #include "sourcehook_test.h"
    23 #include "testbail.h"
    24 
    25 void *___testbail_gabgab;
    26 SourceHook::ISourceHook *___testbail_shptr;
    27 
    28 namespace
    29 {
    30 	int EatYams_Handler1(int a)
    31 	{
    32 		ADD_STATE(State_EatYams_Handler1_Called(a));
    33 		RETURN_META_VALUE(MRES_IGNORED, 0);
    34 	}
    35 }
    36 
    37 // These are here so they can access this CU's g_States
    38 int ___testbail_EatYams_Handler2(int a)
    39 {
    40 	ADD_STATE(State_EatYams_Handler2_Called(a));
    41 	RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, 6, IGaben, EatYams, (0xBEEF));
    42 }
    43 
    44 int ___testbail_EatYams_Handler3(int a)
    45 {
    46 	ADD_STATE(State_EatYams_Handler3_Called(a));
    47 	RETURN_META_VALUE(MRES_IGNORED, 0);
    48 }
    49 
    50 bool TestBail(std::string &error)
    51 {
    52 	GET_SHPTR(g_SHPtr);
    53 	g_PLID = 1;
    54 
    55 	g_Gabgab = new IGaben;
    56 	___testbail_gabgab = (void*)g_Gabgab;
    57 	___testbail_shptr = g_SHPtr;
    58 
    59 	SH_ADD_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams_Handler1, false);
    60 
    61 	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
    62 
    63 	CHECK_STATES((&g_States,
    64 		new State_EatYams_Handler1_Called(0xDEAD),
    65 		new State_EatYams_Called(0xDEAD),
    66 		new State_EatYams_Return(5),
    67 		NULL), "Part 1");
    68 
    69 	if (!___TestBail2(error))
    70 		return false;
    71 
    72 	CHECK_STATES((&g_States,
    73 		new State_EatYams_Handler1_Called(0xDEAD),
    74 		new State_EatYams_Handler2_Called(0xDEAD),
    75 		new State_EatYams_Handler3_Called(0xBEEF),
    76 		new State_EatYams_Called(0xBEEF),
    77 		NULL), "Part 2.1");
    78 
    79 	// WHAT IF NOW SOMEONE UNLOADS PLUGIN 2 !?!?!?!?
    80 	Test_UnloadPlugin(g_SHPtr, 2);
    81 
    82 	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
    83 
    84 	CHECK_STATES((&g_States,
    85 		new State_EatYams_Handler1_Called(0xDEAD),
    86 		new State_EatYams_Called(0xDEAD),
    87 		new State_EatYams_Return(5),
    88 		NULL), "Part 3");
    89 
    90 	SH_REMOVE_HOOK_STATICFUNC(IGaben, EatYams, g_Gabgab, EatYams_Handler1, false);
    91 
    92 	ADD_STATE(State_EatYams_Return(g_Gabgab->EatYams(0xDEAD)));
    93 
    94 	CHECK_STATES((&g_States,
    95 		new State_EatYams_Called(0xDEAD),
    96 		new State_EatYams_Return(5),
    97 		NULL), "Part 4");
    98 
    99 	delete g_Gabgab;
   100 
   101 	return true;
   102 }
   103