summaryrefslogtreecommitdiff
path: root/src/testing.c
blob: e8c0fa6b788f09cc656c123d4ed6488bb754fa54 (plain) (blame)
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
#include "config.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "testing.h"


#define COLOUR_RESET  "\033[0m"
#define COLOUR_GREEN  "\033[0;32m"
#define COLOUR_YELLOW "\033[0;33m"

static const char *const
ENVVAR_NAME = "NO_COLOR";


static bool
show_colour(void) {
	const char *const no_colour = getenv(ENVVAR_NAME);
	return !no_colour || no_colour[0] == '\0';
}


/**
 * @tags infallible
 */
void
test_start(const char *const name) {
	(void)fprintf(stderr, "%s:\n", name);

	return;
}


/**
 * @tags infallible
 */
void
testing(const char *const message) {
	if (show_colour()) {
		(void)fprintf(
			stderr,
			COLOUR_YELLOW "testing" COLOUR_RESET ": %s...",
			message
		);
	} else {
		(void)fprintf(
			stderr,
			"testing: %s...",
			message
		);
	}

	return;
}


/**
 * @tags infallible
 */
void
test_ok(void) {
	if (show_colour()) {
		(void)fprintf(stderr, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n");
	} else {
		(void)fprintf(stderr, " OK.\n");
	}

	return;
}