Skip to content

Commit ce122c4

Browse files
authored
Merge pull request #762 from koy-rehme-bae/fixtures_command_line_arguments
New command line options for fixtures
2 parents cbcd08f + df0b5d9 commit ce122c4

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

extras/fixture/src/unity_fixture.c

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,25 @@ int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
4646
return (int)Unity.TestFailures;
4747
}
4848

49-
static int selected(const char* filter, const char* name)
49+
static int selected(const char* filter, const char* select, const char* name)
5050
{
51-
if (filter == 0)
51+
if (filter == 0 && select == 0)
5252
return 1;
53-
return strstr(name, filter) ? 1 : 0;
53+
if (filter && strstr(name, filter))
54+
return 1;
55+
if (select && strcmp(name, select) == 0)
56+
return 1;
57+
return 0;
5458
}
5559

5660
static int testSelected(const char* test)
5761
{
58-
return selected(UnityFixture.NameFilter, test);
62+
return selected(UnityFixture.NameFilter, UnityFixture.Name, test);
5963
}
6064

6165
static int groupSelected(const char* group)
6266
{
63-
return selected(UnityFixture.GroupFilter, group);
67+
return selected(UnityFixture.GroupFilter, UnityFixture.Group, group);
6468
}
6569

6670
void UnityTestRunner(unityfunction* setup,
@@ -96,17 +100,20 @@ void UnityTestRunner(unityfunction* setup,
96100
Unity.NumberOfTests++;
97101
UnityPointer_Init();
98102

99-
UNITY_EXEC_TIME_START();
103+
if (!UnityFixture.DryRun) {
104+
UNITY_EXEC_TIME_START();
100105

101-
if (TEST_PROTECT())
102-
{
103-
setup();
104-
testBody();
105-
}
106-
if (TEST_PROTECT())
107-
{
108-
teardown();
106+
if (TEST_PROTECT())
107+
{
108+
setup();
109+
testBody();
110+
}
111+
if (TEST_PROTECT())
112+
{
113+
teardown();
114+
}
109115
}
116+
110117
if (TEST_PROTECT())
111118
{
112119
UnityPointer_UndoAllSets();
@@ -183,8 +190,11 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
183190
int i;
184191
UnityFixture.Verbose = 0;
185192
UnityFixture.Silent = 0;
193+
UnityFixture.DryRun = 0;
186194
UnityFixture.GroupFilter = 0;
195+
UnityFixture.Group = 0;
187196
UnityFixture.NameFilter = 0;
197+
UnityFixture.Name = 0;
188198
UnityFixture.RepeatCount = 1;
189199

190200
if (argc == 1)
@@ -207,10 +217,16 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
207217
UNITY_PRINT_EOL();
208218
UnityPrint(" -s Silent mode: minimal output showing only test failures");
209219
UNITY_PRINT_EOL();
220+
UnityPrint(" -d Dry run all tests");
221+
UNITY_PRINT_EOL();
210222
UnityPrint(" -g NAME Only run tests in groups that contain the string NAME");
211223
UNITY_PRINT_EOL();
224+
UnityPrint(" -G NAME Only run tests in groups named NAME");
225+
UNITY_PRINT_EOL();
212226
UnityPrint(" -n NAME Only run tests whose name contains the string NAME");
213227
UNITY_PRINT_EOL();
228+
UnityPrint(" -N NAME Only run tests named NAME");
229+
UNITY_PRINT_EOL();
214230
UnityPrint(" -r NUMBER Repeatedly run all tests NUMBER times");
215231
UNITY_PRINT_EOL();
216232
UnityPrint(" -h, --help Display this help message");
@@ -237,6 +253,11 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
237253
UnityFixture.Silent = 1;
238254
i++;
239255
}
256+
else if (strcmp(argv[i], "-d") == 0)
257+
{
258+
UnityFixture.DryRun = 1;
259+
i++;
260+
}
240261
else if (strcmp(argv[i], "-g") == 0)
241262
{
242263
i++;
@@ -245,6 +266,14 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
245266
UnityFixture.GroupFilter = argv[i];
246267
i++;
247268
}
269+
else if (strcmp(argv[i], "-G") == 0)
270+
{
271+
i++;
272+
if (i >= argc)
273+
return 1;
274+
UnityFixture.Group= argv[i];
275+
i++;
276+
}
248277
else if (strcmp(argv[i], "-n") == 0)
249278
{
250279
i++;
@@ -253,6 +282,14 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
253282
UnityFixture.NameFilter = argv[i];
254283
i++;
255284
}
285+
else if (strcmp(argv[i], "-N") == 0)
286+
{
287+
i++;
288+
if (i >= argc)
289+
return 1;
290+
UnityFixture.Name = argv[i];
291+
i++;
292+
}
256293
else if (strcmp(argv[i], "-r") == 0)
257294
{
258295
UnityFixture.RepeatCount = 2;

extras/fixture/src/unity_fixture_internals.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ struct UNITY_FIXTURE_T
1717
{
1818
int Verbose;
1919
int Silent;
20+
int DryRun;
2021
unsigned int RepeatCount;
2122
const char* NameFilter;
23+
const char* Name;
2224
const char* GroupFilter;
25+
const char* Group;
2326
};
2427
extern struct UNITY_FIXTURE_T UnityFixture;
2528

extras/fixture/test/unity_fixture_Test.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,32 @@ TEST_GROUP(UnityCommandOptions);
9090

9191
static int savedVerbose;
9292
static unsigned int savedRepeat;
93+
static int savedDryRun;
9394
static const char* savedName;
9495
static const char* savedGroup;
96+
static const char* savedNameExact;
97+
static const char* savedGroupExact;
9598

9699
TEST_SETUP(UnityCommandOptions)
97100
{
98101
savedVerbose = UnityFixture.Verbose;
99102
savedRepeat = UnityFixture.RepeatCount;
103+
savedDryRun = UnityFixture.DryRun;
100104
savedName = UnityFixture.NameFilter;
101105
savedGroup = UnityFixture.GroupFilter;
106+
savedNameExact = UnityFixture.Name;
107+
savedGroupExact = UnityFixture.Group;
102108
}
103109

104110
TEST_TEAR_DOWN(UnityCommandOptions)
105111
{
106112
UnityFixture.Verbose = savedVerbose;
107113
UnityFixture.RepeatCount= savedRepeat;
114+
UnityFixture.DryRun = savedDryRun;
108115
UnityFixture.NameFilter = savedName;
109116
UnityFixture.GroupFilter = savedGroup;
117+
UnityFixture.Name= savedNameExact;
118+
UnityFixture.Group= savedGroup;
110119
}
111120

112121

@@ -118,8 +127,11 @@ TEST(UnityCommandOptions, DefaultOptions)
118127
{
119128
UnityGetCommandLineOptions(1, noOptions);
120129
TEST_ASSERT_EQUAL(0, UnityFixture.Verbose);
130+
TEST_ASSERT_EQUAL(0, UnityFixture.DryRun);
121131
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.GroupFilter);
122132
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.NameFilter);
133+
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.Group);
134+
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.Name);
123135
TEST_ASSERT_EQUAL(1, UnityFixture.RepeatCount);
124136
}
125137

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

