Skip to content

Commit 774da10

Browse files
authored
Merge pull request #296 from jlindgren90/master
Allow suiteSetUp() and suiteTearDown() to be provided as normal C functions (Thanks @jlindgren90 )
2 parents 0914d80 + 629b86d commit 774da10

File tree

5 files changed

+76
-23
lines changed

5 files changed

+76
-23
lines changed

auto/generate_test_runner.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ def create_header(output, mocks, testfile_includes = [])
157157
output.puts('/* AUTOGENERATED FILE. DO NOT EDIT. */')
158158
create_runtest(output, mocks)
159159
output.puts("\n/*=======Automagically Detected Files To Include=====*/")
160+
output.puts('#ifdef __WIN32__')
161+
output.puts('#define UNITY_INCLUDE_SETUP_STUBS')
162+
output.puts('#endif')
160163
output.puts("#include \"#{@options[:framework]}.h\"")
161164
output.puts('#include "cmock.h"') unless mocks.empty?
162165
output.puts('#include <setjmp.h>')
@@ -235,22 +238,36 @@ def create_mock_management(output, mock_headers)
235238
end
236239

237240
def create_suite_setup(output)
238-
return if @options[:suite_setup].nil?
239-
240241
output.puts("\n/*=======Suite Setup=====*/")
241242
output.puts('static void suite_setup(void)')
242243
output.puts('{')
243-
output.puts(@options[:suite_setup])
244+
if @options[:suite_setup].nil?
245+
# New style, call suiteSetUp() if we can use weak symbols
246+
output.puts('#if defined(UNITY_WEAK_ATTRIBUTE) || defined(UNITY_WEAK_PRAGMA)')
247+
output.puts(' suiteSetUp();')
248+
output.puts('#endif')
249+
else
250+
# Old style, C code embedded in the :suite_setup option
251+
output.puts(@options[:suite_setup])
252+
end
244253
output.puts('}')
245254
end
246255

247256
def create_suite_teardown(output)
248-
return if @options[:suite_teardown].nil?
249-
250257
output.puts("\n/*=======Suite Teardown=====*/")
251258
output.puts('static int suite_teardown(int num_failures)')
252259
output.puts('{')
253-
output.puts(@options[:suite_teardown])
260+
if @options[:suite_teardown].nil?
261+
# New style, call suiteTearDown() if we can use weak symbols
262+
output.puts('#if defined(UNITY_WEAK_ATTRIBUTE) || defined(UNITY_WEAK_PRAGMA)')
263+
output.puts(' return suiteTearDown(num_failures);')
264+
output.puts('#else')
265+
output.puts(' return num_failures;')
266+
output.puts('#endif')
267+
else
268+
# Old style, C code embedded in the :suite_teardown option
269+
output.puts(@options[:suite_teardown])
270+
end
254271
output.puts('}')
255272
end
256273

@@ -342,7 +359,7 @@ def create_main(output, filename, tests, used_mocks)
342359
output.puts("int #{main_name}(void)")
343360
output.puts('{')
344361
end
345-
output.puts(' suite_setup();') unless @options[:suite_setup].nil?
362+
output.puts(' suite_setup();')
346363
output.puts(" UnityBegin(\"#{filename.gsub(/\\/, '\\\\\\')}\");")
347364
if @options[:use_param_tests]
348365
tests.each do |test|
@@ -357,7 +374,7 @@ def create_main(output, filename, tests, used_mocks)
357374
end
358375
output.puts
359376
output.puts(' CMock_Guts_MemFreeFinal();') unless used_mocks.empty?
360-
output.puts(" return #{@options[:suite_teardown].nil? ? '' : 'suite_teardown'}(UnityEnd());")
377+
output.puts(" return suite_teardown(UnityEnd());")
361378
output.puts('}')
362379
end
363380

docs/UnityHelperScriptsGuide.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ demonstrates using a Ruby hash.
124124

125125
##### `:includes`
126126

127-
This option specifies an array of file names to be ?#include?'d at the top of
127+
This option specifies an array of file names to be `#include`'d at the top of
128128
your runner C file. You might use it to reference custom types or anything else
129129
universally needed in your generated runners.
130130

@@ -133,11 +133,23 @@ universally needed in your generated runners.
133133

134134
Define this option with C code to be executed _before any_ test cases are run.
135135

136+
Alternatively, if your C compiler supports weak symbols, you can leave this
137+
option unset and instead provide a `void suiteSetUp(void)` function in your test
138+
suite. The linker will look for this symbol and fall back to a Unity-provided
139+
stub if it is not found.
140+
136141

