Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespaces not working #5

Open
et-nik opened this issue Nov 4, 2019 · 3 comments
Open

Namespaces not working #5

et-nik opened this issue Nov 4, 2019 · 3 comments

Comments

@et-nik
Copy link

et-nik commented Nov 4, 2019

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);
    }
}
@SergiusTheBest
Copy link
Member

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!

@Chandra123539
Copy link

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.

@SergiusTheBest
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants