don't #include <glib.h> from other public headers
[platform/upstream/glib.git] / glib / gtestutils.h
index ccbbcb0..18f1ede 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
+#if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
+#error "Only <glib.h> can be included directly."
+#endif
+
 #ifndef __G_TEST_UTILS_H__
 #define __G_TEST_UTILS_H__
 
-#include <glib.h>
+#include <glib/gmessages.h>
+#include <glib/gstring.h>
+#include <glib/gtypes.h>
+#include <glib/gerror.h>
+#include <glib/gslist.h>
 
-G_BEGIN_DECLS;
+G_BEGIN_DECLS
 
 typedef struct GTestCase  GTestCase;
 typedef struct GTestSuite GTestSuite;
@@ -48,10 +57,24 @@ typedef struct GTestSuite GTestSuite;
                                              if (__n1 cmp __n2) ; else \
                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0)
+#define g_assert_no_error(err)          do { if (err) \
+                                               g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                                 #err, err, 0, 0); } while (0)
+#define g_assert_error(err, dom, c)    do { if (!err || (err)->domain != dom || (err)->code != c) \
+                                               g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                                 #err, err, dom, c); } while (0)
+#ifdef G_DISABLE_ASSERT
+#define g_assert_not_reached()          do { (void) 0; } while (0)
+#define g_assert(expr)                  do { (void) 0; } while (0)
+#else /* !G_DISABLE_ASSERT */
+#define g_assert_not_reached()          do { g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
+#define g_assert(expr)                  do { if G_LIKELY (expr) ; else \
+                                               g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                                 #expr); } while (0)
+#endif /* !G_DISABLE_ASSERT */
+
 int     g_strcmp0                       (const char     *str1,
                                          const char     *str2);
