git/t/unit-tests/clar/test/sample.c

85 lines
1.8 KiB
C
Raw Normal View History

t: import the clar unit testing framework Our unit testing framework is a homegrown solution. While it supports most of our needs, it is likely that the volume of unit tests will grow quite a bit in the future such that we can exercise low-level subsystems directly. This surfaces several shortcomings that the current solution has: - There is no way to run only one specific tests. While some of our unit tests wire this up manually, others don't. In general, it requires quite a bit of boilerplate to get this set up correctly. - Failures do not cause a test to stop execution directly. Instead, the test author needs to return manually whenever an assertion fails. This is rather verbose and is not done correctly in most of our unit tests. - Wiring up a new testcase requires both implementing the test function and calling it in the respective test suite's main function, which is creating code duplication. We can of course fix all of these issues ourselves, but that feels rather pointless when there are already so many unit testing frameworks out there that have those features. We line out some requirements for any unit testing framework in "Documentation/technical/unit-tests.txt". The "clar" unit testing framework, which isn't listed in that table yet, ticks many of the boxes: - It is licensed under ISC, which is compatible. - It is easily vendorable because it is rather tiny at around 1200 lines of code. - It is easily hackable due to the same reason. - It has TAP support. - It has skippable tests. - It preprocesses test files in order to extract test functions, which then get wired up automatically. While it's not perfect, the fact that clar originates from the libgit2 project means that it should be rather easy for us to collaborate with upstream to plug any gaps. Import the clar unit testing framework at commit 1516124 (Merge pull request #97 from pks-t/pks-whitespace-fixes, 2024-08-15). The framework will be wired up in subsequent commits. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-09-04 08:16:48 -06:00
#include "clar_test.h"
#include <sys/stat.h>
static int file_size(const char *filename)
{
struct stat st;
if (stat(filename, &st) == 0)
return (int)st.st_size;
return -1;
}
void test_sample__initialize(void)
{
global_test_counter++;
}
void test_sample__cleanup(void)
{
cl_fixture_cleanup("test");
cl_assert(file_size("test/file") == -1);
}
void test_sample__1(void)
{
cl_assert(1);
cl_must_pass(0); /* 0 == success */
cl_must_fail(-1); /* <0 == failure */
cl_must_pass(-1); /* demonstrate a failing call */
}
void test_sample__2(void)
{
cl_fixture_sandbox("test");
cl_assert(file_size("test/nonexistent") == -1);
cl_assert(file_size("test/file") > 0);
cl_assert(100 == 101);
}
void test_sample__strings(void)
{
const char *actual = "expected";
cl_assert_equal_s("expected", actual);
cl_assert_equal_s_("expected", actual, "second try with annotation");
cl_assert_equal_s_("mismatched", actual, "this one fails");
}
void test_sample__strings_with_length(void)
{
const char *actual = "expected";
cl_assert_equal_strn("expected_", actual, 8);
cl_assert_equal_strn("exactly", actual, 2);
cl_assert_equal_strn_("expected_", actual, 8, "with annotation");
cl_assert_equal_strn_("exactly", actual, 3, "this one fails");
}
void test_sample__int(void)
{
int value = 100;
cl_assert_equal_i(100, value);
cl_assert_equal_i_(101, value, "extra note on failing test");
}
void test_sample__int_fmt(void)
{
int value = 100;
cl_assert_equal_i_fmt(022, value, "%04o");
}
void test_sample__bool(void)
{
int value = 100;
cl_assert_equal_b(1, value); /* test equality as booleans */
cl_assert_equal_b(0, value);
}
void test_sample__ptr(void)
{
const char *actual = "expected";
cl_assert_equal_p(actual, actual); /* pointers to same object */
cl_assert_equal_p(&actual, actual);
}