GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added support for noreturn functions. by informatimago · Pull Request #674 · ThrowTheSwitch/Unity

Conversation

@informatimago

@informatimago informatimago commented May 16, 2023

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@informatimago
Ignore backup files *~.
6eaf693
@informatimago
informatimago force-pushed the noreturn branch from 6b180d6 to 60b64fb Compare November 16, 2023 11:06
@informatimago
Added macros to support noreturn functions
60b64fb
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

mvandervoord commented Jul 3, 2026

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@informatimago @mvandervoord
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.