summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2019-12-09 12:37:34 +1000
committerEdward Thomson <ethomson@edwardthomson.com>2020-05-11 20:13:54 +0100
commitabe2efe1ff84d423ef5f104b1e95e9ef66442c0f (patch)
tree1954c9abfc8d0475f2058842db735d569b111698 /tests
parentb83bc6d4739d10ac3db6513931cedfe8e682f487 (diff)
downloadlibgit2-abe2efe1ff84d423ef5f104b1e95e9ef66442c0f.tar.gz
Introduce GIT_ASSERT macros
Provide macros to replace usages of `assert`. A true `assert` is punishing as a library. Instead we should do our best to not crash. GIT_ASSERT_ARG(x) will now assert that the given argument complies to some format and sets an error message and returns `-1` if it does not. GIT_ASSERT(x) is for internal usage, and available as an internal consistency check. It will set an error message and return `-1` in the event of failure.
Diffstat (limited to 'tests')
-rw-r--r--tests/core/assert.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/core/assert.c b/tests/core/assert.c
new file mode 100644
index 000000000..8fd709367
--- /dev/null
+++ b/tests/core/assert.c
@@ -0,0 +1,39 @@
+#include "clar_libgit2.h"
+
+static const char *hello_world = "hello, world";
+
+static int dummy_fn(const char *myarg)
+{
+ GIT_ASSERT_ARG(myarg);
+ GIT_ASSERT_ARG(myarg != hello_world);
+ return 0;
+}
+
+static int bad_math(void)
+{
+ GIT_ASSERT(1 + 1 == 3);
+ return 42;
+}
+
+void test_core_assert__argument(void)
+{
+ 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"));
+}
+
+void test_core_assert__internal(void)
+{
+ 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);
+}