chyh1990 / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 2 forks source link

problem and solution for customization of EXPECT_EQ #399

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
on gcc 4.4.x it was not possible to extend EXPECT_EQ to a vector class by 
overloading CmpHelperEQ because the compiler issued errors. Test for equality 
for the vector class looks like

if (all(expected == actual))
return AssertionSuccess();

Therefore I changed CmpHelperEQ to partial specialization:

template <typename T1, typename T2>
struct CmpHelperEQ
{
    AssertionResult operator()(const char* expected_expression,
                                const char* actual_expression,
                                const T1& expected,
                                const T2& actual) {
    #ifdef _MSC_VER
    # pragma warning(push)          // Saves the current warning state.
    # pragma warning(disable:4389)  // Temporarily disables warning on
                                   // signed/unsigned mismatch.
    #endif

      if (expected == actual) {
        return AssertionSuccess();
      }

    #ifdef _MSC_VER
    # pragma warning(pop)          // Restores the warning state.
    #endif

      return EqFailure(expected_expression,
                       actual_expression,
                       FormatForComparisonFailureMessage(expected, actual),
                       FormatForComparisonFailureMessage(actual, expected),
                       false);
    }
};

return CmpHelperEQ(...) changes to CmpHelperEQ<T1, T2>()(...)

The CmpHelperEQ for BiggestInt I renamed to CmpHelperEQBiggestInt.

This way it worked to write a specialization for the vector class:

template <typename T1, typename T2>
struct CmpHelperEQ<Vector2<T1>, Vector2<T2> >
{
...
};

-Jochen

Original issue reported on code.google.com by jochen.w...@googlemail.com on 23 Jan 2012 at 5:37

GoogleCodeExporter commented 9 years ago
CmpHelperEQ is an internal construct and overloading it for extending 
functionality EXPECT_EQ is not supported.

vector supports operator == and as such should be comparable using EXPECT_EQ. 
If you want correct printouts, define PrintTo for types of the values you 
compare, as described in 
http://code.google.com/p/googletest/wiki/AdvancedGuide#Teaching_Google_Test_How_
to_Print_Your_Values. If you need more customized output, consider using custom 
assertion predicates 
(http://code.google.com/p/googletest/wiki/AdvancedGuide#Predicate_Assertions_for
_Better_Error_Messages) or EXPECT_THAT() from Google Mock.

Original comment by vladlosev on 9 Feb 2012 at 8:42

GoogleCodeExporter commented 9 years ago
the printing is not the problem as i have overloaded the operator <<. but my 
vector (Vector2<>) returns a Vector2<bool> for operator ==, but seems that this 
can be done with EXPECT_THAT(). I also need a "epsilon equals" which also seems 
possible with EXPECT_THAT(), so i'll look at this.
why is EXPECT_THAT in google mock and not in google test?

Original comment by jochen.w...@googlemail.com on 9 Feb 2012 at 8:58