source: mainline/uspace/lib/pcut/src/report/tap.c@ cca80a2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cca80a2 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[01579ad]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. */
42static int test_counter;
43
[9eb1ff5]44/** Counter of all failures. */
45static int failed_test_counter;
46
[01579ad]47/** Counter for tests in a current suite. */
48static int tests_in_suite;
49
50/** Counter of failed tests in current suite. */
51static int failed_tests_in_suite;
52
[134ac5d]53/** Initialize the TAP output.
54 *
55 * @param all_items Start of the list with all items.
56 */
[1433ecda]57static void tap_init(pcut_item_t *all_items)
58{
[01579ad]59 int tests_total = pcut_count_tests(all_items);
60 test_counter = 0;
[9eb1ff5]61 failed_test_counter = 0;
[01579ad]62
63 printf("1..%d\n", tests_total);
64}
65
[134ac5d]66/** Report that a suite was started.
67 *
68 * @param suite Suite that just started.
69 */
[1433ecda]70static void tap_suite_start(pcut_item_t *suite)
71{
[01579ad]72 tests_in_suite = 0;
73 failed_tests_in_suite = 0;
74
[9b20126]75 printf("#> Starting suite %s.\n", suite->name);
[01579ad]76}
77
[134ac5d]78/** Report that a suite was completed.
79 *
80 * @param suite Suite that just ended.
81 */
[1433ecda]82static void tap_suite_done(pcut_item_t *suite)
83{
[9eb1ff5]84 if (failed_tests_in_suite == 0) {
85 printf("#> Finished suite %s (passed).\n",
[1433ecda]86 suite->name);
[9eb1ff5]87 } else {
88 printf("#> Finished suite %s (failed %d of %d).\n",
[1433ecda]89 suite->name, failed_tests_in_suite, tests_in_suite);
[9eb1ff5]90 }
[01579ad]91}
92
93/** Report that a test was started.
94 *
95 * We do nothing - all handling is done after the test completes.
96 *
97 * @param test Test that is started.
98 */
[1433ecda]99static void tap_test_start(pcut_item_t *test)
100{
[01579ad]101 PCUT_UNUSED(test);
102
103 tests_in_suite++;
104 test_counter++;
105}
106
107/** Print the buffer, prefix new line with a given string.
108 *
109 * @param message Message to print.
110 * @param prefix Prefix for each new line, such as comment character.
111 */
[1433ecda]112static void print_by_lines(const char *message, const char *prefix)
113{
[9b20126]114 char *next_line_start;
[01579ad]115 if ((message == NULL) || (message[0] == 0)) {
116 return;
117 }
[9b20126]118 next_line_start = pcut_str_find_char(message, '\n');
[01579ad]119 while (next_line_start != NULL) {
120 next_line_start[0] = 0;
121 printf("%s%s\n", prefix, message);
122 message = next_line_start + 1;
123 next_line_start = pcut_str_find_char(message, '\n');
124 }
125 if (message[0] != 0) {
126 printf("%s%s\n", prefix, message);
127 }
128}
129
[134ac5d]130/** Report a completed test.
131 *
132 * @param test Test that just finished.
133 * @param outcome Outcome of the test.
134 * @param error_message Buffer with error message.
135 * @param teardown_error_message Buffer with error message from a tear-down function.
136 * @param extra_output Extra output from the test (stdout).
137 */
[01579ad]138static void tap_test_done(pcut_item_t *test, int outcome,
[1433ecda]139 const char *error_message, const char *teardown_error_message,
140 const char *extra_output)
141{
[9b20126]142 const char *test_name = test->name;
143 const char *status_str = NULL;
144 const char *fail_error_str = NULL;
[01579ad]145
[9eb1ff5]146 if (outcome != PCUT_OUTCOME_PASS) {
[01579ad]147 failed_tests_in_suite++;
[9eb1ff5]148 failed_test_counter++;
[01579ad]149 }
150
151 switch (outcome) {
[9eb1ff5]152 case PCUT_OUTCOME_PASS:
[01579ad]153 status_str = "ok";
154 fail_error_str = "";
155 break;
[9eb1ff5]156 case PCUT_OUTCOME_FAIL:
[01579ad]157 status_str = "not ok";
158 fail_error_str = " failed";
159 break;
[9eb1ff5]160 default:
[01579ad]161 status_str = "not ok";
162 fail_error_str = " aborted";
163 break;
164 }
165 printf("%s %d %s%s\n", status_str, test_counter, test_name, fail_error_str);
166
167 print_by_lines(error_message, "# error: ");
168 print_by_lines(teardown_error_message, "# error: ");
169
170 print_by_lines(extra_output, "# stdio: ");
171}
172
173/** Report testing done. */
[1433ecda]174static void tap_done(void)
175{
[9eb1ff5]176 if (failed_test_counter == 0) {
177 printf("#> Done: all tests passed.\n");
178 } else {
179 printf("#> Done: %d of %d tests failed.\n", failed_test_counter, test_counter);
180 }
[01579ad]181}
182
183
184pcut_report_ops_t pcut_report_tap = {
[9b20126]185 tap_init, tap_done,
186 tap_suite_start, tap_suite_done,
187 tap_test_start, tap_test_done
[01579ad]188};
Note: See TracBrowser for help on using the repository browser.