Skip to content

Commit e0f807b

Browse files
Seyi007gitster
authored andcommitted
t/unit-tests: adapt example decorate test to use clar test framework
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 protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 38b066e commit e0f807b

File tree

4 files changed

+66
-76
lines changed

4 files changed

+66
-76
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/%
13401340
THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
13411341

13421342
CLAR_TEST_SUITES += u-ctype
1343+
CLAR_TEST_SUITES += u-example-decorate
13431344
CLAR_TEST_SUITES += u-hash
13441345
CLAR_TEST_SUITES += u-hashmap
13451346
CLAR_TEST_SUITES += u-mem-pool
@@ -1351,7 +1352,6 @@ CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
13511352
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
13521353
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
13531354

1354-
UNIT_TEST_PROGRAMS += t-example-decorate
13551355
UNIT_TEST_PROGRAMS += t-oid-array
13561356
UNIT_TEST_PROGRAMS += t-oidmap
13571357
UNIT_TEST_PROGRAMS += t-oidtree

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
clar_test_suites = [
22
'unit-tests/u-ctype.c',
3+
'unit-tests/u-example-decorate.c',
34
'unit-tests/u-hash.c',
45
'unit-tests/u-hashmap.c',
56
'unit-tests/u-mem-pool.c',
@@ -45,7 +46,6 @@ clar_unit_tests = executable('unit-tests',
4546
test('unit-tests', clar_unit_tests)
4647

4748
unit_test_programs = [
48-
'unit-tests/t-example-decorate.c',
4949
'unit-tests/t-oid-array.c',
5050
'unit-tests/t-oidmap.c',
5151
'unit-tests/t-oidtree.c',

t/unit-tests/t-example-decorate.c

Lines changed: 0 additions & 74 deletions
This file was deleted.

t/unit-tests/u-example-decorate.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
3+
#include "unit-test.h"
4+
#include "object.h"
5+
#include "decorate.h"
6+
#include "repository.h"
7+
8+
struct test_vars {
9+
struct object *one, *two, *three;
10+
struct decoration n;
11+
int decoration_a, decoration_b;
12+
};
13+
14+
static struct test_vars vars;
15+
16+
void test_example_decorate__initialize(void)
17+
{
18+
struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
19+
20+
vars.one = lookup_unknown_object(the_repository, &one_oid);
21+
vars.two = lookup_unknown_object(the_repository, &two_oid);
22+
vars.three = lookup_unknown_object(the_repository, &three_oid);
23+
}
24+
25+
void test_example_decorate__cleanup(void)
26+
{
27+
clear_decoration(&vars.n, NULL);
28+
}
29+
30+
void test_example_decorate__add(void)
31+
{
32+
cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
33+
cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL);
34+
}
35+
36+
void test_example_decorate__readd(void)
37+
{
38+
cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
39+
cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL);
40+
cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), &vars.decoration_a);
41+
cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
42+
}
43+
44+
void test_example_decorate__lookup(void)
45+
{
46+
cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
47+
cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), NULL);
48+
cl_assert_equal_p(lookup_decoration(&vars.n, vars.two), &vars.decoration_b);
49+
cl_assert_equal_p(lookup_decoration(&vars.n, vars.one), NULL);
50+
}
51+
52+
void test_example_decorate__loop(void)
53+
{
54+
int objects_noticed = 0;
55+
56+
cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
57+
cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
58+
59+
for (size_t i = 0; i < vars.n.size; i++)
60+
if (vars.n.entries[i].base)
61+
objects_noticed++;
62+
63+
cl_assert_equal_i(objects_noticed, 2);
64+
}

0 commit comments

Comments
 (0)