1 | /*
|
---|
2 | * Copyright (c) 2013 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @file
|
---|
30 | *
|
---|
31 | * Test-anything-protocol reporting routines.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include "../internal.h"
|
---|
35 | #include "report.h"
|
---|
36 | #ifndef __helenos__
|
---|
37 | #include <string.h>
|
---|
38 | #endif
|
---|
39 | #include <stdio.h>
|
---|
40 |
|
---|
41 | /** Counter of all run tests. */
|
---|
42 | static int test_counter;
|
---|
43 |
|
---|
44 | /** Counter of all failures. */
|
---|
45 | static int failed_test_counter;
|
---|
46 |
|
---|
47 | /** Counter for tests in a current suite. */
|
---|
48 | static int tests_in_suite;
|
---|
49 |
|
---|
50 | /** Counter of failed tests in current suite. */
|
---|
51 | static int failed_tests_in_suite;
|
---|
52 |
|
---|
53 | /** Initialize the TAP output.
|
---|
54 | *
|
---|
55 | * @param all_items Start of the list with all items.
|
---|
56 | */
|
---|
57 | static void tap_init(pcut_item_t *all_items) {
|
---|
58 | int tests_total = pcut_count_tests(all_items);
|
---|
59 | test_counter = 0;
|
---|
60 | failed_test_counter = 0;
|
---|
61 |
|
---|
62 | printf("1..%d\n", tests_total);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Report that a suite was started.
|
---|
66 | *
|
---|
67 | * @param suite Suite that just started.
|
---|
68 | */
|
---|
69 | static void tap_suite_start(pcut_item_t *suite) {
|
---|
70 | tests_in_suite = 0;
|
---|
71 | failed_tests_in_suite = 0;
|
---|
72 |
|
---|
73 | printf("#> Starting suite %s.\n", suite->name);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Report that a suite was completed.
|
---|
77 | *
|
---|
78 | * @param suite Suite that just ended.
|
---|
79 | */
|
---|
80 | static void tap_suite_done(pcut_item_t *suite) {
|
---|
81 | if (failed_tests_in_suite == 0) {
|
---|
82 | printf("#> Finished suite %s (passed).\n",
|
---|
83 | suite->name);
|
---|
84 | } else {
|
---|
85 | printf("#> Finished suite %s (failed %d of %d).\n",
|
---|
86 | suite->name, failed_tests_in_suite, tests_in_suite);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Report that a test was started.
|
---|
91 | *
|
---|
92 | * We do nothing - all handling is done after the test completes.
|
---|
93 | *
|
---|
94 | * @param test Test that is started.
|
---|
95 | */
|
---|
96 | static void tap_test_start(pcut_item_t *test) {
|
---|
97 | PCUT_UNUSED(test);
|
---|
98 |
|
---|
99 | tests_in_suite++;
|
---|
100 | test_counter++;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Print the buffer, prefix new line with a given string.
|
---|
104 | *
|
---|
105 | * @param message Message to print.
|
---|
106 | * @param prefix Prefix for each new line, such as comment character.
|
---|
107 | */
|
---|
108 | static void print_by_lines(const char *message, const char *prefix) {
|
---|
109 | char *next_line_start;
|
---|
110 | if ((message == NULL) || (message[0] == 0)) {
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | next_line_start = pcut_str_find_char(message, '\n');
|
---|
114 | while (next_line_start != NULL) {
|
---|
115 | next_line_start[0] = 0;
|
---|
116 | printf("%s%s\n", prefix, message);
|
---|
117 | message = next_line_start + 1;
|
---|
118 | next_line_start = pcut_str_find_char(message, '\n');
|
---|
119 | }
|
---|
120 | if (message[0] != 0) {
|
---|
121 | printf("%s%s\n", prefix, message);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** Report a completed test.
|
---|
126 | *
|
---|
127 | * @param test Test that just finished.
|
---|
128 | * @param outcome Outcome of the test.
|
---|
129 | * @param error_message Buffer with error message.
|
---|
130 | * @param teardown_error_message Buffer with error message from a tear-down function.
|
---|
131 | * @param extra_output Extra output from the test (stdout).
|
---|
132 | */
|
---|
133 | static void tap_test_done(pcut_item_t *test, int outcome,
|
---|
134 | const char *error_message, const char *teardown_error_message,
|
---|
135 | const char *extra_output) {
|
---|
136 | const char *test_name = test->name;
|
---|
137 | const char *status_str = NULL;
|
---|
138 | const char *fail_error_str = NULL;
|
---|
139 |
|
---|
140 | if (outcome != PCUT_OUTCOME_PASS) {
|
---|
141 | failed_tests_in_suite++;
|
---|
142 | failed_test_counter++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | switch (outcome) {
|
---|
146 | case PCUT_OUTCOME_PASS:
|
---|
147 | status_str = "ok";
|
---|
148 | fail_error_str = "";
|
---|
149 | break;
|
---|
150 | case PCUT_OUTCOME_FAIL:
|
---|
151 | status_str = "not ok";
|
---|
152 | fail_error_str = " failed";
|
---|
153 | break;
|
---|
154 | default:
|
---|
155 | status_str = "not ok";
|
---|
156 | fail_error_str = " aborted";
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | printf("%s %d %s%s\n", status_str, test_counter, test_name, fail_error_str);
|
---|
160 |
|
---|
161 | print_by_lines(error_message, "# error: ");
|
---|
162 | print_by_lines(teardown_error_message, "# error: ");
|
---|
163 |
|
---|
164 | print_by_lines(extra_output, "# stdio: ");
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** Report testing done. */
|
---|
168 | static void tap_done(void) {
|
---|
169 | if (failed_test_counter == 0) {
|
---|
170 | printf("#> Done: all tests passed.\n");
|
---|
171 | } else {
|
---|
172 | printf("#> Done: %d of %d tests failed.\n", failed_test_counter, test_counter);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | pcut_report_ops_t pcut_report_tap = {
|
---|
178 | tap_init, tap_done,
|
---|
179 | tap_suite_start, tap_suite_done,
|
---|
180 | tap_test_start, tap_test_done
|
---|
181 | };
|
---|