Add unit tests for Commit parsing
[libgit2.git] / tests / test_lib.c
blob1ce3f92bf1c7b10aa4c76d1fd51094641ef53cbc
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
26 #define GIT__NO_HIDE_MALLOC
27 #include "test_lib.h"
29 struct test_info {
30 struct test_info *next;
31 const char *test_name;
32 const char *file_name;
33 int line_no;
36 static int first_test = 1;
37 static struct test_info *current_test;
39 static void show_test_result(const char *status)
41 fprintf(stderr, "* %-6s %5d: %s\n",
42 status,
43 current_test->line_no,
44 current_test->test_name);
47 void test_die(const char *fmt, ...)
49 va_list p;
51 if (current_test)
52 show_test_result("FAILED");
54 va_start(p, fmt);
55 vfprintf(stderr, fmt, p);
56 va_end(p);
57 fputc('\n', stderr);
58 fflush(stderr);
59 exit(128);
62 void test_begin(
63 const char *test_name,
64 const char *file_name,
65 int line_no)
67 struct test_info *i = malloc(sizeof(*i));
68 if (!i)
69 test_die("cannot malloc memory");
71 i->test_name = test_name;
72 i->file_name = file_name;
73 i->line_no = line_no;
74 current_test = i;
76 if (first_test) {
77 const char *name = strrchr(i->file_name, '/');
78 if (name)
79 name = name + 1;
80 else
81 name = i->file_name;
82 fprintf(stderr, "*** %s ***\n", name);
83 first_test = 0;
87 void test_end(void)
89 if (!current_test)
90 test_die("BEGIN_TEST() not used before END_TEST");
92 show_test_result("ok");
93 free(current_test);
94 current_test = NULL;