Skip to content

Commit 456b4dc

Browse files
spectre10gitster
authored andcommitted
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 <[email protected]> Mentored-by: Kaartic Sivaraam <[email protected]> Signed-off-by: Ghanshyam Thakkar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7a1d47 commit 456b4dc

File tree

7 files changed

+82
-94
lines changed

7 files changed

+82
-94
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,6 @@ TEST_BUILTINS_OBJS += test-dump-fsmonitor.o
793793
TEST_BUILTINS_OBJS += test-dump-split-index.o
794794
TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
795795
TEST_BUILTINS_OBJS += test-env-helper.o
796-
TEST_BUILTINS_OBJS += test-example-decorate.o
797796
TEST_BUILTINS_OBJS += test-example-tap.o
798797
TEST_BUILTINS_OBJS += test-find-pack.o
799798
TEST_BUILTINS_OBJS += test-fsmonitor-client.o
@@ -1334,6 +1333,7 @@ THIRD_PARTY_SOURCES += compat/regex/%
13341333
THIRD_PARTY_SOURCES += sha1collisiondetection/%
13351334
THIRD_PARTY_SOURCES += sha1dc/%
13361335

1336+
UNIT_TEST_PROGRAMS += t-example-decorate
13371337
UNIT_TEST_PROGRAMS += t-mem-pool
13381338
UNIT_TEST_PROGRAMS += t-strbuf
13391339
UNIT_TEST_PROGRAMS += t-ctype

decorate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/*
55
* 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
77
* functions.
88
*/
99

t/helper/test-example-decorate.c

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

t/helper/test-tool.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ static struct test_cmd cmds[] = {
2929
{ "dump-split-index", cmd__dump_split_index },
3030
{ "dump-untracked-cache", cmd__dump_untracked_cache },
3131
{ "env-helper", cmd__env_helper },
32-
{ "example-decorate", cmd__example_decorate },
3332
{ "example-tap", cmd__example_tap },
3433
{ "find-pack", cmd__find_pack },
3534
{ "fsmonitor-client", cmd__fsmonitor_client },

t/helper/test-tool.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ int cmd__dump_split_index(int argc, const char **argv);
2323
int cmd__dump_untracked_cache(int argc, const char **argv);
2424
int cmd__dump_reftable(int argc, const char **argv);
2525
int cmd__env_helper(int argc, const char **argv);
26-
int cmd__example_decorate(int argc, const char **argv);
2726
int cmd__example_tap(int argc, const char **argv);
2827
int cmd__find_pack(int argc, const char **argv);
2928
int cmd__fsmonitor_client(int argc, const char **argv);

t/t9004-example.sh

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

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)