Subject: [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar

From: Seyi Kuforiji <[email-masked]>
Date: Fri, 31 Jan 2025 23:14:16 +0100

Hello,

This small patch series transitions the existing unit test files to the
Clar testing framework. This change is part of our ongoing effort to
standardize our testing approach and enhance maintainability.

Changes in v2:
- small fixes to the commit messages and how they read
- some small code fix up and refactoring

Thanks
Seyi

Mentored-by: Patrick Steinhardt [email-masked]
Signed-off-by: Seyi Kuforiji [email-masked]

Seyi Kuforiji (4):
  t/unit-tests: convert hashmap test to use clar test framework
  t/unit-tests: adapt example decorate test to use clar test framework
  t/unit-tests: convert strbuf test to use clar test framework
  t/unit-tests: convert strcmp-offset test to use clar test framework

 Makefile                                      |   8 +-
 t/meson.build                                 |   8 +-
 ...xample-decorate.c => u-example-decorate.c} |  76 +++---
 t/unit-tests/{t-hashmap.c => u-hashmap.c}     | 226 +++++++++---------
 t/unit-tests/{t-strbuf.c => u-strbuf.c}       | 115 +++++----
 .../{t-strcmp-offset.c => u-strcmp-offset.c}  |  36 ++-
 6 files changed, 232 insertions(+), 237 deletions(-)
 rename t/unit-tests/{t-example-decorate.c => u-example-decorate.c} (30%)
 rename t/unit-tests/{t-hashmap.c => u-hashmap.c} (60%)
 rename t/unit-tests/{t-strbuf.c => u-strbuf.c} (35%)
 rename t/unit-tests/{t-strcmp-offset.c => u-strcmp-offset.c} (39%)

Range-diff against v1:
1:  90accb2f75 ! 1:  19697be26b t/unit-tests: convert hashmap test to use clar test framework
    @@ Commit message
         t/unit-tests: convert hashmap test to use clar test framework
     
         Adapts hashmap test script to clar framework by using clar assertions
    -    where necessary. Test functions are created as both standalone and
    -    inline to test different test cases.
    +    where necessary.
     
         Mentored-by: Patrick Steinhardt <[email-masked]>
         Signed-off-by: Seyi Kuforiji <[email-masked]>
    @@ t/unit-tests/u-hashmap.c: static void t_add(struct hashmap *map, unsigned int ig
     -			}
     +			int ret = key_val_contains(key_val, seen,
     +						   ARRAY_SIZE(key_val), entry);
    -+			cl_assert(ret == 0);
    ++			cl_assert_equal_i(ret, 0);
     +			count++;
      		}
     -		check_int(count, ==, 2);
    @@ t/unit-tests/u-hashmap.c: static void t_iterate(struct hashmap *map, unsigned in
     -			}
     -		}
     +		int ret = key_val_contains(key_val, seen,
    -+						ARRAY_SIZE(key_val),
    -+						entry);
    ++					   ARRAY_SIZE(key_val),
    ++					   entry);
     +		cl_assert(ret == 0);
      	}
      
