Skip to content

Commit

Permalink
Merge pull request #762 from koy-rehme-bae/fixtures_command_line_argu…
Browse files Browse the repository at this point in the history
…ments

New command line options for fixtures
  • Loading branch information
mvandervoord authored Jan 21, 2025
2 parents cbcd08f + df0b5d9 commit ce122c4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 14 deletions.
65 changes: 51 additions & 14 deletions extras/fixture/src/unity_fixture.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,25 @@ int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
return (int)Unity.TestFailures;
}

static int selected(const char* filter, const char* name)
static int selected(const char* filter, const char* select, const char* name)
{
if (filter == 0)
if (filter == 0 && select == 0)
return 1;
return strstr(name, filter) ? 1 : 0;
if (filter && strstr(name, filter))
return 1;
if (select && strcmp(name, select) == 0)
return 1;
return 0;
}

static int testSelected(const char* test)
{
return selected(UnityFixture.NameFilter, test);
return selected(UnityFixture.NameFilter, UnityFixture.Name, test);
}

static int groupSelected(const char* group)
{
return selected(UnityFixture.GroupFilter, group);
return selected(UnityFixture.GroupFilter, UnityFixture.Group, group);
}

void UnityTestRunner(unityfunction* setup,
Expand Down Expand Up @@ -96,17 +100,20 @@ void UnityTestRunner(unityfunction* setup,
Unity.NumberOfTests++;
UnityPointer_Init();

UNITY_EXEC_TIME_START();
if (!UnityFixture.DryRun) {
UNITY_EXEC_TIME_START();

if (TEST_PROTECT())
{
setup();
testBody();
}
if (TEST_PROTECT())
{
teardown();
if (TEST_PROTECT())
{
setup();
testBody();
}
if (TEST_PROTECT())
{
teardown();
}
}

if (TEST_PROTECT())
{
UnityPointer_UndoAllSets();
Expand Down Expand Up @@ -183,8 +190,11 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
int i;
UnityFixture.Verbose = 0;
UnityFixture.Silent = 0;
UnityFixture.DryRun = 0;
UnityFixture.GroupFilter = 0;
UnityFixture.Group = 0;
UnityFixture.NameFilter = 0;
UnityFixture.Name = 0;
UnityFixture.RepeatCount = 1;

if (argc == 1)
Expand All @@ -207,10 +217,16 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UNITY_PRINT_EOL();
UnityPrint(" -s Silent mode: minimal output showing only test failures");
UNITY_PRINT_EOL();
UnityPrint(" -d Dry run all tests");
UNITY_PRINT_EOL();
UnityPrint(" -g NAME Only run tests in groups that contain the string NAME");
UNITY_PRINT_EOL();
UnityPrint(" -G NAME Only run tests in groups named NAME");
UNITY_PRINT_EOL();
UnityPrint(" -n NAME Only run tests whose name contains the string NAME");
UNITY_PRINT_EOL();
UnityPrint(" -N NAME Only run tests named NAME");
UNITY_PRINT_EOL();
UnityPrint(" -r NUMBER Repeatedly run all tests NUMBER times");
UNITY_PRINT_EOL();
UnityPrint(" -h, --help Display this help message");
Expand All @@ -237,6 +253,11 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UnityFixture.Silent = 1;
i++;
}
else if (strcmp(argv[i], "-d") == 0)
{
UnityFixture.DryRun = 1;
i++;
}
else if (strcmp(argv[i], "-g") == 0)
{
i++;
Expand All @@ -245,6 +266,14 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UnityFixture.GroupFilter = argv[i];
i++;
}
else if (strcmp(argv[i], "-G") == 0)
{
i++;
if (i >= argc)
return 1;
UnityFixture.Group= argv[i];
i++;
}
else if (strcmp(argv[i], "-n") == 0)
{
i++;
Expand All @@ -253,6 +282,14 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UnityFixture.NameFilter = argv[i];
i++;
}
else if (strcmp(argv[i], "-N") == 0)
{
i++;
if (i >= argc)
return 1;
UnityFixture.Name = argv[i];
i++;
}
else if (strcmp(argv[i], "-r") == 0)
{
UnityFixture.RepeatCount = 2;
Expand Down
3 changes: 3 additions & 0 deletions extras/fixture/src/unity_fixture_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ struct UNITY_FIXTURE_T
{
int Verbose;
int Silent;
int DryRun;
unsigned int RepeatCount;
const char* NameFilter;
const char* Name;
const char* GroupFilter;
const char* Group;
};
extern struct UNITY_FIXTURE_T UnityFixture;

Expand Down
45 changes: 45 additions & 0 deletions extras/fixture/test/unity_fixture_Test.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,32 @@ TEST_GROUP(UnityCommandOptions);

static int savedVerbose;
static unsigned int savedRepeat;
static int savedDryRun;
static const char* savedName;
static const char* savedGroup;
static const char* savedNameExact;
static const char* savedGroupExact;

TEST_SETUP(UnityCommandOptions)
{
savedVerbose = UnityFixture.Verbose;
savedRepeat = UnityFixture.RepeatCount;
savedDryRun = UnityFixture.DryRun;
savedName = UnityFixture.NameFilter;
savedGroup = UnityFixture.GroupFilter;
savedNameExact = UnityFixture.Name;
savedGroupExact = UnityFixture.Group;
}

TEST_TEAR_DOWN(UnityCommandOptions)
{
UnityFixture.Verbose = savedVerbose;
UnityFixture.RepeatCount= savedRepeat;
UnityFixture.DryRun = savedDryRun;
UnityFixture.NameFilter = savedName;
UnityFixture.GroupFilter = savedGroup;
UnityFixture.Name= savedNameExact;
UnityFixture.Group= savedGroup;
}


Expand All @@ -118,8 +127,11 @@ TEST(UnityCommandOptions, DefaultOptions)
{
UnityGetCommandLineOptions(1, noOptions);
TEST_ASSERT_EQUAL(0, UnityFixture.Verbose);
TEST_ASSERT_EQUAL(0, UnityFixture.DryRun);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.GroupFilter);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.NameFilter);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.Group);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.Name);
TEST_ASSERT_EQUAL(1, UnityFixture.RepeatCount);
}

