summaryrefslogtreecommitdiff
path: root/tests/test_lib.c
blob: e67ad347a0448cfab3cf50801e3b5da2b2c30f49 (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
#include <assert.h>
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "test_lib.h"

#define DO_ALLOC(TYPE) ((TYPE*) malloc(sizeof(TYPE)))
#define GIT_MAX_TEST_CASES 64

extern git_error *git_errno;

struct git_test {
	char *name;
	char *message;
	char *failed_pos;
	char *description;
	char *error_message;
	git_error *error_stack;

	git_testfunc function;
	unsigned failed:1, ran:1;
	jmp_buf *jump;
};

struct git_testsuite {
	char *name;
	int count, fail_count;
	git_test *list[GIT_MAX_TEST_CASES];
};

static void test_free(git_test *t)
{
	if (t) {
		free(t->name);
		free(t->description);
		free(t->failed_pos);
		free(t->message);
		free(t->error_message);
		git_error_free(t->error_stack);
		free(t);
	}
}

static void test_run(git_test *tc)
{
	jmp_buf buf;
	tc->jump = &buf;

	if (setjmp(buf) == 0) {
		tc->ran = 1;
		(tc->function)(tc);
	}

	tc->jump = 0;
}

static git_test *create_test(git_testfunc function)
{
	git_test *t = DO_ALLOC(git_test);

	memset(t, 0x0, sizeof(git_test));
	t->function = function;

	return t;
}

void git_test__init(git_test *t, const char *name, const char *description)
{
	t->name = strdup(name);
	t->description = strdup(description);
}


/*-------------------------------------------------------------------------*
 * Public assert methods
 *-------------------------------------------------------------------------*/

static void fail_test(git_test *tc, const char *file, int line, const char *message)
{
	char buf[1024];
	const char *last_error = git_lasterror();

	snprintf(buf, 1024, "%s:%d", file, line);

	tc->failed = 1;
	tc->message = strdup(message);
	tc->failed_pos = strdup(buf);
	tc->error_stack = git_errno;
	git_errno = NULL;

	if (last_error)
		tc->error_message = strdup(last_error);

	if (tc->jump != 0)
		longjmp(*(tc->jump), 0);
}

void git_test__fail(git_test *tc, const char *file, int line, const char *message)
{
	fail_test(tc, file, line, message);
}

void git_test__assert(git_test *tc, const char *file, int line, const char *message, int condition)
{
	if (condition == 0)
		fail_test(tc, file, line, message);
}

void git_test__assert_pass(git_test *tc, const char *file, int line, const char *message, int ret_value)
{
	if (ret_value < 0)
		fail_test(tc, file, line, message);
}

/*-------------------------------------------------------------------------*
 * Test Suite
 *-------------------------------------------------------------------------*/

static void testsuite_init(git_testsuite *ts)
{
	ts->count = 0;
	ts->fail_count = 0;
	memset(ts->list, 0, sizeof(ts->list));
}

git_testsuite *git_testsuite_new(const char *name)
{
	git_testsuite *ts = DO_ALLOC(git_testsuite);
	testsuite_init(ts);
	ts->name = strdup(name);
	return ts;
}

static void free_suite(git_testsuite *ts)
{
	unsigned int n;

	for (n = 0; n < GIT_MAX_TEST_CASES; n++)
		if (ts->list[n])
			test_free(ts->list[n]);

	free(ts->name);
	free(ts);
}

void git_testsuite_add(git_testsuite *ts, git_testfunc test)
{
	assert(ts->count < GIT_MAX_TEST_CASES);
	ts->list[ts->count++] = create_test(test);
}

static void print_trace(git_error *error)
{
	git_error *err;

	for (err = error; err; err = err->child)
		printf("\t%s:%u %s\n", err->file, err->line, err->msg);
}

static void print_details(git_testsuite *ts)
{
	int i;
	int failCount = 0;

	if (ts->fail_count == 0) {
		const char *testWord = ts->count == 1 ? "test" : "tests";
		printf("OK (%d %s)\n", ts->count, testWord);
	} else {
		printf("Failed (%d failures):\n", ts->fail_count);

		for (i = 0 ; i < ts->count ; ++i) {
			git_test *tc = ts->list[i];
			if (tc->failed) {
				failCount++;
				printf("  %d) \"%s\" [test %s @ %s]\n\t%s\n",
					failCount, tc->description, tc->name, tc->failed_pos, tc->message);
				if (tc->error_message)
					printf("\tError: %s\n", tc->error_message);
				fprintf(stderr, "\tError stack trace:\n");
				print_trace(tc->error_stack);
			}
		}
	}
}

int git_testsuite_run(git_testsuite *ts)
{
	int i, fail_count;

	printf("Suite \"%s\": ", ts->name);

	for (i = 0 ; i < ts->count ; ++i) {
		git_test *tc = ts->list[i];

		test_run(tc);
		if (tc->failed) {
			ts->fail_count++;
			putchar('F');
		} else
			putchar('.');

		fflush(stdout);
	}
	printf("\n  ");
	print_details(ts);
	fail_count = ts->fail_count;

	free_suite(ts);
	return fail_count;
}