t/: migrate helper/test-example-decorate to the unit testing framework

helper/test-example-decorate.c along with t9004-example.sh provide an example of how to use the functions in decorate.h (which provides a data structure that associates Git objects to void pointers) and also test their output. Migrate them to the new unit testing framework for better debugging and runtime performance. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com> Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ghanshyam Thakkar committed May 28, 2024 at 18:28 UTC 456b4dce4cd11b4fc394db5ce5231b63e297ac77
7 files changed +82 -94
Makefile
+1 -1
@@ -793,7 +793,6 @@ TEST_BUILTINS_OBJS += test-dump-fsmonitor.o
793 TEST_BUILTINS_OBJS += test-dump-split-index.o
794 TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
795 TEST_BUILTINS_OBJS += test-env-helper.o
796 -TEST_BUILTINS_OBJS += test-example-decorate.o
796 TEST_BUILTINS_OBJS += test-example-tap.o
797 TEST_BUILTINS_OBJS += test-find-pack.o
798 TEST_BUILTINS_OBJS += test-fsmonitor-client.o
@@ -1334,6 +1333,7 @@ THIRD_PARTY_SOURCES += compat/regex/%
1333 THIRD_PARTY_SOURCES += sha1collisiondetection/%
1334 THIRD_PARTY_SOURCES += sha1dc/%
1335
1336 +UNIT_TEST_PROGRAMS += t-example-decorate
1337 UNIT_TEST_PROGRAMS += t-mem-pool
1338 UNIT_TEST_PROGRAMS += t-strbuf
1339 UNIT_TEST_PROGRAMS += t-ctype
decorate.h
+1 -1
@@ -3,7 +3,7 @@
3
4 /*
5 * A data structure that associates Git objects to void pointers. See
6 - * t/helper/test-example-decorate.c for a demonstration of how to use these
6 + * t/unit-tests/t-example-decorate.c for a demonstration of how to use these
7 * functions.
8 */
9
t/helper/test-example-decorate.c deleted
-78
@@ -1,78 +0,0 @@
1 -#include "test-tool.h"
2 -#include "git-compat-util.h"
3 -#include "object.h"
4 -#include "decorate.h"
5 -#include "repository.h"
6 -
7 -int cmd__example_decorate(int argc UNUSED, const char **argv UNUSED)
8 -{
9 - struct decoration n;
10 - struct object_id one_oid = { {1} };
11 - struct object_id two_oid = { {2} };
12 - struct object_id three_oid = { {3} };
13 - struct object *one, *two, *three;
14 -
15 - int decoration_a, decoration_b;
16 -
17 - void *ret;
18 -
19 - int i, objects_noticed = 0;
20 -
21 - /*
22 - * The struct must be zero-initialized.
23 - */
24 - memset(&n, 0, sizeof(n));
25 -
26 - /*
27 - * Add 2 objects, one with a non-NULL decoration and one with a NULL
28 - * decoration.
29 - */
30 - one = lookup_unknown_object(the_repository, &one_oid);
31 - two = lookup_unknown_object(the_repository, &two_oid);
32 - ret = add_decoration(&n, one, &decoration_a);
33 - if (ret)
34 - BUG("when adding a brand-new object, NULL should be returned");
35 - ret = add_decoration(&n, two, NULL);
36 - if (ret)
37 - BUG("when adding a brand-new object, NULL should be returned");
38 -
39 - /*
40 - * When re-adding an already existing object, the old decoration is
41 - * returned.
42 - */
43 - ret = add_decoration(&n, one, NULL);
44 - if (ret != &decoration_a)
45 - BUG("when readding an already existing object, existing decoration should be returned");
46 - ret = add_decoration(&n, two, &decoration_b);
47 - if (ret)
48 - BUG("when readding an already existing object, existing decoration should be returned");
49 -
50 - /*
51 - * Lookup returns the added declarations, or NULL if the object was
52 - * never added.
53 - */
54 - ret = lookup_decoration(&n, one);
55 - if (ret)
56 - BUG("lookup should return added declaration");
57 - ret = lookup_decoration(&n, two);
58 - if (ret != &decoration_b)
59 - BUG("lookup should return added declaration");
60 - three = lookup_unknown_object(the_repository, &three_oid);
61 - ret = lookup_decoration(&n, three);
62 - if (ret)
63 - BUG("lookup for unknown object should return NULL");
64 -
65 - /*
66 - * The user can also loop through all entries.
67 - */
68 - for (i = 0; i < n.size; i++) {
69 - if (n.entries[i].base)
70 - objects_noticed++;
71 - }
72 - if (objects_noticed != 2)
73 - BUG("should have 2 objects");
74 -
75 - clear_decoration(&n, NULL);
76 -
77 - return 0;
78 -}
t/helper/test-tool.c
-1
@@ -29,7 +29,6 @@ static struct test_cmd cmds[] = {
29 { "dump-split-index", cmd__dump_split_index },
30 { "dump-untracked-cache", cmd__dump_untracked_cache },
31 { "env-helper", cmd__env_helper },
32 - { "example-decorate", cmd__example_decorate },
32 { "example-tap", cmd__example_tap },
33 { "find-pack", cmd__find_pack },
34 { "fsmonitor-client", cmd__fsmonitor_client },
t/helper/test-tool.h
-1
@@ -23,7 +23,6 @@ int cmd__dump_split_index(int argc, const char **argv);
23 int cmd__dump_untracked_cache(int argc, const char **argv);
24 int cmd__dump_reftable(int argc, const char **argv);
25 int cmd__env_helper(int argc, const char **argv);
26 -int cmd__example_decorate(int argc, const char **argv);
26 int cmd__example_tap(int argc, const char **argv);
27 int cmd__find_pack(int argc, const char **argv);
28 int cmd__fsmonitor_client(int argc, const char **argv);
t/t9004-example.sh deleted
-12
@@ -1,12 +0,0 @@
1 -#!/bin/sh
2 -
3 -test_description='check that example code compiles and runs'
4 -
5 -TEST_PASSES_SANITIZE_LEAK=true
6 -. ./test-lib.sh
7 -
8 -test_expect_success 'decorate' '
9 - test-tool example-decorate
10 -'
11 -
12 -test_done
t/unit-tests/t-example-decorate.c new
+80
@@ -0,0 +1,80 @@
1 +#include "test-lib.h"
2 +#include "object.h"
3 +#include "decorate.h"
4 +#include "repository.h"
5 +
6 +struct test_vars {
7 + struct object *one, *two, *three;
8 + struct decoration n;
9 + int decoration_a, decoration_b;
10 +};
11 +
12 +static void t_add(struct test_vars *vars)
13 +{
14 + void *ret = add_decoration(&vars->n, vars->one, &vars->decoration_a);
15 +
16 + if (!check(ret == NULL))
17 + test_msg("when adding a brand-new object, NULL should be returned");
18 + ret = add_decoration(&vars->n, vars->two, NULL);
19 + if (!check(ret == NULL))
20 + test_msg("when adding a brand-new object, NULL should be returned");
21 +}
22 +
23 +static void t_readd(struct test_vars *vars)
24 +{
25 + void *ret = add_decoration(&vars->n, vars->one, NULL);
26 +
27 + if (!check(ret == &vars->decoration_a))
28 + test_msg("when readding an already existing object, existing decoration should be returned");
29 + ret = add_decoration(&vars->n, vars->two, &vars->decoration_b);
30 + if (!check(ret == NULL))
31 + test_msg("when readding an already existing object, existing decoration should be returned");
32 +}
33 +
34 +static void t_lookup(struct test_vars *vars)
35 +{
36 + void *ret = lookup_decoration(&vars->n, vars->one);
37 +
38 + if (!check(ret == NULL))
39 + test_msg("lookup should return added declaration");
40 + ret = lookup_decoration(&vars->n, vars->two);
41 + if (!check(ret == &vars->decoration_b))
42 + test_msg("lookup should return added declaration");
43 + ret = lookup_decoration(&vars->n, vars->three);
44 + if (!check(ret == NULL))
45 + test_msg("lookup for unknown object should return NULL");
46 +}
47 +
48 +static void t_loop(struct test_vars *vars)
49 +{
50 + int i, objects_noticed = 0;
51 +
52 + for (i = 0; i < vars->n.size; i++) {
53 + if (vars->n.entries[i].base)
54 + objects_noticed++;
55 + }
56 + if (!check_int(objects_noticed, ==, 2))
57 + test_msg("should have 2 objects");
58 +}
59 +
60 +int cmd_main(int argc UNUSED, const char **argv UNUSED)
61 +{
62 + struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
63 + struct test_vars vars = { 0 };
64 +
65 + vars.one = lookup_unknown_object(the_repository, &one_oid);
66 + vars.two = lookup_unknown_object(the_repository, &two_oid);
67 + vars.three = lookup_unknown_object(the_repository, &three_oid);
68 +
69 + TEST(t_add(&vars),
70 + "Add 2 objects, one with a non-NULL decoration and one with a NULL decoration.");
71 + TEST(t_readd(&vars),
72 + "When re-adding an already existing object, the old decoration is returned.");
73 + TEST(t_lookup(&vars),
74 + "Lookup returns the added declarations, or NULL if the object was never added.");
75 + TEST(t_loop(&vars), "The user can also loop through all entries.");
76 +
77 + clear_decoration(&vars.n, NULL);
78 +
79 + return test_done();
80 +}