GithubHelp home page GithubHelp logo

mickaelblet / mockc Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 60 KB

Create mock for c function with google-test

License: MIT License

C++ 81.03% CMake 18.97%
mock libc google-test google-mock header-only

mockc's Introduction

MockC

Mock C function with google mock.
Compile with dl library

using ::testing::_;
// create class mock
MOCKC_METHOD3(ssize_t, write, (int /* fd */, const void* /* buf */, size_t /* nbytes */)) // at top of source

// in test function
MOCKC_NEW_INSTANCE(write); // create a local mock
MOCKC_INSTANCE(write) // get mockc instance of getchar
MOCKC_EXPECT_CALL(write, (_, _, _)) // simply use EXPECT_CALL
// same EXPECT_CALL(MOCKC_INSTANCE(write), write(_, _, _))

// in test function
MOCKC_ENABLE(write) // active mock function
MOCKC_DISABLE(write) // desactive mock function
MOCKC_GUARD(write) // create a guard for mock function
MOCKC_GUARD_REVERSE(write) // disable at constructor > enable at destructor

Example:

#include "unistd.h"

#include "mockc.h"

using ::testing::_;

MOCKC_METHOD3(ssize_t, write, (int /* fd */, const void* /* buf */, size_t /* nbytes */));

ACTION(write) {
    write(STDOUT_FILENO, "write: real in mock!\n", sizeof("write: real in mock!\n") - 1);
    return -42;
}

GTEST_TEST(mockc, example_write) {
    MOCKC_NEW_INSTANCE(write);

    // simply use EXPECT_CALL with MOCKC
    MOCKC_EXPECT_CALL(write, (0, _, 42)).WillRepeatedly(write());

    {
        MOCKC_GUARD(write);                   // enable call to mock
        EXPECT_EQ(write(0, "mock", 42), -42); // use mock
    }                                         // disable call to mock

    write(STDOUT_FILENO, "write: use real\n", sizeof("write: use real\n") - 1); // use real
}

Result:

Running main() from gmock_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from mockc
[ RUN      ] mockc.example_write
write: real in mock!
write: use real
[       OK ] mockc.example_write (3 ms)
[ RUN      ] mockc.example_write2
write: real in mock!
write: use real
[       OK ] mockc.example_write2 (0 ms)
[----------] 2 tests from mockc (3 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (3 ms total)
[  PASSED  ] 2 tests.

mockc's People

Contributors

mickaelblet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.