Search code examples
unit-testingglibgooglemock

Make 2 pointer point to the same adress with google tests actions


I need to test the case where g_ascii_strtoll fails and sets both pointers to the same value, set errno to 0 and return 0 (According to the doc : "If the string conversion fails, zero is returned, and endptr returns nptr (if endptr is non-NULL)") (even if output_str = NULL in my case)
My code to test:

gchar* output_ptr= NULL;
gchar* input_ptr= "f";
result = g_ascii_strtoll(input_ptr, &output_ptr, 10);

My Google test snippet:

const gchar* nptr;
EXPECT_CALL(mock_gstring, g_ascii_strtoll(_,_,_)).WillOnce( DoAll(SaveArg<0>(&nptr), SetArgReferee<1>((char)*nptr), SetErrnoAndReturn(0,0))  );

Basically, I just want to do *output_ptr= (gchar *)input_ptr; but with Google tests actions but I don't manage to make it work ...


Solution

  • Finally I solved the issue by using Invoke(f).
    So, instead of :

    const gchar* nptr;
    EXPECT_CALL(mock_gstring, g_ascii_strtoll(_,_,_)).WillOnce( DoAll(SaveArg<0>(&nptr), SetArgReferee<1>((char)*nptr), SetErrnoAndReturn(0,0))  );
    

    I did this :

    void ut_pointer_cpy(const gchar *nptr, gchar **endptr, guint base){
        *endptr= (gchar *)nptr;
    }
    EXPECT_CALL(mock_gstring, g_ascii_strtoll(_,_,_))
                .WillOnce( DoAll(Invoke(ut_pointer_cpy), SetErrnoAndReturn(0,0))  );
    

    The function you call in Invoke must have the same parameters as the one you mock, or use Unused keywork instead (I didn't test this option).