aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/LongBow/longBow_TestRunner.c
blob: 730333f4375324f5e2c20b4c6a0ce47606586aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 */
#include <config.h>

#include <stdio.h>
#include <assert.h>
#include <inttypes.h>

#include <LongBow/unit-test.h>

#include <LongBow/longBow_TestRunner.h>
#include <LongBow/longBow_Properties.h>

#include <LongBow/private/longBow_String.h>
#include <LongBow/private/longBow_Memory.h>
#include <LongBow/private/longBow_ArrayList.h>

struct LongBowTestRunner {
    const char *name; /**< The name of this LongBow test runner. */

    LongBowStatus (*testRunnerSetup)(LongBowTestRunner *); /**< The Test Runner Setup function */

    void (*testRunner)(LongBowTestRunner *); /**< The Test Case Runner function */

    LongBowStatus (*testRunnerTearDown)(LongBowTestRunner *); /**< The Test Runner TearDown function */

    LongBowArrayList *fixtures; /**< The  LongBowTestFixtures of this Test Runner */

    LongBowConfig *configuration; /**< The LongBowConfiguration for this Test Runner. */

    /**
     * The clipboard of information shared between the fixture setup,
     * the test case, and the fixture tear-down.
     */
    LongBowClipBoard *clipBoard;
};

void
longBowTestRunner_ConfigHelp(void)
{
    printf("Test Runner options:\n");
    printf("  --set <testRunnerName>/iterations=<count>  Run the named test runner <count> times\n");
}

bool
longBowTestRunner_Config(LongBowConfig *config, const char *parameter)
{
    bool result = false;
    LongBowArrayList *tokens = longBowString_Tokenise(parameter, "-=");
    if (tokens != NULL) {
        if (longBowArrayList_Length(tokens) == 2) {
            result = longBowConfig_SetProperty(config, longBowArrayList_Get(tokens, 0), longBowArrayList_Get(tokens, 1));
        }
        longBowArrayList_Destroy(&tokens);
    }
    return result;
}

const char *
longBowTestRunner_GetName(const LongBowTestRunner *testRunner)
{
    assert(testRunner != NULL);
    return testRunner->name;
}

void
longBowTestRunner_AddFixture(LongBowTestRunner *testRunner, LongBowTestFixture *testFixture)
{
    longBowArrayList_Add(testRunner->fixtures, testFixture);
}

LongBowTestRunner *
longBowTestRunner_Create(const char *name,
                         LongBowTestRunnerSetUp *setup,
                         LongBowTestRunnerRun *runner,
                         LongBowTestRunnerTearDown *tearDown)
{
    LongBowTestRunner *testRunner = longBowMemory_Allocate(sizeof(LongBowTestRunner));

    if (testRunner != NULL) {
        testRunner->name = name;
        testRunner->testRunnerSetup = setup;
        testRunner->testRunner = runner;
        testRunner->testRunnerTearDown = tearDown;
        testRunner->fixtures = longBowArrayList_Create((void (*)(void **))longBowTestFixture_Destroy);
        testRunner->clipBoard = longBowClipBoard_Create();
    }
    return testRunner;
}

void
longBowTestRunner_Destroy(LongBowTestRunner **testRunnerPtr)
{
    if (testRunnerPtr != NULL) {
        LongBowTestRunner *testRunner = *testRunnerPtr;

        longBowArrayList_Destroy(&testRunner->fixtures);
        if (testRunner->clipBoard) {
            longBowClipBoard_Destroy(&testRunner->clipBoard);
        }
        if (testRunner != NULL) {
            longBowMemory_Deallocate((void **) testRunnerPtr);
        }
    }
}

