Skip to content

Added support for noreturn functions.#674

Open
informatimago wants to merge 2 commits into
ThrowTheSwitch:masterfrom
informatimago:noreturn
Open

Added support for noreturn functions.#674
informatimago wants to merge 2 commits into
ThrowTheSwitch:masterfrom
informatimago:noreturn

Conversation

@informatimago

Copy link
Copy Markdown

This patch adds macros to support noreturn mocked functions:

Added TEST_PROTECT_NORETURN(), TEST_DO_NOT_RETURN(), and TEST_NOT_RETURNING(call) to test for non-returning functions.
Mock functions that are declared noreturn shall use TEST_DO_NOT_RETURN() instead of return.

More details in the following CMock PR.

Added TEST_PROTECT_NORETURN(), TEST_DO_NOT_RETURN(), and TEST_NOT_RETURNING(call) to test for non-returning functions.
Mock functions that are declared noreturn shall use TEST_DO_NOT_RETURN() instead of return.
@mvandervoord

Copy link
Copy Markdown
Member

I'm not clear on what this PR is doing that can't be handled already with the existing macros.

Am I understanding correctly that the use-case is that you want to mock a function which is supposed to never return. If the mock is called properly, the test should immediately end with a pass. If we continue past that line (returning back to the main source, for example) it should fail. yes?

// Code under test
void run_me(int i) {
  if (i== 0) {
    never_returns();
  }
  
  report_error(i);
}
// Tests
int never_return_calls_expected= 0;

void never_returns_callback(int num) {
  (void)num; //ignore it. we're not using it.

  if (never_return_calls_expected > 0) {
    never_return_calls_expected--;
    TEST_PASS("callback called");
  }
}

void setUp() {
  never_returns_Stub(never_returns_callback);
  never_return_calls_expected= 0;
}

void tearDown() {
  TEST_ASSERT_EQUAL(0, never_return_calls_expected, "We were expecting calls to never return that didn't happen");
}

void test_run_me_SHOULD_report_error_if_one_is_given(void) {
  report_error_Expect(42);

  run_me(42);
}

void test_run_me_SHOULD_start_our_infinite_loop_if_no_errors(void) {
  never_return_calls_expected=1;
  run_me(0);
}

It's completely testable using the current features, I believe. I think this pattern is very useful, though. In fact, I think we should add a CMock plugin for accomplishing this same task?

Am I understanding the goal correctly? Missing anything?

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

Successfully merging this pull request may close these issues.

2 participants