Skip to content

Commit

Permalink
apply: add unit tests for parse_range
Browse files Browse the repository at this point in the history
Also rename parse_range to parse_fragment_range for external linkage.

Signed-off-by: Philip Peterson <[email protected]>
  • Loading branch information
philip-peterson committed May 18, 2024
1 parent d8ab1d4 commit 2126e44
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,7 @@ THIRD_PARTY_SOURCES += compat/regex/%
THIRD_PARTY_SOURCES += sha1collisiondetection/%
THIRD_PARTY_SOURCES += sha1dc/%

UNIT_TEST_PROGRAMS += t-apply
UNIT_TEST_PROGRAMS += t-mem-pool
UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-ctype
Expand Down
6 changes: 4 additions & 2 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "wildmatch.h"
#include "ws.h"

#define parse_range apply_parse_fragment_range

struct gitdiff_data {
struct strbuf *root;
int linenr;
Expand Down Expand Up @@ -1438,8 +1440,8 @@ static int parse_num(const char *line, unsigned long *p)
return ptr - line;
}

static int parse_range(const char *line, int len, int offset, const char *expect,
unsigned long *p1, unsigned long *p2)
int apply_parse_fragment_range(const char *line, int len, int offset, const char *expect,
unsigned long *p1, unsigned long *p2)
{
int digits, ex;

Expand Down
8 changes: 8 additions & 0 deletions apply.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,12 @@ int apply_all_patches(struct apply_state *state,
int argc, const char **argv,
int options);

/*
* exposed only for tests; do not call this as it not
* a part of the API
*/
extern int apply_parse_fragment_range(const char *line, int len, int offset,
const char *expect, unsigned long *p1,
unsigned long *p2);

#endif
101 changes: 101 additions & 0 deletions t/unit-tests/t-apply.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "test-lib.h"
#include "apply.h"

#define FAILURE -1

typedef struct test_case {
const char *line;
const char *expect_suffix;
int offset;
unsigned long expect_p1;
unsigned long expect_p2;
int expect_result;
} test_case;

static void setup_static(struct test_case t)
{
unsigned long p1 = 9999;
unsigned long p2 = 9999;
int result = apply_parse_fragment_range(t.line, strlen(t.line), t.offset,
t.expect_suffix, &p1, &p2);
check_int(result, ==, t.expect_result);
check_int(p1, ==, t.expect_p1);
check_int(p2, ==, t.expect_p2);
}

int cmd_main(int argc, const char **argv)
{
TEST(setup_static((struct test_case) {
.line = "@@ -4,4 +",
.offset = 4,
.expect_suffix = " +",
.expect_result = 9,
.expect_p1 = 4,
.expect_p2 = 4
}), "well-formed range");

TEST(setup_static((struct test_case) {
.line = "@@ -4 +8 @@",
.offset = 4,
.expect_suffix = " +",
.expect_result = 7,
.expect_p1 = 4,
.expect_p2 = 1
}), "non-comma range");

TEST(setup_static((struct test_case) {
.line = "@@ -X,4 +",
.offset = 4,
.expect_suffix = " +",
.expect_result = FAILURE,
.expect_p1 = 9999,
.expect_p2 = 9999
}), "non-digit range (first coordinate)");

TEST(setup_static((struct test_case) {
.line = "@@ -4,X +",
.offset = 4,
.expect_suffix = " +",
.expect_result = FAILURE,
.expect_p1 = 4,
.expect_p2 = 1 // A little strange this is 1, but not end of the world
}), "non-digit range (second coordinate)");

TEST(setup_static((struct test_case) {
.line = "@@ -4,4 -",
.offset = 4,
.expect_suffix = " +",
.expect_result = FAILURE,
.expect_p1 = 4,
.expect_p2 = 4
}), "non-expected trailing text");

TEST(setup_static((struct test_case) {
.line = "@@ -4,4",
.offset = 4,
.expect_suffix = " +",
.expect_result = FAILURE,
.expect_p1 = 4,
.expect_p2 = 4
}), "not long enough for expected trailing text");

TEST(setup_static((struct test_case) {
.line = "@@ -4,4",
.offset = 7,
.expect_suffix = " +",
.expect_result = FAILURE,
.expect_p1 = 9999,
.expect_p2 = 9999
}), "not long enough for offset");

TEST(setup_static((struct test_case) {
.line = "@@ -4,4",
.offset = -1,
.expect_suffix = " +",
.expect_result = FAILURE,
.expect_p1 = 9999,
.expect_p2 = 9999
}), "negative offset");

return test_done();
}

0 comments on commit 2126e44

Please sign in to comment.