| 1 |
#include <stdlib.h> |
| 2 |
#include <string.h> |
| 3 |
#include <glib.h> |
| 4 |
#include <gmodule.h> |
| 5 |
|
| 6 |
#include "test-unit-names.h" |
| 7 |
|
| 8 |
#define MAX_DESC_SIZE 72 |
| 9 |
|
| 10 |
static GModule *module = NULL; |
| 11 |
|
| 12 |
static gpointer |
| 13 |
get_symbol_with_suffix (const char *unit_name, |
| 14 |
const char *suffix) |
| 15 |
{ |
| 16 |
char *main_symbol_name; |
| 17 |
gpointer func; |
| 18 |
|
| 19 |
main_symbol_name = g_strconcat (unit_name, "_", suffix, NULL); |
| 20 |
main_symbol_name = g_strdelimit (main_symbol_name, "-", '_'); |
| 21 |
|
| 22 |
g_module_symbol (module, main_symbol_name, &func); |
| 23 |
|
| 24 |
g_free (main_symbol_name); |
| 25 |
|
| 26 |
return func; |
| 27 |
} |
| 28 |
|
| 29 |
static gpointer |
| 30 |
get_unit_name_main (const char *unit_name) |
| 31 |
{ |
| 32 |
return get_symbol_with_suffix (unit_name, "main"); |
| 33 |
} |
| 34 |
static char * |
| 35 |
get_unit_name_description (const char *unit_name, |
| 36 |
gssize max_len) |
| 37 |
{ |
| 38 |
const char *description; |
| 39 |
gpointer func; |
| 40 |
char *retval; |
| 41 |
|
| 42 |
func = get_symbol_with_suffix (unit_name, "describe"); |
| 43 |
if (func == NULL) |
| 44 |
description = "No description found"; |
| 45 |
else |
| 46 |
{ |
| 47 |
const char *(* unit_test_describe) (void); |
| 48 |
|
| 49 |
unit_test_describe = func; |
| 50 |
|
| 51 |
description = unit_test_describe (); |
| 52 |
} |
| 53 |
|
| 54 |
if (max_len > 0 && strlen (description) >= max_len) |
| 55 |
{ |
| 56 |
GString *buf = g_string_sized_new (max_len); |
| 57 |
char *newline; |
| 58 |
|
| 59 |
newline = strchr (description, '\n'); |
| 60 |
if (newline != NULL) |
| 61 |
{ |
| 62 |
g_string_append_len (buf, description, |
| 63 |
MIN (newline - description - 1, max_len - 3)); |
| 64 |
} |
| 65 |
else |
| 66 |
g_string_append_len (buf, description, max_len - 3); |
| 67 |
|
| 68 |
g_string_append (buf, "..."); |
| 69 |
|
| 70 |
retval = g_string_free (buf, FALSE); |
| 71 |
} |
| 72 |
else |
| 73 |
retval = g_strdup (description); |
| 74 |
|
| 75 |
return retval; |
| 76 |
} |
| 77 |
|
| 78 |
static gboolean list_all = FALSE; |
| 79 |
static gboolean describe = FALSE; |
| 80 |
static char **unit_names = NULL; |
| 81 |
|
| 82 |
static GOptionEntry entries[] = { |
| 83 |
{ |
| 84 |
"describe", 'd', |
| 85 |
0, |
| 86 |
G_OPTION_ARG_NONE, &describe, |
| 87 |
"Describe the interactive unit test", NULL, |
| 88 |
}, |
| 89 |
{ |
| 90 |
"list-all", 'l', |
| 91 |
0, |
| 92 |
G_OPTION_ARG_NONE, &list_all, |
| 93 |
"List all available units", NULL, |
| 94 |
}, |
| 95 |
{ |
| 96 |
G_OPTION_REMAINING, 0, |
| 97 |
0, |
| 98 |
G_OPTION_ARG_STRING_ARRAY, &unit_names, |
| 99 |
"The interactive unit test", "UNIT_NAME" |
| 100 |
}, |
| 101 |
{ NULL } |
| 102 |
}; |
| 103 |
|
| 104 |
int |
| 105 |
main (int argc, char **argv) |
| 106 |
{ |
| 107 |
int ret, i, n_unit_names; |
| 108 |
GOptionContext *context; |
| 109 |
|
| 110 |
context = g_option_context_new (" - Interactive test suite"); |
| 111 |
g_option_context_add_main_entries (context, entries, NULL); |
| 112 |
g_option_context_set_help_enabled (context, TRUE); |
| 113 |
g_option_context_set_ignore_unknown_options (context, TRUE); |
| 114 |
if (!g_option_context_parse (context, &argc, &argv, NULL)) |
| 115 |
{ |
| 116 |
g_print ("Usage: test-interactive <unit_test>\n"); |
| 117 |
return EXIT_FAILURE; |
| 118 |
} |
| 119 |
|
| 120 |
g_option_context_free (context); |
| 121 |
|
| 122 |
module = g_module_open (NULL, 0); |
| 123 |
if (!module) |
| 124 |
g_error ("*** Failed to open self for symbol lookup"); |
| 125 |
|
| 126 |
ret = EXIT_SUCCESS; |
| 127 |
|
| 128 |
if (list_all) |
| 129 |
{ |
| 130 |
g_print ("* Available unit tests:\n"); |
| 131 |
|
| 132 |
for (i = 0; i < G_N_ELEMENTS (test_unit_names); i++) |
| 133 |
{ |
| 134 |
char *str; |
| 135 |
gsize len; |
| 136 |
|
| 137 |
len = MAX_DESC_SIZE - strlen (test_unit_names[i]); |
| 138 |
str = get_unit_name_description (test_unit_names[i], len - 2); |
| 139 |
|
| 140 |
g_print (" - %s:%*s%s\n", |
| 141 |
test_unit_names[i], |
| 142 |
(int) (len - strlen (str)), " ", |
| 143 |
str); |
| 144 |
|
| 145 |
g_free (str); |
| 146 |
} |
| 147 |
|
| 148 |
ret = EXIT_SUCCESS; |
| 149 |
goto out; |
| 150 |
} |
| 151 |
|
| 152 |
if (unit_names != NULL) |
| 153 |
n_unit_names = g_strv_length (unit_names); |
| 154 |
else |
| 155 |
{ |
| 156 |
g_print ("Usage: test-interactive <unit_test>\n"); |
| 157 |
ret = EXIT_FAILURE; |
| 158 |
goto out; |
| 159 |
} |
| 160 |
|
| 161 |
for (i = 0; i < n_unit_names; i++) |
| 162 |
{ |
| 163 |
const char *unit_name = unit_names[i]; |
| 164 |
char *unit_test = NULL; |
| 165 |
gboolean found; |
| 166 |
int j; |
| 167 |
|
| 168 |
unit_test = g_path_get_basename (unit_name); |
| 169 |
|
| 170 |
found = FALSE; |
| 171 |
for (j = 0; j < G_N_ELEMENTS (test_unit_names); j++) |
| 172 |
{ |
| 173 |
if (strcmp (test_unit_names[j], unit_test) == 0) |
| 174 |
{ |
| 175 |
found = TRUE; |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (!found) |
| 181 |
g_error ("*** Unit '%s' does not exist", unit_test); |
| 182 |
|
| 183 |
if (describe) |
| 184 |
{ |
| 185 |
char *str; |
| 186 |
|
| 187 |
str = get_unit_name_description (unit_test, -1); |
| 188 |
|
| 189 |
g_print ("* %s:\n%s\n\n", unit_test, str); |
| 190 |
|
| 191 |
g_free (str); |
| 192 |
|
| 193 |
ret = EXIT_SUCCESS; |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
int (* unit_test_main) (int argc, char **argv); |
| 198 |
gpointer func; |
| 199 |
|
| 200 |
func = get_unit_name_main (unit_test); |
| 201 |
if (func == NULL) |
| 202 |
g_error ("*** Unable to find the main entry point for '%s'", unit_test); |
| 203 |
|
| 204 |
unit_test_main = func; |
| 205 |
|
| 206 |
ret = unit_test_main (n_unit_names, unit_names); |
| 207 |
|
| 208 |
g_free (unit_test); |
| 209 |
|
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
g_free (unit_test); |
| 214 |
} |
| 215 |
|
| 216 |
out: |
| 217 |
g_module_close (module); |
| 218 |
|
| 219 |
return ret; |
| 220 |
} |
| 221 |
|