137142
##### `:suite_teardown`
138143

139-
Define this option with C code to be executed ?after all?test cases have
140-
finished.
144+
Define this option with C code to be executed _after all_ test cases have
145+
finished. An integer variable `num_failures` is available for diagnostics.
146+
The code should end with a `return` statement; the value returned will become
147+
the exit code of `main`. You can normally just return `num_failures`.
148+
149+
Alternatively, if your C compiler supports weak symbols, you can leave this
150+
option unset and instead provide a `int suiteTearDown(int num_failures)`
151+
function in your test suite. The linker will look for this symbol and fall
152+
back to a Unity-provided stub if it is not found.
141153

142154

143155
##### `:enforce_strict_ordering`

src/unity.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[Released under MIT License. Please refer to license.txt for details]
55
============================================================================ */
66

7+
#define UNITY_INCLUDE_SETUP_STUBS
78
#include "unity.h"
89
#include <stddef.h>
910

@@ -1325,17 +1326,6 @@ void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
13251326
UNITY_IGNORE_AND_BAIL;
13261327
}
13271328

1328-
/*-----------------------------------------------*/
1329-
#if defined(UNITY_WEAK_ATTRIBUTE)
1330-
UNITY_WEAK_ATTRIBUTE void setUp(void) { }
1331-
UNITY_WEAK_ATTRIBUTE void tearDown(void) { }
1332-
#elif defined(UNITY_WEAK_PRAGMA)
1333-
#pragma weak setUp
1334-
void setUp(void) { }
1335-
#pragma weak tearDown
1336-
void tearDown(void) { }
1337-
#endif
1338-
13391329
/*-----------------------------------------------*/
13401330
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
13411331
{

src/unity.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,43 @@ extern "C"
1515

1616
#include "unity_internals.h"
1717

18+
/*-------------------------------------------------------
19+
* Test Setup / Teardown
20+
*-------------------------------------------------------*/
21+
22+
/* These functions are intended to be called before and after each test. */
1823
void setUp(void);
1924
void tearDown(void);
2025

26+
/* These functions are intended to be called at the beginning and end of an
27+
* entire test suite. suiteTearDown() is passed the number of tests that
28+
* failed, and its return value becomes the exit code of main(). */
29+
void suiteSetUp(void);
30+
int suiteTearDown(int num_failures);
31+
32+
/* If the compiler supports it, the following block provides stub
33+
* implementations of the above functions as weak symbols. Note that on
34+
* some platforms (MinGW for example), weak function implementations need
35+
* to be in the same translation unit they are called from. This can be
36+
* achieved by defining UNITY_INCLUDE_SETUP_STUBS before including unity.h. */
37+
#ifdef UNITY_INCLUDE_SETUP_STUBS
38+
#ifdef UNITY_WEAK_ATTRIBUTE
39+
UNITY_WEAK_ATTRIBUTE void setUp(void) { }
40+
UNITY_WEAK_ATTRIBUTE void tearDown(void) { }
41+
UNITY_WEAK_ATTRIBUTE void suiteSetUp(void) { }
42+
UNITY_WEAK_ATTRIBUTE int suiteTearDown(int num_failures) { return num_failures; }
43+
#elif defined(UNITY_WEAK_PRAGMA)
44+
#pragma weak setUp
45+
void setUp(void) { }
46+
#pragma weak tearDown
47+
void tearDown(void) { }
48+
#pragma weak suiteSetUp
49+
void suiteSetUp(void) { }
50+
#pragma weak suiteTearDown
51+
int suiteTearDown(int num_failures) { return num_failures; }
52+
#endif
53+
#endif
54+
2155
/*-------------------------------------------------------
2256
* Configuration Options
2357
*-------------------------------------------------------

src/unity_internals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ extern void UNITY_OMIT_OUTPUT_FLUSH_HEADER_DECLARATION;
301301
* Language Features Available
302302
*-------------------------------------------------------*/
303303
#if !defined(UNITY_WEAK_ATTRIBUTE) && !defined(UNITY_WEAK_PRAGMA)
304-
# ifdef __GNUC__ /* includes clang */
304+
# if defined(__GNUC__) || defined(__ghs__) /* __GNUC__ includes clang */
305305
# if !(defined(__WIN32__) && defined(__clang__)) && !defined(__TMS470__)
306306
# define UNITY_WEAK_ATTRIBUTE __attribute__((weak))
307307
# endif

0 commit comments

Comments
 (0)