git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Git List <git@vger.kernel.org>
Cc: Phillip Wood <phillip.wood@dunelm.org.uk>,
	Josh Steadmon <steadmon@google.com>,
	Kyle Lippincott <spectral@google.com>
Subject: [PATCH v4 3/6] unit-tests: add if_test
Date: Tue, 30 Jul 2024 16:08:19 +0200	[thread overview]
Message-ID: <da902de9-288e-4783-8b4b-a2a968d1e1f8@web.de> (raw)
In-Reply-To: <077a178e-eb30-45ff-b653-a514bfd33077@web.de>

The macro TEST only allows defining a test that consists of a single
expression.  Add a new macro, if_test, which provides a way to define
unit tests that are made up of one or more statements.

if_test allows defining self-contained tests en bloc, a bit like
test_expect_success does for regular tests.  It acts like a conditional;
the test body is executed if test_skip_all() had not been called before.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 .clang-format               |  5 +++++
 t/helper/test-example-tap.c | 33 +++++++++++++++++++++++++++++++++
 t/t0080-unit-test-output.sh | 35 ++++++++++++++++++++++++++++++++++-
 t/unit-tests/test-lib.c     | 29 +++++++++++++++++++++++++++++
 t/unit-tests/test-lib.h     | 20 ++++++++++++++++++++
 5 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/.clang-format b/.clang-format
index 6408251577..4c6d317508 100644
--- a/.clang-format
+++ b/.clang-format
@@ -169,6 +169,11 @@ ForEachMacros:
   - 'strmap_for_each_entry'
   - 'strset_for_each_entry'

+# A list of macros that should be interpreted as conditionals instead of as
+# function calls.
+IfMacros:
+  - 'if_test'
+
 # The maximum number of consecutive empty lines to keep.
 MaxEmptyLinesToKeep: 1

diff --git a/t/helper/test-example-tap.c b/t/helper/test-example-tap.c
index 79c12b01cd..914af88e0a 100644
--- a/t/helper/test-example-tap.c
+++ b/t/helper/test-example-tap.c
@@ -94,5 +94,38 @@ int cmd__example_tap(int argc, const char **argv)
 	test_res = TEST(t_empty(), "test with no checks");
 	TEST(check_int(test_res, ==, 0), "test with no checks returns 0");

+	if_test ("if_test passing test")
+		check_int(1, ==, 1);
+	if_test ("if_test failing test")
+		check_int(1, ==, 2);
+	if_test ("if_test passing TEST_TODO()")
+		TEST_TODO(check(0));
+	if_test ("if_test failing TEST_TODO()")
+		TEST_TODO(check(1));
+	if_test ("if_test test_skip()") {
+		check(0);
+		test_skip("missing prerequisite");
+		check(1);
+	}
+	if_test ("if_test test_skip() inside TEST_TODO()")
+		TEST_TODO((test_skip("missing prerequisite"), 1));
+	if_test ("if_test TEST_TODO() after failing check") {
+		check(0);
+		TEST_TODO(check(0));
+	}
+	if_test ("if_test failing check after TEST_TODO()") {
+		check(1);
+		TEST_TODO(check(0));
+		check(0);
+	}
+	if_test ("if_test messages from failing string and char comparison") {
+		check_str("\thello\\", "there\"\n");
+		check_str("NULL", NULL);
+		check_char('a', ==, '\n');
+		check_char('\\', ==, '\'');
+	}
+	if_test ("if_test test with no checks")
+		; /* nothing */
+
 	return test_done();
 }
diff --git a/t/t0080-unit-test-output.sh b/t/t0080-unit-test-output.sh
index fe221f3bdb..3c369c88e2 100755
--- a/t/t0080-unit-test-output.sh
+++ b/t/t0080-unit-test-output.sh
@@ -50,7 +50,40 @@ test_expect_success 'TAP output from unit tests' - <<\EOT
 	# BUG: test has no checks at t/helper/test-example-tap.c:94
 	not ok 18 - test with no checks
 	ok 19 - test with no checks returns 0
