unity_fixture.c 6.6 KB
Newer Older
J
jsalling 已提交
1 2 3 4 5 6
/* Copyright (c) 2010 James Grenning and Contributed to Unity Project
 * ==========================================
 *  Unity Project - A Test Framework for C
 *  Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
 *  [Released under MIT License. Please refer to license.txt for details]
 * ========================================== */
7 8 9

#include "unity_fixture.h"
#include "unity_internals.h"
10
#include <string.h>
11

12
struct UNITY_FIXTURE_T UnityFixture;
13

J
jsalling 已提交
14 15 16
/* If you decide to use the function pointer approach.
 * Build with -D UNITY_OUTPUT_CHAR=outputChar and include <stdio.h>
 * int (*outputChar)(int) = putchar; */
17 18 19 20

void setUp(void)    { /*does nothing*/ }
void tearDown(void) { /*does nothing*/ }

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
}

30
int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
31 32
{
    int result = UnityGetCommandLineOptions(argc, argv);
33
    unsigned int r;
34 35 36 37 38
    if (result != 0)
        return result;

    for (r = 0; r < UnityFixture.RepeatCount; r++)
    {
39
        UnityBegin(argv[0]);
40
        announceTestRun(r);
41
        runAllTests();
42
        if (!UnityFixture.Verbose) UNITY_PRINT_EOL();
43 44 45
        UnityEnd();
    }

46
    return (int)Unity.TestFailures;
47 48
}

49
static int selected(const char* filter, const char* name)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
    if (filter == 0)
        return 1;
    return strstr(name, filter) ? 1 : 0;
}

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

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

void UnityTestRunner(unityfunction* setup,
67 68 69 70 71
                     unityfunction* testBody,
                     unityfunction* teardown,
                     const char* printableName,
                     const char* group,
                     const char* name,
72 73
                     const char* file,
                     unsigned int line)
74 75 76 77 78 79
{
    if (testSelected(name) && groupSelected(group))
    {
        Unity.TestFile = file;
        Unity.CurrentTestName = printableName;
        Unity.CurrentTestLineNumber = line;
D
Dom Postorivo 已提交
80
        if (UnityFixture.Verbose)
81
        {
82
            UnityPrint(printableName);
83 84 85 86
        #ifndef UNITY_REPEAT_TEST_NAME
            Unity.CurrentTestName = NULL;
        #endif
        }
D
Dom Postorivo 已提交
87 88 89 90 91 92 93 94
        else if (UnityFixture.Silent)
        {
            /* Do Nothing */
        }
        else
        {
            UNITY_OUTPUT_CHAR('.');
        }
95 96 97 98

        Unity.NumberOfTests++;
        UnityPointer_Init();

99 100
        UNITY_EXEC_TIME_START();

101 102 103 104 105 106 107 108 109 110 111 112 113
        if (TEST_PROTECT())
        {
            setup();
            testBody();
        }
        if (TEST_PROTECT())
        {
            teardown();
        }
        if (TEST_PROTECT())
        {
            UnityPointer_UndoAllSets();
        }
114
        UnityConcludeFixtureTest();
115 116 117
    }
}

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++;
D
Dom Postorivo 已提交
124
        if (UnityFixture.Verbose)
125
        {
126
            UnityPrint(printableName);
127 128
            UNITY_PRINT_EOL();
        }
D
Dom Postorivo 已提交
129 130 131 132 133 134 135 136
        else if (UnityFixture.Silent)
        {
            /* Do Nothing */
        }
        else
        {
            UNITY_OUTPUT_CHAR('!');
        }
137
    }
138 139
}

J
jsalling 已提交
140 141
/*-------------------------------------------------------- */
/*Automatic pointer restoration functions */
142
struct PointerPair
143
{
144 145
    void** pointer;
    void* old_value;
146
};
147

148
static struct PointerPair pointer_store[UNITY_MAX_POINTERS];
149 150
static int pointer_index = 0;

151
void UnityPointer_Init(void)
152 153 154 155
{
    pointer_index = 0;
}

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 163 164 165 166 167 168
    }
    else
    {
        pointer_store[pointer_index].pointer = pointer;
        pointer_store[pointer_index].old_value = *pointer;
        *pointer = newValue;
        pointer_index++;
    }
169 170
}

171
void UnityPointer_UndoAllSets(void)
172 173 174 175 176
{
    while (pointer_index > 0)
    {
        pointer_index--;
        *(pointer_store[pointer_index].pointer) =
177
            pointer_store[pointer_index].old_value;
178 179 180
    }
}

181
int UnityGetCommandLineOptions(int argc, const char* argv[])
182 183 184
{
    int i;
    UnityFixture.Verbose = 0;
D
Dom Postorivo 已提交
185
    UnityFixture.Silent = 0;
186 187 188 189 190 191 192 193 194 195 196 197 198 199
    UnityFixture.GroupFilter = 0;
    UnityFixture.NameFilter = 0;
    UnityFixture.RepeatCount = 1;

    if (argc == 1)
        return 0;

    for (i = 1; i < argc; )
    {
        if (strcmp(argv[i], "-v") == 0)
        {
            UnityFixture.Verbose = 1;
            i++;
        }
D
Dom Postorivo 已提交
200 201 202 203 204
        else if (strcmp(argv[i], "-s") == 0)
        {
            UnityFixture.Silent = 1;
            i++;
        }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        else if (strcmp(argv[i], "-g") == 0)
        {
            i++;
            if (i >= argc)
                return 1;
            UnityFixture.GroupFilter = argv[i];
            i++;
        }
        else if (strcmp(argv[i], "-n") == 0)
        {
            i++;
            if (i >= argc)
                return 1;
            UnityFixture.NameFilter = argv[i];
            i++;
        }
        else if (strcmp(argv[i], "-r") == 0)
        {
            UnityFixture.RepeatCount = 2;
            i++;
            if (i < argc)
            {
                if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
                {
229 230 231 232 233 234 235
                    unsigned int digit = 0;
                    UnityFixture.RepeatCount = 0;
                    while (argv[i][digit] >= '0' && argv[i][digit] <= '9')
                    {
                        UnityFixture.RepeatCount *= 10;
                        UnityFixture.RepeatCount += (unsigned int)argv[i][digit++] - '0';
                    }
236 237 238
                    i++;
                }
            }
239 240 241
        }
        else
        {
J
jsalling 已提交
242
            /* ignore unknown parameter */
243
            i++;
244 245 246 247 248
        }
    }
    return 0;
}

249
void UnityConcludeFixtureTest(void)
250 251 252 253
{
    if (Unity.CurrentTestIgnored)
    {
        Unity.TestIgnores++;
254
        UNITY_PRINT_EOL();
255 256 257 258 259
    }
    else if (!Unity.CurrentTestFailed)
    {
        if (UnityFixture.Verbose)
        {
260 261
            UnityPrint(" ");
            UnityPrint(UnityStrPass);
262 263
            UNITY_EXEC_TIME_STOP();
            UNITY_PRINT_EXEC_TIME();
264
            UNITY_PRINT_EOL();
265 266
        }
    }
J
jsalling 已提交
267
    else /* Unity.CurrentTestFailed */
268 269
    {
        Unity.TestFailures++;
270
        UNITY_PRINT_EOL();
271 272 273 274 275
    }

    Unity.CurrentTestFailed = 0;
    Unity.CurrentTestIgnored = 0;
}