Skip to content

Commit

Permalink
apply: rewrite unit tests with structured cases
Browse files Browse the repository at this point in the history
The imperative format was a little hard to read, so I rewrote the test cases in
a declarative style by defining a common structure for each test case and its
assertions.

Signed-off-by: Philip Peterson <philip.c.peterson@gmail.com>
  • Loading branch information
philip-peterson committed Feb 19, 2024
1 parent 9f88d9c commit 62c536e
Showing 1 changed file with 78 additions and 42 deletions.
120 changes: 78 additions & 42 deletions t/unit-tests/t-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,101 @@

#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)
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 = 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 result = 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)
{
char* text;

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

t/unit-tests/t-apply.c:27:8: unused variable 'text' [-Werror=unused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-musl (alpine)

t/unit-tests/t-apply.c:27:15: unused variable 'text' [-Werror=unused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora)

t/unit-tests/t-apply.c:27:15: unused variable 'text' [-Werror=unused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

t/unit-tests/t-apply.c:27:8: unused variable 'text' [-Werror,-Wunused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-gcc (ubuntu-20.04)

t/unit-tests/t-apply.c:27:8: unused variable ‘text’ [-Werror=unused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-gcc-default (ubuntu-latest)

t/unit-tests/t-apply.c:27:15: unused variable ‘text’ [-Werror=unused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-leaks (ubuntu-latest)

t/unit-tests/t-apply.c:27:15: unused variable ‘text’ [-Werror=unused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

t/unit-tests/t-apply.c:27:8: unused variable 'text' [-Werror,-Wunused-variable]

Check failure on line 27 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-TEST-vars (ubuntu-20.04)

t/unit-tests/t-apply.c:27:8: unused variable ‘text’ [-Werror=unused-variable]
int expected_result;

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

t/unit-tests/t-apply.c:28:6: unused variable 'expected_result' [-Werror=unused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-musl (alpine)

t/unit-tests/t-apply.c:28:13: unused variable 'expected_result' [-Werror=unused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora)

t/unit-tests/t-apply.c:28:13: unused variable 'expected_result' [-Werror=unused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

t/unit-tests/t-apply.c:28:6: unused variable 'expected_result' [-Werror,-Wunused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-gcc (ubuntu-20.04)

t/unit-tests/t-apply.c:28:6: unused variable ‘expected_result’ [-Werror=unused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-gcc-default (ubuntu-latest)

t/unit-tests/t-apply.c:28:13: unused variable ‘expected_result’ [-Werror=unused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-leaks (ubuntu-latest)

t/unit-tests/t-apply.c:28:13: unused variable ‘expected_result’ [-Werror=unused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

t/unit-tests/t-apply.c:28:6: unused variable 'expected_result' [-Werror,-Wunused-variable]

Check failure on line 28 in t/unit-tests/t-apply.c

View workflow job for this annotation

GitHub Actions / linux-TEST-vars (ubuntu-20.04)

t/unit-tests/t-apply.c:28:6: unused variable ‘expected_result’ [-Werror=unused-variable]

/* Success */
text = "@@ -4,4 +";
expected_result = 9;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4),
"well-formed range");
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");

text = "@@ -4 +8 @@";
expected_result = 7;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 1),
"non-comma 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");

/* Failure */
text = "@@ -X,4 +";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 9999, 9999),
"non-digit range (first coordinate)");
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)");

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

text = "@@ -4,4 -";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 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
}), "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");
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");

text = "@@ -4,4";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), 7, " +", expected_result, 9999, 9999),
"not long enough for offset");
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");

text = "@@ -4,4";
expected_result = FAILURE;
TEST(setup_static(text, strlen(text), -1, " +", expected_result, 9999, 9999),
"negative 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 62c536e

Please sign in to comment.