git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 1/4] t/unit-tests: convert hashmap test to use clar test framework

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 3, 2025, 07:30 UTC
Message-ID
<Z6Bwr6hM54nu8nSS@pks.im>
In-Reply-To
<6be977a0-4bf9-4568-9b28-cdc988a49b89@gmail.com>
On Sun, Feb 02, 2025 at 11:09:25AM +0000, phillip.wood123@gmail.com wrote:
Show 13 quoted lines
> On 31/01/2025 22:14, Seyi Kuforiji wrote:
> > -	if (check(entry != NULL))
> > -		check_str(get_value(entry), "value3");
> > +	cl_assert(entry != NULL);
> > +	cl_assert_equal_s(get_value(entry), "value3");
> 
> Unfortunately cl_assert_equal_s() is not equivalent to check_str()
> because it does not handle NULL correctly. I think it would be very
> helpful to fix that upstream. The diff below shows a possible solution
> which avoids any ambiguity as to which pointer is NULL by only quoting
> non-NULL values. In the long run it would be good to fix
> cl_assert_equal_s() to properly quote control characters as check_str()
> does.
That's indeed something we should fix in clar.
Show 18 quoted lines
> diff --git a/t/unit-tests/clar/clar.c b/t/unit-tests/clar/clar.c
> index d54e4553674..16f86c952f7 100644
> --- a/t/unit-tests/clar/clar.c
> +++ b/t/unit-tests/clar/clar.c
> @@ -754,7 +754,12 @@ void clar__assert_equal(
>                                  p_snprintf(buf, sizeof(buf), "'%s' != '%s' (at byte %d)",
>                                          s1, s2, pos);
>                          } else {
> -                                p_snprintf(buf, sizeof(buf), "'%s' != '%s'", s1, s2);
> +                                const char *q1 = s1 ? "'" : "";
> +                                const char *q2 = s2 ? "'" : "";
> +                                s1 = s1 ? s1 : "NULL";
> +                                s2 = s2 ? s2 : "NULL";
> +                                p_snprintf(buf, sizeof(buf), "%s%s%s != %s%s%s",
> +                                           q1, s1, q1, q2, s2, q2);
>                          }
>                  }
>          }

Would you mind creating an upstream pull request with these changes? I'm happy to review, and then we can update our embedded version of clar.

Show 10 quoted lines
> >   	for (size_t i = 0; i < ARRAY_SIZE(query); i++) {
> >   		entry = get_test_entry(map, query[i][0], ignore_case);
> > -		if (check(entry != NULL))
> > -			check_str(get_value(entry), query[i][1]);
> > -		else
> > -			test_msg("query key: %s", query[i][0]);
> 
> It is a shame that we're removing all of the helpful debugging messages
> from this test. It would be much nicer if we could keep them by using an
> if statement and cl_failf() as we do in u-ctype.c

I honestly think that the debug messages don't add much and only add to the noise. You shouldn't ever see them, and if you do something is broken and you'll likely end up pulling out the debugger anyway. So I'm more in the camp of writing unit tests in a concise way rather than the needlessly-verbose style we previously had.

Patrick
Previous: phillip.wood123@gmail.comNext: phillip.wood123@gmail.com
Message 15 of 21 in “t/unit-tests: convert unit-tests to use clar”
  1. 0/4 t/unit-tests: convert unit-tests to use clarSeyi Kuforiji, Jan 30, 2025
  2. 1/4 t/unit-tests: convert hashmap test to clar frameworkSeyi Kuforiji, Jan 30, 2025
  3. Patrick SteinhardtJan 31, 2025
  4. 2/4 t/unit-tests: adapt example decorate test to clar frameworkSeyi Kuforiji, Jan 30, 2025
  5. Patrick SteinhardtJan 31, 2025
  6. 3/4 t/unit-tests: convert strbuf test to clar frameworkSeyi Kuforiji, Jan 30, 2025
  7. Patrick SteinhardtJan 31, 2025
  8. 4/4 t/unit-tests: convert strcmp-offset test to clar frameworkSeyi Kuforiji, Jan 30, 2025
  9. Patrick SteinhardtJan 31, 2025
  10. Patrick SteinhardtJan 31, 2025
  11. 0/4 t/unit-tests: convert unit-tests to use clarSeyi Kuforiji, Jan 31, 2025
  12. 1/4 t/unit-tests: convert hashmap test to use clar test frameworkSeyi Kuforiji, Jan 31, 2025
  13. Junio C HamanoJan 31, 2025
  14. phillip.wood123@gmail.comFeb 2, 2025
  15. Patrick SteinhardtFeb 3, 2025
  16. phillip.wood123@gmail.comFeb 3, 2025
  17. 2/4 t/unit-tests: adapt example decorate test to use clar test frameworkSeyi Kuforiji, Jan 31, 2025
  18. 3/4 t/unit-tests: convert strbuf test to use clar test frameworkSeyi Kuforiji, Jan 31, 2025
  19. Phillip WoodFeb 2, 2025
  20. 4/4 t/unit-tests: convert strcmp-offset test to use clar test frameworkSeyi Kuforiji, Jan 31, 2025
  21. Junio C HamanoJan 31, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.