LongBowTestRunner *
longBowTestRunner_Run(LongBowTestRunner *testRunner)
{
    LongBowConfig *configuration = longBowTestRunner_GetConfiguration(testRunner);
    unsigned long iterations = longBowConfig_GetUint32(configuration, 1,
                                                       "%s/iterations", longBowTestRunner_GetName(testRunner));

    if (longBowConfig_IsTrace(configuration)) {
        longBowReportTesting_Trace("%s: setup", longBowTestRunner_GetName(testRunner));
    }
    LongBowStatus setupStatus = (*testRunner->testRunnerSetup)(testRunner);

    if (setupStatus != LONGBOW_STATUS_SETUP_SKIPTESTS) {
        if (!longBowStatus_IsSuccessful(setupStatus)) {
            char *statusString = longBowStatus_ToString(setupStatus);
            printf("Warning: %s setup returned: %s.\n", testRunner->name, statusString);
            free(statusString);
            return testRunner;
        }

        for (unsigned long i = 0; i < iterations; i++) {
            if (longBowConfig_IsTrace(configuration)) {
                longBowReportTesting_Trace("%s: run", longBowTestRunner_GetName(testRunner));
            }
            (*testRunner->testRunner)(testRunner);
        }

        if (longBowConfig_IsTrace(configuration)) {
            longBowReportTesting_Trace("%s: tear-down", longBowTestRunner_GetName(testRunner));
        }

        LongBowStatus tearDownStatus = (*testRunner->testRunnerTearDown)(testRunner);
        if (!longBowStatus_IsSuccessful(tearDownStatus)) {
            char *statusString = longBowStatus_ToString(tearDownStatus);
            printf("Warning: %s tear-down returned: %s.\n", testRunner->name, statusString);
            free(statusString);
            return testRunner;
        }
    }

    return testRunner;
}

char *
longBowTestRunner_ToString(const LongBowTestRunner *runner)
{
    LongBowString *string = longBowString_CreateFormat("%s", longBowTestRunner_GetName(runner));

    char *cString = longBowString_ToString(string);
    longBowString_Destroy(&string);
    return cString;
}

LongBowTestFixture *
longBowTestRunner_GetFixture(const LongBowTestRunner *testRunner, size_t index)
{
    return longBowArrayList_Get(testRunner->fixtures, index);
}

size_t
longBowTestRunner_GetFixtureCount(const LongBowTestRunner *testRunner)
{
    return longBowArrayList_Length(testRunner->fixtures);
}

LongBowConfig *
longBowTestRunner_GetConfiguration(const LongBowTestRunner *testRunner)
{
    return testRunner->configuration;
}

LongBowStatus
longBowTestRunner_GetStatus(const LongBowTestRunner *testRunner)
{
    LongBowStatus result = LONGBOW_STATUS_SUCCEEDED;

    // Just return the status of the first non-successful Test Fixture.
    size_t nTestFixtures = longBowTestRunner_GetFixtureCount(testRunner);
    for (size_t i = 0; i < nTestFixtures; i++) {
        LongBowTestFixture *fixture = longBowTestRunner_GetFixture(testRunner, i);
        if (!longBowTestFixture_IsSuccessful(fixture)) {
            result = longBowTestFixture_GetStatus(fixture);
            break;
        }
    }
    return result;
}

bool
longBowTestRunner_IsSuccessful(const LongBowTestRunner *testRunner)
{
    return longBowStatus_IsSuccessful(longBowTestRunner_GetStatus(testRunner));
}

bool
longBowTestRunner_IsFailed(const LongBowTestCase *testCase)
{
    return longBowStatus_IsFailed(longBowTestCase_GetStatus(testCase));
}

bool
longBowTestRunner_IsWarning(const LongBowTestRunner *testCase)
{
    return longBowStatus_IsWarning(longBowTestRunner_GetStatus(testCase));
}

bool
longBowTestRunner_IsIncomplete(const LongBowTestCase *testCase)
{
    return longBowStatus_IsIncomplete(longBowTestCase_GetStatus(testCase));
}

void
longBowTestRunner_SetConfiguration(LongBowTestRunner *testRunner, LongBowConfig *config)
{
    testRunner->configuration = config;
}

LongBowClipBoard *
longBowTestRunner_GetClipBoard(const LongBowTestRunner *testRunner)
{
    return testRunner->clipBoard;
}

bool
longBowTestRunner_SetClipBoardData(const LongBowTestRunner *testRunner, void *shared)
{
    return longBowClipBoard_Set(longBowTestRunner_GetClipBoard(testRunner), "testRunner", shared);
}

void *
longBowTestRunner_GetClipBoardData(const LongBowTestRunner *testRunner)
{
    return longBowClipBoard_Get(longBowTestRunner_GetClipBoard(testRunner), "testRunner");
}