gmock-global icon indicating copy to clipboard operation
gmock-global copied to clipboard

Namespaces not working

Open et-nik opened this issue 5 years ago • 3 comments

How can I use calls with namespace? In my code I use different functions with namespaces (filesystem::exists etc.). But gmock-global not working correctly with namespaces.

I will show what I want with an examples.

I want to mock function:

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

Works

using foo::bar;

namespace {
    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);
    }
}

Other mock I need. Not works


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

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

        ASSERT_EQ(a, 1);
    }
}

Error:

/some/path/to/unit/test.cpp: In member function ‘virtual void {anonymous}::config_parse_test_Success_Test::TestBody()’:
/some/path/to/tests/../gmock-global/include/gmock-global/gmock-global.h:910:45: error: ‘gmock_globalmock_foo’ was not declared in this scope
 #define GLOBAL_MOCK_TYPE(name)              gmock_globalmock_##name

Other variant mock I also need. Not works

using ::foo::bar;

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

    TEST(config_parse_test, Success) {
        EXPECT_GLOBAL_CALL(bar, bar("baz")).WillOnce(Return(1));
        int a = foo::bar("baz"); // Called in my code

        ASSERT_EQ(a, 1);
    }
}

et-nik avatar Nov 04 '19 22:11 et-nik

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!

SergiusTheBest avatar Nov 11 '19 13:11 SergiusTheBest

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.

Chandra123539 avatar Dec 17 '21 13:12 Chandra123539

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.

SergiusTheBest avatar Dec 23 '21 16:12 SergiusTheBest