Skip to content

Commit 4dbca80

Browse files
committed
Merge branch 'rs/unit-tests-test-run'
Unit-test framework has learned a simple control structure to allow embedding test statements in-line instead of having to create a new function to contain them. * rs/unit-tests-test-run: t-strvec: use if_test t-reftable-basics: use if_test t-ctype: use if_test unit-tests: add if_test unit-tests: show location of checks outside of tests t0080: use here-doc test body
2 parents b9849e4 + 9ddec6b commit 4dbca80

File tree

8 files changed

+435
-369
lines changed

8 files changed

+435
-369
lines changed

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ ForEachMacros:
196196
- 'strmap_for_each_entry'
197197
- 'strset_for_each_entry'
198198

199+
# A list of macros that should be interpreted as conditionals instead of as
200+
# function calls.
201+
IfMacros:
202+
- 'if_test'
203+
199204
# The maximum number of consecutive empty lines to keep.
200205
MaxEmptyLinesToKeep: 1
201206

t/helper/test-example-tap.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static void t_empty(void)
7272

7373
int cmd__example_tap(int argc, const char **argv)
7474
{
75+
check(1);
76+
7577
test_res = TEST(check_res = check_int(1, ==, 1), "passing test");
7678
TEST(t_res(1), "passing test and assertion return 1");
7779
test_res = TEST(check_res = check_int(1, ==, 2), "failing test");
@@ -92,5 +94,38 @@ int cmd__example_tap(int argc, const char **argv)
9294
test_res = TEST(t_empty(), "test with no checks");
9395
TEST(check_int(test_res, ==, 0), "test with no checks returns 0");
9496

97+
if_test ("if_test passing test")
98+
check_int(1, ==, 1);
99+
if_test ("if_test failing test")
100+
check_int(1, ==, 2);
101+
if_test ("if_test passing TEST_TODO()")
102+
TEST_TODO(check(0));
103+
if_test ("if_test failing TEST_TODO()")
104+
TEST_TODO(check(1));
105+
if_test ("if_test test_skip()") {
106+
check(0);
107+
test_skip("missing prerequisite");
108+
check(1);
109+
}
110+
if_test ("if_test test_skip() inside TEST_TODO()")
111+
TEST_TODO((test_skip("missing prerequisite"), 1));
112+
if_test ("if_test TEST_TODO() after failing check") {
113+
check(0);
114+
TEST_TODO(check(0));
115+
}
116+
if_test ("if_test failing check after TEST_TODO()") {
117+
check(1);
118+
TEST_TODO(check(0));
119+
check(0);
120+
}
121+
if_test ("if_test messages from failing string and char comparison") {
122+
check_str("\thello\\", "there\"\n");
123+
check_str("NULL", NULL);
124+
check_char('a', ==, '\n');
125+
check_char('\\', ==, '\'');
126+
}
127+
if_test ("if_test test with no checks")
128+
; /* nothing */
129+
95130
return test_done();
96131
}

t/t0080-unit-test-output.sh

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@ test_description='Test the output of the unit test framework'
55
TEST_PASSES_SANITIZE_LEAK=true
66
. ./test-lib.sh
77

