提交 780c17d0 编写于 作者: M mvandervoord

- updated unity test runner generator to support parameterized tests optionally.

- updated docs to better discuss generator options.

git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@104 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e
上级 f2ce354f
...@@ -50,7 +50,7 @@ class UnityTestRunnerGenerator ...@@ -50,7 +50,7 @@ class UnityTestRunnerGenerator
create_externs(output, tests, used_mocks) create_externs(output, tests, used_mocks)
create_mock_management(output, used_mocks) create_mock_management(output, used_mocks)
create_suite_setup_and_teardown(output) create_suite_setup_and_teardown(output)
create_reset(output, used_mocks) create_reset(output, used_mocks)
create_main(output, input_file, tests) create_main(output, input_file, tests)
end end
...@@ -62,6 +62,7 @@ class UnityTestRunnerGenerator ...@@ -62,6 +62,7 @@ class UnityTestRunnerGenerator
def find_tests(input_file) def find_tests(input_file)
tests_raw = [] tests_raw = []
tests_args = []
tests_and_line_numbers = [] tests_and_line_numbers = []
input_file.rewind input_file.rewind
...@@ -72,23 +73,29 @@ class UnityTestRunnerGenerator ...@@ -72,23 +73,29 @@ class UnityTestRunnerGenerator
| (;|\{|\}) /x) # Match ;, {, and } as end of lines | (;|\{|\}) /x) # Match ;, {, and } as end of lines
lines.each_with_index do |line, index| lines.each_with_index do |line, index|
if line =~ /^\s*void\s+test(.*?)\s*\(\s*void\s*\)/ #find tests
tests_raw << ("test" + $1) 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
end end
#determine line numbers and create tests to run
source_lines = source_raw.split("\n") source_lines = source_raw.split("\n")
source_index = 0; source_index = 0;
tests_and_line_numbers.size.times do |i|
tests_raw.each do |test|
source_lines[source_index..-1].each_with_index do |line, index| 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 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 break
end end
end end
end end
return tests_and_line_numbers return tests_and_line_numbers
end end
...@@ -138,7 +145,7 @@ class UnityTestRunnerGenerator ...@@ -138,7 +145,7 @@ class UnityTestRunnerGenerator
output.puts("extern void setUp(void);") output.puts("extern void setUp(void);")
output.puts("extern void tearDown(void);") output.puts("extern void tearDown(void);")
tests.each do |test| tests.each do |test|
output.puts("extern void #{test[:name]}(void);") output.puts("extern void #{test[:name]}(#{test[:call]});")
end end
output.puts('') output.puts('')
end end
...@@ -240,7 +247,11 @@ class UnityTestRunnerGenerator ...@@ -240,7 +247,11 @@ class UnityTestRunnerGenerator
output.puts(" Unity.TestFile = \"#{filename}\";") output.puts(" Unity.TestFile = \"#{filename}\";")
output.puts(" UnityBegin();") output.puts(" UnityBegin();")
tests.each do |test| 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 end
output.puts() output.puts()
output.puts(" return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());") output.puts(" return #{@options[:suite_teardown].nil? ? "" : "suite_teardown"}(UnityEnd());")
...@@ -267,7 +278,7 @@ if ($0 == __FILE__) ...@@ -267,7 +278,7 @@ if ($0 == __FILE__)
#make sure there is at least one parameter left (the input file) #make sure there is at least one parameter left (the input file)
if !ARGV[0] if !ARGV[0]
puts ["usage: ruby #{__FILE__} (yaml) (options) input_test_file output_test_runner (includes)", 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") " -cexception - include cexception support"].join("\n")
exit 1 exit 1
end end
......
此差异由.gitattributes 抑制。
此差异由.gitattributes 抑制。
...@@ -204,7 +204,9 @@ module RakefileHelpers ...@@ -204,7 +204,9 @@ module RakefileHelpers
runner_path = $cfg['compiler']['runner_path'] + runner_name runner_path = $cfg['compiler']['runner_path'] + runner_name
end 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) compile(runner_path, test_defines)
obj_list << runner_name.ext($cfg['compiler']['object_files']['extension']) obj_list << runner_name.ext($cfg['compiler']['object_files']['extension'])
......
...@@ -47,6 +47,8 @@ ...@@ -47,6 +47,8 @@
#define TEST_LINE_NUM (Unity.CurrentTestLineNumber) #define TEST_LINE_NUM (Unity.CurrentTestLineNumber)
#define TEST_IS_IGNORED (Unity.CurrentTestIgnored) #define TEST_IS_IGNORED (Unity.CurrentTestIgnored)
#define TEST_CASE(...)
//------------------------------------------------------- //-------------------------------------------------------
// Basic Fail and Ignore // Basic Fail and Ignore
//------------------------------------------------------- //-------------------------------------------------------
......
/* ==========================================
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 <setjmp.h>
#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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册