Skip to content

Commit 2b80d1a

Browse files
authored
Merge pull request #851 from joniumGit/fix/find-tests-line-search
Fix find_tests line-number search for test names at the start of a line
2 parents 0012495 + b5ebbd7 commit 2b80d1a

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎auto/generate_test_runner.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,10 @@ def find_tests(source)
200200
source_lines = source.split("\n")
201201
source_index = 0
202202
tests_and_line_numbers.size.times do |i|
203+
# Compile once per test
204+
name_regex = /(?:^|\s)#{tests_and_line_numbers[i][:test]}(?:\s|\()/
203205
source_lines[source_index..].each_with_index do |line, index|
204-
next unless line =~ /\s+#{tests_and_line_numbers[i][:test]}(?:\s|\()/
206+
next unless line =~ name_regex
205207

206208
source_index += index
207209
tests_and_line_numbers[i][:line_number] = source_index + 1

‎test/tests/test_generate_test_runner.rb‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,55 @@ def verify_number(expected, expression, output)
13341334
$generate_test_runner_tests += 1
13351335
end
13361336

1337+
should 'FindTestsLineNumbersWhenNameStartsTheLine' do
1338+
# The return type may sit on its own line, leaving the test name at column 0.
1339+
# The line search must still find the definition (and must not scan to the end
1340+
# of the file for every test, which is what made large files slow).
1341+
source = "#include \"unity.h\"\n" \
1342+
"\n" \
1343+
"void\n" \
1344+
"test_FirstAtColumnZero(void)\n" \
1345+
"{\n" \
1346+
"}\n" \
1347+
"\n" \
1348+
"void\n" \
1349+
"test_SecondAtColumnZero(void)\n" \
1350+
"{\n" \
1351+
"}\n"
1352+
found = UnityTestRunnerGenerator.new({}).find_tests(source).map { |t| [t[:test], t[:line_number]] }
1353+
expected = [['test_FirstAtColumnZero', 4], ['test_SecondAtColumnZero', 9]]
1354+
if found == expected
1355+
report 'Runner_FindTestsLineNumbersWhenNameStartsTheLine:PASS'
1356+
else
1357+
report " FAIL: expected #{expected.inspect}, got #{found.inspect}"
1358+
report 'Runner_FindTestsLineNumbersWhenNameStartsTheLine:FAIL'
1359+
$generate_test_runner_failures += 1
1360+
end
1361+
$generate_test_runner_tests += 1
1362+
end
1363+
1364+
should 'FindTestsLineNumbersWhenOneNameIsAPrefixOfAnother' do
1365+
# Issue #288: test_my_function must not be located inside
1366+
# test_my_function_invalid_behavior, which is defined first.
1367+
source = "void test_my_function_invalid_behavior(void)\n" \
1368+
"{\n" \
1369+
"}\n" \
1370+
"\n" \
1371+
"void test_my_function(void)\n" \
1372+
"{\n" \
1373+
"}\n"
1374+
found = UnityTestRunnerGenerator.new({}).find_tests(source).map { |t| [t[:test], t[:line_number]] }
1375+
expected = [['test_my_function_invalid_behavior', 1], ['test_my_function', 5]]
1376+
if found == expected
1377+
report 'Runner_FindTestsLineNumbersWhenOneNameIsAPrefixOfAnother:PASS'
1378+
else
1379+
report " FAIL: expected #{expected.inspect}, got #{found.inspect}"
1380+
report 'Runner_FindTestsLineNumbersWhenOneNameIsAPrefixOfAnother:FAIL'
1381+
$generate_test_runner_failures += 1
1382+
end
1383+
$generate_test_runner_tests += 1
1384+
end
1385+
13371386
RUNNER_TESTS.each do |testset|
13381387
basename = File.basename(testset[:testfile], C_EXTENSION)
13391388
testset_name = "Runner_#{basename}_#{testset[:name]}"

0 commit comments

Comments
 (0)