Skip to content

Commit 87dd938

Browse files
authored
Merge pull request #639 from AJIOB/auto_define_test_case_marco
Provide the way for automatically define TEST_CASE & TEST_RANGE macros (Thanks, Alex. Looks great!)
2 parents 0b92f9b + 48d7210 commit 87dd938

File tree

6 files changed

+51
-20
lines changed

6 files changed

+51
-20
lines changed

docs/UnityConfigurationGuide.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,36 @@ This will rarely be necessary. Most often, Unity will automatically detect if th
438438
In the event that the compiler supports variadic macros, but is primarily C89 (ANSI), defining this option will allow you to use them.
439439
This option is also not necessary when using Ceedling or the test runner generator script.
440440

441+
#### `UNITY_SUPPORT_TEST_CASES`
442+
443+
Unity can automatically define all supported parameterized tests macros.
444+
That feature is disabled by default.
445+
To enable it, use the following example:
446+
447+
```C
448+
#define UNITY_SUPPORT_TEST_CASES
449+
```
450+
451+
You can manually provide required `TEST_CASE` or `TEST_RANGE` macro definitions
452+
before including `unity.h`, and they won't be redefined.
453+
If you provide one of the following macros, some of default definitions will not be
454+
defined:
455+
| User defines macro | Unity will _not_ define following macro |
456+
|---|---|
457+
| `UNITY_EXCLUDE_TEST_CASE` | `TEST_CASE` |
458+
| `UNITY_EXCLUDE_TEST_RANGE` | `TEST_RANGE` |
459+
| `TEST_CASE` | `TEST_CASE` |
460+
| `TEST_RANGE` | `TEST_RANGE` |
461+
462+
`UNITY_EXCLUDE_TEST_*` defines is not processed by test runner generator script.
463+
If you exclude one of them from definition, you should provide your own definition
464+
for them or avoid using undefined `TEST_*` macro as a test generator.
465+
Otherwise, compiler cannot build source code file with provided call.
466+
467+
_Note:_
468+
That feature requires variadic macro support by compiler. If required feature
469+
is not detected, it will not be enabled, even though preprocessor macro is defined.
470+
441471
## Getting Into The Guts
442472

443473
There will be cases where the options above aren't quite going to get everything perfect.

src/unity_internals.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -765,19 +765,35 @@ extern const char UnityStrErrShorthand[];
765765
#define TEST_ABORT() return
766766
#endif
767767

768+
/* Automatically enable variadic macros support, if it not enabled before */
769+
#ifndef UNITY_SUPPORT_VARIADIC_MACROS
770+
#ifdef __STDC_VERSION__
771+
#if __STDC_VERSION__ >= 199901L
772+
#define UNITY_SUPPORT_VARIADIC_MACROS
773+
#endif
774+
#endif
775+
#endif
776+
768777
/* This tricky series of macros gives us an optional line argument to treat it as RUN_TEST(func, num=__LINE__) */
769778
#ifndef RUN_TEST
770-
#ifdef __STDC_VERSION__
771-
#if __STDC_VERSION__ >= 199901L
772-
#define UNITY_SUPPORT_VARIADIC_MACROS
773-
#endif
774-
#endif
775779
#ifdef UNITY_SUPPORT_VARIADIC_MACROS
776780
#define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway)
777781
#define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line)
778782
#endif
779783
#endif
780784

785+
/* Enable default macros for masking param tests test cases */
786+
#ifdef UNITY_SUPPORT_TEST_CASES
787+
#ifdef UNITY_SUPPORT_VARIADIC_MACROS
788+
#if !defined(TEST_CASE) && !defined(UNITY_EXCLUDE_TEST_CASE)
789+
#define TEST_CASE(...)
790+
#endif
791+
#if !defined(TEST_RANGE) && !defined(UNITY_EXCLUDE_TEST_RANGE)
792+
#define TEST_RANGE(...)
793+
#endif
794+
#endif
795+
#endif
796+
781797
/* If we can't do the tricky version, we'll just have to require them to always include the line number */
782798
#ifndef RUN_TEST
783799
#ifdef CMOCK

test/testdata/testRunnerGenerator.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
suitetest- custom prefix for when we want to use custom suite setup/teardown
2020
*/
2121

22-
/* Support for Meta Test Rig */
23-
#define TEST_CASE(a)
24-
2522
/* Include Passthroughs for Linking Tests */
2623
void putcharSpy(int c) { (void)putchar(c);}
2724
void flushSpy(void) {}

test/testdata/testRunnerGeneratorSmall.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ TEST_FILE("some_file.c")
1111
spec - normal default prefix. required to run default setup/teardown calls.
1212
*/
1313

14-
/* Support for Meta Test Rig */
15-
#define TEST_CASE(a)
16-
1714
/* Include Passthroughs for Linking Tests */
1815
void putcharSpy(int c) { (void)putchar(c);}
1916
void flushSpy(void) {}
@@ -67,4 +64,3 @@ void spec_ThisTestPassesWhenNormalTeardownRan(void)
6764
{
6865
TEST_ASSERT_EQUAL_MESSAGE(1, CounterTeardown, "Normal Teardown Wasn't Run");
6966
}
70-

test/testdata/testRunnerGeneratorWithMocks.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
suitetest- custom prefix for when we want to use custom suite setup/teardown
2121
*/
2222

23-
/* Support for Meta Test Rig */
24-
#define TEST_CASE(a)
25-
2623
/* Include Passthroughs for Linking Tests */
2724
void putcharSpy(int c) { (void)putchar(c);}
2825
void flushSpy(void) {}
@@ -194,4 +191,3 @@ void test_ShouldCallMockInitAndVerifyFunctionsForEachTest(void)
194191
TEST_ASSERT_EQUAL_MESSAGE(Unity.NumberOfTests - 1, mockMock_Destroy_Counter, "Mock Destroy Should Be Called Once Per Test Completed");
195192
TEST_ASSERT_EQUAL_MESSAGE(0, CMockMemFreeFinalCounter, "Mock MemFreeFinal Should Not Be Called Until End");
196193
}
197-

test/tests/test_unity_parameterized.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
#include <stdio.h>
99
#include "unity.h"
1010

11-
/* Support for Meta Test Rig */
12-
#define TEST_CASE(...)
13-
#define TEST_RANGE(...)
14-
1511
/* Include Passthroughs for Linking Tests */
1612
void putcharSpy(int c) { (void)putchar(c);}
1713
void flushSpy(void) {}

0 commit comments

Comments
 (0)