diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index 708d806..8be4135 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -50,7 +50,7 @@ class UnityTestRunnerGenerator create_externs(output, tests, used_mocks) create_mock_management(output, used_mocks) create_suite_setup_and_teardown(output) - create_reset(output, used_mocks) + create_reset(output, used_mocks) create_main(output, input_file, tests) end @@ -62,6 +62,7 @@ class UnityTestRunnerGenerator def find_tests(input_file) tests_raw = [] + tests_args = [] tests_and_line_numbers = [] input_file.rewind @@ -72,23 +73,29 @@ class UnityTestRunnerGenerator | (;|\{|\}) /x) # Match ;, {, and } as end of lines lines.each_with_index do |line, index| - if line =~ /^\s*void\s+test(.*?)\s*\(\s*void\s*\)/ - tests_raw << ("test" + $1) + #find tests + if line =~ /^((?:\s*TEST_CASE\s*\(.*?\)\s*)*)\s*void\s+(test.*?)\s*\(\s*(.*)\s*\)/ + name = $2 + call = $3 + args = (@options[:use_param_tests] and $1) ? ($1.gsub(/\s*TEST_CASE\s*\(\s*/,'').strip.split(/\s*\)/).compact) : nil + tests_and_line_numbers << { :name => name, :args => args, :call => call, :line_number => 0 } + tests_args = [] end end + #determine line numbers and create tests to run source_lines = source_raw.split("\n") source_index = 0; - - tests_raw.each do |test| + tests_and_line_numbers.size.times do |i| source_lines[source_index..-1].each_with_index do |line, index| - if (line =~ /#{test}/) + if (line =~ /#{tests_and_line_numbers[i][:name]}/) source_index += index - tests_and_line_numbers << {:name => test, :line_number => (source_index+1)} + tests_and_line_numbers[i][:line_number] = source_index + 1 break end end end + return tests_and_line_numbers end @@ -138,7 +145,7 @@ class UnityTestRunnerGenerator output.puts("extern void setUp(void);") output.puts("extern void tearDown(void);") tests.each do |test| - output.puts("extern void #{test[:name]}(void);") + output.puts("extern void #{test[:name]}(#{test[:call]});") end output.puts('') end @@ -240,7 +247,11 @@ class UnityTestRunnerGenerator output.puts(" Unity.TestFile = \"#{filename}\";") output.puts(" UnityBegin();") tests.each do |test| - output.puts(" RUN_TEST(#{test[:name]}, #{test[:line_number]});") + if ((test[:args].nil?) or (test[:args].empty?)) + output.puts(" RUN_TEST(#{test[:name]}, #{test[:line_number]});") + else + test[:args].each {|args| output.puts(" RUN_TEST(#{test[:name]}, #{test[:line_number]}, #{args});")} + end end output.puts() output.puts(" return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());") @@ -267,7 +278,7 @@ if ($0 == __FILE__) #make sure there is at least one parameter left (the input file) if !ARGV[0] puts ["usage: ruby #{__FILE__} (yaml) (options) input_test_file output_test_runner (includes)", - " blah.yml - will use config options in the yml file (see CMock docs)", + " blah.yml - will use config options in the yml file (see docs)", " -cexception - include cexception support"].join("\n") exit 1 end diff --git a/docs/Unity Summary.odt b/docs/Unity Summary.odt index 1c43695..69ddaa8 100644 Binary files a/docs/Unity Summary.odt and b/docs/Unity Summary.odt differ diff --git a/docs/Unity Summary.pdf b/docs/Unity Summary.pdf index 1e289bb..ad69d46 100644 Binary files a/docs/Unity Summary.pdf and b/docs/Unity Summary.pdf differ diff --git a/rakefile_helper.rb b/rakefile_helper.rb index f24f4dd..f868039 100644 --- a/rakefile_helper.rb +++ b/rakefile_helper.rb @@ -204,7 +204,9 @@ module RakefileHelpers runner_path = $cfg['compiler']['runner_path'] + runner_name end - UnityTestRunnerGenerator.new($cfg_file).run(test, runner_path) + options = $cfg[:unity] + options[:use_param_tests] = (test =~ /parameterized/) ? true : false + UnityTestRunnerGenerator.new(options).run(test, runner_path) compile(runner_path, test_defines) obj_list << runner_name.ext($cfg['compiler']['object_files']['extension']) diff --git a/src/unity.h b/src/unity.h index da6dd99..902edd6 100644 --- a/src/unity.h +++ b/src/unity.h @@ -47,6 +47,8 @@ #define TEST_LINE_NUM (Unity.CurrentTestLineNumber) #define TEST_IS_IGNORED (Unity.CurrentTestIgnored) +#define TEST_CASE(...) + //------------------------------------------------------- // Basic Fail and Ignore //------------------------------------------------------- diff --git a/test/testparameterized.c b/test/testparameterized.c new file mode 100644 index 0000000..a267e9f --- /dev/null +++ b/test/testparameterized.c @@ -0,0 +1,99 @@ +/* ========================================== + Unity Project - A Test Framework for C + Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + [Released under MIT License. Please refer to license.txt for details] +========================================== */ + +#include +#include "unity.h" + +#define EXPECT_ABORT_BEGIN \ + if (TEST_PROTECT()) \ + { + +#define VERIFY_FAILS_END \ + } \ + Unity.CurrentTestFailed = (Unity.CurrentTestFailed == 1) ? 0 : 1; \ + if (Unity.CurrentTestFailed == 1) { \ + SetToOneMeanWeAlreadyCheckedThisGuy = 1; \ + UnityPrint("[[[[ Previous Test Should Have Failed But Did Not ]]]]"); \ + UNITY_OUTPUT_CHAR('\n'); \ + } + +#define VERIFY_IGNORES_END \ + } \ + Unity.CurrentTestFailed = (Unity.CurrentTestIgnored == 1) ? 0 : 1; \ + Unity.CurrentTestIgnored = 0; \ + if (Unity.CurrentTestFailed == 1) { \ + SetToOneMeanWeAlreadyCheckedThisGuy = 1; \ + UnityPrint("[[[[ Previous Test Should Have Ignored But Did Not ]]]]"); \ + UNITY_OUTPUT_CHAR('\n'); \ + } + +int SetToOneToFailInTearDown; +int SetToOneMeanWeAlreadyCheckedThisGuy; + +void setUp(void) +{ + SetToOneToFailInTearDown = 0; + SetToOneMeanWeAlreadyCheckedThisGuy = 0; +} + +void tearDown(void) +{ + if (SetToOneToFailInTearDown == 1) + TEST_FAIL_MESSAGE("<= Failed in tearDown"); + if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0)) + { + UnityPrint("[[[[ Previous Test Should Have Passed But Did Not ]]]]"); + UNITY_OUTPUT_CHAR('\n'); + } +} + +TEST_CASE(0) +TEST_CASE(44) +TEST_CASE(99) +void test_TheseShouldAllPass(int Num) +{ + TEST_ASSERT_TRUE(Num < 100); +} + +TEST_CASE(3) +TEST_CASE(77) +TEST_CASE(99) +void test_TheseShouldAllFail(int Num) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_TRUE(Num > 100); + VERIFY_FAILS_END +} + +TEST_CASE(1) +TEST_CASE(44) +TEST_CASE(99) +TEST_CASE(98) +void test_TheseAreEveryOther(int Num) +{ + if (Num & 1) + { + EXPECT_ABORT_BEGIN + TEST_ASSERT_TRUE(Num > 100); + VERIFY_FAILS_END + } + else + { + TEST_ASSERT_TRUE(Num < 100); + } +} + +void test_NormalPassesStillWork(void) +{ + TEST_ASSERT_TRUE(1); +} + +void test_NormalFailsStillWork(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_TRUE(0); + VERIFY_FAILS_END +}