Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong error message if mock is used #1775

Open
f-zl opened this issue Mar 27, 2024 · 1 comment
Open

Wrong error message if mock is used #1775

f-zl opened this issue Mar 27, 2024 · 1 comment

Comments

@f-zl
Copy link

f-zl commented Mar 27, 2024

// 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
Copy link

fgrie commented May 6, 2024

The current solution (which unfortunately doesn't cover your problem) was added in 97576dc.

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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants