Skip to content

Latest commit

ย 

History

History
309 lines (270 loc) ยท 10 KB

README_en.md

File metadata and controls

309 lines (270 loc) ยท 10 KB

็ฎ€ไฝ“ไธญๆ–‡

What is EMOCK? license Build Status Build status

  • 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

  • ๐Ÿฎ 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
  • ๐Ÿ‘ 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 โœ… โœ… โœ… โœ… โœ… โœ… โœ… โœ… โœ… โœ…
CppUMock โœ… โœ… โŒ โœ… โŒ โœ… โœ… โœ… โœ… โŒ[0]
mockcpp โœ… โœ… โŒ โœ… โŒ โœ… โœ… โŒ โœ… โŒ[1]
googlemock โœ… โœ… โœ… โœ… โŒ โŒ โŒ โŒ โŒ โŒ[2]
mockitopp โœ… โœ… โŒ โœ… โŒ โŒ โŒ โŒ โŒ โŒ[1]
C-Mock โœ… โŒ โŒ โœ… โœ… โœ… โœ… โŒ โŒ โŒ[1]
CppFreeMock โœ… โŒ โŒ โœ… โœ… โœ… โœ… โœ… โœ… โŒ[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 โœ… โœ… Use trampoline (5 bytes)
mockcpp โŒ โŒ Long jump under x64 (14 bytes)
CppFreeMock โŒ โŒ 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

    // 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

    // 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

    // 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<double (*)(double)>(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<void (Foo::*)(double)>(&Foo::bar))
        .expects(never()); // won't be called

Manual

Thanks

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

Buy me a cup of โ˜•๏ธ or ๐Ÿบ

alipay qrcode wechat pay qrcode PayPal qrcode