1.1 --- a/core/sourcehook/test/testrefret.cpp Fri Dec 17 22:20:10 2010 -0500
1.2 +++ b/core/sourcehook/test/testrefret.cpp Fri Dec 17 22:22:44 2010 -0500
1.3 @@ -49,6 +49,11 @@
1.4 }
1.5 };
1.6
1.7 + SH_DECL_HOOK0(Test, Func1, SH_NOATTRIB, 0, int&);
1.8 + SH_DECL_MANUALHOOK0(Manual_Test_Func1, 0, 0, 0, int&);
1.9 + SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, const int&, int);
1.10 + SH_DECL_MANUALHOOK1(Manual_Test_Func2, 1, 0, 0, const int&, int);
1.11 +
1.12 class CHook
1.13 {
1.14 public:
1.15 @@ -76,12 +81,18 @@
1.16 RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
1.17 }
1.18
1.19 - virtual const int& Func2_Pre1(int p1)
1.20 + virtual const int& Func2_PreRecall_OverrideRet_Normal(int p1)
1.21 {
1.22 ADD_STATE(State_Func2_Pre1(p1, &m_Var));
1.23 RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, m_Var, static_cast<const int& (Test::*)(int)>(&Test::Func2), (1337));
1.24 }
1.25
1.26 + virtual const int& Func2_PreRecall_OverrideRet_Manual(int p1)
1.27 + {
1.28 + ADD_STATE(State_Func2_Pre1(p1, &m_Var));
1.29 + RETURN_META_VALUE_MNEWPARAMS(MRES_OVERRIDE, m_Var, Manual_Test_Func2, (1337));
1.30 + }
1.31 +
1.32 virtual const int& Func2_Post1(int p1)
1.33 {
1.34 ADD_STATE(State_Func2_Post1(p1, &META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
1.35 @@ -89,117 +100,220 @@
1.36 }
1.37 };
1.38
1.39 - SH_DECL_HOOK0(Test, Func1, SH_NOATTRIB, 0, int&);
1.40 - SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, const int&, int);
1.41 -
1.42 Test *MyTestFactory()
1.43 {
1.44 return new Test;
1.45 }
1.46 }
1.47
1.48 -bool TestRefRet(std::string &error)
1.49 +class TesterRefRet
1.50 {
1.51 - GET_SHPTR(g_SHPtr);
1.52 - g_PLID = 1;
1.53 -
1.54 - Test *pTest = MyTestFactory();
1.55 - CAutoPtrDestruction<Test> apd(pTest);
1.56 +public:
1.57 + Test *pTest;
1.58 CHook hook;
1.59
1.60 - int &ret1 = pTest->Func1();
1.61 - ADD_STATE(State_Func1_Ret(&ret1));
1.62 + TesterRefRet()
1.63 + {
1.64 + pTest = NULL;
1.65 + }
1.66 +
1.67 + bool doTests(std::string &error)
1.68 + {
1.69 + GET_SHPTR(g_SHPtr);
1.70 + g_PLID = 1;
1.71
1.72 - CHECK_STATES((&g_States,
1.73 - new State_Func1(&pTest->m_Var1),
1.74 - new State_Func1_Ret(&pTest->m_Var1),
1.75 - NULL), "Part 1");
1.76 + pTest = MyTestFactory();
1.77 + CAutoPtrDestruction<Test> apd(pTest);
1.78 +
1.79 + int &ret1 = pTest->Func1();
1.80 + ADD_STATE(State_Func1_Ret(&ret1));
1.81 +
1.82 + CHECK_STATES((&g_States,
1.83 + new State_Func1(&pTest->m_Var1),
1.84 + new State_Func1_Ret(&pTest->m_Var1),
1.85 + NULL), "Part 1");
1.86
1.87 - // Now add Func1_Pre1, which supercedes and returns hook.m_Var
1.88 - SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
1.89 + // Now add Func1_Pre1, which supercedes and returns hook.m_Var
1.90 + AddHook__Test_Func1__Func1_Pre();
1.91 +
1.92 + int &ret2 = pTest->Func1();
1.93 + ADD_STATE(State_Func1_Ret(&ret2));
1.94 +
1.95 + CHECK_STATES((&g_States,
1.96 + new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.97 + new State_Func1(&pTest->m_Var1), // Function says that it's going to return pTest->m_Var1
1.98 + new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
1.99 + NULL), "Part 2");
1.100
1.101 - int &ret2 = pTest->Func1();
1.102 - ADD_STATE(State_Func1_Ret(&ret2));
1.103 -
1.104 - CHECK_STATES((&g_States,
1.105 - new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.106 - new State_Func1(&pTest->m_Var1), // Function says that it's going to return pTest->m_Var1
1.107 - new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
1.108 - NULL), "Part 2");
1.109 + // Now add Func1_Post1, which only reports orig ret and override ret
1.110 + AddHook__Test_Func1__Func1_Post();
1.111 +
1.112 + int &ret3 = pTest->Func1();
1.113 + ADD_STATE(State_Func1_Ret(&ret3));
1.114 +
1.115 + CHECK_STATES((&g_States,
1.116 + new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.117 + new State_Func1(&pTest->m_Var1), // Function says that it's going to return pTest->m_Var1
1.118 + new State_Func1_Post1(&pTest->m_Var1, &hook.m_Var), // origret(=p1) is what it wanted to
1.119 + // return, overrideret(=p2) is pre1's var
1.120 + new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
1.121 + NULL), "Part 3");
1.122
1.123 - // Now add Func1_Post1, which only reports orig ret and override ret
1.124 - SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
1.125 + // Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
1.126 + // and add Func1_Post2, which overrides and returns hook.m_Var again.
1.127 +
1.128 + AddHook__Test_Func1__Func1_Pre2();
1.129 + AddHook__Test_Func1__Func1_Post2();
1.130 +
1.131 + int &ret4 = pTest->Func1();
1.132 + ADD_STATE(State_Func1_Ret(&ret4));
1.133 +
1.134 + CHECK_STATES((&g_States,
1.135 + new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.136 + new State_Func1_Pre2(MRES_OVERRIDE, // current status
1.137 + &hook.m_Var, // override ret (which it set to 1337)
1.138 + &g_Var), // what it's going to ret, AND supercede with
1.139 +
1.140 + new State_Func1_Post1(&g_Var, &g_Var), // origret(=p1) is what pre2 superceded with,
1.141 + // so overrideret(=p2) has to be the same
1.142 + new State_Func1_Post2(&hook.m_Var), // post2 is going to override with hook.m_Var again
1.143 + new State_Func1_Ret(&hook.m_Var), // ==>>> hook.m_Var is returned
1.144 + NULL), "Part 4");
1.145
1.146 - int &ret3 = pTest->Func1();
1.147 - ADD_STATE(State_Func1_Ret(&ret3));
1.148 -
1.149 - CHECK_STATES((&g_States,
1.150 - new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.151 - new State_Func1(&pTest->m_Var1), // Function says that it's going to return pTest->m_Var1
1.152 - new State_Func1_Post1(&pTest->m_Var1, &hook.m_Var), // origret(=p1) is what it wanted to
1.153 - // return, overrideret(=p2) is pre1's var
1.154 - new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
1.155 - NULL), "Part 3");
1.156 + CHECK_COND(hook.m_Var == 1337, "Part 4.1");
1.157 +
1.158 + // Through a callclass
1.159 + SourceHook::CallClass<Test> *cc1 = SH_GET_CALLCLASS(pTest);
1.160 + int &ret5 = SH_CALL(cc1, &Test::Func1)();
1.161 + ADD_STATE(State_Func1_Ret(&ret5));
1.162 +
1.163 + CHECK_STATES((&g_States,
1.164 + new State_Func1(&pTest->m_Var1),
1.165 + new State_Func1_Ret(&pTest->m_Var1),
1.166 + NULL), "Part 5");
1.167 +
1.168 + SH_RELEASE_CALLCLASS(cc1);
1.169
1.170 - // Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
1.171 - // and add Func1_Post2, which overrides and returns hook.m_Var again.
1.172 + ////////////////////////////////////////////////////////////////////////////////////////
1.173 + // Func2 tests
1.174 + const int &ret21 = pTest->Func2(500);
1.175 + ADD_STATE(State_Func2_Ret(&ret21));
1.176
1.177 - SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
1.178 - SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
1.179 + CHECK_STATES((&g_States,
1.180 + new State_Func2(500, &pTest->m_Var2),
1.181 + new State_Func2_Ret(&pTest->m_Var2),
1.182 + NULL), "Part 6");
1.183
1.184 - int &ret4 = pTest->Func1();
1.185 - ADD_STATE(State_Func1_Ret(&ret4));
1.186 + AddHook__Test_Func2__Func2_Pre1();
1.187 + AddHook__Test_Func2__Func2_Post1();
1.188 +
1.189 + const int &ret22 = pTest->Func2(500);
1.190 + ADD_STATE(State_Func2_Ret(&ret22));
1.191
1.192 - CHECK_STATES((&g_States,
1.193 - new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.194 - new State_Func1_Pre2(MRES_OVERRIDE, // current status
1.195 - &hook.m_Var, // override ret (which it set to 1337)
1.196 - &g_Var), // what it's going to ret, AND supercede with
1.197 + CHECK_STATES((&g_States,
1.198 + new State_Func2_Pre1(500, &hook.m_Var), // p1 was 500; it's going to override with hook.m_Var; and also change p1 to 1337
1.199 + new State_Func2(1337, &pTest->m_Var2), // p1 was 1337; it's going to ret pTest->m_Var2
1.200 + new State_Func2_Post1(1337, // p1 was 1337
1.201 + &pTest->m_Var2, // orig ret was pTest->m_Var2
1.202 + &hook.m_Var), // override ret was hook.m_Var
1.203 + new State_Func2_Ret(&hook.m_Var), // really returned hook.m_Var
1.204 + NULL), "Part 7");
1.205 +
1.206 + return true;
1.207 + }
1.208
1.209 - new State_Func1_Post1(&g_Var, &g_Var), // origret(=p1) is what pre2 superceded with,
1.210 - // so overrideret(=p2) has to be the same
1.211 - new State_Func1_Post2(&hook.m_Var), // post2 is going to override with hook.m_Var again
1.212 - new State_Func1_Ret(&hook.m_Var), // ==>>> hook.m_Var is returned
1.213 - NULL), "Part 4");
1.214 + virtual void AddHook__Test_Func1__Func1_Pre() = 0;
1.215 + virtual void AddHook__Test_Func1__Func1_Post() = 0;
1.216 +
1.217 + virtual void AddHook__Test_Func1__Func1_Pre2() = 0;
1.218 + virtual void AddHook__Test_Func1__Func1_Post2() = 0;
1.219 +
1.220 + virtual void AddHook__Test_Func2__Func2_Pre1() = 0;
1.221 + virtual void AddHook__Test_Func2__Func2_Post1() = 0;
1.222 +};
1.223
1.224 - CHECK_COND(hook.m_Var == 1337, "Part 4.1");
1.225 -
1.226 - // Through a callclass
1.227 - SourceHook::CallClass<Test> *cc1 = SH_GET_CALLCLASS(pTest);
1.228 - int &ret5 = SH_CALL(cc1, &Test::Func1)();
1.229 - ADD_STATE(State_Func1_Ret(&ret5));
1.230 +class TesterRefRetNonManual : public TesterRefRet
1.231 +{
1.232 +public:
1.233 + void AddHook__Test_Func1__Func1_Pre()
1.234 + {
1.235 + SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
1.236 + }
1.237 +
1.238 + void AddHook__Test_Func1__Func1_Post()
1.239 + {
1.240 + SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
1.241 + }
1.242
1.243 - CHECK_STATES((&g_States,
1.244 - new State_Func1(&pTest->m_Var1),
1.245 - new State_Func1_Ret(&pTest->m_Var1),
1.246 - NULL), "Part 5");
1.247 + void AddHook__Test_Func1__Func1_Pre2()
1.248 + {
1.249 + SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
1.250 + }
1.251
1.252 - SH_RELEASE_CALLCLASS(cc1);
1.253 + void AddHook__Test_Func1__Func1_Post2()
1.254 + {
1.255 + SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
1.256 + }
1.257 +
1.258 + void AddHook__Test_Func2__Func2_Pre1()
1.259 + {
1.260 + SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_PreRecall_OverrideRet_Normal), false);
1.261 + }
1.262
1.263 - ////////////////////////////////////////////////////////////////////////////////////////
1.264 - // Func2 tests
1.265 - const int &ret21 = pTest->Func2(500);
1.266 - ADD_STATE(State_Func2_Ret(&ret21));
1.267 + void AddHook__Test_Func2__Func2_Post1()
1.268 + {
1.269 + SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
1.270 + }
1.271 +};
1.272 +
1.273 +class TesterRefRetManual : public TesterRefRet
1.274 +{
1.275 +public:
1.276 + void AddHook__Test_Func1__Func1_Pre()
1.277 + {
1.278 + SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
1.279 + }
1.280
1.281 - CHECK_STATES((&g_States,
1.282 - new State_Func2(500, &pTest->m_Var2),
1.283 - new State_Func2_Ret(&pTest->m_Var2),
1.284 - NULL), "Part 6");
1.285 + void AddHook__Test_Func1__Func1_Post()
1.286 + {
1.287 + SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
1.288 + }
1.289
1.290 - SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Pre1), false);
1.291 - SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
1.292 + void AddHook__Test_Func1__Func1_Pre2()
1.293 + {
1.294 + SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
1.295 + }
1.296 +
1.297 + void AddHook__Test_Func1__Func1_Post2()
1.298 + {
1.299 + SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
1.300 + }
1.301
1.302 - const int &ret22 = pTest->Func2(500);
1.303 - ADD_STATE(State_Func2_Ret(&ret22));
1.304 + void AddHook__Test_Func2__Func2_Pre1()
1.305 + {
1.306 + SH_ADD_MANUALHOOK(Manual_Test_Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_PreRecall_OverrideRet_Manual), false);
1.307 + }
1.308 +
1.309 + void AddHook__Test_Func2__Func2_Post1()
1.310 + {
1.311 + SH_ADD_MANUALHOOK(Manual_Test_Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
1.312 + }
1.313 +};
1.314
1.315 - CHECK_STATES((&g_States,
1.316 - new State_Func2_Pre1(500, &hook.m_Var), // p1 was 500; it's going to override with hook.m_Var; and also change p1 to 1337
1.317 - new State_Func2(1337, &pTest->m_Var2), // p1 was 1337; it's going to ret pTest->m_Var2
1.318 - new State_Func2_Post1(1337, // p1 was 1337
1.319 - &pTest->m_Var2, // orig ret was pTest->m_Var2
1.320 - &hook.m_Var), // override ret was hook.m_Var
1.321 - new State_Func2_Ret(&hook.m_Var), // really returned hook.m_Var
1.322 - NULL), "Part 7");
1.323 -
1.324 - return true;
1.325 +bool TestRefRet(std::string &error)
1.326 +{
1.327 + TesterRefRet *testerNonManual = new TesterRefRetNonManual;
1.328 + bool resNonManual = testerNonManual->doTests(error);
1.329 + if (!resNonManual)
1.330 + {
1.331 + return false;
1.332 + }
1.333 +
1.334 + TesterRefRet *testerManual = new TesterRefRetManual;
1.335 + bool resManual = testerManual->doTests(error);
1.336 + if (!resManual)
1.337 + {
1.338 + return false;
1.339 + }
1.340 + return true;
1.341 }