8-
test_expect_success 'TAP output from unit tests' '
8+
test_expect_success 'TAP output from unit tests' - <<\EOT
99
cat >expect <<-EOF &&
10+
# BUG: check outside of test at t/helper/test-example-tap.c:75
1011
ok 1 - passing test
1112
ok 2 - passing test and assertion return 1
12-
# check "1 == 2" failed at t/helper/test-example-tap.c:77
13+
# check "1 == 2" failed at t/helper/test-example-tap.c:79
1314
# left: 1
1415
# right: 2
1516
not ok 3 - failing test
1617
ok 4 - failing test and assertion return 0
1718
not ok 5 - passing TEST_TODO() # TODO
1819
ok 6 - passing TEST_TODO() returns 1
19-
# todo check ${SQ}check(x)${SQ} succeeded at t/helper/test-example-tap.c:26
20+
# todo check 'check(x)' succeeded at t/helper/test-example-tap.c:26
2021
not ok 7 - failing TEST_TODO()
2122
ok 8 - failing TEST_TODO() returns 0
2223
# check "0" failed at t/helper/test-example-tap.c:31
2324
# skipping test - missing prerequisite
24-
# skipping check ${SQ}1${SQ} at t/helper/test-example-tap.c:33
25+
# skipping check '1' at t/helper/test-example-tap.c:33
2526
ok 9 - test_skip() # SKIP
2627
ok 10 - skipped test returns 1
2728
# skipping test - missing prerequisite
@@ -39,21 +40,54 @@ test_expect_success 'TAP output from unit tests' '
3940
# check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:63
4041
# left: "NULL"
4142
# right: NULL
42-
# check "${SQ}a${SQ} == ${SQ}\n${SQ}" failed at t/helper/test-example-tap.c:64
43-
# left: ${SQ}a${SQ}
44-
# right: ${SQ}\012${SQ}
45-
# check "${SQ}\\\\${SQ} == ${SQ}\\${SQ}${SQ}" failed at t/helper/test-example-tap.c:65
46-
# left: ${SQ}\\\\${SQ}
47-
# right: ${SQ}\\${SQ}${SQ}
43+
# check "'a' == '\n'" failed at t/helper/test-example-tap.c:64
44+
# left: 'a'
45+
# right: '\012'
46+
# check "'\\\\' == '\\''" failed at t/helper/test-example-tap.c:65
47+
# left: '\\\\'
48+
# right: '\\''
4849
not ok 17 - messages from failing string and char comparison
49-
# BUG: test has no checks at t/helper/test-example-tap.c:92
50+
# BUG: test has no checks at t/helper/test-example-tap.c:94
5051
not ok 18 - test with no checks
5152
ok 19 - test with no checks returns 0
52-
1..19
53+
ok 20 - if_test passing test
54+
# check "1 == 2" failed at t/helper/test-example-tap.c:100
55+
# left: 1
56+
# right: 2
57+
not ok 21 - if_test failing test
58+
not ok 22 - if_test passing TEST_TODO() # TODO
59+
# todo check 'check(1)' succeeded at t/helper/test-example-tap.c:104
60+
not ok 23 - if_test failing TEST_TODO()
61+
# check "0" failed at t/helper/test-example-tap.c:106
62+
# skipping test - missing prerequisite
63+
# skipping check '1' at t/helper/test-example-tap.c:108
64+
ok 24 - if_test test_skip() # SKIP
65+
# skipping test - missing prerequisite
66+
ok 25 - if_test test_skip() inside TEST_TODO() # SKIP
67+
# check "0" failed at t/helper/test-example-tap.c:113
68+
not ok 26 - if_test TEST_TODO() after failing check
69+
# check "0" failed at t/helper/test-example-tap.c:119
70+
not ok 27 - if_test failing check after TEST_TODO()
71+
# check "!strcmp("\thello\\\\", "there\"\n")" failed at t/helper/test-example-tap.c:122
72+
# left: "\011hello\\\\"
73+
# right: "there\"\012"
74+
# check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:123
75+
# left: "NULL"
76+
# right: NULL
77+
# check "'a' == '\n'" failed at t/helper/test-example-tap.c:124
78+
# left: 'a'
79+
# right: '\012'
80+
# check "'\\\\' == '\\''" failed at t/helper/test-example-tap.c:125
81+
# left: '\\\\'
82+
# right: '\\''
83+
not ok 28 - if_test messages from failing string and char comparison
84+
# BUG: test has no checks at t/helper/test-example-tap.c:127
85+
not ok 29 - if_test test with no checks
86+
1..29
5387
EOF
5488
5589
! test-tool example-tap >actual &&
5690
test_cmp expect actual
57-
'
91+
EOT
5892

5993
test_done

t/unit-tests/t-ctype.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
size_t len = ARRAY_SIZE(string) - 1 + \
55
BUILD_ASSERT_OR_ZERO(ARRAY_SIZE(string) > 0) + \
66
BUILD_ASSERT_OR_ZERO(sizeof(string[0]) == sizeof(char)); \
7-
int skip = test__run_begin(); \
8-
if (!skip) { \
7+
if_test (#class " works") { \
98
for (int i = 0; i < 256; i++) { \
109
if (!check_int(class(i), ==, !!memchr(string, i, len)))\
1110
test_msg(" i: 0x%02x", i); \
1211
} \
1312
check(!class(EOF)); \
1413
} \
15-
test__run_end(!skip, TEST_LOCATION(), #class " works"); \
1614
} while (0)
1715

1816
#define DIGIT "0123456789"

0 commit comments

Comments
 (0)