Skip to content

Commit 927eebb

Browse files
committed
Keep braces in TEST_CASE arguments when finding tests
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 #752
1 parent 2b80d1a commit 927eebb

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎auto/generate_test_runner.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def find_tests(source)
139139
source_scrubbed = source_scrubbed.gsub(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '') # remove line comments that comment out the start of blocks
140140
source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments
141141
source_scrubbed = source_scrubbed.gsub(/\/\/.*$/, '') # remove line comments (all that remain)
142+
source_scrubbed = source_scrubbed.gsub(/TEST_(?:CASE|RANGE|MATRIX)\s*(\((?:[^()]|\g<1>)*\))/) { |s| s.gsub(substring_re, substring_subs) } # hide braces of compound literals in parameter lists
142143
lines = source_scrubbed.split(/(^\s*\#.*$) | (;|\{|\}) /x) # Treat preprocessor directives as a logical line. Match ;, {, and } as end of lines
143144
.map { |line| line.gsub(substring_unre, substring_unsubs) } # unhide the problematic characters previously removed
144145

‎docs/UnityChangeLog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Significant Bugfixes:
1919

2020
- Default `UNITY_INCLUDE_EXEC_TIME` macros compile as ISO C99, are statement-safe, and accept `-Wsign-conversion` (#838)
2121
- `unity_test_summary.rb` honors its own default result directory and root path again. @youdie006
22+
- Test runner generator keeps `TEST_CASE` arguments that contain braces, such as compound literals (#752)
2223

2324
### Unity 2.7.0 (July 2026)
2425

‎test/tests/test_generate_test_runner.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,26 @@ def verify_number(expected, expression, output)
13831383
$generate_test_runner_tests += 1
13841384
end
13851385

1386+
should 'FindTestsKeepsBracesInTestCaseArguments' do
1387+
# Issue #752: a compound literal such as (uint8_t[]){1, 2} contains braces,
1388+
# which must not end the logical line before the test function is found.
1389+
source = "TEST_CASE((uint8_t[]){0x1, 0x2, 0x3}, 3)\n" \
1390+
"TEST_CASE((uint8_t[]){0x4}, 1)\n" \
1391+
"void test_WithArray(const uint8_t * data, unsigned len)\n" \
1392+
"{\n" \
1393+
"}\n"
1394+
found = UnityTestRunnerGenerator.new(use_param_tests: true).find_tests(source).map { |t| [t[:test], t[:args]] }
1395+
expected = [['test_WithArray', ['(uint8_t[]){0x1, 0x2, 0x3}, 3', '(uint8_t[]){0x4}, 1']]]
1396+
if found == expected
1397+
report 'Runner_FindTestsKeepsBracesInTestCaseArguments:PASS'
1398+
else
1399+
report " FAIL: expected #{expected.inspect}, got #{found.inspect}"
1400+
report 'Runner_FindTestsKeepsBracesInTestCaseArguments:FAIL'
1401+
$generate_test_runner_failures += 1
1402+
end
1403+
$generate_test_runner_tests += 1
1404+
end
1405+
13861406
RUNNER_TESTS.each do |testset|
13871407
basename = File.basename(testset[:testfile], C_EXTENSION)
13881408
testset_name = "Runner_#{basename}_#{testset[:name]}"

‎test/tests/test_unity_parameterized.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ void test_CharsArePreserved(unsigned index, char c)
171171
NextExpectedCharIndex++;
172172
}
173173

174+
TEST_CASE((const int[]){1, 2, 3}, 3, 6)
175+
TEST_CASE((const int[]){4}, 1, 4)
176+
void test_CompoundLiteralArgsArePreserved(const int * values, unsigned count, int sum)
177+
{
178+
int total = 0;
179+
unsigned i;
180+
181+
for (i = 0; i < count; i++)
182+
{
183+
total += values[i];
184+
}
185+
186+
TEST_ASSERT_EQUAL_INT(sum, total);
187+
}
188+
174189
TEST_RANGE([0, 10, 2])
175190
void test_SingleRange(unsigned value)
176191
{

0 commit comments

Comments
 (0)