149+
static const char* dryRun[] = {
150+
"testrunner.exe",
151+
"-d"
152+
};
153+
154+
TEST(UnityCommandOptions, OptionDryRun)
155+
{
156+
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, dryRun));
157+
TEST_ASSERT_EQUAL(1, UnityFixture.DryRun);
158+
}
159+
137160
static const char* group[] = {
138161
"testrunner.exe",
139162
"-g", "groupname"
@@ -156,6 +179,28 @@ TEST(UnityCommandOptions, OptionSelectTestByName)
156179
STRCMP_EQUAL("testname", UnityFixture.NameFilter);
157180
}
158181

182+
static const char* groupExact[] = {
183+
"testrunner.exe",
184+
"-G", "groupname"
185+
};
186+
187+
TEST(UnityCommandOptions, OptionSelectTestByGroupExact)
188+
{
189+
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, groupExact));
190+
STRCMP_EQUAL("groupname", UnityFixture.Group);
191+
}
192+
193+
static const char* nameExact[] = {
194+
"testrunner.exe",
195+
"-N", "testname"
196+
};
197+
198+
TEST(UnityCommandOptions, OptionSelectTestByNameExact)
199+
{
200+
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, nameExact));
201+
STRCMP_EQUAL("testname", UnityFixture.Name);
202+
}
203+
159204
static const char* repeat[] = {
160205
"testrunner.exe",
161206
"-r", "99"

extras/fixture/test/unity_fixture_TestRunner.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ TEST_GROUP_RUNNER(UnityCommandOptions)
1919
{
2020
RUN_TEST_CASE(UnityCommandOptions, DefaultOptions);
2121
RUN_TEST_CASE(UnityCommandOptions, OptionVerbose);
22+
RUN_TEST_CASE(UnityCommandOptions, OptionDryRun);
2223
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByGroup);
2324
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByName);
25+
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByGroupExact);
26+
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByNameExact);
2427
RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount);
2528
RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount);
2629
RUN_TEST_CASE(UnityCommandOptions, MultipleOptions);

0 commit comments

Comments
 (0)