Skip to content

Keep braces in TEST_CASE arguments when finding tests - #853

Open
r3wretrhy wants to merge 1 commit into
ThrowTheSwitch:masterfrom
r3wretrhy:fix/test-case-compound-literal-752
Open

r3wretrhy wants to merge 1 commit into
ThrowTheSwitch:masterfrom
r3wretrhy:fix/test-case-compound-literal-752

Conversation

@r3wretrhy

Copy link
Copy Markdown

🍍

Problem

find_tests splits the scrubbed source into logical lines at every ;, { and }. Braces inside string and character literals are hidden before the split, but braces in the TEST_CASE argument list itself are not. A compound literal therefore ends the logical line in the middle of the TEST_CASE:

TEST_CASE((uint8_t[]){0x1, 0x2, 0x3}, 3)
void test_with_array(const uint8_t * data, unsigned len)

The test is found without its arguments, so the runner registers it as a plain void (void) test:

run_test_params_arr[0].func = test_with_array;

GCC 14 rejects that with -Wincompatible-pointer-types. With a compiler that only warns, the test is called without its arguments.

Fix

Hide ;, {, } and / inside the parentheses of TEST_CASE/TEST_RANGE/TEST_MATRIX with the same markers that are already used for literals, so they are restored after the split. The parenthesised list is matched with a recursive group, so nested parentheses such as (uint8_t[]) are kept.

The example above now generates runner_args1_test_with_array() calling test_with_array((uint8_t[]){0x1, 0x2, 0x3}, 3);.

Fixes #752

Verification

  • New FindTestsKeepsBracesInTestCaseArguments in test_generate_test_runner.rb: fails on master (args is nil), passes with the change.
  • New test_CompoundLiteralArgsArePreserved in test_unity_parameterized.c: on master, rake test:unit stops with the incompatible-pointer-types error above (GCC 14.2); with the change both cases run and pass.
  • rake test:scripts, test:unit, test:fixture, test:memory, test:summary and test:make pass on Linux (Ruby 3.3, GCC 14.2). test:style was not run because rubocop and astyle are not installed here.

find_tests splits the scrubbed source into logical lines at every ;, {
and }. Braces inside string and character literals are already hidden
first, but braces in the argument list itself are not, so a compound
literal such as

    TEST_CASE((uint8_t[]){0x1, 0x2}, 2)
    void test_with_array(const uint8_t * data, unsigned len)

ends the logical line inside the TEST_CASE. The test is then found
without its arguments and the runner calls it as a plain void(void)
test, which modern GCC rejects with -Wincompatible-pointer-types.

Hide ;, {, } and / inside TEST_CASE/TEST_RANGE/TEST_MATRIX parentheses
the same way they are hidden inside literals, and restore them after
the split. Add a find_tests regression and a compound literal case to
test_unity_parameterized.c.

Fixes ThrowTheSwitch#752
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.

TEST_CASE with arrays as arguments (generate_test_runner)

1 participant