From 4d5ed3d68b0971689ba3ff045aacdadb4a42345e Mon Sep 17 00:00:00 2001 From: Alex Overchenko Date: Sun, 27 Nov 2022 13:05:13 +0300 Subject: [PATCH 1/3] Adding possibility for automatically defining TEST_CASE & TEST_RANGE macros --- docs/UnityConfigurationGuide.md | 24 ++++++++++++++++++++++++ src/unity_internals.h | 26 +++++++++++++++++++++----- test/tests/test_unity_parameterized.c | 6 ++---- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/docs/UnityConfigurationGuide.md b/docs/UnityConfigurationGuide.md index 7a0fa4c..f24e6a0 100644 --- a/docs/UnityConfigurationGuide.md +++ b/docs/UnityConfigurationGuide.md @@ -438,6 +438,30 @@ This will rarely be necessary. Most often, Unity will automatically detect if th In the event that the compiler supports variadic macros, but is primarily C89 (ANSI), defining this option will allow you to use them. This option is also not necessary when using Ceedling or the test runner generator script. +#### `UNITY_INCLUDE_PARAM_TESTING_MACRO` + +Unity can automatically define all supported parameterized tests macros. +To enable that feature, use the following example: + +```C +#define UNITY_INCLUDE_PARAM_TESTING_MACRO +``` + +You can manually provide required `TEST_CASE` or `TEST_RANGE` macro definitions +before including `unity.h`, and they won't be redefined. +If you provide one of the following macros, some of default definitions will not be +defined: +| User defines macro | Unity will __not__ define following macro | +|---|---| +| `UNITY_NOT_DEFINE_TEST_CASE` | `TEST_CASE` | +| `UNITY_NOT_DEFINE_TEST_RANGE` | `TEST_RANGE` | +| `TEST_CASE` | `TEST_CASE` | +| `TEST_RANGE` | `TEST_RANGE` | + +_Note:_ +That feature requires variadic macro support by compiler. If required feature +is not detected, it will not be enabled, even though preprocessor macro is defined. + ## Getting Into The Guts There will be cases where the options above aren't quite going to get everything perfect. diff --git a/src/unity_internals.h b/src/unity_internals.h index f2c312c..bae842e 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -765,19 +765,35 @@ extern const char UnityStrErrShorthand[]; #define TEST_ABORT() return #endif +/* Automatically enable variadic macros support, if it not enabled before */ +#ifndef UNITY_SUPPORT_VARIADIC_MACROS + #ifdef __STDC_VERSION__ + #if __STDC_VERSION__ >= 199901L + #define UNITY_SUPPORT_VARIADIC_MACROS + #endif + #endif +#endif + /* This tricky series of macros gives us an optional line argument to treat it as RUN_TEST(func, num=__LINE__) */ #ifndef RUN_TEST -#ifdef __STDC_VERSION__ -#if __STDC_VERSION__ >= 199901L -#define UNITY_SUPPORT_VARIADIC_MACROS -#endif -#endif #ifdef UNITY_SUPPORT_VARIADIC_MACROS #define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway) #define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line) #endif #endif +/* Enable default macros for masking param tests test cases */ +#ifdef UNITY_INCLUDE_PARAM_TESTING_MACRO + #ifdef UNITY_SUPPORT_VARIADIC_MACROS + #if !defined(TEST_CASE) && !defined(UNITY_NOT_DEFINE_TEST_CASE) + #define TEST_CASE(...) + #endif + #if !defined(TEST_RANGE) && !defined(UNITY_NOT_DEFINE_TEST_RANGE) + #define TEST_RANGE(...) + #endif + #endif +#endif + /* If we can't do the tricky version, we'll just have to require them to always include the line number */ #ifndef RUN_TEST #ifdef CMOCK diff --git a/test/tests/test_unity_parameterized.c b/test/tests/test_unity_parameterized.c index 5d20826..a393a0a 100644 --- a/test/tests/test_unity_parameterized.c +++ b/test/tests/test_unity_parameterized.c @@ -4,14 +4,12 @@ [Released under MIT License. Please refer to license.txt for details] ========================================== */ +#define UNITY_INCLUDE_PARAM_TESTING_MACRO + #include #include #include "unity.h" -/* Support for Meta Test Rig */ -#define TEST_CASE(...) -#define TEST_RANGE(...) - /* Include Passthroughs for Linking Tests */ void putcharSpy(int c) { (void)putchar(c);} void flushSpy(void) {} From e4085eb8e669e227e8d1e8a4700830e2052df599 Mon Sep 17 00:00:00 2001 From: Alex Overchenko Date: Sun, 27 Nov 2022 14:36:22 +0300 Subject: [PATCH 2/3] Using default macro for TEST_CASEs define. Improving docs about manual definition. --- docs/UnityConfigurationGuide.md | 18 ++++++++++++------ src/unity_internals.h | 6 +++--- test/tests/test_unity_parameterized.c | 2 -- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/UnityConfigurationGuide.md b/docs/UnityConfigurationGuide.md index f24e6a0..d5e4098 100644 --- a/docs/UnityConfigurationGuide.md +++ b/docs/UnityConfigurationGuide.md @@ -438,26 +438,32 @@ This will rarely be necessary. Most often, Unity will automatically detect if th In the event that the compiler supports variadic macros, but is primarily C89 (ANSI), defining this option will allow you to use them. This option is also not necessary when using Ceedling or the test runner generator script. -#### `UNITY_INCLUDE_PARAM_TESTING_MACRO` +#### `UNITY_SUPPORT_TEST_CASES` Unity can automatically define all supported parameterized tests macros. -To enable that feature, use the following example: +That feature is disabled by default. +To enable it, use the following example: ```C -#define UNITY_INCLUDE_PARAM_TESTING_MACRO +#define UNITY_SUPPORT_TEST_CASES ``` You can manually provide required `TEST_CASE` or `TEST_RANGE` macro definitions before including `unity.h`, and they won't be redefined. If you provide one of the following macros, some of default definitions will not be defined: -| User defines macro | Unity will __not__ define following macro | +| User defines macro | Unity will _not_ define following macro | |---|---| -| `UNITY_NOT_DEFINE_TEST_CASE` | `TEST_CASE` | -| `UNITY_NOT_DEFINE_TEST_RANGE` | `TEST_RANGE` | +| `UNITY_EXCLUDE_TEST_CASE` | `TEST_CASE` | +| `UNITY_EXCLUDE_TEST_RANGE` | `TEST_RANGE` | | `TEST_CASE` | `TEST_CASE` | | `TEST_RANGE` | `TEST_RANGE` | +`UNITY_EXCLUDE_TEST_*` defines is not processed by test runner generator script. +If you exclude one of them from definition, you should provide your own definition +for them or avoid using undefined `TEST_*` macro as a test generator. +Otherwise, compiler cannot build source code file with provided call. + _Note:_ That feature requires variadic macro support by compiler. If required feature is not detected, it will not be enabled, even though preprocessor macro is defined. diff --git a/src/unity_internals.h b/src/unity_internals.h index bae842e..641c1b3 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -783,12 +783,12 @@ extern const char UnityStrErrShorthand[]; #endif /* Enable default macros for masking param tests test cases */ -#ifdef UNITY_INCLUDE_PARAM_TESTING_MACRO +#ifdef UNITY_SUPPORT_TEST_CASES #ifdef UNITY_SUPPORT_VARIADIC_MACROS - #if !defined(TEST_CASE) && !defined(UNITY_NOT_DEFINE_TEST_CASE) + #if !defined(TEST_CASE) && !defined(UNITY_EXCLUDE_TEST_CASE) #define TEST_CASE(...) #endif - #if !defined(TEST_RANGE) && !defined(UNITY_NOT_DEFINE_TEST_RANGE) + #if !defined(TEST_RANGE) && !defined(UNITY_EXCLUDE_TEST_RANGE) #define TEST_RANGE(...) #endif #endif diff --git a/test/tests/test_unity_parameterized.c b/test/tests/test_unity_parameterized.c index a393a0a..6b8aeb7 100644 --- a/test/tests/test_unity_parameterized.c +++ b/test/tests/test_unity_parameterized.c @@ -4,8 +4,6 @@ [Released under MIT License. Please refer to license.txt for details] ========================================== */ -#define UNITY_INCLUDE_PARAM_TESTING_MACRO - #include #include #include "unity.h" From 48d7210644842b4e25b735dedd4927e2de26a924 Mon Sep 17 00:00:00 2001 From: Alex Overchenko Date: Sun, 27 Nov 2022 14:46:34 +0300 Subject: [PATCH 3/3] Fixing CI tests passing --- test/testdata/testRunnerGenerator.c | 3 --- test/testdata/testRunnerGeneratorSmall.c | 4 ---- test/testdata/testRunnerGeneratorWithMocks.c | 4 ---- 3 files changed, 11 deletions(-) diff --git a/test/testdata/testRunnerGenerator.c b/test/testdata/testRunnerGenerator.c index b5dd97f..3ea01a1 100644 --- a/test/testdata/testRunnerGenerator.c +++ b/test/testdata/testRunnerGenerator.c @@ -19,9 +19,6 @@ suitetest- custom prefix for when we want to use custom suite setup/teardown */ -/* Support for Meta Test Rig */ -#define TEST_CASE(a) - /* Include Passthroughs for Linking Tests */ void putcharSpy(int c) { (void)putchar(c);} void flushSpy(void) {} diff --git a/test/testdata/testRunnerGeneratorSmall.c b/test/testdata/testRunnerGeneratorSmall.c index c8aaf74..dc687ba 100644 --- a/test/testdata/testRunnerGeneratorSmall.c +++ b/test/testdata/testRunnerGeneratorSmall.c @@ -11,9 +11,6 @@ TEST_FILE("some_file.c") spec - normal default prefix. required to run default setup/teardown calls. */ -/* Support for Meta Test Rig */ -#define TEST_CASE(a) - /* Include Passthroughs for Linking Tests */ void putcharSpy(int c) { (void)putchar(c);} void flushSpy(void) {} @@ -67,4 +64,3 @@ void spec_ThisTestPassesWhenNormalTeardownRan(void) { TEST_ASSERT_EQUAL_MESSAGE(1, CounterTeardown, "Normal Teardown Wasn't Run"); } - diff --git a/test/testdata/testRunnerGeneratorWithMocks.c b/test/testdata/testRunnerGeneratorWithMocks.c index aaceda4..d48692e 100644 --- a/test/testdata/testRunnerGeneratorWithMocks.c +++ b/test/testdata/testRunnerGeneratorWithMocks.c @@ -20,9 +20,6 @@ suitetest- custom prefix for when we want to use custom suite setup/teardown */ -/* Support for Meta Test Rig */ -#define TEST_CASE(a) - /* Include Passthroughs for Linking Tests */ void putcharSpy(int c) { (void)putchar(c);} void flushSpy(void) {} @@ -194,4 +191,3 @@ void test_ShouldCallMockInitAndVerifyFunctionsForEachTest(void) TEST_ASSERT_EQUAL_MESSAGE(Unity.NumberOfTests - 1, mockMock_Destroy_Counter, "Mock Destroy Should Be Called Once Per Test Completed"); TEST_ASSERT_EQUAL_MESSAGE(0, CMockMemFreeFinalCounter, "Mock MemFreeFinal Should Not Be Called Until End"); } -