Commit cbae1c21 by Edward Thomson

assert: allow non-int returning functions to assert

Include GIT_ASSERT_WITH_RETVAL and GIT_ASSERT_ARG_WITH_RETVAL so that
functions that do not return int (or more precisely, where `-1` would
not be an error code) can assert.

This allows functions that return, eg, NULL on an error code to do that
by passing the return value (in this example, `NULL`) as a second
parameter to the GIT_ASSERT_WITH_RETVAL functions.
parent a95096ba
...@@ -21,28 +21,35 @@ ...@@ -21,28 +21,35 @@
# define GIT_ASSERT(expr) assert(expr) # define GIT_ASSERT(expr) assert(expr)
# define GIT_ASSERT_ARG(expr) assert(expr) # define GIT_ASSERT_ARG(expr) assert(expr)
# define GIT_ASSERT_WITH_RETVAL(expr, fail) assert(expr)
# define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) assert(expr)
#else #else
/** Internal consistency check to stop the function. */
# define GIT_ASSERT(expr) GIT_ASSERT_WITH_RETVAL(expr, -1)
/** /**
* Assert that a consumer-provided argument is valid, setting an * Assert that a consumer-provided argument is valid, setting an
* actionable error message and returning -1 if it is not. * actionable error message and returning -1 if it is not.
*/ */
# define GIT_ASSERT_ARG(expr) do { \ # define GIT_ASSERT_ARG(expr) GIT_ASSERT_ARG_WITH_RETVAL(expr, -1)
if (!(expr)) { \
git_error_set(GIT_ERROR_INVALID, \ /** Internal consistency check to return the `fail` param on failure. */
"invalid argument: '%s'", \ # define GIT_ASSERT_WITH_RETVAL(expr, fail) \
#expr); \ GIT_ASSERT__WITH_RETVAL(expr, GIT_ERROR_INTERNAL, "unrecoverable internal error", fail)
return -1; \
} \ /**
} while(0) * Assert that a consumer-provided argument is valid, setting an
* actionable error message and returning the `fail` param if not.
*/
# define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) \
GIT_ASSERT__WITH_RETVAL(expr, GIT_ERROR_INVALID, "invalid argument", fail)
/* Internal consistency check to stop the function. */ # define GIT_ASSERT__WITH_RETVAL(expr, code, msg, fail) do { \
# define GIT_ASSERT(expr) do { \
if (!(expr)) { \ if (!(expr)) { \
git_error_set(GIT_ERROR_INTERNAL, \ git_error_set(code, "%s: '%s'", msg, #expr); \
"unrecoverable internal error: '%s'", \ return fail; \
#expr); \
return -1; \
} \ } \
} while(0) } while(0)
......
#ifdef GIT_ASSERT_HARD
# undef GIT_ASSERT_HARD
#endif
#define GIT_ASSERT_HARD 0 #define GIT_ASSERT_HARD 0
#include "clar_libgit2.h" #include "clar_libgit2.h"
static const char *hello_world = "hello, world"; static const char *hello_world = "hello, world";
static const char *fail = "FAIL";
static int dummy_fn(const char *myarg) static int dummy_fn(const char *myarg)
{ {
...@@ -11,12 +16,26 @@ static int dummy_fn(const char *myarg) ...@@ -11,12 +16,26 @@ static int dummy_fn(const char *myarg)
return 0; return 0;
} }
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;
}
static int bad_math(void) static int bad_math(void)
{ {
GIT_ASSERT(1 + 1 == 3); GIT_ASSERT(1 + 1 == 3);
return 42; return 42;
} }
static const char *bad_returns_string(void)
{
GIT_ASSERT_WITH_RETVAL(1 + 1 == 3, NULL);
return hello_world;
}
void test_core_assert__argument(void) void test_core_assert__argument(void)
{ {
cl_git_fail(dummy_fn(NULL)); cl_git_fail(dummy_fn(NULL));
...@@ -32,10 +51,44 @@ void test_core_assert__argument(void) ...@@ -32,10 +51,44 @@ void test_core_assert__argument(void)
cl_git_pass(dummy_fn("foo")); cl_git_pass(dummy_fn("foo"));
} }
void test_core_assert__argument_with_non_int_return_type(void)
{
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));
}
void test_core_assert__argument_with_void_return_type(void)
{
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());
}
void test_core_assert__internal(void) void test_core_assert__internal(void)
{ {
cl_git_fail(bad_math()); cl_git_fail(bad_math());
cl_assert(git_error_last()); cl_assert(git_error_last());
cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass); 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); cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message);
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);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment