Add TEST_RANGE to specify arg ranges in parameterized tests

TEST_RANGE([start, stop, step]) generates following runs of the test
function: test(start), test(start + step), ..., test(start + n * step),
where start + n * step <= stop. The step must be positive.

If the test function takes several arguments, the following syntax must be used:
TEST_RANGE([arg1_start, arg1_stop, arg1_step], ..., [argN_start, argN_stop, argN_step])

This addresses issues #53 and #144.

Reported-by: Alex Rodriguez <alejmrm@gmail.com>
Reported-by: Hiroaki Yamazoe <PastelParasol@gmail.com>
This commit is contained in:
Andrei Korigodskii
2020-02-02 22:00:27 +03:00
parent d0714178a8
commit 218fa2cbe8

View File

@ -128,7 +128,7 @@ class UnityTestRunnerGenerator
lines.each_with_index do |line, _index|
# find tests
next unless line =~ /^((?:\s*TEST_CASE\s*\(.*?\)\s*)*)\s*void\s+((?:#{@options[:test_prefix]}).*)\s*\(\s*(.*)\s*\)/m
next unless line =~ /^((?:\s*(?:TEST_CASE|TEST_RANGE)\s*\(.*?\)\s*)*)\s*void\s+((?:#{@options[:test_prefix]}).*)\s*\(\s*(.*)\s*\)/m
arguments = Regexp.last_match(1)
name = Regexp.last_match(2)
@ -139,6 +139,21 @@ class UnityTestRunnerGenerator
if @options[:use_param_tests] && !arguments.empty?
args = []
arguments.scan(/\s*TEST_CASE\s*\((.*)\)\s*$/) { |a| args << a[0] }
arguments.scan(/\s*TEST_RANGE\s*\((.*)\)\s*$/).flatten.each do |range_str|
args += range_str.scan(/\[(-?\d+.?\d*), *(-?\d+.?\d*), *(-?\d+.?\d*)\]/)
.map { |arg_values_str|
arg_values_str.map { |arg_value_str|
(arg_value_str.include? ".") ? arg_value_str.to_f : arg_value_str.to_i
}
}.map { |arg_values|
(arg_values[0]..arg_values[1]).step(arg_values[2]).to_a
}.reduce { |result, arg_range_expanded|
result.product(arg_range_expanded)
}.map { |arg_combinations|
arg_combinations.flatten.join(", ")
}
end
end
tests_and_line_numbers << { test: name, args: args, call: call, params: params, line_number: 0 }