0
0
mirror of https://github.com/google/googletest.git synced 2025-03-20 19:03:48 +00:00
Zhanyong Wan 0bdccf4aa2 Add a DistanceFrom() matcher for general distance comparison.
We have a bunch of matchers for asserting that a value is near the target value, e.g.
`DoubleNear()` and `FloatNear()`. These matchers only work for specific types (`double` and `float`). They are not flexible enough to support other types that have the notion of a "distance" (e.g. N-dimensional points and vectors, which are commonly used in ML).

In this diff, we generalize the idea to a `DistanceFrom(target, get_distance, m)` matcher that works on arbitrary types that have the "distance" concept (the `get_distance` argument is optional and can be omitted for types that support `-`, and `std::abs()`). What it does:

1. compute the distance between the value and the target using `get_distance(value, target)`; if `get_distance` is omitted, compute the distance as `std::abs(value - target)`.
2. match the distance against matcher `m`; if the match succeeds, the `DistanceFrom()` match succeeds.

Examples:

```
  // 0.5's distance from 0.6 should be <= 0.2.
  EXPECT_THAT(0.5, DistanceFrom(0.6, Le(0.2)));

  Vector2D v1(3.0, 4.0), v2(3.2, 6.0);
  // v1's distance from v2, as computed by EuclideanDistance(v1, v2),
  // should be >= 1.0.
  EXPECT_THAT(v1, DistanceFrom(v2, EuclideanDistance, Ge(1.0)));
```

PiperOrigin-RevId: 734593292
Change-Id: Id6bb7074dc4aa4d8abd78b57ad2426637e590de5
2025-03-07 09:54:00 -08:00
..
2021-01-13 20:59:12 -05:00
2024-07-23 03:57:23 -07:00

Googletest Mocking (gMock) Framework

Overview

Google's framework for writing and using C++ mock classes. It can help you derive better designs of your system and write better tests.

It is inspired by:

It is designed with C++'s specifics in mind.

gMock:

  • Provides a declarative syntax for defining mocks.
  • Can define partial (hybrid) mocks, which are a cross of real and mock objects.
  • Handles functions of arbitrary types and overloaded functions.
  • Comes with a rich set of matchers for validating function arguments.
  • Uses an intuitive syntax for controlling the behavior of a mock.
  • Does automatic verification of expectations (no record-and-replay needed).
  • Allows arbitrary (partial) ordering constraints on function calls to be expressed.
  • Lets a user extend it by defining new matchers and actions.
  • Does not use exceptions.
  • Is easy to learn and use.

Details and examples can be found here:

GoogleMock is a part of GoogleTest C++ testing framework and a subject to the same requirements.