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
14 changes: 13 additions & 1 deletion src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ void UnityPopDetail(UNITY_DETAIL_LABEL_TYPE label, UNITY_DETAIL_VALUE_TYPE value
*-----------------------------------------------*/
#ifdef UNITY_USE_COMMAND_LINE_ARGS

char* UnityOptionIncludeNamed = NULL;
const char* UnityOptionIncludeNamed = NULL;
char* UnityOptionExcludeNamed = NULL;
int UnityVerbosity = 1;
int UnityStrictMatch = 0;
Expand All @@ -2413,10 +2413,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 = testbridge_filter;
}

for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
Expand Down
5 changes: 3 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CFLAGS += -Wbad-function-cast -Wcast-qual -Wold-style-definition -Wshadow -Wstri
CFLAGS += $(DEBUG)
UNITY_SUPPORT_64 = -D UNITY_SUPPORT_64
UNITY_INCLUDE_DOUBLE = -D UNITY_INCLUDE_DOUBLE
UNITY_USE_COMMAND_LINE_ARGS = -D UNITY_USE_COMMAND_LINE_ARGS
DEFINES = -D UNITY_OUTPUT_CHAR=putcharSpy
DEFINES += -D UNITY_OUTPUT_CHAR_HEADER_DECLARATION=putcharSpy\(int\)
DEFINES += -D UNITY_OUTPUT_FLUSH=flushSpy
Expand Down Expand Up @@ -53,7 +54,7 @@ coverage: $(SRC1) $(SRC2) $(SRC3) $(SRC4) $(SRC5) $(SRC6) $(SRC7) $(SRC8)
gcov unity.c | head -3
grep '###' $(BUILD_DIR)/unity.c.gcov -C2 || true
cd $(BUILD_DIR) && \
$(CC) $(CFLAGS) $(DEFINES) $(foreach i,$(SRC2), ../$i) $(COV_FLAGS) -o ../$(TARGET)
$(CC) $(CFLAGS) $(DEFINES) $(UNITY_USE_COMMAND_LINE_ARGS) $(foreach i,$(SRC2), ../$i) $(COV_FLAGS) -o ../$(TARGET)
rm -f $(BUILD_DIR)/*.gcda
./$(TARGET) | grep 'Tests\|]]]' -A1
cd $(BUILD_DIR) && \
Expand Down Expand Up @@ -105,7 +106,7 @@ coverage: $(SRC1) $(SRC2) $(SRC3) $(SRC4) $(SRC5) $(SRC6) $(SRC7) $(SRC8)
test: $(SRC1) $(SRC2) $(SRC3) $(SRC4) $(SRC5) $(SRC6) $(SRC7) $(SRC8)
$(CC) $(CFLAGS) $(DEFINES) $(INC_DIR) $(SRC1) -o $(TARGET)
./$(TARGET)
$(CC) $(CFLAGS) $(DEFINES) $(INC_DIR) $(SRC2) -o $(TARGET)
$(CC) $(CFLAGS) $(DEFINES) $(UNITY_USE_COMMAND_LINE_ARGS) $(INC_DIR) $(SRC2) -o $(TARGET)
./$(TARGET)
$(CC) $(CFLAGS) $(DEFINES) $(INC_DIR) $(SRC3) -o $(TARGET)
./$(TARGET)
Expand Down
2 changes: 1 addition & 1 deletion test/rakefile_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def run_tests(test_files)

report "\nRunning Tests in #{test}"
obj_list = []
test_defines = []
test_defines = File.basename(test) == 'test_unity_core.c' ? ['UNITY_USE_COMMAND_LINE_ARGS'] : []

# Detect dependencies and build required modules
extract_headers(test).each do |header|
Expand Down
77 changes: 77 additions & 0 deletions test/tests/test_unity_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
SPDX-License-Identifier: MIT
========================================================================= */

#if defined(UNITY_USE_COMMAND_LINE_ARGS) && !defined(_WIN32) && !defined(_MSC_VER) && !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200112L
#endif

#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 @@ -189,6 +197,75 @@ 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 };
const char* testfile = Unity.TestFile;
const char* testname = Unity.CurrentTestName;

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);
UnityParseOptions(1, argv);
UnitySetTestContext(testfile, testname);
}

void testUnityParseOptionsArgsOverrideTestbridgeFilter(void)
{
char* argv[] = { (char*)"prog", (char*)"-n", (char*)"other", NULL };
const char* testfile = Unity.TestFile;
const char* testname = Unity.CurrentTestName;

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);
UnityParseOptions(1, argv);
UnitySetTestContext(testfile, testname);
}
#endif

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