提交 2593c31b 编写于 作者: J John Lindgren

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.
上级 60def109
......@@ -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
......
......@@ -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
/*-----------------------------------------------*/
......
......@@ -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
*-------------------------------------------------------
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册