* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
+
#include "config.h"
+
#include "gtestutils.h"
-#include "galias.h"
+
#include <sys/types.h>
#ifdef G_OS_UNIX
#include <sys/wait.h>
#endif
#include <string.h>
#include <stdlib.h>
+#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/select.h>
#endif /* HAVE_SYS_SELECT_H */
+#include "gmain.h"
+#include "gpattern.h"
+#include "grand.h"
+#include "gstrfuncs.h"
+#include "gtimer.h"
+
+
+/* Global variable for storing assertion messages; this is the counterpart to
+ * glibc's (private) __abort_msg variable, and allows developers and crash
+ * analysis systems like Apport and ABRT to fish out assertion messages from
+ * core dumps, instead of having to catch them on screen output. */
+char *__glib_assert_msg = NULL;
+
/* --- structures --- */
struct GTestCase
{
static gchar *test_run_name = "";
static guint test_run_forks = 0;
static guint test_run_count = 0;
+static guint test_run_success = FALSE;
static guint test_skip_count = 0;
static GTimer *test_user_timer = NULL;
static double test_user_stamp = 0;
}
if (test_debug_log)
{
- GTestLogBuffer *lbuffer = g_test_log_buffer_new();
+ GTestLogBuffer *lbuffer = g_test_log_buffer_new ();
GTestLogMsg *msg;
guint ui;
g_test_log_buffer_push (lbuffer, n_bytes, buffer);
msg = g_test_log_buffer_pop (lbuffer);
- g_assert (msg != NULL); /* FIXME: should be g_awrn_if_fail */
- g_assert (lbuffer->data->len == 0); /* FIXME: should be g_awrn_if_fail */
+ g_warn_if_fail (msg != NULL);
+ g_warn_if_fail (lbuffer->data->len == 0);
g_test_log_buffer_free (lbuffer);
/* print message */
g_printerr ("{*LOG(%s)", g_test_log_type_name (msg->log_type));
}
}
+/* We intentionally parse the command line without GOptionContext
+ * because otherwise you would never be able to test it.
+ */
static void
parse_args (gint *argc_p,
gchar ***argv_p)
}
argv[i] = NULL;
}
+ else if (strcmp ("-?", argv[i]) == 0 || strcmp ("--help", argv[i]) == 0)
+ {
+ printf ("Usage:\n"
+ " %s [OPTION...]\n\n"
+ "Help Options:\n"
+ " -?, --help Show help options\n"
+ "Test Options:\n"
+ " -l List test cases available in a test executable\n"
+ " -seed=RANDOMSEED Provide a random seed to reproduce test\n"
+ " runs using random numbers\n"
+ " --verbose Run tests verbosely\n"
+ " -q, --quiet Run tests quietly\n"
+ " -p TESTPATH execute all tests matching TESTPATH\n"
+ " -m {perf|slow|thorough|quick} Execute tests according modes\n"
+ " --debug-log debug test logging output\n"
+ " -k, --keep-going gtester-specific argument\n"
+ " --GTestLogFD=N gtester-specific argument\n"
+ " --GTestSkipCount=N gtester-specific argument\n",
+ argv[0]);
+ exit (0);
+ }
}
/* collapse argv */
e = 1;
* test random number generator, the name for g_get_prgname()
* and parsing test related command line args.
* So far, the following arguments are understood:
- * <informalexample>
- * -l list test cases available in a test executable.
- * --seed RANDOMSEED provide a random seed to reproduce test runs using random numbers.
- * --verbose run tests verbosely.
- * -q, --quiet run tests quietly.
- * -p TESTPATH execute all tests matching TESTPATH.
- * -m {perf|slow|thorough|quick}
- * execute tests according to these test modes:
- * perf - performance tests, may take long and report results.
- * slow - slow and thorough tests, may take quite long and maximize coverage.
- * thorough - currently an alias for "slow".
- * quick - quick tests, should run really quickly and give good coverage.
- * --debug-log debug test logging output.
- * -k, --keep-going gtester specific argument.
- * --GTestLogFD N gtester specific argument.
- * --GTestSkipCount N gtester specific argument.
- * </informalexample>
+ * <variablelist>
+ * <varlistentry>
+ * <term><option>-l</option></term>
+ * <listitem><para>
+ * list test cases available in a test executable.
+ * </para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>--seed=<replaceable>RANDOMSEED</replaceable></option></term>
+ * <listitem><para>
+ * provide a random seed to reproduce test runs using random numbers.
+ * </para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>--verbose</option></term>
+ * <listitem><para>run tests verbosely.</para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>-q</option>, <option>--quiet</option></term>
+ * <listitem><para>run tests quietly.</para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>-p <replaceable>TESTPATH</replaceable></option></term>
+ * <listitem><para>
+ * execute all tests matching <replaceable>TESTPATH</replaceable>.
+ * </para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>-m {perf|slow|thorough|quick}</option></term>
+ * <listitem><para>
+ * execute tests according to these test modes:
+ * <variablelist>
+ * <varlistentry>
+ * <term>perf</term>
+ * <listitem><para>
+ * performance tests, may take long and report results.
+ * </para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term>slow, thorough</term>
+ * <listitem><para>
+ * slow and thorough tests, may take quite long and
+ * maximize coverage.
+ * </para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term>quick</term>
+ * <listitem><para>
+ * quick tests, should run really quickly and give good coverage.
+ * </para></listitem>
+ * </varlistentry>
+ * </variablelist>
+ * </para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>--debug-log</option></term>
+ * <listitem><para>debug test logging output.</para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>-k</option>, <option>--keep-going</option></term>
+ * <listitem><para>gtester-specific argument.</para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>--GTestLogFD <replaceable>N</replaceable></option></term>
+ * <listitem><para>gtester-specific argument.</para></listitem>
+ * </varlistentry>
+ * <varlistentry>
+ * <term><option>--GTestSkipCount <replaceable>N</replaceable></option></term>
+ * <listitem><para>gtester-specific argument.</para></listitem>
+ * </varlistentry>
+ * </variablelist>
*
* Since: 2.16
*/
* g_test_rand_int:
*
* Get a reproducible random integer number.
+ *
* The random numbers generated by the g_test_rand_*() family of functions
* change with every new test program start, unless the --seed option is
* given when starting test programs.
+ *
* For individual test cases however, the random number generator is
* reseeded, to avoid dependencies between tests and to make --seed
* effective for all test cases.
long double largs = minimized_quantity;
gchar *buffer;
va_list args;
+
va_start (args, format);
buffer = g_strdup_vprintf (format, args);
va_end (args);
+
g_test_log (G_TEST_LOG_MIN_RESULT, buffer, NULL, 1, &largs);
g_free (buffer);
}
long double largs = maximized_quantity;
gchar *buffer;
va_list args;
+
va_start (args, format);
buffer = g_strdup_vprintf (format, args);
va_end (args);
+
g_test_log (G_TEST_LOG_MAX_RESULT, buffer, NULL, 1, &largs);
g_free (buffer);
}
{
gchar *buffer;
va_list args;
+
va_start (args, format);
buffer = g_strdup_vprintf (format, args);
va_end (args);
+
g_test_log (G_TEST_LOG_MESSAGE, buffer, NULL, 0, NULL);
g_free (buffer);
}
* @uri_pattern: the base pattern for bug URIs
*
* Specify the base URI for bug reports.
+ *
* The base URI is used to construct bug report messages for
* g_test_message() when g_test_bug() is called.
* Calling this function outside of a test case sets the
g_test_bug (const char *bug_uri_snippet)
{
char *c;
+
g_return_if_fail (test_uri_base != NULL);
g_return_if_fail (bug_uri_snippet != NULL);
+
c = strstr (test_uri_base, "%s");
if (c)
{
g_free (test_suite_root->name);
test_suite_root->name = g_strdup ("");
}
+
return test_suite_root;
}
* Since: 2.16
*/
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))
+g_test_create_case (const char *test_name,
+ gsize data_size,
+ gconstpointer test_data,
+ GTestFixtureFunc data_setup,
+ GTestFixtureFunc data_test,
+ GTestFixtureFunc data_teardown)
{
GTestCase *tc;
+
g_return_val_if_fail (test_name != NULL, NULL);
g_return_val_if_fail (strchr (test_name, '/') == NULL, NULL);
g_return_val_if_fail (test_name[0] != 0, NULL);
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_setup = (void*) data_setup;
tc->fixture_test = (void*) data_test;
tc->fixture_teardown = (void*) data_teardown;
+
return tc;
}
+/**
+ * GTestFixtureFunc:
+ * @fixture: the test fixture
+ * @user_data: the data provided when registering the test
+ *
+ * The type used for functions that operate on test fixtures. This is
+ * used for the fixture setup and teardown functions as well as for the
+ * testcases themselves.
+ *
+ * @user_data is a pointer to the data that was given when registering
+ * the test case.
+ *
+ * @fixture will be a pointer to the area of memory allocated by the
+ * test framework, of the size requested. If the requested size was
+ * zero then @fixture will be equal to @user_data.
+ *
+ * Since: 2.28
+ */
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))
+g_test_add_vtable (const char *testpath,
+ gsize data_size,
+ gconstpointer test_data,
+ GTestFixtureFunc data_setup,
+ GTestFixtureFunc fixture_test_func,
+ GTestFixtureFunc data_teardown)
{
gchar **segments;
guint ui;
}
/**
+ * g_test_fail:
+ *
+ * Indicates that a test failed. This function can be called
+ * multiple times from the same test. You can use this function
+ * if your test failed in a recoverable way.
+ *
+ * Do not use this function if the failure of a test could cause
+ * other tests to malfunction.
+ *
+ * Calling this function will not stop the test from running, you
+ * need to return from the test function yourself. So you can
+ * produce additional diagnostic messages or even continue running
+ * the test.
+ *
+ * If not called from inside a test, this function does nothing.
+ *
+ * Since: 2.30
+ **/
+void
+g_test_fail (void)
+{
+ test_run_success = FALSE;
+}
+
+/**
+ * GTestFunc:
+ *
+ * The type used for test case functions.
+ *
+ * Since: 2.28
+ */
+
+/**
* g_test_add_func:
* @testpath: Slash-separated test case path name for the test.
* @test_func: The test function to invoke for this test.
* Since: 2.16
*/
void
-g_test_add_func (const char *testpath,
- void (*test_func) (void))
+g_test_add_func (const char *testpath,
+ GTestFunc test_func)
{
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, NULL, test_func, NULL);
+ g_test_add_vtable (testpath, 0, NULL, NULL, (GTestFixtureFunc) test_func, NULL);
}
/**
+ * GTestDataFunc:
+ * @user_data: the data provided when registering the test
+ *
+ * The type used for test case functions that take an extra pointer
+ * argument.
+ *
+ * Since: 2.28
+ */
+
+/**
* g_test_add_data_func:
* @testpath: Slash-separated test case path name for the test.
* @test_data: Test data argument for the test function.
void
g_test_add_data_func (const char *testpath,
gconstpointer test_data,
- void (*test_func) (gconstpointer))
+ GTestDataFunc test_func)
{
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);
+ g_test_add_vtable (testpath, 0, test_data, NULL, (GTestFixtureFunc) test_func, NULL);
}
/**
{
g_return_if_fail (suite != NULL);
g_return_if_fail (test_case != NULL);
+
suite->cases = g_slist_prepend (suite->cases, test_case);
}
{
g_return_if_fail (suite != NULL);
g_return_if_fail (nestedsuite != NULL);
+
suite->suites = g_slist_prepend (suite->suites, nestedsuite);
}
gpointer destroy_data)
{
DestroyEntry *dentry;
+
g_return_if_fail (destroy_func != NULL);
+
dentry = g_slice_new0 (DestroyEntry);
dentry->destroy_func = destroy_func;
dentry->destroy_data = destroy_data;
test_destroy_queue = dentry;
}
-static int
+static gboolean
test_case_run (GTestCase *tc)
{
gchar *old_name = test_run_name, *old_base = g_strdup (test_uri_base);
+ gboolean success = TRUE;
+
test_run_name = g_strconcat (old_name, "/", tc->name, NULL);
if (++test_run_count <= test_skip_count)
g_test_log (G_TEST_LOG_SKIP_CASE, test_run_name, NULL, 0, NULL);
void *fixture;
g_test_log (G_TEST_LOG_START_CASE, test_run_name, NULL, 0, NULL);
test_run_forks = 0;
+ test_run_success = TRUE;
+ g_test_log_set_fatal_handler (NULL, NULL);
g_timer_start (test_run_timer);
fixture = tc->fixture_size ? g_malloc0 (tc->fixture_size) : tc->test_data;
test_run_seed (test_run_seedstr);
if (tc->fixture_size)
g_free (fixture);
g_timer_stop (test_run_timer);
- largs[0] = 0; /* OK */
+ success = test_run_success;
+ test_run_success = FALSE;
+ largs[0] = success ? 0 : 1; /* OK */
largs[1] = test_run_forks;
largs[2] = g_timer_elapsed (test_run_timer, NULL);
g_test_log (G_TEST_LOG_STOP_CASE, NULL, NULL, G_N_ELEMENTS (largs), largs);
test_run_name = old_name;
g_free (test_uri_base);
test_uri_base = old_base;
- return 0;
+
+ return success;
}
static int
g_test_run_suite_internal (GTestSuite *suite,
const char *path)
{
- guint n_bad = 0, n_good = 0, bad_suite = 0, l;
+ guint n_bad = 0, l;
gchar *rest, *old_name = test_run_name;
GSList *slist, *reversed;
+
g_return_val_if_fail (suite != NULL, -1);
+
while (path[0] == '/')
path++;
l = strlen (path);
guint n = l ? strlen (tc->name) : 0;
if (l == n && strncmp (path, tc->name, n) == 0)
{
- n_good++;
- n_bad += test_case_run (tc) != 0;
+ if (!test_case_run (tc))
+ n_bad++;
}
}
g_slist_free (reversed);
GTestSuite *ts = slist->data;
guint n = l ? strlen (ts->name) : 0;
if (l == n && strncmp (path, ts->name, n) == 0)
- bad_suite += g_test_run_suite_internal (ts, rest ? rest : "") != 0;
+ n_bad += g_test_run_suite_internal (ts, rest ? rest : "");
}
g_slist_free (reversed);
g_free (test_run_name);
test_run_name = old_name;
- return n_bad || bad_suite;
+
+ return n_bad;
}
/**
*
* Execute the tests within @suite and all nested #GTestSuites.
* The test suites to be executed are filtered according to
- * test path arguments (-p <testpath>) as parsed by g_test_init().
+ * test path arguments (-p <replaceable>testpath</replaceable>)
+ * as parsed by g_test_init().
* g_test_run_suite() or g_test_run() may only be called once
* in a program.
*
g_test_run_suite (GTestSuite *suite)
{
guint n_bad = 0;
+
g_return_val_if_fail (g_test_config_vars->test_initialized, -1);
g_return_val_if_fail (g_test_run_once == TRUE, -1);
+
g_test_run_once = FALSE;
+
if (!test_paths)
test_paths = g_slist_prepend (test_paths, "");
while (test_paths)
path++;
if (!n) /* root suite, run unconditionally */
{
- n_bad += 0 != g_test_run_suite_internal (suite, path);
+ n_bad += g_test_run_suite_internal (suite, path);
continue;
}
/* regular suite, match path */
l = strlen (path);
l = rest ? MIN (l, rest - path) : l;
if ((!l || l == n) && strncmp (path, suite->name, n) == 0)
- n_bad += 0 != g_test_run_suite_internal (suite, rest ? rest : "");
+ n_bad += g_test_run_suite_internal (suite, rest ? rest : "");
}
+
return n_bad;
}
gpointer unused_data)
{
const gchar *strv[16];
+ gboolean fatal = FALSE;
gchar *msg;
guint i = 0;
+
if (log_domain)
{
strv[i++] = log_domain;
strv[i++] = "-";
}
if (log_level & G_LOG_FLAG_FATAL)
- strv[i++] = "FATAL-";
+ {
+ strv[i++] = "FATAL-";
+ fatal = TRUE;
+ }
if (log_level & G_LOG_FLAG_RECURSION)
strv[i++] = "RECURSIVE-";
if (log_level & G_LOG_LEVEL_ERROR)
strv[i++] = ": ";
strv[i++] = message;
strv[i++] = NULL;
+
msg = g_strjoinv ("", (gchar**) strv);
- g_test_log (G_TEST_LOG_ERROR, msg, NULL, 0, NULL);
+ g_test_log (fatal ? G_TEST_LOG_ERROR : G_TEST_LOG_MESSAGE, msg, NULL, 0, NULL);
g_log_default_handler (log_domain, log_level, message, unused_data);
+
g_free (msg);
}
{
char lstr[32];
char *s;
+
if (!message)
message = "code should not be reached";
g_snprintf (lstr, 32, "%d", line);
s = g_strconcat (domain ? domain : "", domain && domain[0] ? ":" : "",
- "ERROR:(", file, ":", lstr, "):",
+ "ERROR:", file, ":", lstr, ":",
func, func[0] ? ":" : "",
" ", message, NULL);
- g_printerr ("**\n** %s\n", s);
+ g_printerr ("**\n%s\n", s);
+
+ /* store assertion message in global variable, so that it can be found in a
+ * core dump */
+ if (__glib_assert_msg != NULL)
+ /* free the old one */
+ free (__glib_assert_msg);
+ __glib_assert_msg = (char*) malloc (strlen (s) + 1);
+ strcpy (__glib_assert_msg, s);
+
g_test_log (G_TEST_LOG_ERROR, s, NULL, 0, NULL);
g_free (s);
abort();
switch (numtype)
{
case 'i': s = g_strdup_printf ("assertion failed (%s): (%.0Lf %s %.0Lf)", expr, arg1, cmp, arg2); break;
- case 'x': s = g_strdup_printf ("assertion failed (%s): (0x%08" G_GUINT64_FORMAT " %s 0x%08" G_GUINT64_FORMAT ")", expr, (guint64) arg1, cmp, (guint64) arg2); break;
+ case 'x': s = g_strdup_printf ("assertion failed (%s): (0x%08" G_GINT64_MODIFIER "x %s 0x%08" G_GINT64_MODIFIER "x)", expr, (guint64) arg1, cmp, (guint64) arg2); break;
case 'f': s = g_strdup_printf ("assertion failed (%s): (%.9Lg %s %.9Lg)", expr, arg1, cmp, arg2); break;
/* ideally use: floats=%.7g double=%.17g */
}
g_free (s);
}
+void
+g_assertion_message_error (const char *domain,
+ const char *file,
+ int line,
+ const char *func,
+ const char *expr,
+ const GError *error,
+ GQuark error_domain,
+ int error_code)
+{
+ GString *gstring;
+
+ /* This is used by both g_assert_error() and g_assert_no_error(), so there
+ * are three cases: expected an error but got the wrong error, expected
+ * an error but got no error, and expected no error but got an error.
+ */
+
+ gstring = g_string_new ("assertion failed ");
+ if (error_domain)
+ g_string_append_printf (gstring, "(%s == (%s, %d)): ", expr,
+ g_quark_to_string (error_domain), error_code);
+ else
+ g_string_append_printf (gstring, "(%s == NULL): ", expr);
+
+ if (error)
+ g_string_append_printf (gstring, "%s (%s, %d)", error->message,
+ g_quark_to_string (error->domain), error->code);
+ else
+ g_string_append_printf (gstring, "%s is NULL", expr);
+
+ g_assertion_message (domain, file, line, func, gstring->str);
+ g_string_free (gstring, TRUE);
+}
+
/**
* g_strcmp0:
* @str1: a C string or %NULL
* @str2: another C string or %NULL
*
- * Compares @str1 and @str2 like strcmp(). Handles %NULL strings gracefully.
+ * Compares @str1 and @str2 like strcmp(). Handles %NULL
+ * gracefully by sorting it before non-%NULL strings.
+ * Comparing two %NULL pointers returns 0.
*
* Returns: -1, 0 or 1, if @str1 is <, == or > than @str2.
*
* Fork the current test program to execute a test case that might
* not return or that might abort. The forked test case is aborted
* and considered failing if its run time exceeds @usec_timeout.
- * The forking behavior can be configured with the following flags:
- * %G_TEST_TRAP_SILENCE_STDOUT - redirect stdout of the test child
- * to /dev/null so it cannot be observed on the console during test
- * runs. The actual output is still captured though to allow later
- * tests with g_test_trap_assert_stdout().
- * %G_TEST_TRAP_SILENCE_STDERR - redirect stderr of the test child
- * to /dev/null so it cannot be observed on the console during test
- * runs. The actual output is still captured though to allow later
- * tests with g_test_trap_assert_stderr().
- * %G_TEST_TRAP_INHERIT_STDIN - if this flag is given, stdin of the
- * forked child process is shared with stdin of its parent process.
- * It is redirected to /dev/null otherwise.
+ *
+ * The forking behavior can be configured with the #GTestTrapFlags flags.
*
* In the following example, the test code forks, the forked child
* process produces some sample output and exits successfully.
- * The forking parent process then asserts successfull child program
+ * The forking parent process then asserts successful child program
* termination and validates child program outputs.
*
* |[
* Since: 2.16
**/
/* --- macros docs END --- */
-
-#define __G_TEST_UTILS_C__
-#include "galiasdef.c"