2011-09-27 16:40:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-11-07 19:32:28 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gmodule.h>
|
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
#include "test-unit-names.h"
|
2008-11-07 19:32:28 +00:00
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
#define MAX_DESC_SIZE 72
|
|
|
|
|
|
|
|
static GModule *module = NULL;
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
get_symbol_with_suffix (const char *unit_name,
|
|
|
|
const char *suffix)
|
2008-11-07 19:32:28 +00:00
|
|
|
{
|
|
|
|
char *main_symbol_name;
|
2008-11-10 12:28:42 +00:00
|
|
|
gpointer func;
|
2008-11-07 19:32:28 +00:00
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
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);
|
|
|
|
|
2008-11-07 19:32:28 +00:00
|
|
|
module = g_module_open (NULL, 0);
|
|
|
|
if (!module)
|
2011-09-27 16:40:51 +00:00
|
|
|
g_error ("*** Failed to open self for symbol lookup");
|
2008-11-07 19:32:28 +00:00
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
ret = EXIT_SUCCESS;
|
2008-11-07 19:32:28 +00:00
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
if (list_all)
|
|
|
|
{
|
|
|
|
g_print ("* Available unit tests:\n");
|
2008-11-07 19:32:28 +00:00
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS (test_unit_names); i++)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
gsize len;
|
2008-11-07 19:32:28 +00:00
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
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],
|
2011-10-12 08:57:53 +00:00
|
|
|
(int) (len - strlen (str)), " ",
|
2011-09-27 16:40:51 +00:00
|
|
|
str);
|
|
|
|
|
|
|
|
g_free (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = EXIT_SUCCESS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-27 16:40:51 +00:00
|
|
|
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;
|
|
|
|
|
2011-09-29 13:01:03 +00:00
|
|
|
ret = unit_test_main (n_unit_names, unit_names);
|
|
|
|
|
|
|
|
g_free (unit_test);
|
|
|
|
|
|
|
|
break;
|
2011-09-27 16:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (unit_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2008-11-07 19:32:28 +00:00
|
|
|
g_module_close (module);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|