Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/UnityChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Prior to 2008, the project was an internal project and not released to the publi
New Features:

- Add `-n` comand line option as strict matcher again
- Support `TESTBRIDGE_TEST_ONLY` for Bazel `--test_filter` when `UNITY_USE_COMMAND_LINE_ARGS` is enabled

Significant Bugfixes:

Expand Down
23 changes: 13 additions & 10 deletions docs/UnityHelperScriptsGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ These are the available options:
| `-v` | increase Verbosity |
| `-x NAME` | eXclude tests whose name includes NAME |

Unity also supports the `TESTBRIDGE_TEST_ONLY` environment variable (used by
Bazel's `--test_filter`) when `UNITY_USE_COMMAND_LINE_ARGS` is enabled.

##### `:setup_name`

Override the default test `setUp` function name.
Expand Down Expand Up @@ -377,11 +380,11 @@ tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(4, 6, 30):PASS

As we can see:

| Parameter | Format | Possible values | Total of values | Format number |
|---|---|---|---|---|
| `a` | `[3, 4, 1]` | `3`, `4` | 2 | Format 1 |
| `b` | `[10, 5, -2]` | `10`, `8`, `6` | 3 | Format 1, negative step, end number is not included |
| `c` | `<30, 31, 1>` | `30` | 1 | Format 2 |
| Parameter | Format | Possible values | Total of values | Format number |
| --------- | ------------- | --------------- | --------------- | --------------------------------------------------- |
| `a` | `[3, 4, 1]` | `3`, `4` | 2 | Format 1 |
| `b` | `[10, 5, -2]` | `10`, `8`, `6` | 3 | Format 1, negative step, end number is not included |
| `c` | `<30, 31, 1>` | `30` | 1 | Format 2 |

_Note_, that format 2 also supports negative step.

Expand Down Expand Up @@ -448,11 +451,11 @@ tests/test_unity_parameterizedDemo.c:18:test_demoParamFunction(7, 1, 20.0f):PASS

As we can see:

| Parameter | Format | Count of values |
|---|---|---|
| `a` | `[3, 4, 7]` | 3 |
| `b` | `[10, 8, 2, 1]` | 4 |
| `c` | `[30u, 20.0f]` | 2 |
| Parameter | Format | Count of values |
| --------- | --------------- | --------------- |
| `a` | `[3, 4, 7]` | 3 |
| `b` | `[10, 8, 2, 1]` | 4 |
| `c` | `[30u, 20.0f]` | 2 |

We totally have 3 * 4 * 2 = 24 equal test cases, that can be written as following:

Expand Down
12 changes: 12 additions & 0 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -2402,10 +2402,22 @@ int UnityStrictMatch = 0;
int UnityParseOptions(int argc, char** argv)
{
int i;
const char* testbridge_filter = NULL;
UnityOptionIncludeNamed = NULL;
UnityOptionExcludeNamed = NULL;
UnityStrictMatch = 0;

#ifndef UNITY_GETENV
/*Allow Bazel test filtering via TESTBRIDGE_TEST_ONLY*/
extern char* getenv(const char* name);
#define UNITY_GETENV(name) getenv(name)
#endif
testbridge_filter = UNITY_GETENV("TESTBRIDGE_TEST_ONLY");
if (testbridge_filter && testbridge_filter[0] != 0)
{
UnityOptionIncludeNamed = (char*)testbridge_filter;
}

for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
Expand Down
65 changes: 65 additions & 0 deletions test/tests/test_unity_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
========================================================================= */

#include "unity.h"
#ifdef UNITY_USE_COMMAND_LINE_ARGS
#include "unity_internals.h"
#include <stdlib.h>
#endif
#define TEST_INSTANCES
#include "self_assessment_utils.h"

Expand Down Expand Up @@ -194,6 +198,67 @@ void testFail(void)
VERIFY_FAILS_END
}

#ifdef UNITY_USE_COMMAND_LINE_ARGS
static void UnitySetTestbridgeFilter(const char* value)
{
#if defined(_WIN32) || defined(_MSC_VER)
if (value)
{
_putenv_s("TESTBRIDGE_TEST_ONLY", value);
}
else
{
_putenv_s("TESTBRIDGE_TEST_ONLY", "");
}
#else
if (value)
{
setenv("TESTBRIDGE_TEST_ONLY", value, 1);
}
else
{
unsetenv("TESTBRIDGE_TEST_ONLY");
}
#endif
}

static void UnitySetTestContext(const char* testfile, const char* testname)
{
Unity.TestFile = testfile;
Unity.CurrentTestName = testname;
}

void testUnityParseOptionsUsesTestbridgeFilter(void)
{
char* argv[] = { (char*)"prog", NULL };

UnitySetTestbridgeFilter("test_my_function");
UnityParseOptions(1, argv);

UnitySetTestContext("file.c", "test_my_function");
TEST_ASSERT_TRUE(UnityTestMatches());
UnitySetTestContext("file.c", "other");
TEST_ASSERT_FALSE(UnityTestMatches());

UnitySetTestbridgeFilter(NULL);
}

void testUnityParseOptionsArgsOverrideTestbridgeFilter(void)
{
char* argv[] = { (char*)"prog", (char*)"-n", (char*)"other", NULL };

UnitySetTestbridgeFilter("test_my_function");
UnityParseOptions(3, argv);

UnitySetTestContext("file.c", "other");
TEST_ASSERT_TRUE(UnityTestMatches());
UnitySetTestContext("file.c", "test_my_function");
TEST_ASSERT_FALSE(UnityTestMatches());

UnitySetTestbridgeFilter(NULL);
}
#endif

void testIsNull(void)
{
char* ptr1 = NULL;
Expand Down