• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ==========================================
2     Unity Project - A Test Framework for C
3     Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4     [Released under MIT License. Please refer to license.txt for details]
5 ========================================== */
6 
7 #include "unity.h"
8 #define TEST_INSTANCES
9 #include "self_assessment_utils.h"
10 
11 static int SetToOneToFailInTearDown;
12 static int SetToOneMeanWeAlreadyCheckedThisGuy;
13 
setUp(void)14 void setUp(void)
15 {
16     SetToOneToFailInTearDown = 0;
17     SetToOneMeanWeAlreadyCheckedThisGuy = 0;
18 }
19 
tearDown(void)20 void tearDown(void)
21 {
22     endPutcharSpy(); /* Stop suppressing test output */
23     if (SetToOneToFailInTearDown == 1)
24     {
25         /* These will be skipped internally if already failed/ignored */
26         TEST_FAIL_MESSAGE("<= Failed in tearDown");
27         TEST_IGNORE_MESSAGE("<= Ignored in tearDown");
28     }
29     if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0))
30     {
31         UnityPrint(": [[[[ Test Should Have Passed But Did Not ]]]]");
32         UNITY_OUTPUT_CHAR('\n');
33     }
34 }
35 
testUnitySizeInitializationReminder(void)36 void testUnitySizeInitializationReminder(void)
37 {
38     /* This test ensures that sizeof(struct UNITY_STORAGE_T) doesn't change. If this
39      * test breaks, go look at the initialization of the Unity global variable
40      * in unity.c and make sure we're filling in the proper fields. */
41     const char* message = "Unexpected size for UNITY_STORAGE_T struct. Please check that "
42                      "the initialization of the Unity symbol in unity.c is "
43                      "still correct.";
44 
45     /* Define a structure with all the same fields as `struct UNITY_STORAGE_T`. */
46 #ifdef UNITY_EXCLUDE_DETAILS
47     struct {
48         const char* TestFile;
49         const char* CurrentTestName;
50         UNITY_LINE_TYPE CurrentTestLineNumber;
51         UNITY_COUNTER_TYPE NumberOfTests;
52         UNITY_COUNTER_TYPE TestFailures;
53         UNITY_COUNTER_TYPE TestIgnores;
54         UNITY_COUNTER_TYPE CurrentTestFailed;
55         UNITY_COUNTER_TYPE CurrentTestIgnored;
56 #ifdef UNITY_INCLUDE_EXEC_TIME
57         UNITY_TIME_TYPE CurrentTestStartTime;
58         UNITY_TIME_TYPE CurrentTestStopTime;
59 #endif
60 #ifndef UNITY_EXCLUDE_SETJMP_H
61         jmp_buf AbortFrame;
62 #endif
63     } _Expected_Unity;
64 #else
65     struct {
66         const char* TestFile;
67         const char* CurrentTestName;
68         const char* CurrentDetails1;
69         const char* CurrentDetails2;
70         UNITY_LINE_TYPE CurrentTestLineNumber;
71         UNITY_COUNTER_TYPE NumberOfTests;
72         UNITY_COUNTER_TYPE TestFailures;
73         UNITY_COUNTER_TYPE TestIgnores;
74         UNITY_COUNTER_TYPE CurrentTestFailed;
75         UNITY_COUNTER_TYPE CurrentTestIgnored;
76 #ifdef UNITY_INCLUDE_EXEC_TIME
77         UNITY_COUNTER_TYPE CurrentTestStartTime;
78         UNITY_COUNTER_TYPE CurrentTestStopTime;
79 #endif
80 #ifndef UNITY_EXCLUDE_SETJMP_H
81         jmp_buf AbortFrame;
82 #endif
83     } _Expected_Unity;
84 #endif
85 
86     /* Compare our fake structure's size to the actual structure's size. They
87      * should be the same.
88      *
89      * This accounts for alignment, padding, and packing issues that might come
90      * up between different architectures. */
91     TEST_ASSERT_EQUAL_MESSAGE(sizeof(_Expected_Unity), sizeof(Unity), message);
92 }
93 
testPassShouldEndImmediatelyWithPass(void)94 void testPassShouldEndImmediatelyWithPass(void)
95 {
96     TEST_PASS();
97     TEST_FAIL_MESSAGE("We should have passed already and finished this test");
98 }
99 
testPassShouldEndImmediatelyWithPassAndMessage(void)100 void testPassShouldEndImmediatelyWithPassAndMessage(void)
101 {
102     TEST_PASS_MESSAGE("Woohoo! This Automatically Passes!");
103     TEST_FAIL_MESSAGE("We should have passed already and finished this test");
104 }
105 
testMessageShouldDisplayMessageWithoutEndingAndGoOnToPass(void)106 void testMessageShouldDisplayMessageWithoutEndingAndGoOnToPass(void)
107 {
108     TEST_MESSAGE("This is just a message");
109     TEST_MESSAGE("This is another message");
110     TEST_PASS();
111 }
112 
testMessageShouldDisplayMessageWithoutEndingAndGoOnToFail(void)113 void testMessageShouldDisplayMessageWithoutEndingAndGoOnToFail(void)
114 {
115     TEST_MESSAGE("This is yet another message");
116 
117     EXPECT_ABORT_BEGIN
118     TEST_FAIL();
119     VERIFY_FAILS_END
120 }
121 
testTrue(void)122 void testTrue(void)
123 {
124     TEST_ASSERT(1);
125 
126     TEST_ASSERT_TRUE(1);
127 }
128 
testFalse(void)129 void testFalse(void)
130 {
131     TEST_ASSERT_FALSE(0);
132 
133     TEST_ASSERT_UNLESS(0);
134 }
135 
testSingleStatement(void)136 void testSingleStatement(void)
137 {
138     for(int i = 0; i < 2; i++)
139     {
140         /* TEST_ASSERT_TRUE should expand to a single C statement, minus
141          * the semicolon. This if-else will fail to compile otherwise. */
142         if(i > 0)
143             TEST_ASSERT_TRUE(i);
144         else
145             TEST_ASSERT_FALSE(i);
146     }
147 }
148 
testPreviousPass(void)149 void testPreviousPass(void)
150 {
151     TEST_ASSERT_EQUAL_INT(0U, Unity.TestFailures);
152 }
153 
testNotVanilla(void)154 void testNotVanilla(void)
155 {
156     EXPECT_ABORT_BEGIN
157     TEST_ASSERT(0);
158     VERIFY_FAILS_END
159 }
160 
testNotTrue(void)161 void testNotTrue(void)
162 {
163     EXPECT_ABORT_BEGIN
164     TEST_ASSERT_TRUE(0);
165     VERIFY_FAILS_END
166 }
167 
testNotFalse(void)168 void testNotFalse(void)
169 {
170     EXPECT_ABORT_BEGIN
171     TEST_ASSERT_FALSE(1);
172     VERIFY_FAILS_END
173 }
174 
testNotUnless(void)175 void testNotUnless(void)
176 {
177     EXPECT_ABORT_BEGIN
178     TEST_ASSERT_UNLESS(1);
179     VERIFY_FAILS_END
180 }
181 
testNotNotEqual(void)182 void testNotNotEqual(void)
183 {
184     EXPECT_ABORT_BEGIN
185     TEST_ASSERT_NOT_EQUAL(10, 10);
186     VERIFY_FAILS_END
187 }
188 
testFail(void)189 void testFail(void)
190 {
191     EXPECT_ABORT_BEGIN
192     TEST_FAIL_MESSAGE("Expected for testing");
193     VERIFY_FAILS_END
194 }
195 
testIsNull(void)196 void testIsNull(void)
197 {
198     char* ptr1 = NULL;
199     const char* ptr2 = "hello";
200 
201     TEST_ASSERT_NULL(ptr1);
202     TEST_ASSERT_NOT_NULL(ptr2);
203 }
204 
testIsNullShouldFailIfNot(void)205 void testIsNullShouldFailIfNot(void)
206 {
207     const char* ptr1 = "hello";
208 
209     EXPECT_ABORT_BEGIN
210     TEST_ASSERT_NULL(ptr1);
211     VERIFY_FAILS_END
212 }
213 
testNotNullShouldFailIfNULL(void)214 void testNotNullShouldFailIfNULL(void)
215 {
216     char* ptr1 = NULL;
217 
218     EXPECT_ABORT_BEGIN
219     TEST_ASSERT_NOT_NULL(ptr1);
220     VERIFY_FAILS_END
221 }
222 
testIsEmpty(void)223 void testIsEmpty(void)
224 {
225     const char* ptr1 = "\0";
226     const char* ptr2 = "hello";
227 
228     TEST_ASSERT_EMPTY(ptr1);
229     TEST_ASSERT_NOT_EMPTY(ptr2);
230 }
231 
testIsEmptyShouldFailIfNot(void)232 void testIsEmptyShouldFailIfNot(void)
233 {
234     const char* ptr1 = "hello";
235 
236     EXPECT_ABORT_BEGIN
237     TEST_ASSERT_EMPTY(ptr1);
238     VERIFY_FAILS_END
239 }
240 
testNotEmptyShouldFailIfEmpty(void)241 void testNotEmptyShouldFailIfEmpty(void)
242 {
243     const char* ptr1 = "\0";
244 
245     EXPECT_ABORT_BEGIN
246     TEST_ASSERT_NOT_EMPTY(ptr1);
247     VERIFY_FAILS_END
248 }
249 
testIgnore(void)250 void testIgnore(void)
251 {
252     EXPECT_ABORT_BEGIN
253     TEST_IGNORE();
254     TEST_FAIL_MESSAGE("This should not be reached");
255     VERIFY_IGNORES_END
256 }
257 
testIgnoreMessage(void)258 void testIgnoreMessage(void)
259 {
260     EXPECT_ABORT_BEGIN
261     TEST_IGNORE_MESSAGE("This is an expected TEST_IGNORE_MESSAGE string!");
262     TEST_FAIL_MESSAGE("This should not be reached");
263     VERIFY_IGNORES_END
264 }
265 
testProtection(void)266 void testProtection(void)
267 {
268     volatile int mask = 0;
269 
270     if (TEST_PROTECT())
271     {
272         mask |= 1;
273         TEST_ABORT();
274     }
275     else
276     {
277         Unity.CurrentTestFailed = 0;
278         mask |= 2;
279     }
280 
281     TEST_ASSERT_EQUAL(3, mask);
282 }
283 
testIgnoredAndThenFailInTearDown(void)284 void testIgnoredAndThenFailInTearDown(void)
285 {
286     SetToOneToFailInTearDown = 1;
287     TEST_IGNORE();
288 }
289 
testFailureCountIncrementsAndIsReturnedAtEnd(void)290 void testFailureCountIncrementsAndIsReturnedAtEnd(void)
291 {
292 #ifndef USING_OUTPUT_SPY
293     TEST_IGNORE();
294 #else
295     UNITY_UINT savedFailures = Unity.TestFailures;
296     Unity.CurrentTestFailed = 1;
297     startPutcharSpy(); /* Suppress output */
298     startFlushSpy();
299     TEST_ASSERT_EQUAL(0, getFlushSpyCalls());
300     UnityConcludeTest();
301     endPutcharSpy();
302     TEST_ASSERT_EQUAL(savedFailures + 1, Unity.TestFailures);
303 #if defined(UNITY_OUTPUT_FLUSH) && defined(UNITY_OUTPUT_FLUSH_HEADER_DECLARATION)
304     TEST_ASSERT_EQUAL(1, getFlushSpyCalls());
305 #else
306     TEST_ASSERT_EQUAL(0, getFlushSpyCalls());
307 #endif
308     endFlushSpy();
309 
310     startPutcharSpy(); /* Suppress output */
311     int failures = UnityEnd();
312     Unity.TestFailures--;
313     endPutcharSpy();
314     TEST_ASSERT_EQUAL(savedFailures + 1, failures);
315 #endif
316 }
317 
318 /* ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DETAIL SUPPORT ================== */
319 
testThatDetailsCanBeHandleOneDetail(void)320 void testThatDetailsCanBeHandleOneDetail(void)
321 {
322 #ifdef UNITY_EXCLUDE_DETAILS
323     TEST_IGNORE();
324 #else
325     UNITY_SET_DETAIL("Detail1");
326 
327     EXPECT_ABORT_BEGIN
328     TEST_ASSERT_EQUAL_INT_MESSAGE(5, 6, "Should Fail And Say Detail1");
329     VERIFY_FAILS_END
330 #endif
331 }
332 
testThatDetailsCanHandleTestFail(void)333 void testThatDetailsCanHandleTestFail(void)
334 {
335 #ifdef UNITY_EXCLUDE_DETAILS
336     TEST_IGNORE();
337 #else
338     UNITY_SET_DETAILS("Detail1","Detail2");
339 
340     EXPECT_ABORT_BEGIN
341     TEST_FAIL_MESSAGE("Should Fail And Say Detail1 and Detail2");
342     VERIFY_FAILS_END
343 #endif
344 }
345 
testThatDetailsCanBeHandleTwoDetails(void)346 void testThatDetailsCanBeHandleTwoDetails(void)
347 {
348 #ifdef UNITY_EXCLUDE_DETAILS
349     TEST_IGNORE();
350 #else
351     UNITY_SET_DETAILS("Detail1","Detail2");
352 
353     EXPECT_ABORT_BEGIN
354     TEST_ASSERT_EQUAL_HEX8_MESSAGE(7, 8, "Should Fail And Say Detail1 and Detail2");
355     VERIFY_FAILS_END
356 #endif
357 }
358 
testThatDetailsCanBeHandleSingleDetailClearingTwoDetails(void)359 void testThatDetailsCanBeHandleSingleDetailClearingTwoDetails(void)
360 {
361 #ifdef UNITY_EXCLUDE_DETAILS
362     TEST_IGNORE();
363 #else
364     UNITY_SET_DETAILS("Detail1","Detail2");
365     UNITY_SET_DETAIL("DetailNew");
366 
367     EXPECT_ABORT_BEGIN
368     TEST_ASSERT_EQUAL_STRING_MESSAGE("MEH", "GUH", "Should Fail And Say DetailNew");
369     VERIFY_FAILS_END
370 #endif
371 }
372