Expand All @@ -134,6 +146,17 @@ TEST(UnityCommandOptions, OptionVerbose)
TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
}

static const char* dryRun[] = {
"testrunner.exe",
"-d"
};

TEST(UnityCommandOptions, OptionDryRun)
{
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, dryRun));
TEST_ASSERT_EQUAL(1, UnityFixture.DryRun);
}

static const char* group[] = {
"testrunner.exe",
"-g", "groupname"
Expand All @@ -156,6 +179,28 @@ TEST(UnityCommandOptions, OptionSelectTestByName)
STRCMP_EQUAL("testname", UnityFixture.NameFilter);
}

static const char* groupExact[] = {
"testrunner.exe",
"-G", "groupname"
};

TEST(UnityCommandOptions, OptionSelectTestByGroupExact)
{
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, groupExact));
STRCMP_EQUAL("groupname", UnityFixture.Group);
}

static const char* nameExact[] = {
"testrunner.exe",
"-N", "testname"
};

TEST(UnityCommandOptions, OptionSelectTestByNameExact)
{
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, nameExact));
STRCMP_EQUAL("testname", UnityFixture.Name);
}

static const char* repeat[] = {
"testrunner.exe",
"-r", "99"
Expand Down
3 changes: 3 additions & 0 deletions extras/fixture/test/unity_fixture_TestRunner.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ TEST_GROUP_RUNNER(UnityCommandOptions)
{
RUN_TEST_CASE(UnityCommandOptions, DefaultOptions);
RUN_TEST_CASE(UnityCommandOptions, OptionVerbose);
RUN_TEST_CASE(UnityCommandOptions, OptionDryRun);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByGroup);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByName);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByGroupExact);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByNameExact);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount);
RUN_TEST_CASE(UnityCommandOptions, MultipleOptions);
Expand Down

0 comments on commit ce122c4

Please sign in to comment.