Skip to content

Commit

Permalink
Merge branch 'gt/decorate-unit-test'
Browse files Browse the repository at this point in the history
A test helper that essentially is unit tests on the "decorate"
logic has been rewritten using the unit-tests framework.

* gt/decorate-unit-test:
  t/: migrate helper/test-example-decorate to the unit testing framework
  • Loading branch information
gitster committed Jun 12, 2024
2 parents 51ea70c + 456b4dc commit 2a061a6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,6 @@ TEST_BUILTINS_OBJS += test-dump-fsmonitor.o
TEST_BUILTINS_OBJS += test-dump-split-index.o
TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
TEST_BUILTINS_OBJS += test-env-helper.o
TEST_BUILTINS_OBJS += test-example-decorate.o
TEST_BUILTINS_OBJS += test-example-tap.o
TEST_BUILTINS_OBJS += test-find-pack.o
TEST_BUILTINS_OBJS += test-fsmonitor-client.o
Expand Down Expand Up @@ -1334,6 +1333,7 @@ THIRD_PARTY_SOURCES += sha1collisiondetection/%
THIRD_PARTY_SOURCES += sha1dc/%

UNIT_TEST_PROGRAMS += t-ctype
UNIT_TEST_PROGRAMS += t-example-decorate
UNIT_TEST_PROGRAMS += t-hash
UNIT_TEST_PROGRAMS += t-mem-pool
UNIT_TEST_PROGRAMS += t-prio-queue
Expand Down
2 changes: 1 addition & 1 deletion decorate.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
* A data structure that associates Git objects to void pointers. See
* t/helper/test-example-decorate.c for a demonstration of how to use these
* t/unit-tests/t-example-decorate.c for a demonstration of how to use these
* functions.
*/

Expand Down
78 changes: 0 additions & 78 deletions t/helper/test-example-decorate.c

This file was deleted.

1 change: 0 additions & 1 deletion t/helper/test-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ static struct test_cmd cmds[] = {
{ "dump-split-index", cmd__dump_split_index },
{ "dump-untracked-cache", cmd__dump_untracked_cache },
{ "env-helper", cmd__env_helper },
{ "example-decorate", cmd__example_decorate },
{ "example-tap", cmd__example_tap },
{ "find-pack", cmd__find_pack },
{ "fsmonitor-client", cmd__fsmonitor_client },
Expand Down
1 change: 0 additions & 1 deletion t/helper/test-tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ int cmd__dump_split_index(int argc, const char **argv);
int cmd__dump_untracked_cache(int argc, const char **argv);
int cmd__dump_reftable(int argc, const char **argv);
int cmd__env_helper(int argc, const char **argv);
int cmd__example_decorate(int argc, const char **argv);
int cmd__example_tap(int argc, const char **argv);
int cmd__find_pack(int argc, const char **argv);
int cmd__fsmonitor_client(int argc, const char **argv);
Expand Down
12 changes: 0 additions & 12 deletions t/t9004-example.sh

This file was deleted.

80 changes: 80 additions & 0 deletions t/unit-tests/t-example-decorate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "test-lib.h"
#include "object.h"
#include "decorate.h"
#include "repository.h"

struct test_vars {
struct object *one, *two, *three;
struct decoration n;
int decoration_a, decoration_b;
};

static void t_add(struct test_vars *vars)
{
void *ret = add_decoration(&vars->n, vars->one, &vars->decoration_a);

if (!check(ret == NULL))
test_msg("when adding a brand-new object, NULL should be returned");
ret = add_decoration(&vars->n, vars->two, NULL);
if (!check(ret == NULL))
test_msg("when adding a brand-new object, NULL should be returned");
}

static void t_readd(struct test_vars *vars)
{
void *ret = add_decoration(&vars->n, vars->one, NULL);

if (!check(ret == &vars->decoration_a))
test_msg("when readding an already existing object, existing decoration should be returned");
ret = add_decoration(&vars->n, vars->two, &vars->decoration_b);
if (!check(ret == NULL))
test_msg("when readding an already existing object, existing decoration should be returned");
}

static void t_lookup(struct test_vars *vars)
{
void *ret = lookup_decoration(&vars->n, vars->one);

if (!check(ret == NULL))
test_msg("lookup should return added declaration");
ret = lookup_decoration(&vars->n, vars->two);
if (!check(ret == &vars->decoration_b))
test_msg("lookup should return added declaration");
ret = lookup_decoration(&vars->n, vars->three);
if (!check(ret == NULL))
test_msg("lookup for unknown object should return NULL");
}

static void t_loop(struct test_vars *vars)
{
int i, objects_noticed = 0;

for (i = 0; i < vars->n.size; i++) {
if (vars->n.entries[i].base)
objects_noticed++;
}
if (!check_int(objects_noticed, ==, 2))
test_msg("should have 2 objects");
}

int cmd_main(int argc UNUSED, const char **argv UNUSED)
{
struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
struct test_vars vars = { 0 };

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);

TEST(t_add(&vars),
"Add 2 objects, one with a non-NULL decoration and one with a NULL decoration.");
TEST(t_readd(&vars),
"When re-adding an already existing object, the old decoration is returned.");
TEST(t_lookup(&vars),
"Lookup returns the added declarations, or NULL if the object was never added.");
TEST(t_loop(&vars), "The user can also loop through all entries.");

clear_decoration(&vars.n, NULL);

return test_done();
}

0 comments on commit 2a061a6

Please sign in to comment.