cpputest / cpputest

CppUTest unit testing and mocking framework for C/C++
http://cpputest.github.io
BSD 3-Clause "New" or "Revised" License
1.34k stars 504 forks source link

Wrong error message if mock is used #1775

Open f-zl opened 3 months ago

f-zl commented 3 months ago
// code_under_test.c
#include "code_under_test.h" // declares `int function_under_test(int x, int y);`
#include "code_dependency.h" // declares `int dependency_function(int);`
int function_under_test(int x, int y) { return dependency_function(x) + y; }
// test.cpp
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
extern "C" {
#include "code_dependency.h"
#include "code_under_test.h"
}
TEST_GROUP(OneTestGroup){
  void teardown() {
    mock().clear();
  }
};
// mock function
int dependency_function(int arg) {
  return mock()
      .actualCall("dependency_function")
      .withParameter("arg", arg)
      .returnIntValueOrDefault(arg);
}
TEST(OneTestGroup, OneTest) {
  mock().expectOneCall("dependency_function").withParameter("arg", 2);
  // the error message is Mock Failure: Unexpected additional (2nd) call to function: dependency_function
  // the error message should be CHECK_EQUAL error, expect=6, actual=5
  CHECK_EQUAL(6, function_under_test(2, 3));
  mock().checkExpectations();
}

#include "CppUTest/CommandLineTestRunner.h"
int main(int ac, char **av) {
  return CommandLineTestRunner::RunAllTests(ac, av);
}

The test OneTest calls dependency_function only once by calling function_under_test. The expect value and the actual value is different. The error message is Mock Failure: Unexpected additional (2nd) call to function: dependency_function. It should be CHECK_EQUAL error, expect=6, actual=5.

Maybe the test macro should evaluate the argument only once to avoid repeating the side effect of the argument function. For example:

#define CHECK_EQUAL_LOCATION(expected, actual, text, file, line)\
  decltype(expected) expectedValue = expected;\
  decltype(actual) actualValue = actual;\
  do { if ((expectedValue) != (actualValue)) { \
  ...
fgrie commented 2 months ago

The current solution (which unfortunately doesn't cover your problem) was added in https://github.com/cpputest/cpputest/commit/97576dc71dae6ee70fca30721ed226963f39bc63.

I would guess that the reasoning behind this implementation was the fact that decltype is not supported in language standards prior C++11. Maybe @basvodde knows more on the background of this decision back then.

In fact, I'd say, there's one central question: does CppUTest want to stay compatible with older C++-standards (if it's still compatible with older standards, at all), or does it address modern C++? I don't know if there is any statement, which language standard is officially supported/targeted?

Edit: I quickly checked it, CppUTest is still C++98-compatible (tested using w64devkit 1.17.0, which includes GCC )