Raw
1 #include "test-tool.h"
2 #include "t/unit-tests/test-lib.h"
3
4 /*
5 * The purpose of this "unit test" is to verify a few invariants of the unit
6 * test framework itself, as well as to provide examples of output from actually
7 * failing tests. As such, it is intended that this test fails, and thus it
8 * should not be run as part of `make unit-tests`. Instead, we verify it behaves
9 * as expected in the integration test t0080-unit-test-output.sh
10 */
11
12 /* Used to store the return value of check_int(). */
13 static int check_res;
14
15 /* Used to store the return value of TEST(). */
16 static int test_res;
17
18 static void t_res(int expect)
19 {
20 check_int(check_res, ==, expect);
21 check_int(test_res, ==, expect);
22 }
23
24 static void t_todo(int x)
25 {
26 check_res = TEST_TODO(check(x));
27 }
28
29 static void t_skip(void)
30 {
31 check(0);
32 test_skip("missing prerequisite");
33 check(1);
34 }
35
36 static int do_skip(void)
37 {
38 test_skip("missing prerequisite");
39 return 1;
40 }
41
42 static void t_skip_todo(void)
43 {
44 check_res = TEST_TODO(do_skip());
45 }
46
47 static void t_todo_after_fail(void)
48 {
49 check(0);
50 TEST_TODO(check(0));
51 }
52
53 static void t_fail_after_todo(void)
54 {
55 check(1);
56 TEST_TODO(check(0));
57 check(0);
58 }
59
60 static void t_messages(void)
61 {
62 check_str("\thello\\", "there\"\n");
63 check_str("NULL", NULL);
64 check_char('a', ==, '\n');
65 check_char('\\', ==, '\'');
66 check_char('\a', ==, '\v');
67 check_char('\x00', ==, '\x01');
68 }
69
70 static void t_empty(void)
71 {
72 ; /* empty */
73 }
74
75 int cmd__example_tap(int argc UNUSED, const char **argv UNUSED)
76 {
77 check(1);
78
79 test_res = TEST(check_res = check_int(1, ==, 1), "passing test");
80 TEST(t_res(1), "passing test and assertion return 1");
81 test_res = TEST(check_res = check_int(1, ==, 2), "failing test");
82 TEST(t_res(0), "failing test and assertion return 0");
83 test_res = TEST(t_todo(0), "passing TEST_TODO()");
84 TEST(t_res(1), "passing TEST_TODO() returns 1");
85 test_res = TEST(t_todo(1), "failing TEST_TODO()");
86 TEST(t_res(0), "failing TEST_TODO() returns 0");
87 test_res = TEST(t_skip(), "test_skip()");
88 TEST(check_int(test_res, ==, 1), "skipped test returns 1");
89 test_res = TEST(t_skip_todo(), "test_skip() inside TEST_TODO()");
90 TEST(t_res(1), "test_skip() inside TEST_TODO() returns 1");
91 test_res = TEST(t_todo_after_fail(), "TEST_TODO() after failing check");
92 TEST(check_int(test_res, ==, 0), "TEST_TODO() after failing check returns 0");
93 test_res = TEST(t_fail_after_todo(), "failing check after TEST_TODO()");
94 TEST(check_int(test_res, ==, 0), "failing check after TEST_TODO() returns 0");
95 TEST(t_messages(), "messages from failing string and char comparison");
96 test_res = TEST(t_empty(), "test with no checks");
97 TEST(check_int(test_res, ==, 0), "test with no checks returns 0");
98
99 if_test ("if_test passing test")
100 check_int(1, ==, 1);
101 if_test ("if_test failing test")
102 check_int(1, ==, 2);
103 if_test ("if_test passing TEST_TODO()")
104 TEST_TODO(check(0));
105 if_test ("if_test failing TEST_TODO()")
106 TEST_TODO(check(1));
107 if_test ("if_test test_skip()") {
108 check(0);
109 test_skip("missing prerequisite");
110 check(1);
111 }
112 if_test ("if_test test_skip() inside TEST_TODO()")
113 TEST_TODO((test_skip("missing prerequisite"), 1));
114 if_test ("if_test TEST_TODO() after failing check") {
115 check(0);
116 TEST_TODO(check(0));
117 }
118 if_test ("if_test failing check after TEST_TODO()") {
119 check(1);
120 TEST_TODO(check(0));
121 check(0);
122 }
123 if_test ("if_test messages from failing string and char comparison") {
124 check_str("\thello\\", "there\"\n");
125 check_str("NULL", NULL);
126 check_char('a', ==, '\n');
127 check_char('\\', ==, '\'');
128 check_char('\a', ==, '\v');
129 check_char('\x00', ==, '\x01');
130 }
131 if_test ("if_test test with no checks")
132 ; /* nothing */
133
134 return test_done();
135 }