-	1..19
+	ok 20 - if_test passing test
+	# check "1 == 2" failed at t/helper/test-example-tap.c:100
+	#    left: 1
+	#   right: 2
+	not ok 21 - if_test failing test
+	not ok 22 - if_test passing TEST_TODO() # TODO
+	# todo check 'check(1)' succeeded at t/helper/test-example-tap.c:104
+	not ok 23 - if_test failing TEST_TODO()
+	# check "0" failed at t/helper/test-example-tap.c:106
+	# skipping test - missing prerequisite
+	# skipping check '1' at t/helper/test-example-tap.c:108
+	ok 24 - if_test test_skip() # SKIP
+	# skipping test - missing prerequisite
+	ok 25 - if_test test_skip() inside TEST_TODO() # SKIP
+	# check "0" failed at t/helper/test-example-tap.c:113
+	not ok 26 - if_test TEST_TODO() after failing check
+	# check "0" failed at t/helper/test-example-tap.c:119
+	not ok 27 - if_test failing check after TEST_TODO()
+	# check "!strcmp("\thello\\\\", "there\"\n")" failed at t/helper/test-example-tap.c:122
+	#    left: "\011hello\\\\"
+	#   right: "there\"\012"
+	# check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:123
+	#    left: "NULL"
+	#   right: NULL
+	# check "'a' == '\n'" failed at t/helper/test-example-tap.c:124
+	#    left: 'a'
+	#   right: '\012'
+	# check "'\\\\' == '\\''" failed at t/helper/test-example-tap.c:125
+	#    left: '\\\\'
+	#   right: '\\''
+	not ok 28 - if_test messages from failing string and char comparison
+	# BUG: test has no checks at t/helper/test-example-tap.c:127
+	not ok 29 - if_test test with no checks
+	1..29
 	EOF

 	! test-tool example-tap >actual &&
diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c
index 989dc758e6..fa1f95965c 100644
--- a/t/unit-tests/test-lib.c
+++ b/t/unit-tests/test-lib.c
@@ -16,6 +16,8 @@ static struct {
 	unsigned running :1;
 	unsigned skip_all :1;
 	unsigned todo :1;
+	char location[100];
+	char description[100];
 } ctx = {
 	.lazy_plan = 1,
 	.result = RESULT_NONE,
@@ -125,6 +127,8 @@ void test_plan(int count)

 int test_done(void)
 {
+	if (ctx.running && ctx.location[0] && ctx.description[0])
+		test__run_end(1, ctx.location, "%s", ctx.description);
 	assert(!ctx.running);

 	if (ctx.lazy_plan)
@@ -167,13 +171,38 @@ void test_skip_all(const char *format, ...)
 	va_end(ap);
 }

+void test__run_describe(const char *location, const char *format, ...)
+{
+	va_list ap;
+	int len;
+
+	assert(ctx.running);
+	assert(!ctx.location[0]);
+	assert(!ctx.description[0]);
+
+	xsnprintf(ctx.location, sizeof(ctx.location), "%s",
+		  make_relative(location));
+
+	va_start(ap, format);
+	len = vsnprintf(ctx.description, sizeof(ctx.description), format, ap);
+	va_end(ap);
+	if (len < 0)
+		die("unable to format message: %s", format);
+	if (len >= sizeof(ctx.description))
+		BUG("ctx.description too small to format %s", format);
+}
+
 int test__run_begin(void)
 {
+	if (ctx.running && ctx.location[0] && ctx.description[0])
+		test__run_end(1, ctx.location, "%s", ctx.description);
 	assert(!ctx.running);

 	ctx.count++;
 	ctx.result = RESULT_NONE;
 	ctx.running = 1;
+	ctx.location[0] = '\0';
+	ctx.description[0] = '\0';

 	return ctx.skip_all;
 }
diff --git a/t/unit-tests/test-lib.h b/t/unit-tests/test-lib.h
index 2de6d715d5..f15dceb29e 100644
--- a/t/unit-tests/test-lib.h
+++ b/t/unit-tests/test-lib.h
@@ -14,6 +14,23 @@
 	test__run_end(test__run_begin() ? 0 : (t, 1),	\
 		      TEST_LOCATION(),  __VA_ARGS__)

+/*
+ * Run a test unless test_skip_all() has been called.  Acts like a
+ * conditional; the test body is expected as a statement or block after
+ * the closing parenthesis.  The description for each test should be
+ * unique.  E.g.:
+ *
+ *  if_test ("something else %d %d", arg1, arg2) {
+ *          prepare();
+ *          test_something_else(arg1, arg2);
+ *          cleanup();
+ *  }
+ */
+#define if_test(...)							\
+	if (test__run_begin() ?						\
+	    (test__run_end(0, TEST_LOCATION(),  __VA_ARGS__), 0) :	\
+	    (test__run_describe(TEST_LOCATION(),  __VA_ARGS__), 1))
+
 /*
  * Print a test plan, should be called before any tests. If the number
  * of tests is not known in advance test_done() will automatically
@@ -153,6 +170,9 @@ union test__tmp {

 extern union test__tmp test__tmp[2];

+__attribute__((format (printf, 2, 3)))
+void test__run_describe(const char *, const char *, ...);
+
 int test__run_begin(void);
 __attribute__((format (printf, 3, 4)))
 int test__run_end(int, const char *, const char *, ...);
--
2.46.0

  parent reply	other threads:[~2024-07-30 14:08 UTC|newest]

Thread overview: 115+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-29 15:33 [PATCH 0/6] unit-tests: add and use TEST_RUN to simplify tests René Scharfe
2024-06-29 15:35 ` [PATCH 1/6] t0080: move expected output to a file René Scharfe
2024-07-01  3:20   ` Jeff King
2024-07-01 19:17     ` Junio C Hamano
2024-07-01 22:10       ` Jeff King
2024-07-01 23:38         ` Junio C Hamano
2024-07-02  0:57           ` Jeff King
2024-07-01 19:51     ` René Scharfe
2024-07-01 22:18       ` Jeff King
2024-06-29 15:43 ` [PATCH 2/6] unit-tests: add TEST_RUN René Scharfe
2024-07-02 15:13   ` phillip.wood123
2024-07-02 15:51     ` Junio C Hamano
2024-07-02 20:55       ` René Scharfe
2024-07-02 20:55     ` René Scharfe
2024-07-05  9:42       ` phillip.wood123
2024-07-05 18:01         ` René Scharfe
2024-07-07  7:20           ` René Scharfe
2024-07-08 15:18             ` phillip.wood123
2024-07-08 15:39               ` Junio C Hamano
2024-07-11 15:34                 ` Junio C Hamano
2024-07-13 13:27                   ` Phillip Wood
2024-07-13 15:48                     ` Junio C Hamano
2024-07-08 15:12           ` phillip.wood123
2024-06-29 15:44 ` [PATCH 3/6] t-ctype: use TEST_RUN René Scharfe
2024-07-01 19:49   ` Josh Steadmon
2024-07-01 20:04     ` René Scharfe
2024-07-02 15:14       ` phillip.wood123
2024-07-02 20:55         ` René Scharfe
2024-06-29 15:45 ` [PATCH 4/6] t-reftable-basics: " René Scharfe
2024-06-29 15:46 ` [PATCH 5/6] t-strvec: " René Scharfe
2024-07-02 15:14   ` phillip.wood123
2024-07-02 20:55     ` René Scharfe
2024-06-29 15:47 ` [PATCH 6/6] t-strbuf: " René Scharfe
2024-07-01 19:58   ` Josh Steadmon
2024-07-01 20:18     ` René Scharfe
2024-07-02 15:14     ` phillip.wood123
2024-07-02 20:55       ` René Scharfe
2024-07-04 13:09         ` phillip.wood123
2024-07-10 13:55       ` Phillip Wood
2024-07-14 11:44         ` René Scharfe
2024-07-15 14:46         ` Ghanshyam Thakkar
2024-07-02 17:29     ` Ghanshyam Thakkar
2024-07-02 20:55       ` René Scharfe
2024-07-03  3:42         ` Ghanshyam Thakkar
2024-07-08 18:11       ` Josh Steadmon
2024-07-08 21:59         ` Ghanshyam Thakkar
2024-07-01 19:59 ` [PATCH 0/6] unit-tests: add and use TEST_RUN to simplify tests Josh Steadmon
2024-07-10 22:13 ` Junio C Hamano
2024-07-11 10:05   ` Phillip Wood
2024-07-11 15:12     ` Junio C Hamano
2024-07-14 10:35     ` René Scharfe
2024-07-21  6:12 ` [PATCH v2 0/6] unit-tests: add and use for_test " René Scharfe
2024-07-21  6:15   ` [PATCH v2 1/6] t0080: move expected output to a file René Scharfe
2024-07-23 20:54     ` Jeff King
2024-07-21  6:21   ` [PATCH v2 2/6] unit-tests: add for_test René Scharfe
2024-07-22 19:13     ` Kyle Lippincott
2024-07-22 19:36       ` Junio C Hamano
2024-07-22 20:31         ` René Scharfe
2024-07-22 20:41           ` Junio C Hamano
2024-07-22 22:47           ` Kyle Lippincott
2024-07-23 12:37             ` René Scharfe
2024-07-23  6:02           ` [PATCH v2] unit-tests: show location of checks outside of tests René Scharfe
2024-07-23 13:25             ` Phillip Wood
2024-07-22 22:41         ` [PATCH v2 2/6] unit-tests: add for_test Kyle Lippincott
2024-07-23  7:18           ` René Scharfe
2024-07-23  6:36         ` Patrick Steinhardt
2024-07-23  9:25           ` René Scharfe
2024-07-23  9:53             ` Patrick Steinhardt
2024-07-23 12:37               ` René Scharfe
2024-07-23 13:00                 ` Patrick Steinhardt
2024-07-23 13:23                 ` Phillip Wood
2024-07-23 13:58                   ` René Scharfe
2024-07-23 13:24       ` Phillip Wood
2024-07-25  9:45         ` Phillip Wood
2024-07-30 14:00           ` René Scharfe
2024-07-21  6:22   ` [PATCH v2 3/6] t-ctype: use for_test René Scharfe
2024-07-21  6:23   ` [PATCH v2 4/6] t-reftable-basics: " René Scharfe
2024-07-21  6:24   ` [PATCH v2 5/6] t-strvec: " René Scharfe
2024-07-21  6:26   ` [PATCH v2 6/6] t-strbuf: " René Scharfe
2024-07-23 13:23     ` Phillip Wood
2024-07-24 14:42 ` [PATCH v3 0/7] add and use for_test to simplify tests René Scharfe
2024-07-24 14:48   ` [PATCH v3 1/7] t0080: use here-doc test body René Scharfe
2024-07-24 14:50   ` [PATCH v3 2/7] unit-tests: show location of checks outside of tests René Scharfe
2024-07-24 14:51   ` [PATCH v3 3/7] unit-tests: add for_test René Scharfe
2024-07-24 19:24     ` Kyle Lippincott
2024-07-25  9:45       ` Phillip Wood
2024-07-25 16:02       ` Junio C Hamano
2024-07-25 21:31         ` Kyle Lippincott
2024-07-26  2:41           ` Junio C Hamano
2024-07-26 12:56             ` Patrick Steinhardt
2024-07-26 15:59               ` Junio C Hamano
2024-07-29  9:48                 ` Patrick Steinhardt
2024-07-29 18:55                   ` Junio C Hamano
2024-07-30  4:49                     ` Patrick Steinhardt
2024-07-30 14:00                       ` René Scharfe
2024-07-31  5:19                         ` Patrick Steinhardt
2024-07-31 16:48                           ` René Scharfe
2024-08-01  6:51                             ` Patrick Steinhardt
2024-07-24 14:52   ` [PATCH v3 4/7] t-ctype: use for_test René Scharfe
2024-07-24 14:54   ` [PATCH v3 5/7] t-reftable-basics: " René Scharfe
2024-07-24 14:54   ` [PATCH v3 6/7] t-strvec: " René Scharfe
2024-07-24 14:55   ` [PATCH v3 7/7] t-strbuf: " René Scharfe
2024-07-30 14:03 ` [PATCH v4 0/6] add and use if_test to simplify tests René Scharfe
2024-07-30 14:05   ` [PATCH v4 1/6] t0080: use here-doc test body René Scharfe
2024-07-31 20:52     ` Kyle Lippincott
2024-07-30 14:07   ` [PATCH v4 2/6] unit-tests: show location of checks outside of tests René Scharfe
2024-07-31 21:03     ` Kyle Lippincott
2024-08-01  7:23       ` René Scharfe
2024-07-30 14:08   ` René Scharfe [this message]
2024-07-31 22:04     ` [PATCH v4 3/6] unit-tests: add if_test Kyle Lippincott
2024-08-01  7:32       ` René Scharfe
2024-08-02  0:48         ` Kyle Lippincott
2024-07-30 14:10   ` [PATCH v4 4/6] t-ctype: use if_test René Scharfe
2024-07-30 14:10   ` [PATCH v4 5/6] t-reftable-basics: " René Scharfe
2024-07-30 14:12   ` [PATCH v4 6/6] t-strvec: " René Scharfe

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=da902de9-288e-4783-8b4b-a2a968d1e1f8@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=spectral@google.com \
    --cc=steadmon@google.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).