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