# emock
**Repository Path**: UnitTestTools/emock
## Basic Information
- **Project Name**: emock
- **Description**: No description available
- **Primary Language**: C++
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 1
- **Created**: 2024-06-29
- **Last Updated**: 2025-04-23
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
[简体中文](./README.md)
# What is EMOCK? [](https://github.com/ez8-co/emock/blob/master/LICENSE) [](https://orca-zhang.semaphoreci.com/projects/emock) [](https://ci.appveyor.com/project/orca-zhang/emock)
- EMOCK is next generation easy-to-use C++ Mock Library based on mockcpp.
- **[Easy to Use]** only one MACRO, without extra studies.
- **[No Dependencies]** unless STL and std C libraries.
- **[Cross Platform]** support popular OS (both x86 & x64, *nix/windows/macOS).
- **[Fully Support]** support all kinds of functions.
- **[No Intrusions]** no need to modify any source code.
## Work-in-process features
- :cow: reflection-like support: declaration free, support mocking invisible static global function & functions inside dynamic libraries.
- using dwarf technology under linux
- support `extern "C"` functions
- support mocking functions with `throw` specifications
- change default testing framework to gtest
- incomplete caller matcher feature
## Recently supported
- all-in-one `EMOCK` macro (no-need to do IoC for virtual member functions)
- support mocking variadic function, e.g. `int test(int a, ...)`
- support mocking overloaded member functions under Windows
- reduce warning of getting address of virtual method under Linux
- trampoline that extend `this` pointer as first argument for member functions under Windows
- :clap: `near jump + trampoline` under x64 avoid unexcepted coverage by long jump
## Feature matrix
### comparision with popular mock libraries
- some of the conclusion below may be not exactly, contact me to correct if there were mistakes
|
Platform |
Member function |
General function |
Misc |
| Library |
Linux |
Windows |
MacOS |
Virtual |
Normal |
Static |
Global |
Variadic |
Template |
Intrusion-free |
| EMOCK |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
| CppUMock |
:white_check_mark: |
:white_check_mark: |
:x: |
:white_check_mark: |
:x: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:x:[0] |
| mockcpp |
:white_check_mark: |
:white_check_mark: |
:x: |
:white_check_mark: |
:x: |
:white_check_mark: |
:white_check_mark: |
:x: |
:white_check_mark: |
:x:[1] |
| googlemock |
:white_check_mark: |
:white_check_mark: |
:x: |
:white_check_mark: |
:x: |
:x: |
:x: |
:x: |
:x: |
:x:[2] |
| mockitopp |
:white_check_mark: |
:white_check_mark: |
:x: |
:white_check_mark: |
:x: |
:x: |
:x: |
:x: |
:x: |
:x:[1] |
| C-Mock |
:white_check_mark: |
:x: |
:x: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:x: |
:x: |
:x:[1] |
| CppFreeMock |
:white_check_mark: |
:x: |
:x: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:white_check_mark: |
:x:[1] |
- NOTES:
- [0]: need IoC setter and override virtual functions of base class
- [1]: need declarartion of interface(with pure virtual funtions), not support hybrid class (virtual & normal mem_fun at same time)
- [2]: need IoC setter and declaration of mock interface contains mem_fun with same arg list and return type that to be tested
- [0][1][2]: cannot test embedded object or reference
### comparison with libraries those using api hook tech
| Library |
Jump-safe |
Visible of this pointer[Windows] |
Comment |
| EMOCK |
:white_check_mark: |
:white_check_mark: |
Use trampoline (5 bytes) |
| mockcpp |
:x: |
:x: |
Long jump under x64 (14 bytes) |
| CppFreeMock |
:x: |
:x: |
Long jump under x64 (14 bytes) |
- EMOCK should also work under \*nix(UNIX, Android, MacOS and iOS), or maybe need minor adaptation.
## Quick view
#### Global function
```cpp
// function to be tested
int target_func(int x);
// how to mock
EMOCK(target_func)
.stubs()
.with(any())
.will(returnValue(1));
// assert return 1
ASSERT_EQ(target_func(0), 1);
```
#### Member functions
```cpp
// member functions to be tested
class Foo
{
public:
void bar1(int);
virtual void bar2(double);
static int bar3();
};
////////////////////////////////////
// mock functions specified to be called
void EMOCK_API mock_bar1(Foo* obj, int) {
// ...
}
void EMOCK_API mock_bar2(Foo* obj, double) {
// ...
}
// how to mock kinds of member functions
EMOCK(&Foo::bar1)
.stubs()
.will(invoke(mock_bar1)); // invoke user denfined mocker instead of return value
EMOCK(&Foo::bar2) // virtual mem_fun isn't special
.stubs()
.will(invoke(mock_bar2));
EMOCK(Foo::bar3) // static mem_fun is like global function
.stubs()
.will(returnValue(1));
```
#### Overloaded member functions
```cpp
// overloaded function to be tested
int foobar(int x) {
return x;
}
double foobar(double x) {
return x;
}
// how to mock overloaded functions
EMOCK((int (*)(int))foobar)
.stubs()
.will(returnValue(1));
EMOCK(static_cast(foobar))
.stubs()
.will(returnValue(1.0));
// overloaded member functions to be tested
class Foo
{
public:
void bar(int);
void bar(double);
};
// how to mock overloaded member functions
EMOCK((void (Foo::*)(int))&Foo::bar)
.expects(once()); // call only once
EMOCK(static_cast(&Foo::bar))
.expects(never()); // won't be called
```
## Manual
- [Visit wiki to see how to use EMOCK](https://github.com/ez8-co/emock/wiki)
## Thanks
- EMOCK is evolved from mockcpp
- credit to author & contributor of mockcpp:
- [Darwin Yuan: darwin.yuan@gmail.com](https://github.com/godsme)
- [Chen Guodong: sinojelly@gmail.com](https://github.com/sinojelly)
- initial version is imported from [mockcpp@bitbucket](https://bitbucket.org/godsme/mockcpp)
## Acknowledged issues
- work with `valgrind`
- add `--smc-check=all` to avoid invalidation of dynamically-generated code (API hook).
- unable to mock `syscall` related functions (e.g. `gettimeofday`) yet.
## What's more
- Please feel free to use EMOCK.
- Looking forward to your feeback.[create new issues](https://github.com/ez8-co/emock/issues/new).
## Buy me a cup of ☕️ or 🍺