More options for running tests

master
Yevgen Muntyan 2008-05-05 10:13:42 -05:00
parent 3fc5bfa2d2
commit d9738e5d26
3 changed files with 133 additions and 30 deletions

View File

@ -128,11 +128,18 @@ moo_test_suite_free (MooTestSuite *ts)
static void static void
run_test (MooTest *test, run_test (MooTest *test,
MooTestSuite *ts) MooTestSuite *ts,
MooTestOptions opts)
{ {
MooTestEnv env; MooTestEnv env;
gboolean failed; gboolean failed;
if (opts & MOO_TEST_LIST_ONLY)
{
fprintf (stdout, " Test: %s\n", test->name);
return;
}
env.suite_data = ts->data; env.suite_data = ts->data;
env.test_data = test->data; env.test_data = test->data;
@ -173,44 +180,99 @@ run_test (MooTest *test,
} }
static void static void
run_suite (MooTestSuite *ts) run_suite (MooTestSuite *ts,
MooTest *single_test,
MooTestOptions opts)
{ {
GSList *l; GSList *l;
gboolean run = !(opts & MOO_TEST_LIST_ONLY);
if (ts->init_func && !ts->init_func (ts->data)) if (run && ts->init_func && !ts->init_func (ts->data))
return; return;
registry.current_suite = ts; registry.current_suite = ts;
g_print ("Suite: %s\n", ts->name); g_print ("Suite: %s\n", ts->name);
if (single_test)
run_test (single_test, ts, opts);
else
for (l = ts->tests; l != NULL; l = l->next) for (l = ts->tests; l != NULL; l = l->next)
run_test (l->data, ts); run_test (l->data, ts, opts);
if (ts->cleanup_func) if (run && ts->cleanup_func)
ts->cleanup_func (ts->data); ts->cleanup_func (ts->data);
registry.current_suite = NULL; registry.current_suite = NULL;
registry.tr.suites += 1; registry.tr.suites += 1;
} }
void static gboolean
moo_test_run_tests (const char *data_dir) find_test (const char *name,
MooTestSuite **ts_p,
MooTest **test_p)
{ {
GSList *l; GSList *l;
for (l = registry.test_suites; l != NULL; l = l->next)
{
int idx;
MooTestSuite *ts = l->data;
if (!g_str_has_prefix (name, ts->name))
continue;
*ts_p = ts;
name += strlen (ts->name);
if (!name[0])
return TRUE;
else if (!name[1])
return FALSE;
idx = strtol (name + 1, NULL, 10);
*test_p = g_slist_nth_data (ts->tests, idx - 1);
return *test_p != NULL;
}
return FALSE;
}
void
moo_test_run_tests (const char *name,
const char *data_dir,
MooTestOptions opts)
{
GSList *l;
MooTestSuite *single_ts = NULL;
MooTest *single_test = NULL;
g_return_if_fail (data_dir != NULL); g_return_if_fail (data_dir != NULL);
g_return_if_fail (g_file_test (data_dir, G_FILE_TEST_IS_DIR)); g_return_if_fail (g_file_test (data_dir, G_FILE_TEST_IS_DIR));
if (name && !find_test (name, &single_ts, &single_test))
{
g_printerr ("could not find test %s", name);
exit (EXIT_FAILURE);
}
g_free (registry.data_dir); g_free (registry.data_dir);
registry.data_dir = g_strdup (data_dir); registry.data_dir = g_strdup (data_dir);
fprintf (stdout, "\n"); fprintf (stdout, "\n");
if (single_ts)
run_suite (single_ts, single_test, opts);
else
for (l = registry.test_suites; l != NULL; l = l->next) for (l = registry.test_suites; l != NULL; l = l->next)
run_suite (l->data); run_suite (l->data, NULL, opts);
fprintf (stdout, "\n"); fprintf (stdout, "\n");
if (!(opts & MOO_TEST_LIST_ONLY))
{
fprintf (stdout, "Run Summary: Type Total Ran Passed Failed\n"); fprintf (stdout, "Run Summary: Type Total Ran Passed Failed\n");
fprintf (stdout, " suites %5d %3d n/a n/a\n", fprintf (stdout, " suites %5d %3d n/a n/a\n",
registry.tr.suites, registry.tr.suites); registry.tr.suites, registry.tr.suites);
@ -221,6 +283,7 @@ moo_test_run_tests (const char *data_dir)
registry.tr.asserts, registry.tr.asserts, registry.tr.asserts_passed, registry.tr.asserts, registry.tr.asserts, registry.tr.asserts_passed,
registry.tr.asserts - registry.tr.asserts_passed); registry.tr.asserts - registry.tr.asserts_passed);
fprintf (stdout, "\n"); fprintf (stdout, "\n");
}
} }
void void

