Skip to content

Commit b87bfa8

Browse files
Merge pull request #20 from Neko-Box-Coder/v2
v2 overhaul
2 parents 3ed78d2 + 2db25e8 commit b87bfa8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3350
-2719
lines changed

Example.cpp

Lines changed: 154 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
#include "CppOverride.hpp"
22

3+
#include <iostream>
4+
#include <vector>
5+
#include <string>
6+
37
CO_DECLARE_INSTANCE(OverrideInstanceName);
48

9+
namespace MyNameSpace
10+
{
11+
int OverrideMyScopedFunc(int value1, float& value2)
12+
{
13+
CO_INSERT_IMPL(OverrideInstanceName, int, (value1, value2));
14+
//The rest of the implementations...
15+
return 0;
16+
}
17+
}
18+
519
int OverrideMyReturnValue(int value1, float value2)
620
{
7-
CO_OVERRIDE_IMPL(OverrideInstanceName, int, (value1, value2));
21+
CO_INSERT_IMPL(OverrideInstanceName, int, (value1, value2));
822
//The rest of the implementations...
923
return 0;
1024
}
@@ -13,19 +27,19 @@ int SomeRef = 0;
1327

1428
int& OverrideMyReturnRef(int value1, float value2)
1529
{
16-
CO_OVERRIDE_IMPL(OverrideInstanceName, int&, (value1, value2));
30+
CO_INSERT_IMPL(OverrideInstanceName, int&, (value1, value2));
1731
//The rest of the implementations...
1832
return SomeRef;
1933
}
2034

2135
void OverrideMyArgs(float& value1, int* value2)
2236
{
23-
CO_OVERRIDE_IMPL(OverrideInstanceName, void, (value1, value2));
37+
CO_INSERT_IMPL(OverrideInstanceName, void, (value1, value2));
2438
}
2539

2640
bool OverrideMyArgsWithStstus(float& value1, int* value2)
2741
{
28-
CO_OVERRIDE_IMPL(OverrideInstanceName, bool, (value1, value2));
42+
CO_INSERT_IMPL(OverrideInstanceName, bool, (value1, value2));
2943
return false;
3044
}
3145

@@ -46,19 +60,17 @@ do\
4660

4761
void ResetAll()
4862
{
49-
CO_CLEAR_ALL_OVERRIDE_SETUP(OverrideInstanceName);
63+
CO_CLEAR_ALL_INSTRUCTS(OverrideInstanceName);
5064
}
5165

5266
void OverrideReturnsExample()
5367
{
54-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnValue)
55-
.Returns<int>(1);
68+
CO_INSTRUCT_REF(OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue).Returns<int>(1);
5669

5770
CO_QUICK_ASSERT(OverrideMyReturnValue(0, 0.f) == 1);
5871

5972
int returnRef = 1;
60-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnRef)
61-
.Returns<int&>(returnRef);
73+
CO_INSTRUCT_REF(OverrideInstanceName, CO_GLOBAL, OverrideMyReturnRef).Returns<int&>(returnRef);
6274

6375
CO_QUICK_ASSERT(&returnRef == &OverrideMyReturnRef(0, 0.f));
6476

@@ -67,8 +79,7 @@ void OverrideReturnsExample()
6779

6880
void OverrideArgumentsExample()
6981
{
70-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyArgs)
71-
.SetArgs<float&, int*>(1.f, 3);
82+
CO_INSTRUCT_REF(OverrideInstanceName, CO_GLOBAL, OverrideMyArgs).SetArgs<float&, int*>(1.f, 3);
7283

7384
float arg1 = 0.f;
7485
int arg2 = 0;
@@ -79,16 +90,38 @@ void OverrideArgumentsExample()
7990
ResetAll();
8091
}
8192

93+
void OverrideScopedFunctionExample()
94+
{
95+
CO_INSTRUCT_REF(OverrideInstanceName, MyNameSpace, OverrideMyScopedFunc).Returns<int>(1);
96+
97+
float floatArg = 0.f;
98+
CO_QUICK_ASSERT(MyNameSpace::OverrideMyScopedFunc(1, floatArg) == 1);
99+
100+
CO_REMOVE_INSTRUCT_REF(OverrideInstanceName, MyNameSpace, OverrideMyScopedFunc);
101+
102+
CO_INSTRUCT_REF (OverrideInstanceName, MyNameSpace, OverrideMyScopedFunc)
103+
.SetArgs<CO_ANY_TYPE, float&>(CO_DONT_SET, 3);
104+
105+
MyNameSpace::OverrideMyScopedFunc(1, floatArg);
106+
CO_QUICK_ASSERT(floatArg == 3);
107+
}
108+
82109
void OverrideReturnsWithActionLambda()
83110
{
84-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnValue)
85-
.ReturnsByAction<int>
86-
(
87-
[](void*, const std::vector<void*>&, void* out)
88-
{
89-
*static_cast<int*>(out) = 5;
90-
}
91-
);
111+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
112+
.ReturnsByAction<int>
113+
(
114+
[]( void*,
115+
const std::vector<CppOverride::TypedDataInfo>& args,
116+
CppOverride::TypedDataInfo& out)
117+
{
118+
(void)args;
119+
//Can access args with type safety if needed
120+
//e.g., if(args.at(0).IsType<int>()) { ... }
121+
if(out.IsType<int>())
122+
*out.GetTypedDataPtr<int>() = 5;
123+
}
124+
);
92125

