mbox series

[v3,0/7] add and use for_test to simplify tests

Message ID 73465c3d-1be0-456b-9471-f875e819c566@web.de (mailing list archive)
Headers show
Series add and use for_test to simplify tests | expand

Message

René Scharfe July 24, 2024, 2:42 p.m. UTC
Changes since v2:
- use nested here-docs in t0080 to reduce quoting and while keeping it
  self-contained,
- include spin-off patch 4e1041dc39 (unit-tests: show location of checks
  outside of tests) from seen, but with a test,
- document incompatibility of for_test and break,
- add blank line between variable declarations and rest of strbuf test
  code to match the original style.

  t0080: use here-doc test body
  unit-tests: show location of checks outside of tests
  unit-tests: add for_test
  t-ctype: use for_test
  t-reftable-basics: use for_test
  t-strvec: use for_test
  t-strbuf: use for_test

 .clang-format                    |   2 +
 t/helper/test-example-tap.c      |  35 +++
 t/t0080-unit-test-output.sh      |  60 ++++--
 t/unit-tests/t-ctype.c           |   4 +-
 t/unit-tests/t-reftable-basics.c | 228 +++++++++-----------
 t/unit-tests/t-strbuf.c          | 127 +++++------
 t/unit-tests/t-strvec.c          | 356 ++++++++++++++-----------------
 t/unit-tests/test-lib.c          |   7 +-
 t/unit-tests/test-lib.h          |  20 ++
 9 files changed, 438 insertions(+), 401 deletions(-)

Range-Diff gegen v2:
1:  5faabaea54 < -:  ---------- t0080: move expected output to a file
-:  ---------- > 1:  9b919853df t0080: use here-doc test body
-:  ---------- > 2:  9cea2b43b9 unit-tests: show location of checks outside of tests
2:  d4f9fa0938 ! 3:  5ea7472d8a unit-tests: add for_test
    @@ t/helper/test-example-tap.c: int cmd__example_tap(int argc, const char **argv)
      	return test_done();
      }

    - ## t/t0080/expect ##
    -@@ t/t0080/expect: not ok 17 - messages from failing string and char comparison
    - # BUG: test has no checks at t/helper/test-example-tap.c:92
    - not ok 18 - test with no checks
    - ok 19 - test with no checks returns 0
    --1..19
    -+ok 20 - for_test passing test
    -+# check "1 == 2" failed at t/helper/test-example-tap.c:98
    -+#    left: 1
    -+#   right: 2
    -+not ok 21 - for_test failing test
    -+not ok 22 - for_test passing TEST_TODO() # TODO
    -+# todo check 'check(1)' succeeded at t/helper/test-example-tap.c:102
    -+not ok 23 - for_test failing TEST_TODO()
    -+# check "0" failed at t/helper/test-example-tap.c:104
    -+# skipping test - missing prerequisite
    -+# skipping check '1' at t/helper/test-example-tap.c:106
    -+ok 24 - for_test test_skip() # SKIP
    -+# skipping test - missing prerequisite
    -+ok 25 - for_test test_skip() inside TEST_TODO() # SKIP
    -+# check "0" failed at t/helper/test-example-tap.c:111
    -+not ok 26 - for_test TEST_TODO() after failing check
    -+# check "0" failed at t/helper/test-example-tap.c:117
    -+not ok 27 - for_test failing check after TEST_TODO()
    -+# check "!strcmp("\thello\\", "there\"\n")" failed at t/helper/test-example-tap.c:120
    -+#    left: "\011hello\\"
    -+#   right: "there\"\012"
    -+# check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:121
    -+#    left: "NULL"
    -+#   right: NULL
    -+# check "'a' == '\n'" failed at t/helper/test-example-tap.c:122
    -+#    left: 'a'
    -+#   right: '\012'
    -+# check "'\\' == '\''" failed at t/helper/test-example-tap.c:123
    -+#    left: '\\'
    -+#   right: '\''
    -+not ok 28 - for_test messages from failing string and char comparison
    -+# BUG: test has no checks at t/helper/test-example-tap.c:125
    -+not ok 29 - for_test test with no checks
    -+1..29
    + ## t/t0080-unit-test-output.sh ##
    +@@ t/t0080-unit-test-output.sh: 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 - for_test passing test
    ++	# check "1 == 2" failed at t/helper/test-example-tap.c:100
    ++	#    left: 1
    ++	#   right: 2
    ++	not ok 21 - for_test failing test
    ++	not ok 22 - for_test passing TEST_TODO() # TODO
    ++	# todo check 'check(1)' succeeded at t/helper/test-example-tap.c:104
    ++	not ok 23 - for_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 - for_test test_skip() # SKIP
    ++	# skipping test - missing prerequisite
    ++	ok 25 - for_test test_skip() inside TEST_TODO() # SKIP
    ++	# check "0" failed at t/helper/test-example-tap.c:113
    ++	not ok 26 - for_test TEST_TODO() after failing check
    ++	# check "0" failed at t/helper/test-example-tap.c:119
    ++	not ok 27 - for_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 - for_test messages from failing string and char comparison
    ++	# BUG: test has no checks at t/helper/test-example-tap.c:127
    ++	not ok 29 - for_test test with no checks
    ++	1..29
    + 	EOF
    +
    + 	! test-tool example-tap >actual &&

      ## t/unit-tests/test-lib.h ##
     @@
    @@ t/unit-tests/test-lib.h
     + * 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.:
    ++ * Supports continue to end the test early, but not break. The
    ++ * description for each test should be unique.  E.g.:
     + *
     + *  for_test ("something else %d %d", arg1, arg2) {
     + *          prepare();
3:  a7cd5a2a3a = 4:  cf2fa74e9c t-ctype: use for_test
4:  cc07910f88 = 5:  f2e6271124 t-reftable-basics: use for_test
5:  11c1675a13 = 6:  c87a17189c t-strvec: use for_test
6:  cd79132f95 ! 7:  beaf8194fd t-strbuf: use for_test
    @@ t/unit-tests/t-strbuf.c: static void t_addstr(struct strbuf *buf, const void *da
     +
     +	for_test ("strbuf_addch adds char") {
     +		struct strbuf sb = STRBUF_INIT;
    ++
     +		t_addch(&sb, 'a');
     +		t_release(&sb);
     +	}
     +
     +	for_test ("strbuf_addch adds NUL char") {
     +		struct strbuf sb = STRBUF_INIT;
    ++
     +		t_addch(&sb, '\0');
     +		t_release(&sb);
     +	}
     +
     +	for_test ("strbuf_addch appends to initial value") {
     +		struct strbuf sb = STRBUF_INIT;
    ++
     +		t_addstr(&sb, "initial value");
     +		t_addch(&sb, 'a');
     +		t_release(&sb);
    @@ t/unit-tests/t-strbuf.c: static void t_addstr(struct strbuf *buf, const void *da
     +
     +	for_test ("strbuf_addstr adds string") {
     +		struct strbuf sb = STRBUF_INIT;
    ++
     +		t_addstr(&sb, "hello there");
     +		t_release(&sb);
     +	}
     +
     +	for_test ("strbuf_addstr appends string to initial value") {
     +		struct strbuf sb = STRBUF_INIT;
    ++
     +		t_addstr(&sb, "initial value");
     +		t_addstr(&sb, "hello there");
     +		t_release(&sb);
7:  83dafb009d < -:  ---------- unit-tests: show location of checks outside of tests
--
2.45.2