Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· openaiHugging Face Suzhen/CodeChat

how can we define EXPECT_CALL for a function returns noz zero value via argument, if the…

  1. User

    how can we define EXPECT_CALL for a function returns noz zero value via argument, if the argument is &ret

  2. Assistant

    In Google Test's Google Mock framework, EXPECT_CALL is used to express expectations on a method, including how many times it will be called, with what arguments, and what it will return or how it will behave. When a function doesn't return a value directly (i.e., it returns a value through a pointer or a reference argument), you can still set expectations on the function call. However, the mechanism to do this is slightly different from dealing with return values directly.

    Assuming you are working with a mock method that returns a non-zero value via an argument (e.g., a pointer or reference), here's a general approach on how to set expectations and actions for such methods using Google Mock.

    Let's consider a scenario where you have a method signature like:

    void getSomething(int& ret);
    

    or

    void getSomething(int* ret);
    

    You will need to use the DoAll action combined with SetArgPointee (for pointers) or SetArgReferee (for references) to assign a value to the passed-in argument:

    For a reference argument (int& ret):

    First, define your mock class and method:

    class MockClass {
    public:
        MOCK_METHOD(void, getSomething, (int& ret), ());
    };
    

    Then, set your expectation and action:

    MockClass mockObj;
    int expectedValue = 123; // The value you want 'ret' to be set to
    
    EXPECT_CALL(mockObj, getSomething(::testing::_)) // ::testing::_ is a matcher that matches any argument
        .WillOnce(::testing::DoAll(::testing::SetArgReferee<0>(expectedValue),
                                   ::testing::Return())); // <0> indicates the first argument
    

    For a pointer argument (int* ret):

    Define your mock class and method if not already done:

    class MockClass {
    public:
        MOCK_METHOD(void, getSomething, (int* ret), ());
    };
    

    Set expectation and action similarly, but use SetArgPointee for pointers:

    MockClass mockObj;
    int expectedValue = 123; // The value you want *ret to be set to
    
    EXPECT_CALL(mockObj, getSomething(::testing::_)) // ::testing::_ matches any argument
        .WillOnce(::testing::DoAll(::testing::SetArgPointee<0>(expectedValue),
                                   ::testing::Return())); // <0> indicates the first argument
    

    Explanation:

    • EXPECT_CALL: Sets an expectation on a mock method.
    • ::testing::_: A wildcard matcher that matches any argument.
    • ::testing::DoAll: Allows combining actions. Here, it sets the argument and then uses ::testing::Return() to indicate the method returns (useful in methods that return void).
    • ::testing::SetArgReferee<0>: Sets the first argument (<0>) when it is a reference.
    • ::testing::SetArgPointee<0>: Sets the pointed-to value of the first argument (<0>) when it is a pointer.

    Remember, the specifics of your implementation might vary depending on the actual return type and whether you have more complex conditions or multiple arguments to deal with. Google Mock provides a versatile set of matchers and actions to tailor expectations to most scenarios.

  3. User

    how to write expect call for the function glGetShaderiv(shader, GL_COMPILE_STATUS, &ret1);

  4. Assistant

    Writing an EXPECT_CALL for a function like glGetShaderiv involves a bit more complexity due to its nature of being an OpenGL function and its behavior of writing to a pointer provided as an argument. To test such functions using Google Mock, you typically need to wrap this function in an interface that you can then mock.

    For this example, let's assume we're dealing with an OpenGL wrapper that provides an interface to glGetShaderiv, and we'd like to set up expectations on how it's called.

    First, define the interface that wraps OpenGL calls:

    class IGLWrapper {
    public:
        virtual ~IGLWrapper() = default;
        virtual void glGetShaderiv(GLuint shader, GLenum pname, GLint* params) const = 0;
    };
    

    Then, implement a mock class based on this interface:

    class MockGLWrapper : public IGLWrapper {
    public:
        MOCK_METHOD(void, glGetShaderiv, (GLuint shader, GLenum pname, GLint* params), (const, override));
    };
    

    Now, to write an EXPECT_CALL for glGetShaderiv, you'd want to specify that when this method is called, it should write a specific value (e.g., GL_TRUE to indicate success) to the memory pointed to by params. With Google Mock, you can achieve this through a combination of WillOnce and WithArgs, utilizing a lambda to set the value:

    MockGLWrapper mockGLWrapper;
    
    GLuint shader = 1; // Assuming we're working with shader ID 1
    GLenum statusToCheck = GL_COMPILE_STATUS;
    GLint expectedStatusValue = GL_TRUE; // Assume the shader compilation is expected to be successful
    
    EXPECT_CALL(mockGLWrapper, glGetShaderiv(shader, statusToCheck, ::testing::_))
        .WillOnce(::testing::WithArgs<2>([expectedStatusValue](GLint* params) {
            *params = expectedStatusValue;
        }));
    

    Here’s the breakdown:

    • EXPECT_CALL sets up the expectation on mockGLWrapper.glGetShaderiv with the specified shader and statusToCheck values. The third parameter (params) is matched with ::testing::_, which means any value is allowed (since it's a pointer, and we're focusing on what it points to, not the pointer's value itself).
    • WillOnce specifies that this expectation happens once. Inside WillOnce, you use WithArgs<2> to select the third argument (<2> because it's zero-indexed) which is the GLint* params.
    • The lambda [expectedStatusValue](GLint* params) { *params = expectedStatusValue; } is used to directly modify the value pointed to by params, simulating what glGetShaderiv does.

    This way, you're effectively mocking the glGetShaderiv function, allowing you to test your shader compilation status checking logic without depending on the actual OpenGL driver's behavior. Remember, since OpenGL functions are part of an external library, this sort of wrapping and mocking is necessary to isolate your tests from external dependencies, enhancing test reliability and speed.