|
| 1 | +#ifdef GIT_ASSERT_HARD |
| 2 | +# undef GIT_ASSERT_HARD |
| 3 | +#endif |
| 4 | + |
| 5 | +#define GIT_ASSERT_HARD 0 |
| 6 | + |
| 7 | +#include "clar_libgit2.h" |
| 8 | + |
| 9 | +static const char *hello_world = "hello, world"; |
| 10 | +static const char *fail = "FAIL"; |
| 11 | + |
| 12 | +static int dummy_fn(const char *myarg) |
| 13 | +{ |
| 14 | + GIT_ASSERT_ARG(myarg); |
| 15 | + GIT_ASSERT_ARG(myarg != hello_world); |
| 16 | + return 0; |
| 17 | +} |
| 18 | + |
| 19 | +static const char *fn_returns_string(const char *myarg) |
| 20 | +{ |
| 21 | + GIT_ASSERT_ARG_WITH_RETVAL(myarg, fail); |
| 22 | + GIT_ASSERT_ARG_WITH_RETVAL(myarg != hello_world, fail); |
| 23 | + |
| 24 | + return myarg; |
| 25 | +} |
| 26 | + |
| 27 | +static int bad_math(void) |
| 28 | +{ |
| 29 | + GIT_ASSERT(1 + 1 == 3); |
| 30 | + return 42; |
| 31 | +} |
| 32 | + |
| 33 | +static const char *bad_returns_string(void) |
| 34 | +{ |
| 35 | + GIT_ASSERT_WITH_RETVAL(1 + 1 == 3, NULL); |
| 36 | + return hello_world; |
| 37 | +} |
| 38 | + |
| 39 | +void test_core_assert__argument(void) |
| 40 | +{ |
| 41 | + cl_git_fail(dummy_fn(NULL)); |
| 42 | + cl_assert(git_error_last()); |
| 43 | + cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass); |
| 44 | + cl_assert_equal_s("invalid argument: 'myarg'", git_error_last()->message); |
| 45 | + |
| 46 | + cl_git_fail(dummy_fn(hello_world)); |
| 47 | + cl_assert(git_error_last()); |
| 48 | + cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass); |
| 49 | + cl_assert_equal_s("invalid argument: 'myarg != hello_world'", git_error_last()->message); |
| 50 | + |
| 51 | + cl_git_pass(dummy_fn("foo")); |
| 52 | +} |
| 53 | + |
| 54 | +void test_core_assert__argument_with_non_int_return_type(void) |
| 55 | +{ |
| 56 | + const char *foo = "foo"; |
| 57 | + |
| 58 | + cl_assert_equal_p(fail, fn_returns_string(NULL)); |
| 59 | + cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass); |
| 60 | + cl_assert_equal_s("invalid argument: 'myarg'", git_error_last()->message); |
| 61 | + |
| 62 | + cl_assert_equal_p(fail, fn_returns_string(hello_world)); |
| 63 | + cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass); |
| 64 | + cl_assert_equal_s("invalid argument: 'myarg != hello_world'", git_error_last()->message); |
| 65 | + |
| 66 | + cl_assert_equal_p(foo, fn_returns_string(foo)); |
| 67 | +} |
| 68 | + |
| 69 | +void test_core_assert__argument_with_void_return_type(void) |
| 70 | +{ |
| 71 | + const char *foo = "foo"; |
| 72 | + |
| 73 | + git_error_clear(); |
| 74 | + fn_returns_string(hello_world); |
| 75 | + cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass); |
| 76 | + cl_assert_equal_s("invalid argument: 'myarg != hello_world'", git_error_last()->message); |
| 77 | + |
| 78 | + git_error_clear(); |
| 79 | + cl_assert_equal_p(foo, fn_returns_string(foo)); |
| 80 | + cl_assert_equal_p(NULL, git_error_last()); |
| 81 | +} |
| 82 | + |
| 83 | +void test_core_assert__internal(void) |
| 84 | +{ |
| 85 | + cl_git_fail(bad_math()); |
| 86 | + cl_assert(git_error_last()); |
| 87 | + cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass); |
| 88 | + cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message); |
| 89 | + |
| 90 | + cl_assert_equal_p(NULL, bad_returns_string()); |
| 91 | + cl_assert(git_error_last()); |
| 92 | + cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass); |
| 93 | + cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message); |
| 94 | +} |
0 commit comments