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