mirror of
https://github.com/git/git
synced 2024-08-27 11:39:22 +00:00
![Phillip Wood](/assets/img/avatar_default.png)
This patch contains an implementation for writing unit tests with TAP output. Each test is a function that contains one or more checks. The test is run with the TEST() macro and if any of the checks fail then the test will fail. A complete program that tests STRBUF_INIT would look like #include "test-lib.h" #include "strbuf.h" static void t_static_init(void) { struct strbuf buf = STRBUF_INIT; check_uint(buf.len, ==, 0); check_uint(buf.alloc, ==, 0); check_char(buf.buf[0], ==, '\0'); } int main(void) { TEST(t_static_init(), "static initialization works); return test_done(); } The output of this program would be ok 1 - static initialization works 1..1 If any of the checks in a test fail then they print a diagnostic message to aid debugging and the test will be reported as failing. For example a failing integer check would look like # check "x >= 3" failed at my-test.c:102 # left: 2 # right: 3 not ok 1 - x is greater than or equal to three There are a number of check functions implemented so far. check() checks a boolean condition, check_int(), check_uint() and check_char() take two values to compare and a comparison operator. check_str() will check if two strings are equal. Custom checks are simple to implement as shown in the comments above test_assert() in test-lib.h. Tests can be skipped with test_skip() which can be supplied with a reason for skipping which it will print. Tests can print diagnostic messages with test_msg(). Checks that are known to fail can be wrapped in TEST_TODO(). There are a couple of example test programs included in this patch. t-basic.c implements some self-tests and demonstrates the diagnostic output for failing test. The output of this program is checked by t0080-unit-test-output.sh. t-strbuf.c shows some example unit tests for strbuf.c The unit tests will be built as part of the default "make all" target, to avoid bitrot. If you wish to build just the unit tests, you can run "make build-unit-tests". To run the tests, you can use "make unit-tests" or run the test binaries directly, as in "./t/unit-tests/bin/t-strbuf". Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
96 lines
2.5 KiB
C
96 lines
2.5 KiB
C
#include "test-lib.h"
|
|
|
|
/*
|
|
* The purpose of this "unit test" is to verify a few invariants of the unit
|
|
* test framework itself, as well as to provide examples of output from actually
|
|
* failing tests. As such, it is intended that this test fails, and thus it
|
|
* should not be run as part of `make unit-tests`. Instead, we verify it behaves
|
|
* as expected in the integration test t0080-unit-test-output.sh
|
|
*/
|
|
|
|
/* Used to store the return value of check_int(). */
|
|
static int check_res;
|
|
|
|
/* Used to store the return value of TEST(). */
|
|
static int test_res;
|
|
|
|
static void t_res(int expect)
|
|
{
|
|
check_int(check_res, ==, expect);
|
|
check_int(test_res, ==, expect);
|
|
}
|
|
|
|
static void t_todo(int x)
|
|
{
|
|
check_res = TEST_TODO(check(x));
|
|
}
|
|
|
|
static void t_skip(void)
|
|
{
|
|
check(0);
|
|
test_skip("missing prerequisite");
|
|
check(1);
|
|
}
|
|
|
|
static int do_skip(void)
|
|
{
|
|
test_skip("missing prerequisite");
|
|
return 1;
|
|
}
|
|
|
|
static void t_skip_todo(void)
|
|
{
|
|
check_res = TEST_TODO(do_skip());
|
|
}
|
|
|
|
static void t_todo_after_fail(void)
|
|
{
|
|
check(0);
|
|
TEST_TODO(check(0));
|
|
}
|
|
|
|
static void t_fail_after_todo(void)
|
|
{
|
|
check(1);
|
|
TEST_TODO(check(0));
|
|
check(0);
|
|
}
|
|
|
|
static void t_messages(void)
|
|
{
|
|
check_str("\thello\\", "there\"\n");
|
|
check_str("NULL", NULL);
|
|
check_char('a', ==, '\n');
|
|
check_char('\\', ==, '\'');
|
|
}
|
|
|
|
static void t_empty(void)
|
|
{
|
|
; /* empty */
|
|
}
|
|
|
|
int cmd_main(int argc, const char **argv)
|
|
{
|
|
test_res = TEST(check_res = check_int(1, ==, 1), "passing test");
|
|
TEST(t_res(1), "passing test and assertion return 1");
|
|
test_res = TEST(check_res = check_int(1, ==, 2), "failing test");
|
|
TEST(t_res(0), "failing test and assertion return 0");
|
|
test_res = TEST(t_todo(0), "passing TEST_TODO()");
|
|
TEST(t_res(1), "passing TEST_TODO() returns 1");
|
|
test_res = TEST(t_todo(1), "failing TEST_TODO()");
|
|
TEST(t_res(0), "failing TEST_TODO() returns 0");
|
|
test_res = TEST(t_skip(), "test_skip()");
|
|
TEST(check_int(test_res, ==, 1), "skipped test returns 1");
|
|
test_res = TEST(t_skip_todo(), "test_skip() inside TEST_TODO()");
|
|
TEST(t_res(1), "test_skip() inside TEST_TODO() returns 1");
|
|
test_res = TEST(t_todo_after_fail(), "TEST_TODO() after failing check");
|
|
TEST(check_int(test_res, ==, 0), "TEST_TODO() after failing check returns 0");
|
|
test_res = TEST(t_fail_after_todo(), "failing check after TEST_TODO()");
|
|
TEST(check_int(test_res, ==, 0), "failing check after TEST_TODO() returns 0");
|
|
TEST(t_messages(), "messages from failing string and char comparison");
|
|
test_res = TEST(t_empty(), "test with no checks");
|
|
TEST(check_int(test_res, ==, 0), "test with no checks returns 0");
|
|
|
|
return test_done();
|
|
}
|