nebtest: Re-enable checking callbacks
[nagios-reports-module.git] / test_utils.c
blob56ae83efb49e6bc15e0387c7c92ab7364e077c9c
1 #include <stdlib.h>
2 #include "nagios/objects.h"
3 #include "nagios/nagios.h"
4 #include "logutils.h" /* for colors */
5 #include "test_utils.h"
7 /*
8 * These only exist so dt_depth_*() functions
9 * in utils.c can fail gracefully
11 host *find_host(char *h)
13 return NULL;
16 service *find_service(char *h, char *s)
18 return NULL;
21 host *host_list = NULL, *host_list_tail = NULL;
22 service *service_list = NULL, *service_list_tail = NULL;
23 sched_info scheduling_info = { 0 };
26 /* test helper functions */
27 const char *cyan = "", *red = "", *green = "", *yellow = "", *reset = "";
28 uint passed, failed, t_verbose = 0;
29 static uint t_depth;
30 static const char *indent_str = " ";
32 void t_set_colors(int force)
34 if (force == 1 || isatty(fileno(stdout))) {
35 cyan = CLR_CYAN;
36 red = CLR_RED;
37 yellow = CLR_YELLOW;
38 green = CLR_GREEN;
39 reset = CLR_RESET;
43 static void t_indent(int depth)
45 uint i;
46 for (i = 0; i < depth; i++) {
47 printf("%s", indent_str);
51 void t_start(const char *fmt, ...)
53 va_list ap;
55 t_depth++;
57 va_start(ap, fmt);
58 printf("%s", cyan);
59 vfprintf(stdout, fmt, ap);
60 printf("%s", reset);
61 va_end(ap);
64 int t_end(void)
66 if (t_depth)
67 t_depth--;
68 if (!t_depth || failed) {
69 t_indent(t_depth);
70 printf("Test results: %s%u passed%s, %s%u failed%s\n",
71 green, passed, reset, failed ? red : "", failed, failed ? reset : "");
73 return failed ? EXIT_FAILURE : EXIT_SUCCESS;
76 void t_pass(const char *fmt, ...)
78 va_list ap;
80 passed++;
81 if (!t_verbose)
82 return;
83 t_indent(t_depth);
84 printf("%sPASS%s ", green, reset);
85 va_start(ap, fmt);
86 vfprintf(stdout, fmt, ap);
87 va_end(ap);
88 putchar('\n');
91 void t_fail(const char *fmt, ...)
93 va_list ap;
95 failed++;
96 t_indent(t_depth);
97 printf("%sFAIL%s ", red, reset);
98 va_start(ap, fmt);
99 vfprintf(stdout, fmt, ap);
100 va_end(ap);
101 putchar('\n');
104 void t_diag(const char *fmt, ...)
106 if (fmt) {
107 va_list ap;
108 t_indent(t_depth + 1);
109 va_start(ap, fmt);
110 vfprintf(stdout, fmt, ap);
111 va_end(ap);
112 putchar('\n');
116 int ok_int(int a, int b, const char *name)
118 if (a == b) {
119 t_pass(name);
120 return TEST_PASS;
123 t_fail(name);
124 t_diag("%d != %d", a, b);
125 return TEST_FAIL;
128 int ok_uint(uint a, uint b, const char *name)
130 if (a == b) {
131 return TEST_PASS;
132 t_pass(name);
135 t_fail(name);
136 t_diag("%u != %d", a, b);
137 return TEST_FAIL;
140 int ok_str(const char *a, const char *b, const char *name)
142 if ((!a && !b) || (a && b && !strcmp(a, b))) {
143 t_pass(name);
144 return TEST_PASS;
147 t_fail(name);
148 t_diag("'%s' != '%s'", a, b);
149 return TEST_FAIL;