aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/LongBow/longBow_TestFixture.c
blob: d4efc86f98a42ff9c7f7f18f0cb1c5de56176a3a (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * 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 <LongBow/private/longBow_ArrayList.h>
#include <LongBow/private/longBow_Memory.h>
#include <LongBow/private/longBow_String.h>

#include <LongBow/longBow_TestFixture.h>
#include <LongBow/longBow_TestFixtureConfig.h>
#include <LongBow/longBow_TestCase.h>
#include <LongBow/longBow_RuntimeResult.h>

#include <LongBow/unit-test.h>

/**
 * @struct longbow_fixture
 * @brief The LongBow Test Fixture.
 *
 * This structure is created and initialized by the LongBow Test Runner.
 * When the Test Fixture represented by this structure is executed,
 * this structure is updated with the results of each LongBow Test Case that belongs to this Fixture.
 */
struct longbow_fixture {
    /**
     * The name of this test fixture.
     */
    const char *name;

    /**
     * The fully qualified name of this test fixture.
     */
    char *fullName;

    /**
     *
     */
    LongBowTestFixtureConfig *config;

    /**
     * The Test Runner of this test fixture.
     */
    LongBowTestRunner *runner;

    /**
     * The `LongBowTestFixtureSummary` of this test fixture.
     */
    LongBowTestFixtureSummary summary;

    /**
     * A list of `LongBowTestCase` structures, one for each executed test case.
     */
    LongBowArrayList *testCases;

    /**
     * The function to call to setup the Test Fixture.
     *
     * This function is called before each Test Case is run.
     * @param testCase The LongBow Test Case that is being set-up.
     */
    LongBowTestFixtureSetupFunction *setUp;

    /**
     * The function to call to execute the Test Fixture.
     *
     * @param testFixture A pointer to this `struct longbow_fixture` structure.
     */
    LongBowTestFixtureFunction *fixture;

    /**
     * The function to call to tear-down the Test Fixture.
     *
     * This function is called after each Test Case is run.
     * @param testCase The LongBow Test Case that is being torn-down.
     */
    LongBowTestFixtureTearDownFunction *tearDown;
};

static void
_longBowTestFixture_TestSucceeded(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalSucceeded++;
}

static void
_longBowTestFixture_TestFailed(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalFailed++;
}

static void
_longBowTestFixture_TestLeakedResources(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalFailed++;
}

static void
_longBowTestFixture_TestSkipped(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalSkipped++;
}

static void
_longBowTestFixture_TestSkipTests(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalSkipped++;
}

static void
_longBowTestFixture_TestWarned(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalWarned++;
}

static void
_longBowTestFixture_TestSetupFailed(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalSetupFailed++;
}

static void
_longBowTestFixture_TestSignalled(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalSignalled++;
}

static void
_longBowTestFixture_TestStopped(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalStopped++;
}

static void
_longBowTestFixture_TestUnimplemented(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalUnimplemented++;
}

static void
_longBowTestFixture_TestImpotent(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalUnimplemented++;
}

static void
_longBowTestFixture_Untested(LongBowTestFixtureSummary *summary __attribute__((unused)))
{
}

static void
_longBowTestFixture_TearDownFailed(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalTearDownFailed++;
}

static void
_longBowTestFixture_TearDownWarned(LongBowTestFixtureSummary *summary)
{
    summary->totalTested++;
    summary->totalTearDownWarned++;
}

void
longBowTestFixture_ConfigHelp(void)
{
    printf("Test Fixture options:\n");
    printf("  --set <runnerName>/<fixtureName>/iterations=<integer>  Run the named test fixture <integer> times.\n");
    printf("  --set <runnerName>/<fixtureName>/enable=(true|false) Enable or disable execution of the named test fixture.\n");
}

bool
longBowTestFixture_Config(LongBowConfig *config, const char *parameter)
{
    bool result = false;
    return result;
}

LongBowTestFixture *
longBowTestFixture_Create(const LongBowTestRunner *testRunner,
                          const char *fixtureName,
                          LongBowTestFixtureSetupFunction *setup,
                          LongBowTestFixtureFunction *fixtureFunction,
                          LongBowTestFixtureTearDownFunction *tearDown)
{
    LongBowTestFixture *fixture = longBowMemory_Allocate(sizeof(LongBowTestFixture));
    if (fixture != NULL) {
        fixture->runner = (LongBowTestRunner *) testRunner;
        fixture->name = fixtureName;

        int status = asprintf(&fixture->fullName, "%s/%s", longBowTestRunner_GetName(testRunner), fixtureName);
        if (status == -1) {
            return NULL;
        }

        fixture->testCases = longBowArrayList_Create((void (*)(void **))longBowTestCase_Destroy);
        fixture->setUp = setup;
        fixture->fixture = fixtureFunction;
        fixture->tearDown = tearDown;
    }

    return fixture;
}

void
longBowTestFixture_Destroy(LongBowTestFixture **fixturePtr)
{
    LongBowTestFixture *fixture = *fixturePtr;

    free(fixture->fullName);

    longBowArrayList_Destroy(&fixture->testCases);
    longBowMemory_Deallocate((void **) fixturePtr);
}

const char *
longBowTestFixture_GetFullName(const LongBowTestFixture *testFixture)
{
    return testFixture->fullName;
}

void
longBowTestFixture_UpdateSummary(LongBowTestCase *longBowTestCase)
{
    switch (longBowRuntimeResult_GetStatus(longBowTestCase_GetActualResult(longBowTestCase))) {
        case LONGBOW_STATUS_SUCCEEDED:
            _longBowTestFixture_TestSucceeded(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LONGBOW_STATUS_SKIPPED:
            _longBowTestFixture_TestSkipped(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LongBowStatus_WARNED:
            _longBowTestFixture_TestWarned(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LONGBOW_STATUS_SETUP_FAILED:
            _longBowTestFixture_TestSetupFailed(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LONGBOW_STATUS_SETUP_SKIPTESTS:
            _longBowTestFixture_TestSkipTests(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LONGBOW_STATUS_TEARDOWN_FAILED:
            _longBowTestFixture_TearDownFailed(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LongBowStatus_TEARDOWN_WARNED:
            _longBowTestFixture_TearDownWarned(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LONGBOW_STATUS_FAILED:
            _longBowTestFixture_TestFailed(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LongBowStatus_STOPPED:
            _longBowTestFixture_TestStopped(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LongBowStatus_UNIMPLEMENTED:
            _longBowTestFixture_TestUnimplemented(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LongBowStatus_IMPOTENT:
            _longBowTestFixture_TestImpotent(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LongBowStatus_UNTESTED:
            _longBowTestFixture_Untested(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        case LONGBOW_STATUS_MEMORYLEAK:
            _longBowTestFixture_TestLeakedResources(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            break;

        default:
            if (longBowTestCase_GetActualResult(longBowTestCase)->status >= LongBowStatus_SIGNALLED) {
                _longBowTestFixture_TestSignalled(&longBowTestCase_GetFixture(longBowTestCase)->summary);
            } else {
                printf("longBowTestFixture_UpdateSummary: unhandled status %d\n",
                       longBowTestCase_GetActualResult(longBowTestCase)->status);
            }
    }
}

void
longBowTestFixture_AddTestCase(const LongBowTestFixture *fixture, LongBowTestCase *longBowTestCase)
{
    longBowTestFixture_UpdateSummary(longBowTestCase);

    longBowArrayList_Add(fixture->testCases, longBowTestCase);
}

LongBowTestCase *
longBowTestFixture_GetTestCase(const LongBowTestFixture *fixture, size_t index)
{
    LongBowTestCase *testCase = longBowArrayList_Get(fixture->testCases, index);

    return testCase;
}

const char *
longBowTestFixture_GetName(const LongBowTestFixture *fixture)
{
    return fixture->name;
}

const LongBowTestFixtureSummary *
longBowTestFixture_GetSummary(const LongBowTestFixture *fixture)
{
    return &(fixture->summary);
}

size_t
longBowTestFixture_GetTestCaseCount(const LongBowTestFixture *fixture)
{
    return longBowArrayList_Length(fixture->testCases);
}

LongBowStatus
longBowTestFixture_Setup(LongBowTestFixture *fixture, LongBowTestCase *testCase)
{
    if (longBowConfig_IsTrace(longBowTestRunner_GetConfiguration(longBowTestFixture_GetRunner(fixture)))) {
        longBowReportTesting_Trace("    %s/%s: setup",
                                   longBowTestRunner_GetName(longBowTestFixture_GetRunner(fixture)),
                                   longBowTestFixture_GetName(fixture));
    }
    LongBowClipBoard *testClipBoard = longBowTestFixture_GetClipBoard(fixture);
    LongBowStatus result = (longBowTestCase_GetFixture(testCase)->setUp)(longBowTestFixture_GetRunner(fixture), fixture, testCase, testClipBoard);
    return result;
}

LongBowStatus
longBowTestFixture_TearDown(LongBowTestFixture *fixture, LongBowTestCase *testCase)
{
    if (longBowConfig_IsTrace(longBowTestRunner_GetConfiguration(longBowTestFixture_GetRunner(fixture)))) {
        longBowReportTesting_Trace("    %s/%s: tearDown",
                                   longBowTestRunner_GetName(longBowTestFixture_GetRunner(fixture)),
                                   longBowTestFixture_GetName(fixture));
    }
    LongBowClipBoard *testClipBoard = longBowTestFixture_GetClipBoard(fixture);
    LongBowStatus result = (longBowTestCase_GetFixture(testCase)->tearDown)(longBowTestFixture_GetRunner(fixture), fixture, testCase, testClipBoard);
    return result;
}

LongBowStatus
longBowTestFixture_GetStatus(const LongBowTestFixture *fixture)
{
    size_t nTestCases = longBowTestFixture_GetTestCaseCount(fixture);

    LongBowStatus result = LONGBOW_STATUS_SUCCEEDED;

    // Just return the status of the first non-successful test case.
    for (size_t i = 0; i < nTestCases; i++) {
        LongBowTestCase *testCase = longBowTestFixture_GetTestCase(fixture, i);
        if (!longBowTestCase_IsSuccessful(testCase)) {
            result = longBowTestCase_GetStatus(testCase);
            break;
        }
    }
    return result;
}

bool
longBowTestFixture_IsSuccessful(const LongBowTestFixture *testFixture)
{
    return longBowStatus_IsSuccessful(longBowTestFixture_GetStatus(testFixture));
}

char *
longBowTestFixture_ToString(const LongBowTestFixture *fixture)
{
    char *runnerString = longBowTestRunner_ToString(longBowTestFixture_GetRunner(fixture));

    char *string;
    if (asprintf(&string, "%s/%s", runnerString, longBowTestFixture_GetName(fixture)) == -1) {
        return NULL;
    }

    free(runnerString);

    return string;
}

LongBowTestFixture *
longBowTestFixture_Run(const LongBowTestRunner *testRunner,
                       const char *fixtureName,
                       const LongBowTestFixtureConfig *config,
                       LongBowTestFixtureSetupFunction *setup,
                       LongBowTestFixtureFunction *fixtureRun,
                       LongBowTestFixtureTearDownFunction *tearDown)
{
    LongBowTestFixture *testFixture = longBowTestFixture_Create(testRunner, fixtureName, setup, fixtureRun, tearDown);

    LongBowConfig *configuration = longBowTestRunner_GetConfiguration(testRunner);

    bool enabled = longBowConfig_GetBoolean(configuration, config->enabled, "%s/enabled", longBowTestFixture_GetFullName(testFixture));
    unsigned long iterations = longBowConfig_GetUint32(configuration, 1, "%s/iterations", longBowTestFixture_GetFullName(testFixture));

    if (enabled) {
        for (unsigned long i = 0; i < iterations; i++) {
            (*testFixture->fixture)(testRunner, testFixture);
        }
        longBowTestRunner_AddFixture(testFixture->runner, testFixture);
    }

    return testFixture;
}

LongBowTestRunner *
longBowTestFixture_GetRunner(const LongBowTestFixture *fixture)
{
    return fixture->runner;
}

LongBowClipBoard *
longBowTestFixture_GetClipBoard(const LongBowTestFixture *fixture)
{
    return longBowTestRunner_GetClipBoard(fixture->runner);
}