Skip to content

Commit

Permalink
apply: add unit tests for parse_range and rename parse_range to parse…
Browse files Browse the repository at this point in the history
…_fragment_range

This patchset makes the parse_range function in apply be non-internal linkage
in order to expose to the unit testing framework. In so doing, because there is
another function called parse_range, I gave this one a more specific name,
parse_fragment_range. Other than that, this commit adds several test cases
(positive and negative) for the function.

Signed-off-by: Philip Peterson <philip.c.peterson@gmail.com>
  • Loading branch information
philip-peterson committed Feb 19, 2024
1 parent 186b115 commit 9f88d9c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += compat/regex/%
THIRD_PARTY_SOURCES += sha1collisiondetection/%
THIRD_PARTY_SOURCES += sha1dc/%

UNIT_TEST_PROGRAMS += t-apply
UNIT_TEST_PROGRAMS += t-basic
UNIT_TEST_PROGRAMS += t-mem-pool
UNIT_TEST_PROGRAMS += t-strbuf
Expand Down
8 changes: 4 additions & 4 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,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 parse_fragment_range(const char *line, int len, int offset, const char *expect,
unsigned long *p1, unsigned long *p2)
{
int digits, ex;

Expand Down Expand Up @@ -1530,8 +1530,8 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
return -1;

/* Figure out the number of lines in a fragment */
offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
offset = parse_fragment_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
offset = parse_fragment_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);

return offset;
}
Expand Down
4 changes: 4 additions & 0 deletions apply.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,7 @@ int apply_all_patches(struct apply_state *state,
int options);

#endif


int parse_fragment_range(const char *line, int len, int offset, const char *expect,
unsigned long *p1, unsigned long *p2);
67 changes: 67 additions & 0 deletions t/unit-tests/t-apply.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "test-lib.h"
#include "apply.h"

#define FAILURE -1

static void setup_static(const char *line, int len, int offset,
const char *expect, int assert_result,
unsigned long assert_p1,
unsigned long assert_p2)
{
unsigned long p1 = 9999;
unsigned long p2 = 9999;
int result = parse_fragment_range(line, len, offset, expect, &p1, &p2);
check_int(result, ==, assert_result);
check_int(p1, ==, assert_p1);
check_int(p2, ==, assert_p2);
}

int cmd_main(int argc, const char **argv)
{
char* text;
int expected_result;

/* Success */
text = "@@ -4,4 +";
expected_result = 9;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4),
"well-formed range");

text = "@@ -4 +8 @@";
expected_result = 7;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 1),
"non-comma range");

/* Failure */
text = "@@ -X,4 +";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 9999, 9999),
"non-digit range (first coordinate)");

text = "@@ -4,X +";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 1), // p2 is 1, a little strange but not catastrophic
"non-digit range (second coordinate)");

text = "@@ -4,4 -";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4),
"non-expected trailing text");

text = "@@ -4,4";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4),
"not long enough for expected trailing text");

text = "@@ -4,4";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 7, " +", expected_result, 9999, 9999),
"not long enough for offset");

text = "@@ -4,4";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), -1, " +", expected_result, 9999, 9999),
"negative offset");

return test_done();
}

0 comments on commit 9f88d9c

Please sign in to comment.