93126
CO_QUICK_ASSERT(OverrideMyReturnValue(0, 0.f) == 5);
94127

@@ -97,13 +130,15 @@ void OverrideReturnsWithActionLambda()
97130

98131
void OverrideArgumentsWithActionLambda()
99132
{
100-
CO_SETUP_OVERRIDE(OverrideInstanceName, OverrideMyArgs)
133+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyArgs)
101134
.SetArgsByAction<float&, int*>
102135
(
103-
[](void*, std::vector<void*>& args)
136+
[](void*, std::vector<CppOverride::TypedDataInfo>& args)
104137
{
105-
*static_cast<float*>(args.at(0)) = 1.f;
106-
**static_cast<int**>(args.at(1)) = 2;
138+
if(args.at(0).IsType<float>())
139+
*args.at(0).GetTypedDataPtr<float>() = 1.f;
140+
if(args.at(1).IsType<int*>())
141+
**args.at(1).GetTypedDataPtr<int*>() = 2;
107142
}
108143
);
109144

@@ -118,9 +153,9 @@ void OverrideArgumentsWithActionLambda()
118153

119154
void WhenCalledWithExample()
120155
{
121-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnValue)
122-
.WhenCalledWith(2, 3.f)
123-
.Returns<int>(1);
156+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
157+
.WhenCalledWith(2, 3.f)
158+
.Returns<int>(1);
124159

125160
int ret1 = OverrideMyReturnValue(2, 3.f); //Returns 1
126161
int ret2 = OverrideMyReturnValue(1, 2.f); //Won't return 1
@@ -133,9 +168,9 @@ void WhenCalledWithExample()
133168

134169
void TimesExample()
135170
{
136-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyArgs)
137-
.SetArgs<float&, int*>(1.f, 2)
138-
.Times(1);
171+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyArgs)
172+
.SetArgs<float&, int*>(1.f, 2)
173+
.Times(1);
139174

140175
float testFloat = 2.f;
141176
int testInt = 3;
@@ -156,18 +191,18 @@ void TimesExample()
156191

157192
void IfConditionLambdaExample()
158193
{
159-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnValue)
160-
.If
161-
(
162-
[](void*, const std::vector<void*>& args)
163-
{
164-
if(*static_cast<int*>(args.at(0)) == 1)
165-
return true;
166-
else
167-
return false;
168-
}
169-
)
170-
.Returns<int>(1);
194+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
195+
.If
196+
(
197+
[](void*, const std::vector<CppOverride::TypedDataInfo>& args)
198+
{
199+
if(args.at(0).IsType<int>() && *args.at(0).GetTypedDataPtr<int>() == 1)
200+
return true;
201+
else
202+
return false;
203+
}
204+
)
205+
.Returns<int>(1);
171206

172207
int ret1 = OverrideMyReturnValue(1, 2.f); //Returns 1
173208
int ret2 = OverrideMyReturnValue(2, 3.f); //Won't return 1
@@ -181,16 +216,22 @@ void IfConditionLambdaExample()
181216
void WhenCalledExpectedlyDoLambdaExample()
182217
{
183218
bool called = false;
184-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnValue)
185-
.WhenCalledWith(2, 3.f)
186-
.Returns<int>(1)
187-
.WhenCalledExpectedly_Do
188-
(
189-
[&called](...)
190-
{
191-
called = true;
192-
}
193-
);
219+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
220+
.WhenCalledWith(2, 3.f)
221+
.Returns<int>(1)
222+
.WhenCalledExpectedly_Do
223+
(
224+
[&called]( void* instance,
225+
const std::vector<CppOverride::TypedDataInfo>& args)
226+
{
227+
(void)instance;
228+
(void)args;
229+
//Can access args with type safety if needed
230+
//e.g., if(args.at(0).IsType<int>()) { ... }
231+
232+
called = true;
233+
}
234+
);
194235

195236
int ret1 = OverrideMyReturnValue(2, 3.f); //Returns 1 and sets called to true
196237
CO_QUICK_ASSERT(ret1 == 1);
@@ -202,16 +243,22 @@ void WhenCalledExpectedlyDoLambdaExample()
202243
void OtherwiseDoLambdaExample()
203244
{
204245
bool called = false;
205-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnValue)
206-
.WhenCalledWith(2, 3.f)
207-
.Returns<int>(1)
208-
.Otherwise_Do
209-
(
210-
[&called](...)
211-
{
212-
called = true;
213-
}
214-
);
246+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
247+
.WhenCalledWith(2, 3.f)
248+
.Returns<int>(1)
249+
.Otherwise_Do
250+
(
251+
[&called]( void* instance,
252+
const std::vector<CppOverride::TypedDataInfo>& args)
253+
{
254+
(void)instance;
255+
(void)args;
256+
//Can access args with type safety if needed
257+
//e.g., if(args.at(0).IsType<int>()) { ... }
258+
259+
called = true;
260+
}
261+
);
215262

