From 2593c31bb7e69219deaffd360d90def3711bce8f Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Wed, 13 Sep 2017 17:59:52 -0400 Subject: [PATCH] Allow suiteSetUp() and suiteTearDown() to be provided as normal C functions. This is simpler and more flexible than embedding C code in the Ruby options (:suite_setup and :suite_teardown). However, support for :suite_setup and :suite_teardown is kept for backwards compatibility. Several configurations are possible: 1. :suite_setup and :suite_teardown options provided and used. 2. :suite_setup and :suite_teardown options not provided (nil): 2a. Weak symbols not supported; suiteSetUp() and suiteTearDown() are not called. It would be simpler to make user-provided functions mandatory in this case, but it could break some pre-existing test suites. 2b. Weak symbols are supported and the stub implementations of suiteSetUp() and suiteTearDown() are called if there are no user-provided functions. 2c. Weak symbols are supported but overridden by user-provided suiteSetUp() and suiteTearDown() functions. --- auto/generate_test_runner.rb | 30 ++++++++++++++++++++++-------- src/unity.c | 6 ++++++ src/unity.h | 11 +++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index 07bde81..edb7989 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -235,22 +235,36 @@ class UnityTestRunnerGenerator end def create_suite_setup(output) - return if @options[:suite_setup].nil? - output.puts("\n/*=======Suite Setup=====*/") output.puts('static void suite_setup(void)') output.puts('{') - output.puts(@options[:suite_setup]) + if @options[:suite_setup].nil? + # New style, call suiteSetUp() if we can use weak symbols + output.puts('#if defined(UNITY_WEAK_ATTRIBUTE) || defined(UNITY_WEAK_PRAGMA)') + output.puts(' suiteSetUp();') + output.puts('#endif') + else + # Old style, C code embedded in the :suite_setup option + output.puts(@options[:suite_setup]) + end output.puts('}') end def create_suite_teardown(output) - return if @options[:suite_teardown].nil? - output.puts("\n/*=======Suite Teardown=====*/") output.puts('static int suite_teardown(int num_failures)') output.puts('{') - output.puts(@options[:suite_teardown]) + if @options[:suite_teardown].nil? + # New style, call suiteTearDown() if we can use weak symbols + output.puts('#if defined(UNITY_WEAK_ATTRIBUTE) || defined(UNITY_WEAK_PRAGMA)') + output.puts(' return suiteTearDown(num_failures);') + output.puts('#else') + output.puts(' return num_failures;') + output.puts('#endif') + else + # Old style, C code embedded in the :suite_teardown option + output.puts(@options[:suite_teardown]) + end output.puts('}') end @@ -342,7 +356,7 @@ class UnityTestRunnerGenerator output.puts("int #{main_name}(void)") output.puts('{') end - output.puts(' suite_setup();') unless @options[:suite_setup].nil? + output.puts(' suite_setup();') output.puts(" UnityBegin(\"#{filename.gsub(/\\/, '\\\\\\')}\");") if @options[:use_param_tests] tests.each do |test| @@ -357,7 +371,7 @@ class UnityTestRunnerGenerator end output.puts output.puts(' CMock_Guts_MemFreeFinal();') unless used_mocks.empty? - output.puts(" return #{@options[:suite_teardown].nil? ? '' : 'suite_teardown'}(UnityEnd());") + output.puts(" return suite_teardown(UnityEnd());") output.puts('}') end diff --git a/src/unity.c b/src/unity.c index 9783efa..08ecf63 100644 --- a/src/unity.c +++ b/src/unity.c @@ -1314,11 +1314,17 @@ void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line) #if defined(UNITY_WEAK_ATTRIBUTE) UNITY_WEAK_ATTRIBUTE void setUp(void) { } UNITY_WEAK_ATTRIBUTE void tearDown(void) { } + UNITY_WEAK_ATTRIBUTE void suiteSetUp(void) { } + UNITY_WEAK_ATTRIBUTE int suiteTearDown(int num_failures) { return num_failures; } #elif defined(UNITY_WEAK_PRAGMA) #pragma weak setUp void setUp(void) { } #pragma weak tearDown void tearDown(void) { } + #pragma weak suiteSetUp + void suiteSetUp(void) { } + #pragma weak suiteTearDown + int suiteTearDown(int num_failures) { return num_failures; } #endif /*-----------------------------------------------*/ diff --git a/src/unity.h b/src/unity.h index 258e21c..1fa8b94 100644 --- a/src/unity.h +++ b/src/unity.h @@ -15,9 +15,20 @@ extern "C" #include "unity_internals.h" +/* These functions are intended to be called before and after each test. Unity + * provides stub implementations annotated as weak symbols (if supported by the + * compiler). */ void setUp(void); void tearDown(void); +/* These functions are intended to be called at the beginning and end of an + * entire test suite. suiteTearDown() is passed the number of tests that + * failed, and its return value becomes the exit code of main(). Unity + * provides stub implementations annotated as weak symbols (if supported by the + * compiler). */ +void suiteSetUp(void); +int suiteTearDown(int num_failures); + /*------------------------------------------------------- * Configuration Options *-------------------------------------------------------