Ok, so I'm using gtest for unit testing, and I've got something I want to do:

class A {
    /* Private members */
public:
    bool function_to_test(int index);
}

In the test function, I'd like to use:

A testEntity;
const int b = 40;
ASSERT_PRED1(testEntity.function_to_test, b);

This doesn't work as ASSERT_PREDx seems to be designed for global scope functions. I get a message on the lines of

argument of type ‘bool (A::)(int) {aka bool (A::)(int)}’ does not match ‘bool (A::*)(int)’

I was wondering if there was a good work around for this? I can always use a function with a global variable, but I wasn't sure if there was a one-line way around it.

有帮助吗?

解决方案

The first argument to ASSERT_PRED1(pred1, val1); should be a callable object; a unary-function or functor.

For example, if you can use C++11 lambdas, you could do:

ASSERT_PRED1([&testEntity](int i) { return testEntity.function_to_test(i); }, b);

Or if you want to use a unary function helper:

struct TesterA : public std::unary_function <int, bool> {
  explicit TesterA(A& a) : a_(a) {}
  bool operator()(int i) { return a_.function_to_test(i); }
  A& a_;
};

ASSERT_PRED1(TesterA(testEntity), b);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top