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

static int TOTAL = 0;
static int SECTION_TOTAL = 0;
static int SECTION_NUMBER = 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++;
    SECTION_TOTAL++;
    printf( "%d.%d %s: %s\n", SECTION_NUMBER, SECTION_TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL));
    if (expression) SUCCESS++;
}

void start_test_section(const char* name) {
  SECTION_TOTAL = 0;
  SECTION_NUMBER++;
  printf("%d.%s %s %s\n", SECTION_NUMBER, CYAN, name, RESET);
}

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;
}