View File

@ -13,6 +13,10 @@ typedef struct {
gpointer test_data; gpointer test_data;
} MooTestEnv; } MooTestEnv;
typedef enum {
MOO_TEST_LIST_ONLY = 1 << 0
} MooTestOptions;
typedef struct MooTestSuite MooTestSuite; typedef struct MooTestSuite MooTestSuite;
typedef gboolean (*MooTestSuiteInit) (gpointer data); typedef gboolean (*MooTestSuiteInit) (gpointer data);
typedef void (*MooTestSuiteCleanup) (gpointer data); typedef void (*MooTestSuiteCleanup) (gpointer data);
@ -27,7 +31,9 @@ void moo_test_suite_add_test (MooTestSuite *ts,
MooTestFunc test_func, MooTestFunc test_func,
gpointer data); gpointer data);
void moo_test_run_tests (const char *data_dir); void moo_test_run_tests (const char *single_test,
const char *data_dir,
MooTestOptions opts);
void moo_test_cleanup (void); void moo_test_cleanup (void);
gboolean moo_test_get_result (void); gboolean moo_test_get_result (void);

View File

@ -1,14 +1,9 @@
#include <moo.h> #include <moo.h>
#include <moo-tests.h> #include <moo-tests.h>
int static void
main (int argc, char *argv[]) add_tests (void)
{ {
const char *data_dir;
g_thread_init (NULL);
gtk_init (&argc, &argv);
moo_test_gobject (); moo_test_gobject ();
moo_test_mooaccel (); moo_test_mooaccel ();
moo_test_mooutils_fs (); moo_test_mooutils_fs ();
@ -24,6 +19,24 @@ main (int argc, char *argv[])
moo_test_key_file (); moo_test_key_file ();
moo_test_editor (); moo_test_editor ();
}
int
main (int argc, char *argv[])
{
const char *data_dir;
GOptionContext *ctx;
GOptionGroup *grp;
GError *error = NULL;
MooTestOptions opts = 0;
gboolean list_only = FALSE;
GOptionEntry options[] = {
{ "list", 0, 0, G_OPTION_ARG_NONE, &list_only, "List available tests", NULL },
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
g_thread_init (NULL);
#ifdef __WIN32__ #ifdef __WIN32__
data_dir = "test-data"; data_dir = "test-data";
@ -31,8 +44,29 @@ main (int argc, char *argv[])
data_dir = SRCDIR "/data"; data_dir = SRCDIR "/data";
#endif #endif
moo_test_run_tests (data_dir); grp = g_option_group_new ("run-tests", "run-tests", "run-tests", NULL, NULL);
g_option_group_add_entries (grp, options);
ctx = g_option_context_new ("[TEST_SUITE]");
g_option_context_set_main_group (ctx, grp);
g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
if (!g_option_context_parse (ctx, &argc, &argv, &error))
{
g_printerr ("%s\n", error->message);
exit (EXIT_FAILURE);
}
if (argc > 2)
{
g_printerr ("invalid arguments\n");
exit (EXIT_FAILURE);
}
if (list_only)
opts |= MOO_TEST_LIST_ONLY;
add_tests ();
moo_test_run_tests (argv[1], data_dir, opts);
moo_test_cleanup (); moo_test_cleanup ();
#ifdef __WIN32__ #ifdef __WIN32__