From: Phillip Wood <phillip.wood123@gmail.com>
To: "Kyle Lippincott" <spectral@google.com>, "René Scharfe" <l.s.r@web.de>
Cc: Git List <git@vger.kernel.org>,
Phillip Wood <phillip.wood@dunelm.org.uk>,
Josh Steadmon <steadmon@google.com>,
Junio C Hamano <gitster@pobox.com>,
Patrick Steinhardt <ps@pks.im>
Subject: Re: [PATCH v2 2/6] unit-tests: add for_test
Date: Tue, 23 Jul 2024 14:24:31 +0100 [thread overview]
Message-ID: <b4cbc8ce-5e39-491d-a6c9-627a7fd97c8f@gmail.com> (raw)
In-Reply-To: <CAO_smVhoobWpsbYHnHJqTj7TJJ1udo_UaGdbOnUqe5jzL+tyaQ@mail.gmail.com>
On 22/07/2024 20:13, Kyle Lippincott wrote:
> On Sat, Jul 20, 2024 at 11:22 PM René Scharfe <l.s.r@web.de> wrote:
>>
>> The macro TEST only allows defining a test that consists of a single
>> expression. Add a new macro, for_test, which provides a way to define
>> unit tests that are made up of one or more statements.
>>
>> for_test allows defining self-contained tests en bloc, a bit like
>> test_expect_success does for regular tests. It acts like a for loop
>> that runs at most once; the test body is executed if test_skip_all()
>> had not been called before.
>
> I can see based on this description where the name came from, but
> without this context, it's not clear when reading a test what it
> actually does. The name comes from an implementation detail, and is
> not describing what it _does_, just _how_ it does it.
That's my feeling too. In fact I think the name "for_test" actually
works against us by implicitly suggesting that "break" can be used to
exit the test early when in fact that will not work because we'll skip
the call to test__run_end()
>> + * Run a test unless test_skip_all() has been called. Acts like a for
>> + * loop that runs at most once, with the test description between the
>> + * parentheses and the test body as a statement or block after them.
>> + * The description for each test should be unique. E.g.:
>> + *
>> + * for_test ("something else %d %d", arg1, arg2) {
>> + * prepare();
>> + * test_something_else(arg1, arg2);
>> + * cleanup();
>> + * }
>> + */
>> +#define for_test(...) \
>> + for (int for_test_running_ = test__run_begin() ? \
>> + (test__run_end(0, TEST_LOCATION(), __VA_ARGS__), 0) : 1;\
>> + for_test_running_; \
>> + test__run_end(1, TEST_LOCATION(), __VA_ARGS__), \
>> + for_test_running_ = 0)
>
> IMHO: this is borderline "too much magic" for my tastes.
Yes, compared to TEST_RUN() in v1 the magic level has jumped from Muggle
to Dumbledore. Using a for loop is clever as it ensures test__run_end()
is called at the end of the test without the need for separate
TEST_END()[1] macro. The alternative is something like
test_if_enabled()[2] which was discussed in the last round.
[1]
https://lore.kernel.org/git/4f41f509-1d44-4476-92b0-9bb643f64576@gmail.com
[2] https://lore.kernel.org/git/xmqqa5iot01s.fsf@gitster.g
> I think
> having multiple test functions is generally easier to understand, and
> the overhead isn't really relevant. It's not _as_ compact in the
> source file, and requires that we have both the TEST statement and the
> function (and forgetting the TEST statement means that we won't invoke
> the function). If that is the main issue we're facing here, I wonder
> if there's some other way of resolving that (such as unused function
> detection via some compiler flags; even if it's not detected on all
> platforms, getting at least one of the CI platforms should be
> sufficient to prevent the issue [but we should target as many as
> possible, so it's caught earlier than "I tried to send it to the
> list"])
I also have a preference for function based tests but I do think
something like for_test() can be useful in certain situations. For
example in test-ctype.c where testing the ctype macros leads to a lot of
repetition or a giant macro with a function based approach. If isalpha()
and friends were functions instead we could write a single helper
function which is passed the function under test together with the input
data and expect result. Because they are macros that approach does not work.
Another example is where we are using a helper function with several
inputs and we would prefer to write
for_test("test 1") {
int input[] = ...
int expect[] = ...
test_helper(input, expect);
...
for_test("test 10") {
int input[] = ...
int expect[] = ...
test_helper(input, expect);
}
rather then declaring all the input and expect variables up front with
int input_1 = ...
int input_2 = ...
...
int expect_1 = ...
int expect_2 = ...
TEST(test_helper(input_1, expect_1), "test 1");
...
TEST(test_helper(input_10, expect_10), "test 10");
> If others agree that this is a good simplification for the people
> reading the test code (and hopefully for the test author), I'm fine
> with this going in (with a different name). I'm not trying to veto the
> concept.
That's my feeling too.
Best Wishes
Phillip
>
>> +
>> /*
>> * Print a test plan, should be called before any tests. If the number
>> * of tests is not known in advance test_done() will automatically
>> --
>> 2.45.2
>>
>
next prev parent reply other threads:[~2024-07-23 13:24 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 [this message]
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 ` [PATCH v4 3/6] unit-tests: add if_test René Scharfe
2024-07-31 22:04 ` 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=b4cbc8ce-5e39-491d-a6c9-627a7fd97c8f@gmail.com \
--to=phillip.wood123@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
--cc=phillip.wood@dunelm.org.uk \
--cc=ps@pks.im \
--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).