aboutsummaryrefslogtreecommitdiff
path: root/src/unittest.c
blob: 753dac3a97cb68c5d5a5a2934e8e61ec272e138a (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
#include <stdbool.h>
#include <stdio.h>
#include "unittest.h"

static int TOTAL = 0;
static int SUCCESS = 0;


bool assert(bool expression, const char* message) {
    if (!expression) {
        printf("%s\n", draw(RED, DEFAULT_ASSERT_MESSAGE));
        printf("%s\n", message);
    }
    return expression;
}

void unit_test(bool expression, const char* subject) {
    TOTAL++;
    printf( "%d. %s: %s\n", TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL));
    if (expression) SUCCESS++;
}

double coverage() {
    return 100 * (TOTAL? (double)SUCCESS/TOTAL : 1);
}

bool report() {
    printf("%s ", SUCCESS == TOTAL ? draw(GRN, "All checks successful!") : draw(RED, "Some tests have failed"));
    printf("[%d / %d]\n", SUCCESS, TOTAL);
    printf("Total coverage: %.2f%%\n", coverage());
    return TOTAL - SUCCESS;
}