{"id":17049163,"url":"https://github.com/hedayat/powerfake","last_synced_at":"2025-07-19T03:32:23.740Z","repository":{"id":41139213,"uuid":"160325442","full_name":"hedayat/powerfake","owner":"hedayat","description":"C++ Faking library, which allows faking/mocking regular functions, static member functions and non-virtual member functions for testing purposes.","archived":false,"fork":false,"pushed_at":"2025-05-07T14:28:56.000Z","size":730,"stargazers_count":28,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-16T03:14:22.761Z","etag":null,"topics":["clang","cplusplus","cplusplus-17","faking","gcc","mocking","non-virtual-interface","testing","testing-tools","unit-test","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hedayat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE_1_0.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-12-04T08:40:12.000Z","updated_at":"2025-05-07T14:28:59.000Z","dependencies_parsed_at":"2024-04-09T12:55:50.981Z","dependency_job_id":"383445d4-3d2a-405a-a6d3-b77dee53c9ee","html_url":"https://github.com/hedayat/powerfake","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hedayat/powerfake","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedayat%2Fpowerfake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedayat%2Fpowerfake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedayat%2Fpowerfake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedayat%2Fpowerfake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hedayat","download_url":"https://codeload.github.com/hedayat/powerfake/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedayat%2Fpowerfake/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265883614,"owners_count":23843792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clang","cplusplus","cplusplus-17","faking","gcc","mocking","non-virtual-interface","testing","testing-tools","unit-test","unit-testing"],"created_at":"2024-10-14T09:53:59.491Z","updated_at":"2025-07-19T03:32:23.716Z","avatar_url":"https://github.com/hedayat.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"PowerFake\n=============\nPowerFake is a C++ library/tool for faking \u0026 mocking free functions and\nnon-virtual member functions. Using PowerFake, you can mock non-virtual member\nfunctions and free functions with existing mocking frameworks. Currently,\nit provides seamless integration with FakeIt and Google Mock frameworks and\nmakes all of their facilities available.\n\nIt is built on top of GNU Linker's --wrap option and linker scripts.\n\n## Features\n* Faking C/C++ free functions\n* Faking static class member functions\n* Faking non-virtual member functions of a class\n* Faking non-virtual dispatch of virtual functions\n* Faking private and protected member functions\n* Provides access to private \u0026 protected member variables\n* Provides control over the life time of faking/mocking\n* Provides integration with FakeIt(https://github.com/eranpeer/FakeIt)\n* Provides integration with Google Mock(https://github.com/google/googletest/)\n\n## At a Glance\n\n### Mark Target Functions\nEvery function we want to override in tests should be marked as such for PowerFake,\nwhich can happen in a single source file, which we call `wrap.cpp` using\nWRAP_FUNCTION() and HIDE_FUNCTION() macros. Using HIDE_FUNCTION(), you'll lose\naccess to the original function, but you can also capture calls in the same\ntranslation unit.\n\n```\n#include \u003cpowerfake.h\u003e\n#include \u003cfunctinos_header.h\u003e\n\nWRAP_FUNCTION(std::string (float), overloaded2);\nWRAP_FUNCTION(normal_func);\n\n#ifdef HIDE_FUNCTION\nHIDE_FUNCTION(called_by_normal_func);\n#endif\n\nWRAP_FUNCTION(STATIC(SampleClass, SampleClass::StaticFunc));\nWRAP_FUNCTION(STATIC(SampleClass, PRIVATE(SampleClass::PrivateStaticFunc)));\nWRAP_FUNCTION(SampleClass::CallThis);\nWRAP_FUNCTION(int (int) const, SampleClass::OverloadedCall);\nWRAP_FUNCTION(PFK_PRIVATE(SampleClass::SamplePrivate));\nWRAP_FUNCTION(void (int), PRIVATE(SampleClass::OverloadedPrivate));\n```\n\n### Build System Setup\nPowerFake needs build system integration. It provides CMake support itself\nthrough `bind_fakes()` function:\n\n```\nadd_library(mainlib STATIC ${main_sources}) # production code (test subject) as a library\n\nadd_library(wrap_lib STATIC wrap.cpp) # library containing WRAP_FUNCTION()/HIDE_FUNCTION() macros\n\n# Test runner build\nadd_executable(test_runner ${test_sources})\ntarget_link_libraries(samples wrap_lib mainlib)\nbind_fakes(samples SUBJECT mainlib WRAPPERS wrap_lib CACHE)\n\n```\n\n### Use with FakeIt\n```\nTAG_PRIVATE(SamplePrivate, SampleClass::SamplePrivate);\nTAG_PRIVATE(VirtualPrivate, SampleBase::CallPrivateVirtual);\nTAG_PRIVATE(OverloadedPrivateInt, void (int), SampleClass::OverloadedPrivate);\n\nvoid UseFakeIt()\n{\n    PowerFakeIt\u003c\u003e pfk;\n\n    When(Function(pfk, normal_func)).Do([](int, const char **const *,\n            const std::string \u0026, const char *(*)(const char *))\n            {\n                cout \u003c\u003c \"WOW :) \" \u003c\u003c endl;\n            });\n\n    normal_func(100, nullptr, string(), nullptr);\n\n    Verify(Function(pfk, normal_func).Using(100, nullptr, string(), nullptr)).Exactly(Once);\n\n    PowerFakeIt\u003cSampleClass\u003e pfk2;\n    When(Method(pfk2, CallThis)).Do([]() { cout \u003c\u003c \"WOW2\" \u003c\u003c endl; });\n    When(OverloadedMethod(pfk2, OverloadedCall, int())).Return(4);\n    When(ConstOverloadedMethod(pfk2, OverloadedCall, int(int))).Return(5);\n    When(PrivateMethod(pfk2, SamplePrivate)).Do([]() { cout \u003c\u003c \"Private WOW!\" \u003c\u003c endl; });\n    When(PrivateMethod(pfk2, OverloadedPrivateInt)).Do([](int) { cout \u003c\u003c \"Overloaded(int) Private WOW!\" \u003c\u003c endl; });\n\n    SampleClass s;\n    s.CallThis();\n    s.OverloadedCall();\n    s.CallSamplePrivate();\n    s.CallOverloadedPrivate(4);\n\n    Verify(Method(pfk2, CallThis)).Exactly(Once);\n    Using(pfk2).Verify(Method(pfk2, CallThis)\n        + OverloadedMethod(pfk2, OverloadedCall, int()));\n\n    VerifyNoOtherInvocations(Method(pfk2, CallThis));\n\n    ExMock\u003cSampleBase\u003e mock;\n    Fake(PrivateMethod(mock, VirtualPrivate));\n\n    mock.get().CallNonPublics(30);\n    Verify(PrivateMethod(mock, VirtualPrivate)).Exactly(Once);\n}\n```\n\n### Use with Google Mock (gMock)\n```\nclass MockFreeFunctions\n{\n    public:\n        GPFK_MOCK_FUNCTION(void, normal_func, (int b, const char **const *c,\n                const std::string \u0026d, const char *(*e)(const char *)));\n        GPFK_MOCK_FUNCTION(std::string, overloaded2, (int b));\n};\n\nclass MockSample: public GPowerFake\u003cSampleClass\u003e\n{\n    public:\n        GPFK_MOCK_METHOD(void, CallThis, (), (const));\n        GPFK_MOCK_METHOD(int, OverloadedCall, ());\n\n        // free functions can be mocked here too\n        GPFK_MOCK_FUNCTION(void, noexcept_func, ());\n};\n\nvoid GMockSamples()\n{\n    MockSample mock;\n    MockFreeFunctions freemock;\n    EXPECT_CALL(mock, CallThis()).Times(AtLeast(1));\n    EXPECT_CALL(mock, OverloadedCall()).Times(AtLeast(1));\n    EXPECT_CALL(freemock, normal_func(1, nullptr, \"\", nullptr))\n        .Times(AtLeast(1));\n    EXPECT_CALL(freemock, overloaded2(_)).Times(AtLeast(1));\n    EXPECT_CALL(mock, noexcept_func()).Times(AtLeast(1));\n\n    normal_func(1, nullptr, \"\", nullptr);\n    noexcept_func();\n    SampleClass s;\n    s.CallThis();\n    s.OverloadedCall();\n    overloaded2(60);\n}\n```\n\n### Independent use\n```\nTAG_PRIVATE(SamplePrivate, SampleClass::SamplePrivate);\n\nvoid PowerFakeSamples()\n{\n    overloaded(6.0F); // Real call\n    auto offk = MakeFake\u003cvoid (float)\u003e(overloaded,\n        [](float) { cout \u003c\u003c \"Fake called for overloaded(float)\" \u003c\u003c endl; }\n    );\n    overloaded(6.0F); // Fake call for overloaded free function\n\n    // For member functions, you can optionally access object's this ptr\n    auto ccfk = MakeFake(\u0026SampleClass::CallThis,\n        [](SampleClass *this_ptr) { cout \u003c\u003c \"Fake called for SampleClass::CallThis for object: \" \u003c\u003c this_ptr \u003c\u003c endl; }\n    );\n    auto oc2fk = MakeFake\u003cint (int) const\u003e(\u0026SampleClass::OverloadedCall,\n        [](int) {\n            cout \u003c\u003c \"Fake called for SampleClass::OverloadedCall(int) const\" \u003c\u003c endl;\n            return 0;\n        }\n    );\n\n    SampleClass sc;\n    sc.CallThis();\n    sc.OverloadedCall();\n\n    SampleClass2 sc2;\n    sc2.CallThis(); // Real call\n    {\n        auto ct2fk = MakeFake\u003cvoid (int)\u003e(\u0026SampleClass2::CallThis,\n            [](int) { cout \u003c\u003c \"Fake called for SampleClass2::CallThis\" \u003c\u003c endl; }\n        );\n        sc2.CallThis(4); // Fake call\n        {\n            auto ct2fk = MakeFake\u003cvoid (int)\u003e(\u0026SampleClass2::CallThis,\n                [](int) { cout \u003c\u003c \"Nested Fake called for SampleClass2::CallThis\" \u003c\u003c endl; }\n            );\n            sc2.CallThis(4); // Nested fake call\n        }\n        sc2.CallThis(4); // Fake call\n    }\n\n    auto stfk = MakeFake(\u0026SampleClass2::StaticFunc, []() { cout \u003c\u003c \"Faked call for StaticFunc\" \u003c\u003c endl; });\n    SampleClass::StaticFunc();\n\n    auto pfk = MakeFake\u003cSamplePrivate\u003e([]() { cout \u003c\u003c \"Faked call for SamplePrivate\" \u003c\u003c endl; });\n    sc2.CallSamplePrivate();\n}\n```\n\n## Installation\nPowerFake can be either installed into an address and be used as an external\ndependency by projects; or be copied into another project's source directory and\nbe used directly without any installation.\n\n### Using Embedded PowerFake\nYou can place powerfake source directory inside your project (e.g. in third_party/\ndirectory), and then `include()` powerfake's `EmbedPowerFake.cmake` into your\nown project's `CMakeLists.txt`:\n\n```\ninclude(${CMAKE_SOURCE_DIR}/third_party/powerfake/EmbedPowerFake.cmake)\n```\n\n### System Wide Installation\nYou can also install PowerFake and use it using CMake's `find_package(PowerFake)`\nin the `CMakeLists.txt` of your project. You can install PowerFake using\nconventional cmake commands:\n\n```\n$ mkdir build\n$ cd build\n$ cmake ..\n$ cmake --build . -j\n$ cmake --install .\n```\n\n## Limitations\n* Cannot fake constructors/destructors\n* Cannot fake inlined functions\n* Faking function calls in the same translation unit as the target function has\nsome limitations\n* Supported compilers: GCC, MinGW, and Clang (experimental)\n* Supported linkers: LLD, GNU Linker\n* Cannot work with GCC LTO, since ld's --wrap is not supported in this case\n* Currently, it only provides CMake integration\n\n## Dependencies\n* GNU Linker (ld) or LLVM Linker (lld)\n* GCC or Clang with C++17 support\n* CMake\n* Boost Core\n\n## Roadmap\n* Provide support for other build systems (e.g. QMake, Autotools, Meson)\n* Integrate with more C++ mocking frameworks\n* Support faking virtual functions with virtual dispatch\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedayat%2Fpowerfake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhedayat%2Fpowerfake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedayat%2Fpowerfake/lists"}