• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2010 James Grenning and Contributed to Unity Project
2  * ==========================================
3  *  Unity Project - A Test Framework for C
4  *  Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
5  *  [Released under MIT License. Please refer to license.txt for details]
6  * ========================================== */
7 
8 #include "unity_fixture.h"
9 #include <stdlib.h>
10 #include <string.h>
11 
12 TEST_GROUP(UnityFixture);
13 
TEST_SETUP(UnityFixture)14 TEST_SETUP(UnityFixture)
15 {
16 }
17 
TEST_TEAR_DOWN(UnityFixture)18 TEST_TEAR_DOWN(UnityFixture)
19 {
20 }
21 
22 static int* pointer1 = 0;
23 static int* pointer2 = (int*)2;
24 static int* pointer3 = (int*)3;
25 static int int1;
26 static int int2;
27 static int int3;
28 static int int4;
29 
TEST(UnityFixture,PointerSetting)30 TEST(UnityFixture, PointerSetting)
31 {
32     TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
33     UT_PTR_SET(pointer1, &int1);
34     UT_PTR_SET(pointer2, &int2);
35     UT_PTR_SET(pointer3, &int3);
36     TEST_ASSERT_POINTERS_EQUAL(pointer1, &int1);
37     TEST_ASSERT_POINTERS_EQUAL(pointer2, &int2);
38     TEST_ASSERT_POINTERS_EQUAL(pointer3, &int3);
39     UT_PTR_SET(pointer1, &int4);
40     UnityPointer_UndoAllSets();
41     TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
42     TEST_ASSERT_POINTERS_EQUAL(pointer2, (int*)2);
43     TEST_ASSERT_POINTERS_EQUAL(pointer3, (int*)3);
44 }
45 
46 static char *p1;
47 static char *p2;
48 
TEST(UnityFixture,PointerSet)49 TEST(UnityFixture, PointerSet)
50 {
51     char c1;
52     char c2;
53     char newC1;
54     char newC2;
55     p1 = &c1;
56     p2 = &c2;
57 
58     UnityPointer_Init();
59     UT_PTR_SET(p1, &newC1);
60     UT_PTR_SET(p2, &newC2);
61     TEST_ASSERT_POINTERS_EQUAL(&newC1, p1);
62     TEST_ASSERT_POINTERS_EQUAL(&newC2, p2);
63     UnityPointer_UndoAllSets();
64     TEST_ASSERT_POINTERS_EQUAL(&c1, p1);
65     TEST_ASSERT_POINTERS_EQUAL(&c2, p2);
66 }
67 
TEST(UnityFixture,FreeNULLSafety)68 TEST(UnityFixture, FreeNULLSafety)
69 {
70     free(NULL);
71 }
72 
TEST(UnityFixture,ConcludeTestIncrementsFailCount)73 TEST(UnityFixture, ConcludeTestIncrementsFailCount)
74 {
75     UNITY_UINT savedFails = Unity.TestFailures;
76     UNITY_UINT savedIgnores = Unity.TestIgnores;
77     Unity.CurrentTestFailed = 1;
78     UnityConcludeFixtureTest(); /* Resets TestFailed for this test to pass */
79     Unity.CurrentTestIgnored = 1;
80     UnityConcludeFixtureTest(); /* Resets TestIgnored */
81     TEST_ASSERT_EQUAL(savedFails + 1, Unity.TestFailures);
82     TEST_ASSERT_EQUAL(savedIgnores + 1, Unity.TestIgnores);
83     Unity.TestFailures = savedFails;
84     Unity.TestIgnores = savedIgnores;
85 }
86 
87 /*------------------------------------------------------------ */
88 
89 TEST_GROUP(UnityCommandOptions);
90 
91 static int savedVerbose;
92 static unsigned int savedRepeat;
93 static const char* savedName;
94 static const char* savedGroup;
95 
TEST_SETUP(UnityCommandOptions)96 TEST_SETUP(UnityCommandOptions)
97 {
98     savedVerbose = UnityFixture.Verbose;
99     savedRepeat = UnityFixture.RepeatCount;
100     savedName = UnityFixture.NameFilter;
101     savedGroup = UnityFixture.GroupFilter;
102 }
103 
TEST_TEAR_DOWN(UnityCommandOptions)104 TEST_TEAR_DOWN(UnityCommandOptions)
105 {
106     UnityFixture.Verbose = savedVerbose;
107     UnityFixture.RepeatCount= savedRepeat;
108     UnityFixture.NameFilter = savedName;
109     UnityFixture.GroupFilter = savedGroup;
110 }
111 
112 
113 static const char* noOptions[] = {
114         "testrunner.exe"
115 };
116 
TEST(UnityCommandOptions,DefaultOptions)117 TEST(UnityCommandOptions, DefaultOptions)
118 {
119     UnityGetCommandLineOptions(1, noOptions);
120     TEST_ASSERT_EQUAL(0, UnityFixture.Verbose);
121     TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.GroupFilter);
122     TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.NameFilter);
123     TEST_ASSERT_EQUAL(1, UnityFixture.RepeatCount);
124 }
125 
126 static const char* verbose[] = {
127         "testrunner.exe",
128         "-v"
129 };
130 
TEST(UnityCommandOptions,OptionVerbose)131 TEST(UnityCommandOptions, OptionVerbose)
132 {
133     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, verbose));
134     TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
135 }
136 
137 static const char* group[] = {
138         "testrunner.exe",
139         "-g", "groupname"
140 };
141 
TEST(UnityCommandOptions,OptionSelectTestByGroup)142 TEST(UnityCommandOptions, OptionSelectTestByGroup)
143 {
144     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, group));
145     STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
146 }
147 
148 static const char* name[] = {
149         "testrunner.exe",
150         "-n", "testname"
151 };
152 
TEST(UnityCommandOptions,OptionSelectTestByName)153 TEST(UnityCommandOptions, OptionSelectTestByName)
154 {
155     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, name));
156     STRCMP_EQUAL("testname", UnityFixture.NameFilter);
157 }
158 
159 static const char* repeat[] = {
160         "testrunner.exe",
161         "-r", "99"
162 };
163 
TEST(UnityCommandOptions,OptionSelectRepeatTestsDefaultCount)164 TEST(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount)
165 {
166     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, repeat));
167     TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
168 }
169 
TEST(UnityCommandOptions,OptionSelectRepeatTestsSpecificCount)170 TEST(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount)
171 {
172     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, repeat));
173     TEST_ASSERT_EQUAL(99, UnityFixture.RepeatCount);
174 }
175 
176 static const char* multiple[] = {
177         "testrunner.exe",
178         "-v",
179         "-g", "groupname",
180         "-n", "testname",
181         "-r", "98"
182 };
183 
TEST(UnityCommandOptions,MultipleOptions)184 TEST(UnityCommandOptions, MultipleOptions)
185 {
186     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(8, multiple));
187     TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
188     STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
189     STRCMP_EQUAL("testname", UnityFixture.NameFilter);
190     TEST_ASSERT_EQUAL(98, UnityFixture.RepeatCount);
191 }
192 
193 static const char* dashRNotLast[] = {
194         "testrunner.exe",
195         "-v",
196         "-g", "gggg",
197         "-r",
198         "-n", "tttt",
199 };
200 
TEST(UnityCommandOptions,MultipleOptionsDashRNotLastAndNoValueSpecified)201 TEST(UnityCommandOptions, MultipleOptionsDashRNotLastAndNoValueSpecified)
202 {
203     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(7, dashRNotLast));
204     TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
205     STRCMP_EQUAL("gggg", UnityFixture.GroupFilter);
206     STRCMP_EQUAL("tttt", UnityFixture.NameFilter);
207     TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
208 }
209 
210 static const char* unknownCommand[] = {
211         "testrunner.exe",
212         "-v",
213         "-g", "groupname",
214         "-n", "testname",
215         "-r", "98",
216         "-z"
217 };
TEST(UnityCommandOptions,UnknownCommandIsIgnored)218 TEST(UnityCommandOptions, UnknownCommandIsIgnored)
219 {
220     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(9, unknownCommand));
221     TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
222     STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
223     STRCMP_EQUAL("testname", UnityFixture.NameFilter);
224     TEST_ASSERT_EQUAL(98, UnityFixture.RepeatCount);
225 }
226 
TEST(UnityCommandOptions,GroupOrNameFilterWithoutStringFails)227 TEST(UnityCommandOptions, GroupOrNameFilterWithoutStringFails)
228 {
229     TEST_ASSERT_EQUAL(1, UnityGetCommandLineOptions(3, unknownCommand));
230     TEST_ASSERT_EQUAL(1, UnityGetCommandLineOptions(5, unknownCommand));
231     TEST_ASSERT_EQUAL(1, UnityMain(3, unknownCommand, NULL));
232 }
233 
TEST(UnityCommandOptions,GroupFilterReallyFilters)234 TEST(UnityCommandOptions, GroupFilterReallyFilters)
235 {
236     UNITY_UINT saved = Unity.NumberOfTests;
237     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(4, unknownCommand));
238     UnityIgnoreTest(NULL, "non-matching", NULL);
239     TEST_ASSERT_EQUAL(saved, Unity.NumberOfTests);
240 }
241 
IGNORE_TEST(UnityCommandOptions,TestShouldBeIgnored)242 IGNORE_TEST(UnityCommandOptions, TestShouldBeIgnored)
243 {
244     TEST_FAIL_MESSAGE("This test should not run!");
245 }
246