added g_test_add_data_func() to pass data into tests. allow data arguments

2007-12-05 11:43:22  Tim Janik  <timj@imendio.com>

        * glib/gtestutils.[hc]: added g_test_add_data_func() to pass data
        into tests. allow data arguments for fixture tests.

        * glib/gtestutils.c: fixed fatal log flag setup, so tests really abort
        upon criticals/warnings/errors.

        * glib/tests/testing.c: test test_data arguments.

        * glib/gtester.c: some prototype fixups.



svn path=/trunk/; revision=6049
This commit is contained in:
11:43:22 Tim Janik 2007-12-05 10:42:09 +00:00 committed by Tim Janik
parent 2c362b7f9e
commit 510d4ec634
6 changed files with 91 additions and 26 deletions

View File

@ -1,3 +1,15 @@
2007-12-05 11:43:22 Tim Janik <timj@imendio.com>
* glib/gtestutils.[hc]: added g_test_add_data_func() to pass data
into tests. allow data arguments for fixture tests.
* glib/gtestutils.c: fixed fatal log flag setup, so tests really abort
upon criticals/warnings/errors.
* glib/tests/testing.c: test test_data arguments.
* glib/gtester.c: some prototype fixups.
2007-12-05 Tor Lillqvist <tml@novell.com>
* glib/win_iconv.c: Add "shift-jis" as an alternative spelling of

View File

@ -1273,6 +1273,7 @@ g_assertion_message_cmpnum
g_assertion_message_cmpstr
g_assertion_message_expr
g_strcmp0
g_test_add_data_func
g_test_add_func
g_test_add_vtable
g_test_bug

View File