2:  13a407d504 ! 2:  1d8f8974a5 t/unit-tests: adapt example decorate test to use clar test framework
    @@ Metadata
      ## Commit message ##
         t/unit-tests: adapt example decorate test to use clar test framework
     
    -    Adapts example decorate test script to clar framework by using clar
    -    assertions where necessary. Test functions are created as standalone to
    -    test different test cases.
    +    Introduce `test_example_decorate__initialize()` to explicitly set up
    +    object IDs and retrieve corresponding objects before tests run. This
    +    ensures a consistent and predictable test state without relying on data
    +    from previous tests.
    +
    +    Add `test_example_decorate__cleanup()` to clear decorations after each
    +    test, preventing interference between tests and ensuring each runs in
    +    isolation.
    +
    +    Adapt example decorate test script to clar framework by using clar
    +    assertions where necessary. Previously, tests relied on data written by
    +    earlier tests, leading to unintended dependencies between them. This
    +    explicitly initializes the necessary state within
    +    `test_example_decorate__readd`, ensuring it does not depend on prior
    +    test executions.
     
         Mentored-by: Patrick Steinhardt <[email-masked]>
         Signed-off-by: Seyi Kuforiji <[email-masked]>
    @@ t/unit-tests/u-example-decorate.c (new)
     +
     +static struct test_vars vars;
     +
    -+void test_example_decorate__add(void)
    ++void test_example_decorate__initialize(void)
     +{
    -+	void *ret = add_decoration(&vars.n, vars.one, &vars.decoration_a);
    -+	cl_assert(ret == NULL);
    -+	ret = add_decoration(&vars.n, vars.two, NULL);
    -+	cl_assert(ret == NULL);
    ++	struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
    ++
    ++	vars.one = lookup_unknown_object(the_repository, &one_oid);
    ++	vars.two = lookup_unknown_object(the_repository, &two_oid);
    ++	vars.three = lookup_unknown_object(the_repository, &three_oid);
     +}
     +
    -+void test_example_decorate__readd(void)
    ++void test_example_decorate__cleanup(void)
     +{
    -+	void *ret;
    ++	clear_decoration(&vars.n, NULL);
    ++}
     +
    -+	cl_assert(add_decoration(&vars.n, vars.one, &vars.decoration_a) == NULL);
    -+	cl_assert(add_decoration(&vars.n, vars.two, NULL) == NULL);
    ++void test_example_decorate__add(void)
    ++{
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL);
    ++}
     +
    -+	ret = add_decoration(&vars.n, vars.one, NULL);
    -+	cl_assert(ret == &vars.decoration_a);
    -+	ret = add_decoration(&vars.n, vars.two, &vars.decoration_b);
    -+	cl_assert(ret == NULL);
    ++void test_example_decorate__readd(void)
    ++{
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), &vars.decoration_a);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
     +}
     +
     +void test_example_decorate__lookup(void)
     +{
    -+	void *ret;
    -+
    -+	add_decoration(&vars.n, vars.two, &vars.decoration_b);
    -+	add_decoration(&vars.n, vars.one, NULL);
    -+
    -+	ret = lookup_decoration(&vars.n, vars.two);
    -+	cl_assert(ret == &vars.decoration_b);
    -+	ret = lookup_decoration(&vars.n, vars.one);
    -+	cl_assert(ret == NULL);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), NULL);
    ++	cl_assert_equal_p(lookup_decoration(&vars.n, vars.two), &vars.decoration_b);
    ++	cl_assert_equal_p(lookup_decoration(&vars.n, vars.one), NULL);
     +}
     +
     +void test_example_decorate__loop(void)
     +{
     +	int objects_noticed = 0;
     +
    -+	add_decoration(&vars.n, vars.one, &vars.decoration_a);
    -+	add_decoration(&vars.n, vars.two, &vars.decoration_b);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
    ++	cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
     +
    -+	for (size_t i = 0; i < vars.n.size; i++) {
    ++	for (size_t i = 0; i < vars.n.size; i++)
     +		if (vars.n.entries[i].base)
     +			objects_noticed++;
    -+	}
    -+	cl_assert_equal_i(objects_noticed, 2);
    -+}
     +
    -+void test_example_decorate__initialize(void)
    -+{
    -+	struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
    -+
    -+	vars.one = lookup_unknown_object(the_repository, &one_oid);
    -+	vars.two = lookup_unknown_object(the_repository, &two_oid);
    -+	vars.three = lookup_unknown_object(the_repository, &three_oid);
    -+}
    -+
    -+void test_example_decorate__cleanup(void)
    -+{
    -+	clear_decoration(&vars.n, NULL);
    ++	cl_assert_equal_i(objects_noticed, 2);
     +}
3:  08ade6b5cf ! 3:  e88ab7ab5f t/unit-tests: convert strbuf test to use clar test framework
    @@ Commit message
         t/unit-tests: convert strbuf test to use clar test framework
     
         Adapt strbuf test script to clar framework by using clar assertions
    -    where necessary. Test functions are created as standalone to test
    -    different test cases.
    +    where necessary.
     
         Mentored-by: Patrick Steinhardt <[email-masked]>
         Signed-off-by: Seyi Kuforiji <[email-masked]>
    @@ t/unit-tests/u-strbuf.c (new)
     +	/* Buffers should always be NUL-terminated */
     +	cl_assert(buf->buf[buf->len] == '\0');
     +	/*
    -+	 * Freshly-initialized strbufs may not have a dynamically allocated
    -+	 * buffer
    -+	 */
    -+	if (buf->len == 0 && buf->alloc == 0)
    -+		return;
    -+	/* alloc must be at least one byte larger than len */
    -+	cl_assert(buf->len < buf->alloc);
    ++         * In case the buffer contains anything, `alloc` must alloc must
    ++         * be at least one byte larger than `len`.
    ++         */
    ++	if (buf->len)
    ++            cl_assert(buf->len < buf->alloc);
     +}
     +
     +void test_strbuf__static_init(void)
    @@ t/unit-tests/u-strbuf.c (new)
     +	setup(t_addch, "");
     +}
     +
    -+void test_strbuf__add_multi_char(void)
    ++void test_strbuf__add_append_char(void)
     +{
     +	setup_populated(t_addch, "initial value", "a");
     +}
    @@ t/unit-tests/u-strbuf.c (new)
     +	setup(t_addstr, "hello there");
     +}
     +
    -+void test_strbuf__add_multi_str(void)
    ++void test_strbuf__add_append_str(void)
     +{
     +	setup_populated(t_addstr, "initial value", "hello there");
     +}
4:  f648cf4a4d ! 4:  2dde9110c2 t/unit-tests: convert strcmp-offset test to use clar test framework
    @@ Commit message
         t/unit-tests: convert strcmp-offset test to use clar test framework
     
         Adapt strcmp-offset test script to clar framework by using clar
    -    assertions where necessary. Test functions are created as standalone to
    -    test different test cases.
    +    assertions where necessary. Introduce `test_strcmp_offset__empty()` to
    +    verify `check_strcmp_offset()` behavior when both input strings are
    +    empty. This ensures the function correctly handles edge cases and
    +    returns expected values.
     
         Mentored-by: Patrick Steinhardt <[email-masked]>
         Signed-off-by: Seyi Kuforiji <[email-masked]>
-- 
2.47.0.86.g15030f9556