216263
int ret1 = OverrideMyReturnValue(1, 2.f); //Won't return 1
217264
CO_QUICK_ASSERT(ret1 != 1);
@@ -222,11 +269,11 @@ void OtherwiseDoLambdaExample()
222269

223270
void AssignStatusExample()
224271
{
225-
std::shared_ptr<CppOverride::OverrideResult> result = CppOverride::CreateOverrideResult();
226-
CO_SETUP_OVERRIDE (OverrideInstanceName, OverrideMyReturnValue)
227-
.WhenCalledWith(2, 3.f)
228-
.Returns<int>(1)
229-
.AssignResult(result);
272+
CppOverride::ResultPtr result;
273+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
274+
.WhenCalledWith(2, 3.f)
275+
.Returns<int>(1)
276+
.AssignsResult(result);
230277

231278
int ret1 = OverrideMyReturnValue(1, 2.f);
232279

@@ -238,12 +285,51 @@ void AssignStatusExample()
238285
ResetAll();
239286
}
240287

288+
void ExpectedExample()
289+
{
290+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
291+
.WhenCalledWith(2, 3.f)
292+
.Times(1)
293+
.Returns<int>(1)
294+
.Expected();
295+
296+
CO_INSTRUCT_REF (OverrideInstanceName, CO_GLOBAL, OverrideMyReturnValue)
297+
.WhenCalledWith(3, 4.f)
298+
.Returns<int>(1)
299+
.ExpectedNotSatisfy();
300+
301+
std::vector<CppOverride::FunctionName> failedFunctions;
302+
303+
//Not triggering any instructed overrides when we expected to do so,
304+
//therefore failed expectations.
305+
int ret1 = OverrideMyReturnValue(1, 2.f);
306+
failedFunctions = CO_GET_FAILED_EXPECTS(OverrideInstanceName);
307+
CO_QUICK_ASSERT(ret1 != 1);
308+
CO_QUICK_ASSERT(failedFunctions.size() == 1);
309+
310+
//Triggered the first instructed override, filled all expectations.
311+
ret1 = OverrideMyReturnValue(2, 3.f);
312+
failedFunctions = CO_GET_FAILED_EXPECTS(OverrideInstanceName);
313+
CO_QUICK_ASSERT(ret1 == 1);
314+
CO_QUICK_ASSERT(failedFunctions.empty());
315+
316+
//Triggered the second instructed override when we expect to not be triggered,
317+
//therefore failing expectation
318+
OverrideMyReturnValue(3, 4.f);
319+
failedFunctions = CO_GET_FAILED_EXPECTS(OverrideInstanceName);
320+
CO_QUICK_ASSERT(failedFunctions.size() == 1);
321+
322+
ResetAll();
323+
}
324+
241325
int main(int, char**)
242326
{
243327
OverrideReturnsExample();
244328

245329
OverrideArgumentsExample();
246330

331+
OverrideScopedFunctionExample();
332+
247333
OverrideReturnsWithActionLambda();
248334

249335
OverrideArgumentsWithActionLambda();
@@ -258,6 +344,8 @@ int main(int, char**)
258344

259345
AssignStatusExample();
260346

347+
ExpectedExample();
348+
261349
std::cout << "All examples are running correctly" << std::endl;
262350

263351
return 0;

External/ssTest

Submodule ssTest updated 1 file

Include_MultiHeaders/ArgsDataActionInfo.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#ifndef CO_ARGS_DATA_ACTION_INFO_HPP
22
#define CO_ARGS_DATA_ACTION_INFO_HPP
33

4+
#include "./TypedDataInfo.hpp"
45
#include <functional>
56
#include <vector>
67

78
namespace CppOverride
89
{
910
struct ArgsDataActionInfo
1011
{
11-
std::function<void(void* instance, std::vector<void*>& args)> DataAction;
12+
std::function<void(void* instance, std::vector<TypedDataInfo>& args)> DataAction;
1213
std::vector<std::size_t> DataTypes;
1314
std::vector<bool> DataTypesSet;
1415
bool DataActionSet = false;

Include_MultiHeaders/ConditionInfo.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CO_CONDITION_INFO_HPP
33

44
#include "./DataInfo.hpp"
5+
#include "./TypedDataInfo.hpp"
56

67
#include <functional>
78
#include <vector>
@@ -10,7 +11,7 @@ namespace CppOverride
1011
{
1112
struct ConditionInfo
1213
{
13-
std::function<bool(void* instance, const std::vector<void*>& args)> LambdaCondition;
14+
std::function<bool(void* instance, const std::vector<TypedDataInfo>& args)> LambdaCondition;
1415
std::vector<DataInfo> ArgsCondition = {};
1516
int Times = -1;
1617
int CalledTimes = 0;

0 commit comments

Comments
 (0)