assert.c 2.96 KB
Newer Older
1 2 3 4
#ifdef GIT_ASSERT_HARD
# undef GIT_ASSERT_HARD
#endif

5 6
#define GIT_ASSERT_HARD 0

7 8 9
#include "clar_libgit2.h"

static const char *hello_world = "hello, world";
10
static const char *fail = "FAIL";
11 12 13 14 15 16 17 18

static int dummy_fn(const char *myarg)
{
	GIT_ASSERT_ARG(myarg);
	GIT_ASSERT_ARG(myarg != hello_world);
	return 0;
}

19 20 21 22 23 24 25 26
static const char *fn_returns_string(const char *myarg)
{
	GIT_ASSERT_ARG_WITH_RETVAL(myarg, fail);
	GIT_ASSERT_ARG_WITH_RETVAL(myarg != hello_world, fail);

	return myarg;
}

27 28 29 30 31 32
static int bad_math(void)
{
	GIT_ASSERT(1 + 1 == 3);
	return 42;
}

33 34 35 36 37 38
static const char *bad_returns_string(void)
{
	GIT_ASSERT_WITH_RETVAL(1 + 1 == 3, NULL);
	return hello_world;
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static int has_cleanup(void)
{
	int error = 42;

	GIT_ASSERT_WITH_CLEANUP(1 + 1 == 3, {
		error = 99;
		goto foobar;
	});

	return 0;

foobar:
	return error;
}

54
void test_assert__argument(void)
55 56 57 58 59 60 61 62 63 64 65 66 67 68
{
	cl_git_fail(dummy_fn(NULL));
	cl_assert(git_error_last());
	cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
	cl_assert_equal_s("invalid argument: 'myarg'", git_error_last()->message);

	cl_git_fail(dummy_fn(hello_world));
	cl_assert(git_error_last());
	cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
	cl_assert_equal_s("invalid argument: 'myarg != hello_world'", git_error_last()->message);

	cl_git_pass(dummy_fn("foo"));
}

69
void test_assert__argument_with_non_int_return_type(void)
70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
	const char *foo = "foo";

	cl_assert_equal_p(fail, fn_returns_string(NULL));
	cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
	cl_assert_equal_s("invalid argument: 'myarg'", git_error_last()->message);

	cl_assert_equal_p(fail, fn_returns_string(hello_world));
	cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
	cl_assert_equal_s("invalid argument: 'myarg != hello_world'", git_error_last()->message);

	cl_assert_equal_p(foo, fn_returns_string(foo));
}

84
void test_assert__argument_with_void_return_type(void)
85 86 87 88 89 90 91 92 93 94 95 96 97
{
	const char *foo = "foo";

	git_error_clear();
	fn_returns_string(hello_world);
	cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
	cl_assert_equal_s("invalid argument: 'myarg != hello_world'", git_error_last()->message);

	git_error_clear();
	cl_assert_equal_p(foo, fn_returns_string(foo));
	cl_assert_equal_p(NULL, git_error_last());
}

98
void test_assert__internal(void)
99 100 101 102 103
{
	cl_git_fail(bad_math());
	cl_assert(git_error_last());
	cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass);
	cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message);
104 105 106 107 108

	cl_assert_equal_p(NULL, bad_returns_string());
	cl_assert(git_error_last());
	cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass);
	cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message);
109
}
110 111 112 113 114 115 116 117

void test_assert__with_cleanup(void)
{
	cl_git_fail_with(99, has_cleanup());
	cl_assert(git_error_last());
	cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass);
	cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message);
}