mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-01-26 09:51:36 +01:00
Merge branch 'feature/cmd_line_args'
This commit is contained in:
175
src/unity.c
175
src/unity.c
@@ -1300,4 +1300,179 @@ int UnityEnd(void)
|
||||
return (int)(Unity.TestFailures);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------
|
||||
* Command Line Argument Support
|
||||
*-----------------------------------------------*/
|
||||
#ifdef UNITY_USE_COMMAND_LINE_ARGS
|
||||
|
||||
char* UnityOptionIncludeNamed = NULL;
|
||||
char* UnityOptionExcludeNamed = NULL;
|
||||
int UnityVerbosity = 1;
|
||||
|
||||
int UnityParseOptions(int argc, char** argv)
|
||||
{
|
||||
UnityOptionIncludeNamed = NULL;
|
||||
UnityOptionExcludeNamed = NULL;
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (argv[i][0] == '-')
|
||||
{
|
||||
switch(argv[i][1])
|
||||
{
|
||||
case 'l': /* list tests */
|
||||
return -1;
|
||||
case 'n': /* include tests with name including this string */
|
||||
case 'f': /* an alias for -n */
|
||||
if (argv[i][2] == '=')
|
||||
UnityOptionIncludeNamed = &argv[i][3];
|
||||
else if (++i < argc)
|
||||
UnityOptionIncludeNamed = argv[i];
|
||||
else
|
||||
{
|
||||
UnityPrint("ERROR: No Test String to Include Matches For");
|
||||
UNITY_PRINT_EOL();
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
case 'q': /* quiet */
|
||||
UnityVerbosity = 0;
|
||||
break;
|
||||
case 'v': /* verbose */
|
||||
UnityVerbosity = 2;
|
||||
break;
|
||||
case 'x': /* exclude tests with name including this string */
|
||||
if (argv[i][2] == '=')
|
||||
UnityOptionExcludeNamed = &argv[i][3];
|
||||
else if (++i < argc)
|
||||
UnityOptionExcludeNamed = argv[i];
|
||||
else
|
||||
{
|
||||
UnityPrint("ERROR: No Test String to Exclude Matches For");
|
||||
UNITY_PRINT_EOL();
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
UnityPrint("ERROR: Unknown Option ");
|
||||
UNITY_OUTPUT_CHAR(argv[i][1]);
|
||||
UNITY_PRINT_EOL();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IsStringInBiggerString(const char* longstring, const char* shortstring)
|
||||
{
|
||||
char* lptr = (char*)longstring;
|
||||
char* sptr = (char*)shortstring;
|
||||
char* lnext = lptr;
|
||||
|
||||
if (*sptr == '*')
|
||||
return 1;
|
||||
|
||||
while (*lptr)
|
||||
{
|
||||
lnext = lptr + 1;
|
||||
|
||||
/* If they current bytes match, go on to the next bytes */
|
||||
while (*lptr && *sptr && (*lptr == *sptr))
|
||||
{
|
||||
lptr++;
|
||||
sptr++;
|
||||
|
||||
/* We're done if we match the entire string or up to a wildcard */
|
||||
if (*sptr == '*')
|
||||
return 1;
|
||||
if (*sptr == ',')
|
||||
return 1;
|
||||
if (*sptr == '"')
|
||||
return 1;
|
||||
if (*sptr == '\'')
|
||||
return 1;
|
||||
if (*sptr == ':')
|
||||
return 2;
|
||||
if (*sptr == 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Otherwise we start in the long pointer 1 character further and try again */
|
||||
lptr = lnext;
|
||||
sptr = (char*)shortstring;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int UnityStringArgumentMatches(const char* str)
|
||||
{
|
||||
int retval;
|
||||
const char* ptr1;
|
||||
const char* ptr2;
|
||||
const char* ptrf;
|
||||
|
||||
//Go through the options and get the substrings for matching one at a time
|
||||
ptr1 = str;
|
||||
while (ptr1[0] != 0)
|
||||
{
|
||||
if ((ptr1[0] == '"') || (ptr1[0] == '\''))
|
||||
ptr1++;
|
||||
|
||||
//look for the start of the next partial
|
||||
ptr2 = ptr1;
|
||||
ptrf = 0;
|
||||
do {
|
||||
ptr2++;
|
||||
if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','))
|
||||
ptrf = &ptr2[1];
|
||||
} while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','));
|
||||
while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ',')))
|
||||
ptr2++;
|
||||
|
||||
//done if complete filename match
|
||||
retval = IsStringInBiggerString(Unity.TestFile, ptr1);
|
||||
if (retval == 1)
|
||||
return retval;
|
||||
|
||||
//done if testname match after filename partial match
|
||||
if ((retval == 2) && (ptrf != 0))
|
||||
{
|
||||
if (IsStringInBiggerString(Unity.CurrentTestName, ptrf))
|
||||
return 1;
|
||||
}
|
||||
|
||||
//done if complete testname match
|
||||
if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1)
|
||||
return 1;
|
||||
|
||||
ptr1 = ptr2;
|
||||
}
|
||||
|
||||
//we couldn't find a match for any substrings
|
||||
return 0;
|
||||
}
|
||||
|
||||
int UnityTestMatches(void)
|
||||
{
|
||||
/* Check if this test name matches the included test pattern */
|
||||
int retval;
|
||||
if (UnityOptionIncludeNamed)
|
||||
{
|
||||
retval = UnityStringArgumentMatches(UnityOptionIncludeNamed);
|
||||
}
|
||||
else
|
||||
retval = 1;
|
||||
|
||||
/* Check if this test name matches the excluded test pattern */
|
||||
if (UnityOptionExcludeNamed)
|
||||
{
|
||||
if (UnityStringArgumentMatches(UnityOptionExcludeNamed))
|
||||
retval = 0;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
#endif /* UNITY_USE_COMMAND_LINE_ARGS */
|
||||
/*-----------------------------------------------*/
|
||||
|
||||
@@ -60,6 +60,9 @@ void tearDown(void);
|
||||
* Parameterized Tests
|
||||
* - you'll want to create a define of TEST_CASE(...) which basically evaluates to nothing
|
||||
|
||||
* Tests with Arguments
|
||||
* - you'll want to define UNITY_USE_COMMAND_LINE_ARGS if you have the test runner passing arguments to Unity
|
||||
|
||||
*-------------------------------------------------------
|
||||
* Basic Fail and Ignore
|
||||
*-------------------------------------------------------*/
|
||||
|
||||
@@ -640,6 +640,15 @@ extern const char UnityStrErr64[];
|
||||
|
||||
#define UNITY_UNUSED(x) (void)(sizeof(x))
|
||||
|
||||
/*-----------------------------------------------
|
||||
* Command Line Argument Support
|
||||
*-----------------------------------------------*/
|
||||
|
||||
#ifdef UNITY_USE_COMMAND_LINE_ARGS
|
||||
int UnityParseOptions(int argc, char** argv);
|
||||
int UnityTestMatches(void);
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------
|
||||
* Basic Fail and Ignore
|
||||
*-------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user