git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Philip Peterson via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Philip Peterson <philip.c.peterson@gmail.com>,
	Philip Peterson <philip.c.peterson@gmail.com>
Subject: [PATCH 1/2] apply: add unit tests for parse_range and rename to parse_fragment_range
Date: Mon, 19 Feb 2024 04:45:37 +0000	[thread overview]
Message-ID: <2c60c4406d4eb1307a32f23604f3ef8e34ad56d6.1708317938.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1677.git.git.1708317938.gitgitgadget@gmail.com>

From: Philip Peterson <philip.c.peterson@gmail.com>

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>
---
 Makefile               |  1 +
 apply.c                |  8 ++---
 apply.h                |  4 +++
 t/unit-tests/t-apply.c | 67 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 76 insertions(+), 4 deletions(-)
 create mode 100644 t/unit-tests/t-apply.c

diff --git a/Makefile b/Makefile
index 15990ff3122..369092aedfe 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/apply.c b/apply.c
index 7608e3301ca..199a1150df6 100644
--- a/apply.c
+++ b/apply.c
@@ -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;
 
@@ -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;
 }
diff --git a/apply.h b/apply.h
index 7cd38b1443c..bbc5e3caeb5 100644
--- a/apply.h
+++ b/apply.h
@@ -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);
diff --git a/t/unit-tests/t-apply.c b/t/unit-tests/t-apply.c
new file mode 100644
index 00000000000..ff0abfb2e0b
--- /dev/null
+++ b/t/unit-tests/t-apply.c
@@ -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();
+}
-- 
gitgitgadget


  reply	other threads:[~2024-02-19  4:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-19  4:45 [PATCH 0/2] apply: add unit tests for parse_range Philip Peterson via GitGitGadget
2024-02-19  4:45 ` Philip Peterson via GitGitGadget [this message]
2024-02-19 21:35   ` [PATCH 1/2] apply: add unit tests for parse_range and rename to parse_fragment_range Junio C Hamano
2024-04-04  3:53     ` Philip
2024-04-04 19:27       ` Junio C Hamano
2024-02-19  4:45 ` [PATCH 2/2] apply: rewrite unit tests with structured cases Philip Peterson via GitGitGadget
2024-02-19 21:49   ` Junio C Hamano
2024-02-19 22:04   ` Kristoffer Haugsbakk
2024-05-26  7:54 ` [PATCH v2] apply: add unit tests for parse_range Philip Peterson via GitGitGadget
2024-06-06 17:24   ` Junio C Hamano
2024-06-07 15:00   ` Phillip Wood
2024-06-07 16:59     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2c60c4406d4eb1307a32f23604f3ef8e34ad56d6.1708317938.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=philip.c.peterson@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).