-//      g_assert(condition)             /*...*/
-//      g_assert_not_reached()          /*...*/
 
 /* report performance results */
 void    g_test_minimized_result         (double          minimized_quantity,
@@ -68,6 +91,7 @@ void    g_test_init                     (int            *argc,
 /* query testing framework config */
 #define g_test_quick()                  (g_test_config_vars->test_quick)
 #define g_test_slow()                   (!g_test_config_vars->test_quick)
+#define g_test_thorough()               (!g_test_config_vars->test_quick)
 #define g_test_perf()                   (g_test_config_vars->test_perf)
 #define g_test_verbose()                (g_test_config_vars->test_verbose)
 #define g_test_quiet()                  (g_test_config_vars->test_quiet)
@@ -81,14 +105,17 @@ void    g_test_add_data_func            (const char     *testpath,
                                          void          (*test_func) (gconstpointer));
 /* hook up a test with fixture under test path */
 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
-                                        ((void (*) (const char*,       \
+                                       G_STMT_START {                  \
+                                         void (*add_vtable) (const char*,       \
                                                     gsize,             \
                                                     gconstpointer,     \
                                                     void (*) (Fixture*, gconstpointer),   \
                                                     void (*) (Fixture*, gconstpointer),   \
-                                                    void (*) (Fixture*, gconstpointer)))  \
-                                         (void*) g_test_add_vtable) \
-                                          (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown)
+                                                    void (*) (Fixture*, gconstpointer)) =  (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
+                                         add_vtable \
+                                          (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
+                                       } G_STMT_END
+
 /* add test messages to the test report */
 void    g_test_message                  (const char *format,
                                          ...) G_GNUC_PRINTF (1, 2);
@@ -96,8 +123,8 @@ void    g_test_bug_base                 (const char *uri_pattern);
 void    g_test_bug                      (const char *bug_uri_snippet);
 /* measure test timings */
 void    g_test_timer_start              (void);
-double  g_test_timer_elapsed            (void); // elapsed seconds
-double  g_test_timer_last               (void); // repeat last elapsed() result
+double  g_test_timer_elapsed            (void); /* elapsed seconds */
+double  g_test_timer_last               (void); /* repeat last elapsed() result */
 
 /* automatically g_free or g_object_unref upon teardown */
 void    g_test_queue_free               (gpointer gfree_pointer);
@@ -109,16 +136,18 @@ void    g_test_queue_destroy            (GDestroyNotify destroy_func,
 typedef enum {
   G_TEST_TRAP_SILENCE_STDOUT    = 1 << 7,
   G_TEST_TRAP_SILENCE_STDERR    = 1 << 8,
-  G_TEST_TRAP_INHERIT_STDIN     = 1 << 9,
+  G_TEST_TRAP_INHERIT_STDIN     = 1 << 9
 } GTestTrapFlags;
 gboolean g_test_trap_fork               (guint64              usec_timeout,
                                          GTestTrapFlags       test_trap_flags);
 gboolean g_test_trap_has_passed         (void);
 gboolean g_test_trap_reached_timeout    (void);
-#define  g_test_trap_assert_passed()            g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0, 0, 0)
-#define  g_test_trap_assert_failed()            g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 1, 0, 0)
-#define  g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0, soutpattern, 0)
-#define  g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0, 0, serrpattern)
+#define  g_test_trap_assert_passed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
+#define  g_test_trap_assert_failed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
+#define  g_test_trap_assert_stdout(soutpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
+#define  g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
+#define  g_test_trap_assert_stderr(serrpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
+#define  g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
 
 /* provide seed-able random numbers for tests */
 #define  g_test_rand_bit()              (0 != (g_test_rand_int() & (1 << 15)))
@@ -149,20 +178,18 @@ void    g_test_trap_assertions          (const char     *domain,
                                          const char     *file,
                                          int             line,
                                          const char     *func,
-                                         gboolean        must_pass,
-                                         gboolean        must_fail,
-                                         const char     *stdout_pattern,
-                                         const char     *stderr_pattern);
+                                         guint64         assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
+                                         const char     *pattern);
 void    g_assertion_message             (const char     *domain,
                                          const char     *file,
                                          int             line,
                                          const char     *func,
-                                         const char     *message);
+                                         const char     *message) G_GNUC_NORETURN;
 void    g_assertion_message_expr        (const char     *domain,
                                          const char     *file,
                                          int             line,
                                          const char     *func,
-                                         const char     *expr);
+                                         const char     *expr) G_GNUC_NORETURN;
 void    g_assertion_message_cmpstr      (const char     *domain,
                                          const char     *file,
                                          int             line,
@@ -170,7 +197,7 @@ void    g_assertion_message_cmpstr      (const char     *domain,
                                          const char     *expr,
                                          const char     *arg1,
                                          const char     *cmp,
-                                         const char     *arg2);
+                                         const char     *arg2) G_GNUC_NORETURN;
 void    g_assertion_message_cmpnum      (const char     *domain,
                                          const char     *file,
                                          int             line,
@@ -179,7 +206,15 @@ void    g_assertion_message_cmpnum      (const char     *domain,
                                          long double     arg1,
                                          const char     *cmp,
                                          long double     arg2,
-                                         char            numtype);
+                                         char            numtype) G_GNUC_NORETURN;
+void    g_assertion_message_error       (const char     *domain,
+                                         const char     *file,
+                                         int             line,
+                                         const char     *func,
+                                         const char     *expr,
+                                         GError         *error,
+                                         GQuark          error_domain,
+                                         int             error_code) G_GNUC_NORETURN;
 void    g_test_add_vtable               (const char     *testpath,
                                          gsize           data_size,
                                          gconstpointer   test_data,
@@ -198,21 +233,21 @@ GLIB_VAR const GTestConfig * const g_test_config_vars;
 /* internal logging API */
 typedef enum {
   G_TEST_LOG_NONE,
-  G_TEST_LOG_ERROR,             // s:msg
-  G_TEST_LOG_START_BINARY,      // s:binaryname s:seed
-  G_TEST_LOG_LIST_CASE,         // s:testpath
-  G_TEST_LOG_SKIP_CASE,         // s:testpath
-  G_TEST_LOG_START_CASE,        // s:testpath
-  G_TEST_LOG_STOP_CASE,         // d:status d:nforks d:elapsed
-  G_TEST_LOG_MIN_RESULT,        // s:blurb d:result
-  G_TEST_LOG_MAX_RESULT,        // s:blurb d:result
-  G_TEST_LOG_MESSAGE,           // s:blurb
+  G_TEST_LOG_ERROR,             /* s:msg */
+  G_TEST_LOG_START_BINARY,      /* s:binaryname s:seed */
+  G_TEST_LOG_LIST_CASE,         /* s:testpath */
+  G_TEST_LOG_SKIP_CASE,         /* s:testpath */
+  G_TEST_LOG_START_CASE,        /* s:testpath */
+  G_TEST_LOG_STOP_CASE,         /* d:status d:nforks d:elapsed */
+  G_TEST_LOG_MIN_RESULT,        /* s:blurb d:result */
+  G_TEST_LOG_MAX_RESULT,        /* s:blurb d:result */
+  G_TEST_LOG_MESSAGE            /* s:blurb */
 } GTestLogType;
 
 typedef struct {
   GTestLogType  log_type;
   guint         n_strings;
-  gchar       **strings; // NULL terminated
+  gchar       **strings; /* NULL terminated */
   guint         n_nums;
   long double  *nums;
 } GTestLogMsg;
@@ -231,6 +266,27 @@ void            g_test_log_buffer_push  (GTestLogBuffer *tbuffer,
 GTestLogMsg*    g_test_log_buffer_pop   (GTestLogBuffer *tbuffer);
 void            g_test_log_msg_free     (GTestLogMsg    *tmsg);
 
-G_END_DECLS;
+/**
+ * GTestLogFatalFunc:
+ * @log_domain: the log domain of the message
+ * @log_level: the log level of the message (including the fatal and recursion flags)
+ * @message: the message to process
+ * @user_data: user data, set in g_test_log_set_fatal_handler()
+ *
+ * Specifies the prototype of fatal log handler functions.
+ *
+ * Return value: %TRUE if the program should abort, %FALSE otherwise
+ *
+ * Since: 2.22
+ */
+typedef gboolean        (*GTestLogFatalFunc)    (const gchar    *log_domain,
+                                                 GLogLevelFlags  log_level,
+                                                 const gchar    *message,
+                                                 gpointer        user_data);
+void
+g_test_log_set_fatal_handler            (GTestLogFatalFunc log_func,
+                                         gpointer          user_data);
+
+G_END_DECLS
 
 #endif /* __G_TEST_UTILS_H__ */