https://github.com/neko-box-coder/simpleoverride
Allow overriding return value or setting arguments, similar to mocking but more flexible
https://github.com/neko-box-coder/simpleoverride
c-plus-plus cpp cpp11 mock mocking-framework
Last synced: 10 months ago
JSON representation
Allow overriding return value or setting arguments, similar to mocking but more flexible
- Host: GitHub
- URL: https://github.com/neko-box-coder/simpleoverride
- Owner: Neko-Box-Coder
- License: mit
- Created: 2023-05-31T15:07:44.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-11-20T21:42:54.000Z (over 2 years ago)
- Last Synced: 2025-01-16T01:48:49.026Z (about 1 year ago)
- Topics: c-plus-plus, cpp, cpp11, mock, mocking-framework
- Language: C++
- Homepage:
- Size: 179 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Simple Override
---
> #### This Has Been Superceeded By [CppOverride](https://github.com/Neko-Box-Coder/CppOverride)
---
A simple framework for overriding function behaviours.
This allows overriding return value or setting arguments, similar to mocking but more flexible
```cpp
#include "SimpleOverride.hpp"
SimpleOverride Overrider;
int ChangeReturnValue(int a, float* b)
{
//SO_RETURN_IF_FOUND(functionOverrideObj, functionRef, returnType, args...)
SO_RETURN_IF_FOUND(Overrider, ChangeReturnValue(int, float*), int, a, b);
return 0;
}
void SetArgumentValue(int a, float& b, int* c)
{
//SO_MODIFY_ARGUMENTS_IF_FOUND(overrideObj, functionRef, args...)
SO_MODIFY_ARGUMENTS_IF_FOUND(Overrider, SetArgumentValue(int, float&, int*), a, b, c);
//You can also use SO_ARGUMENTS_AND_RETURN_IF_FOUND to return a specify value
// When the condition is met
}
int main()
{
//Example of overriding return value
SO_OVERRIDE_RETURNS (Overrider, ChangeReturnValue(int, float*))
.Returns(1)
//Pointers are automatically dereferenced when getting compared.
// Unless it is cast to void*, then it won't be dereferenced.
.WhenCalledWith(2, 3.f)
//By default, it is infinite times if not specified
.Times(2);
float testFloat = 3.f;
assert(ChangeReturnValue(2, &testFloat) == 1 && ChangeReturnValue(2, &testFloat) == 1);
testFloat = 4.f;
assert(ChangeReturnValue(4, &testFloat) == 0);
//Example of overriding argument value
SO_OVERRIDE_ARGS(Overrider, SetArgumentValue(int, float&, int*))
//Again, pointers are automatically dereferenced when getting set
.SetArgs(SO_DONT_SET, 2.f, 3)
.WhenCalledWith(4, SO_ANY, SO_ANY);
int testInt = 5;
SetArgumentValue(4, testFloat, &testInt);
assert(testFloat == 2.f && testInt == 3);
std::cout << "Success\n";
//------------------------------------------------
//Extra:
//------------------------------------------------
//ReturnsByAction and SetArgsByAction can also be used to return or set arguments by lambda
//SO_DECLARE_INSTNACE(OverrideObjName) macro can be used inside a class declaration to
// declare an instance of overriding object
//SO_DECLARE_OVERRIDE_METHODS(OverrideObjName) macro can be used inside a class declaration
// to declare proxy methods inline implementations to the override object
//SO_NonCopyable and SO_NonComparable can be used for objects that are not copyable or comparable.
// These objects won't be compared against in WhenCalledWith.
//You also have access to "If" clause for additonal conditions
//Similarly, you have "WhenCalledExpectedly_Do" and "Otherwise_Do" clauses for performing custom actions
// when the condition is matched or not respectively
//For usage on these extra actions, see Tests/SimpleOverrideTests.cpp
return 0;
}
```