Skip to content

Commit 4d5ed3d

Browse files
author
Alex Overchenko
committed
Adding possibility for automatically defining
TEST_CASE & TEST_RANGE macros
1 parent 67ca5c5 commit 4d5ed3d

3 files changed

Lines changed: 47 additions & 9 deletions

File tree

docs/UnityConfigurationGuide.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,30 @@ 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_INCLUDE_PARAM_TESTING_MACRO`
442+
443+
Unity can automatically define all supported parameterized tests macros.
444+
To enable that feature, use the following example:
445+
446+
```C
447+
#define UNITY_INCLUDE_PARAM_TESTING_MACRO
448+
```
449+
450+
You can manually provide required `TEST_CASE` or `TEST_RANGE` macro definitions
451+
before including `unity.h`, and they won't be redefined.
452+
If you provide one of the following macros, some of default definitions will not be
453+
defined:
454+
| User defines macro | Unity will __not__ define following macro |
455+
|---|---|
456+
| `UNITY_NOT_DEFINE_TEST_CASE` | `TEST_CASE` |
457+
| `UNITY_NOT_DEFINE_TEST_RANGE` | `TEST_RANGE` |
458+
| `TEST_CASE` | `TEST_CASE` |
459+
| `TEST_RANGE` | `TEST_RANGE` |
460+
461+
_Note:_
462+
That feature requires variadic macro support by compiler. If required feature
463+
is not detected, it will not be enabled, even though preprocessor macro is defined.
464+
441465
## Getting Into The Guts
442466

443467
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_INCLUDE_PARAM_TESTING_MACRO
787+
#ifdef UNITY_SUPPORT_VARIADIC_MACROS
788+
#if !defined(TEST_CASE) && !defined(UNITY_NOT_DEFINE_TEST_CASE)
789+
#define TEST_CASE(...)
790+
#endif
791+
#if !defined(TEST_RANGE) && !defined(UNITY_NOT_DEFINE_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/tests/test_unity_parameterized.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
[Released under MIT License. Please refer to license.txt for details]
55
========================================== */
66

7+
#define UNITY_INCLUDE_PARAM_TESTING_MACRO
8+
79
#include <setjmp.h>
810
#include <stdio.h>
911
#include "unity.h"
1012

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

0 commit comments

Comments
 (0)