20f38da8c9052634c6663832f1ae418862fb9d2f
[ira/wip.git] / lib / subunit / c / lib / child.c
1 /**
2  *
3  *  subunit C child-side bindings: report on tests being run.
4  *  Copyright (C) 2006  Robert Collins <robertc@robertcollins.net>
5  *
6  *  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
7  *  license at the users choice. A copy of both licenses are available in the
8  *  project source as Apache-2.0 and BSD. You may not use this file except in
9  *  compliance with one of these two licences.
10  *  
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under these licenses is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the license you chose for the specific language governing permissions
15  *  and limitations under that license.
16  **/
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "subunit/child.h"
21
22 /* Write details about a test event. It is the callers responsibility to ensure
23  * that details are only provided for events the protocol expects details on.
24  * @event: The event - e.g. 'skip'
25  * @name: The test name/id.
26  * @details: The details of the event, may be NULL if no details are present.
27  */
28 static void
29 subunit_send_event(char const * const event, char const * const name,
30                    char const * const details)
31 {
32   if (NULL == details) {
33     fprintf(stdout, "%s: %s\n", event, name);
34   } else {
35     fprintf(stdout, "%s: %s [\n", event, name);
36     fprintf(stdout, "%s", details);
37     if (details[strlen(details) - 1] != '\n')
38       fprintf(stdout, "\n");
39     fprintf(stdout, "]\n");
40   }
41   fflush(stdout);
42 }
43
44 /* these functions all flush to ensure that the test runner knows the action
45  * that has been taken even if the subsequent test etc takes a long time or
46  * never completes (i.e. a segfault).
47  */
48
49 void
50 subunit_test_start(char const * const name)
51 {
52   subunit_send_event("test", name, NULL);
53 }
54
55
56 void
57 subunit_test_pass(char const * const name)
58 {
59   /* TODO: add success details as an option */
60   subunit_send_event("success", name, NULL);
61 }
62
63
64 void
65 subunit_test_fail(char const * const name, char const * const error)
66 {
67   subunit_send_event("failure", name, error);
68 }
69
70
71 void
72 subunit_test_error(char const * const name, char const * const error)
73 {
74   subunit_send_event("error", name, error);
75 }
76
77
78 void
79 subunit_test_skip(char const * const name, char const * const reason)
80 {
81   subunit_send_event("skip", name, reason);
82 }
83
84 void
85 subunit_progress(enum subunit_progress_whence whence, int offset)
86 {
87         switch (whence) {
88         case SUBUNIT_PROGRESS_SET:
89                 printf("progress: %d\n", offset);
90                 break;
91         case SUBUNIT_PROGRESS_CUR:
92                 printf("progress: %+-d\n", offset);
93                 break;
94         case SUBUNIT_PROGRESS_POP:
95                 printf("progress: pop\n");
96                 break;
97         case SUBUNIT_PROGRESS_PUSH:
98                 printf("progress: push\n");
99                 break;
100         default:
101                 fprintf(stderr, "Invalid whence %d in subunit_progress()\n", whence);
102                 break;
103         }
104 }