• 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 "unity_internals.h"
10 #include <string.h>
11 
12 struct UNITY_FIXTURE_T UnityFixture;
13 
14 /* If you decide to use the function pointer approach.
15  * Build with -D UNITY_OUTPUT_CHAR=outputChar and include <stdio.h>
16  * int (*outputChar)(int) = putchar; */
17 
setUp(void)18 void setUp(void)    { /*does nothing*/ }
tearDown(void)19 void tearDown(void) { /*does nothing*/ }
20 
announceTestRun(unsigned int runNumber)21 static void announceTestRun(unsigned int runNumber)
22 {
23     UnityPrint("Unity test run ");
24     UnityPrintNumberUnsigned(runNumber+1);
25     UnityPrint(" of ");
26     UnityPrintNumberUnsigned(UnityFixture.RepeatCount);
27     UNITY_PRINT_EOL();
28 }
29 
UnityMain(int argc,const char * argv[],void (* runAllTests)(void))30 int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
31 {
32     int result = UnityGetCommandLineOptions(argc, argv);
33     unsigned int r;
34     if (result != 0)
35         return result;
36 
37     for (r = 0; r < UnityFixture.RepeatCount; r++)
38     {
39         UnityBegin(argv[0]);
40         announceTestRun(r);
41         runAllTests();
42         if (!UnityFixture.Verbose) UNITY_PRINT_EOL();
43         UnityEnd();
44     }
45 
46     return (int)Unity.TestFailures;
47 }
48 
selected(const char * filter,const char * name)49 static int selected(const char* filter, const char* name)
50 {
51     if (filter == 0)
52         return 1;
53     return strstr(name, filter) ? 1 : 0;
54 }
55 
testSelected(const char * test)56 static int testSelected(const char* test)
57 {
58     return selected(UnityFixture.NameFilter, test);
59 }
60 
groupSelected(const char * group)61 static int groupSelected(const char* group)
62 {
63     return selected(UnityFixture.GroupFilter, group);
64 }
65 
UnityTestRunner(unityfunction * setup,unityfunction * testBody,unityfunction * teardown,const char * printableName,const char * group,const char * name,const char * file,unsigned int line)66 void UnityTestRunner(unityfunction* setup,
67                      unityfunction* testBody,
68                      unityfunction* teardown,
69                      const char* printableName,
70                      const char* group,
71                      const char* name,
72                      const char* file,
73                      unsigned int line)
74 {
75     if (testSelected(name) && groupSelected(group))
76     {
77         Unity.TestFile = file;
78         Unity.CurrentTestName = printableName;
79         Unity.CurrentTestLineNumber = line;
80         if (UnityFixture.Verbose)
81         {
82             UnityPrint(printableName);
83         #ifndef UNITY_REPEAT_TEST_NAME
84             Unity.CurrentTestName = NULL;
85         #endif
86         }
87         else if (UnityFixture.Silent)
88         {
89             /* Do Nothing */
90         }
91         else
92         {
93             UNITY_OUTPUT_CHAR('.');
94         }
95 
96         Unity.NumberOfTests++;
97         UnityPointer_Init();
98 
99         UNITY_EXEC_TIME_START();
100 
101         if (TEST_PROTECT())
102         {
103             setup();
104             testBody();
105         }
106         if (TEST_PROTECT())
107         {
108             teardown();
109         }
110         if (TEST_PROTECT())
111         {
112             UnityPointer_UndoAllSets();
113         }
114         UnityConcludeFixtureTest();
115     }
116 }
117 
UnityIgnoreTest(const char * printableName,const char * group,const char * name)118 void UnityIgnoreTest(const char* printableName, const char* group, const char* name)
119 {
120     if (testSelected(name) && groupSelected(group))
121     {
122         Unity.NumberOfTests++;
123         Unity.TestIgnores++;
124         if (UnityFixture.Verbose)
125         {
126             UnityPrint(printableName);
127             UNITY_PRINT_EOL();
128         }
129         else if (UnityFixture.Silent)
130         {
131             /* Do Nothing */
132         }
133         else
134         {
135             UNITY_OUTPUT_CHAR('!');
136         }
137     }
138 }
139 
140 /*-------------------------------------------------------- */
141 /*Automatic pointer restoration functions */
142 struct PointerPair
143 {
144     void** pointer;
145     void* old_value;
146 };
147 
148 static struct PointerPair pointer_store[UNITY_MAX_POINTERS];
149 static int pointer_index = 0;
150 
UnityPointer_Init(void)151 void UnityPointer_Init(void)
152 {
153     pointer_index = 0;
154 }
155 
UnityPointer_Set(void ** pointer,void * newValue,UNITY_LINE_TYPE line)156 void UnityPointer_Set(void** pointer, void* newValue, UNITY_LINE_TYPE line)
157 {
158     if (pointer_index >= UNITY_MAX_POINTERS)
159     {
160         UNITY_TEST_FAIL(line, "Too many pointers set");
161     }
162     else
163     {
164         pointer_store[pointer_index].pointer = pointer;
165         pointer_store[pointer_index].old_value = *pointer;
166         *pointer = newValue;
167         pointer_index++;
168     }
169 }
170 
UnityPointer_UndoAllSets(void)171 void UnityPointer_UndoAllSets(void)
172 {
173     while (pointer_index > 0)
174     {
175         pointer_index--;
176         *(pointer_store[pointer_index].pointer) =
177             pointer_store[pointer_index].old_value;
178     }
179 }
180 
UnityGetCommandLineOptions(int argc,const char * argv[])181 int UnityGetCommandLineOptions(int argc, const char* argv[])
182 {
183     int i;
184     UnityFixture.Verbose = 0;
185     UnityFixture.Silent = 0;
186     UnityFixture.GroupFilter = 0;
187     UnityFixture.NameFilter = 0;
188     UnityFixture.RepeatCount = 1;
189 
190     if (argc == 1)
191         return 0;
192 
193     for (i = 1; i < argc; )
194     {
195         if (strcmp(argv[i], "-v") == 0)
196         {
197             UnityFixture.Verbose = 1;
198             i++;
199         }
200         else if (strcmp(argv[i], "-s") == 0)
201         {
202             UnityFixture.Silent = 1;
203             i++;
204         }
205         else if (strcmp(argv[i], "-g") == 0)
206         {
207             i++;
208             if (i >= argc)
209                 return 1;
210             UnityFixture.GroupFilter = argv[i];
211             i++;
212         }
213         else if (strcmp(argv[i], "-n") == 0)
214         {
215             i++;
216             if (i >= argc)
217                 return 1;
218             UnityFixture.NameFilter = argv[i];
219             i++;
220         }
221         else if (strcmp(argv[i], "-r") == 0)
222         {
223             UnityFixture.RepeatCount = 2;
224             i++;
225             if (i < argc)
226             {
227                 if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
228                 {
229                     unsigned int digit = 0;
230                     UnityFixture.RepeatCount = 0;
231                     while (argv[i][digit] >= '0' && argv[i][digit] <= '9')
232                     {
233                         UnityFixture.RepeatCount *= 10;
234                         UnityFixture.RepeatCount += (unsigned int)argv[i][digit++] - '0';
235                     }
236                     i++;
237                 }
238             }
239         }
240         else
241         {
242             /* ignore unknown parameter */
243             i++;
244         }
245     }
246     return 0;
247 }
248 
UnityConcludeFixtureTest(void)249 void UnityConcludeFixtureTest(void)
250 {
251     if (Unity.CurrentTestIgnored)
252     {
253         Unity.TestIgnores++;
254         UNITY_PRINT_EOL();
255     }
256     else if (!Unity.CurrentTestFailed)
257     {
258         if (UnityFixture.Verbose)
259         {
260             UnityPrint(" ");
261             UnityPrint(UnityStrPass);
262             UNITY_EXEC_TIME_STOP();
263             UNITY_PRINT_EXEC_TIME();
264             UNITY_PRINT_EOL();
265         }
266     }
267     else /* Unity.CurrentTestFailed */
268     {
269         Unity.TestFailures++;
270         UNITY_PRINT_EOL();
271     }
272 
273     Unity.CurrentTestFailed = 0;
274     Unity.CurrentTestIgnored = 0;
275 }
276