Giter VIP home page Giter VIP logo

Comments (3)

SergiusTheBest avatar SergiusTheBest commented on July 21, 2024

Hi @et-nik,

Unfortunately :: symbol breaks variable name generation in C++ macro. So it's not easy to fix. You can use the following approach with using namespace foo in anonymous namespace block:

namespace foo {
    int bar(std::string a) {
        return 2;
    }
}

namespace {
    using namespace foo;

    MOCK_GLOBAL_FUNC1(bar, int(std::string));

    TEST(config_parse_test, Success) {
        EXPECT_GLOBAL_CALL(bar, bar("baz")).WillOnce(Return(1));
        int a = bar("baz");

        ASSERT_EQ(a, 1);
    }
}

Another option is to provide a different macro that accepts namespace, for example:

MOCK_GLOBAL_FUNC1_NS(foo, bar,  int(std::string));

PR is welcomed!

from gmock-global.

Chandra123539 avatar Chandra123539 commented on July 21, 2024

Hi Sergey,

In the above test case, if we have foo namespace in a library and when we want to mock foo bar global function , mocking does not work.

Lets consider a case here.

foo namespace is used in static library "lib1". There is a method ( method 1) in this lib1 that calls foo bar function.

Now to mock foo bar in method 1, i have done following.

  1. created a unit test project. Added lib1 as reference along with gmock.

Test code looks like this.

namespace
{

//using namespace foo; // removed // foo is in one of the cpp files of lib1, so not adding
// if we keep foo in header file and add here, it creates multiple definition issue.

MOCK_GLOBAL_FUNC1(bar, int(std::string));

TEST(config_parse_test, Success) {
    EXPECT_GLOBAL_CALL(bar, bar("baz")).WillOnce(Return(1));
    int a =lib1->method1();   // this internally calls  bar("baz");

    ASSERT_EQ(a, 1);
}

}

after doing this, i am getting original bar call not the mocked one
please let me know on this.

from gmock-global.

SergiusTheBest avatar SergiusTheBest commented on July 21, 2024

Hi @Chandra123539 ,

gmock-global can't magically replace existing functions, it can only create new ones. So you have to create a mechanism for function replacement by yourself.

You can set linker options to exploit a multiple definition issue and use the definition provided by mocks instead of the real one. Or it can be conditional compilation. Or linker scripts. Or function pointers. Whatever works best for you. I hope you've got the idea.

from gmock-global.

Related Issues (6)

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.