GTest and GoogleMock EXPECT_CALL Doesn't work on Windows, passes to Mac with char * parameter

I have a test in a project that I have inherited that is similar to this

std::string value("test string");
const char * buffer = value.c_str();
EXPECT_CALL(object, foo(_,_,buffer, buffer.size(), _)).WillOnce(Return(0));
bar(value);

      

The buffer is a char * pointing to a string of data. I have inserted dummy values ​​as an object to focus on the problem that seems to be used in EXPECT_CALL. Immediately after this EXPECT_CALL, the method bar is called, which takes the original string value as a parameter, and then inside the method calls foo with a buffer built from the original string value.

This test works on the Mac build of this project, but does not work on the Windows version. It seems to be comparing pointer addresses for two char pointers expected and actual and then fail because they are different. The foo method is definitely called inside the bar.

If this test method (EXPECT_CALL) compares the addresses of the pointers and not the data with that pointer, then shouldn't the test fail on Mac as well?

Anyone familiar with the excellent difference between Mac and windows when using EXPECT_CALL and pointers?

The error I see

unknown file: error:
Unexpected mock function call - returning default value.
    Function call: foo(NULL, 1, 0000000001CAAE78 pointing to "test string", 11,_)
           Returns: 0
Google Mock tried the following 1 expectation, but it didn't match:

test.cpp(235): EXPECT_CALL(object, foo(_,_,buffer,buffer.size(),_)...
  Expected arg #2: is equal to 0000000001CAAF78 pointing to "test string"
           Actual: 0000000001CAAE78 pointing to "test string"
         Expected: to be called once
           Actual: never called - unsatisfied and active
   test.cpp(235): error: Actual function call count doesn't match EXPECT_CALL(object, foo(_,_,buffer, buffer.size(), _)...
     Expected: to be called once

      

I've modified this error to reflect my example.

Thank you in advance for your help.

+3


source to share


2 answers


There seems to be no obvious difference between Mac and Windows regarding EXPECT_CALL. I suppose there could also be differences between implementation string

and how compilers handle constant strings, which may explain the difference in behavior.

However, I expect the pointer arguments to be matched by comparing addresses. You have to use special Matchers to compare values . In particular, there are many String Matchers for your case , including StrEq

for string equality, which you can use like:



EXPECT_CALL(object, foo(_,_,testing::StrEq(buffer),value.size(),_))
  .WillOnce(Return(0));

      

+4


source


The reason this works on Windows is because MSVC implements string pool ( /GF

). See here:

http://msdn.microsoft.com/en-us/library/s0s0asdt.aspx



So why does this make it work? Because gmock is comparing pointer addresses, and when you have two identical constant strings, the string pool will cause them to have the same value. Turn off this compiler option and see how it worked.

+1


source







All Articles