@ -610,13 +610,15 @@ main (int argc,
}
static void
fixture_setup (guint *fix)
fixture_setup (guint *fix,
gconstpointer test_data)
{
g_assert_cmphex (*fix, ==, 0);
*fix = 0xdeadbeef;
}
static void
fixture_test (guint *fix)
fixture_test (guint *fix,
gconstpointer test_data)
{
g_assert_cmphex (*fix, ==, 0xdeadbeef);
g_test_message ("This is a test message API test message.");
@ -626,7 +628,8 @@ fixture_test (guint *fix)
g_test_bug ("456");
}
static void
fixture_teardown (guint *fix)
fixture_teardown (guint *fix,
gconstpointer test_data)
{
g_assert_cmphex (*fix, ==, 0xdeadbeef);
}
@ -637,6 +640,6 @@ main_selftest (int argc,
{
/* gtester main() for --gtester-selftest invokations */
g_test_init (&argc, &argv, NULL);
g_test_add ("/gtester/fixture-test", guint, fixture_setup, fixture_test, fixture_teardown);
g_test_add ("/gtester/fixture-test", guint, NULL, fixture_setup, fixture_test, fixture_teardown);
return g_test_run();
}

View File

@ -44,9 +44,10 @@ struct GTestCase
{
gchar *name;
guint fixture_size;
void (*fixture_setup) (void*);
void (*fixture_test) (void*);
void (*fixture_teardown) (void*);
void (*fixture_setup) (void*, gconstpointer);
void (*fixture_test) (void*, gconstpointer);
void (*fixture_teardown) (void*, gconstpointer);
gpointer test_data;
};
struct GTestSuite
{
@ -381,6 +382,7 @@ g_test_init (int *argc,
/* make warnings and criticals fatal for all test programs */
GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
g_log_set_always_fatal (fatal_mask);
/* check caller args */
g_return_if_fail (argc != NULL);
g_return_if_fail (argv != NULL);
@ -729,6 +731,7 @@ g_test_run (void)
* g_test_create_case:
* @test_name: the name for the test case
* @data_size: the size of the fixture data structure
* @test_data: test data argument for the test functions
* @data_setup: the function to set up the fixture data
* @data_test: the actual test function
* @data_teardown: the function to teardown the fixture data
@ -752,6 +755,7 @@ g_test_run (void)
GTestCase*
g_test_create_case (const char *test_name,
gsize data_size,
gconstpointer test_data,
void (*data_setup) (void),
void (*data_test) (void),
void (*data_teardown) (void))
@ -763,6 +767,7 @@ g_test_create_case (const char *test_name,
g_return_val_if_fail (data_test != NULL, NULL);
tc = g_slice_new0 (GTestCase);
tc->name = g_strdup (test_name);
tc->test_data = (gpointer) test_data;
tc->fixture_size = data_size;
tc->fixture_setup = (void*) data_setup;
tc->fixture_test = (void*) data_test;
@ -773,6 +778,7 @@ g_test_create_case (const char *test_name,
void
g_test_add_vtable (const char *testpath,
gsize data_size,
gconstpointer test_data,
void (*data_setup) (void),
void (*fixture_test_func) (void),
void (*data_teardown) (void))
@ -803,7 +809,7 @@ g_test_add_vtable (const char *testpath,
}
else /* islast */
{
GTestCase *tc = g_test_create_case (seg, data_size, data_setup, fixture_test_func, data_teardown);
GTestCase *tc = g_test_create_case (seg, data_size, test_data, data_setup, fixture_test_func, data_teardown);
g_test_suite_add (suite, tc);
}
}
@ -827,7 +833,30 @@ g_test_add_func (const char *testpath,
g_return_if_fail (testpath != NULL);
g_return_if_fail (testpath[0] == '/');
g_return_if_fail (test_func != NULL);
g_test_add_vtable (testpath, 0, NULL, test_func, NULL);
g_test_add_vtable (testpath, 0, NULL, NULL, test_func, NULL);
}
/**
* g_test_add_data_func:
* @testpath: Slash seperated test case path name for the test.
* @test_data: Test data argument for the test function.
* @test_func: The test function to invoke for this test.
*
* Create a new test case, similar to g_test_create_case(). However
* the test is assumed to use no fixture, and test suites are automatically
* created on the fly and added to the root fixture, based on the
* slash seperated portions of @testpath. The @test_data argument
* will be passed as first argument to @test_func.
*/
void
g_test_add_data_func (const char *testpath,
gconstpointer test_data,
void (*test_func) (gconstpointer))
{
g_return_if_fail (testpath != NULL);
g_return_if_fail (testpath[0] == '/');
g_return_if_fail (test_func != NULL);
g_test_add_vtable (testpath, 0, test_data, NULL, (void(*)(void)) test_func, NULL);
}
/**
@ -942,11 +971,11 @@ test_case_run (GTestCase *tc)
g_test_log (G_TEST_LOG_START_CASE, test_run_name, NULL, 0, NULL);
test_run_forks = 0;
g_timer_start (test_run_timer);
fixture = g_malloc0 (tc->fixture_size);
fixture = tc->fixture_size ? g_malloc0 (tc->fixture_size) : tc->test_data;
test_run_seed (test_run_seedstr);
if (tc->fixture_setup)
tc->fixture_setup (fixture);
tc->fixture_test (fixture);
tc->fixture_setup (fixture, tc->test_data);
tc->fixture_test (fixture, tc->test_data);
test_trap_clear();
while (test_destroy_queue)
{
@ -956,8 +985,9 @@ test_case_run (GTestCase *tc)
g_slice_free (DestroyEntry, dentry);
}
if (tc->fixture_teardown)
tc->fixture_teardown (fixture);
g_free (fixture);
tc->fixture_teardown (fixture, tc->test_data);
if (tc->fixture_size)
g_free (fixture);
g_timer_stop (test_run_timer);
largs[0] = 0; /* OK */
largs[1] = test_run_forks;

View File

@ -73,18 +73,22 @@ void g_test_init (int *argc,
#define g_test_quiet() (g_test_config_vars->test_quiet)
/* run all tests under toplevel suite (path: /) */
int g_test_run (void);
/* hook up a simple test function under test path */
/* hook up a test functions under test path */
void g_test_add_func (const char *testpath,
void (*test_func) (void));
void g_test_add_data_func (const char *testpath,
gconstpointer test_data,
void (*test_func) (gconstpointer));
/* hook up a test with fixture under test path */
#define g_test_add(testpath, Fixture, fsetup, ftest, fteardown) \
((void (*) (const char*, \
gsize, \
void (*) (Fixture*), \
void (*) (Fixture*), \
void (*) (Fixture*))) \
#define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
((void (*) (const char*, \
gsize, \
gconstpointer, \
void (*) (Fixture*, gconstpointer), \
void (*) (Fixture*, gconstpointer), \
void (*) (Fixture*, gconstpointer))) \
(void*) g_test_add_vtable) \
(testpath, sizeof (Fixture), fsetup, ftest, fteardown)
(testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown)
/* add test messages to the test report */
void g_test_message (const char *format,
...) G_GNUC_PRINTF (1, 2);
@ -128,6 +132,7 @@ double g_test_rand_double_range (double range_start,
/* semi-internal API */
GTestCase* g_test_create_case (const char *test_name,
gsize data_size,
gconstpointer test_data,
void (*data_setup) (void),
void (*data_test) (void),
void (*data_teardown) (void));
@ -177,6 +182,7 @@ void g_assertion_message_cmpnum (const char *domain,
char numtype);
void g_test_add_vtable (const char *testpath,
gsize data_size,
gconstpointer test_data,
void (*data_setup) (void),
void (*data_test) (void),
void (*data_teardown) (void));

View File

@ -108,23 +108,29 @@ typedef struct {
gchar *msg;
} Fixturetest;
static void
fixturetest_setup (Fixturetest *fix)
fixturetest_setup (Fixturetest *fix,
gconstpointer test_data)
{
g_assert (test_data == (void*) 0xc0cac01a);
fix->seed = 18;
fix->prime = 19;
fix->msg = g_strdup_printf ("%d", fix->prime);
}
static void
fixturetest_test (Fixturetest *fix)
fixturetest_test (Fixturetest *fix,
gconstpointer test_data)
{
guint prime = g_spaced_primes_closest (fix->seed);
g_assert_cmpint (prime, ==, fix->prime);
prime = g_ascii_strtoull (fix->msg, NULL, 0);
g_assert_cmpint (prime, ==, fix->prime);
g_assert (test_data == (void*) 0xc0cac01a);
}
static void
fixturetest_teardown (Fixturetest *fix)
fixturetest_teardown (Fixturetest *fix,
gconstpointer test_data)
{
g_assert (test_data == (void*) 0xc0cac01a);
g_free (fix->msg);
}
@ -164,6 +170,12 @@ test_rand2 (void)
g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
}
static void
test_data_test (gconstpointer test_data)
{
g_assert (test_data == (void*) 0xc0c0baba);
}
int
main (int argc,
char *argv[])
@ -173,7 +185,8 @@ main (int argc,
g_test_add_func ("/random-generator/rand-1", test_rand1);
g_test_add_func ("/random-generator/rand-2", test_rand2);
g_test_add_func ("/misc/assertions", test_assertions);
g_test_add ("/misc/primetoul", Fixturetest, fixturetest_setup, fixturetest_test, fixturetest_teardown);
g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
if (g_test_perf())
g_test_add_func ("/misc/timer", test_timer);
g_test_add_func ("/forking/fail assertion", test_fork_fail);