2001-08-17 17:49:17 -05:00
|
|
|
/* Modified by bstanley 20010320
|
|
|
|
* Added do_test macro, do_test_call and do_test_call_args,
|
|
|
|
* print_test_results, set_success_print.
|
|
|
|
*
|
|
|
|
* Modified by bstanley 20010323
|
|
|
|
* removed testing functionality which depends on the rest of gnucash -
|
|
|
|
* sepearated into gnc-test-stuff.h
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Outline of a test program using the new testing functions:
|
|
|
|
#include "test-stuff.h"
|
|
|
|
int main( int argc, char* argv[] )
|
|
|
|
{
|
|
|
|
int a, b;
|
|
|
|
g_log_set_always_fatal( G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING );
|
|
|
|
a = b = 1;
|
2006-03-31 15:08:24 -06:00
|
|
|
do_test( a == b, "integer equality" );
|
|
|
|
do_test( a != b, "integer inequality? (should fail)" );
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
do_test_args( a == b, "fancy info", __FILE__, __LINE__, "a = %d, b = %b", a, b );
|
|
|
|
|
|
|
|
print_test_results();
|
|
|
|
return get_rv();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/* If you want to see test passes, use
|
|
|
|
set_success_print(TRUE);
|
|
|
|
before you execute the tests.
|
|
|
|
Otherwise, only failures are printed out.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef TEST_STUFF_H
|
|
|
|
#define TEST_STUFF_H
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this to indicate the result of a test.
|
|
|
|
* The result is TRUE for success, FALSE for failure.
|
|
|
|
* title describes the test
|
|
|
|
* Tests are automatically identified by their source file and line.
|
|
|
|
*/
|
2001-09-28 04:04:43 -05:00
|
|
|
#define do_test( result, title ) do_test_call( result, title, __FILE__, __LINE__ )
|
2001-08-17 17:49:17 -05:00
|
|
|
#define success( title ) success_call( title, __FILE__, __LINE__ );
|
|
|
|
#define failure( title ) failure_call( title, __FILE__, __LINE__ );
|
|
|
|
|
|
|
|
/** This one doesn't work because macros can't take a variable number of arguments.
|
|
|
|
* well, apparently gcc can, but it's non-standard.
|
|
|
|
* Apparently C99 can, too, but it's not exactly standard either.
|
|
|
|
#define do_test_args( result, title, format ) do_test_call( result, title, __FILE__, __LINE__, format, ... );
|
|
|
|
*/
|
|
|
|
|
2011-08-22 02:10:56 -05:00
|
|
|
/**
|
2011-06-18 18:20:20 -05:00
|
|
|
* Use this macro to format informative test path output when using g_test_add.
|
2011-08-22 02:10:56 -05:00
|
|
|
* Suite stands for tests' pack, while path for individual test name.
|
2011-06-18 18:20:20 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define GNC_TEST_ADD( suite, path, fixture, data, setup, test, teardown )\
|
|
|
|
{\
|
|
|
|
gchar *testpath = g_strdup_printf( "%s/%s", suite, path );\
|
2011-07-14 12:06:04 -05:00
|
|
|
g_test_add( testpath, fixture, data, setup, test, teardown );\
|
2011-06-18 18:20:20 -05:00
|
|
|
g_free( testpath );\
|
|
|
|
}
|
|
|
|
|
2011-08-22 02:10:56 -05:00
|
|
|
/**
|
2011-06-18 18:20:20 -05:00
|
|
|
* Use this macro to format informative test path output when using g_test_add_func.
|
2011-08-22 02:10:56 -05:00
|
|
|
* Suite stands for tests' pack, while path for individual test name.
|
2011-06-18 18:20:20 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define GNC_TEST_ADD_FUNC( suite, path, test )\
|
|
|
|
{\
|
|
|
|
gchar *testpath = g_strdup_printf( "%s/%s", suite, path );\
|
|
|
|
g_test_add_func( testpath, test );\
|
|
|
|
g_free( testpath );\
|
|
|
|
}
|
|
|
|
|
2011-07-14 12:06:13 -05:00
|
|
|
/**
|
|
|
|
* Test Support
|
|
|
|
*
|
|
|
|
* Struct and functions for unit test support:
|
|
|
|
* Intercept and report GLib error messages to test functions.
|
|
|
|
* Ensure that mock functions are called, and with the right data pointer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GLogLevelFlags log_level;
|
|
|
|
gchar *log_domain;
|
|
|
|
gchar *msg;
|
|
|
|
} TestErrorStruct;
|
|
|
|
|
2011-08-22 02:10:56 -05:00
|
|
|
/**
|
|
|
|
* Pass this to g_test_log_set_fatal_handler(), setting user_data to
|
|
|
|
* a pointer to TestErrorStruct to intercept and handle expected
|
|
|
|
* error and warning messages. It will g_assert if an error is
|
|
|
|
* received which doesn't match the log_level, log_domain, and
|
|
|
|
* message in the struct (if they're set), or return FALSE to prevent
|
|
|
|
* the message from aborting. Be sure to g_free() the
|
|
|
|
* TestErrorData:msg after you're done testing it.
|
|
|
|
*/
|
2011-07-14 12:06:13 -05:00
|
|
|
gboolean test_handle_faults( const char *log_domain, GLogLevelFlags log_level,
|
2011-08-22 02:10:56 -05:00
|
|
|
const gchar *msg, gpointer user_data);
|
2011-07-14 12:06:13 -05:00
|
|
|
/**
|
|
|
|
* When you know you're going to get a useless log message, pass this
|
|
|
|
* to g_log_set_default_handler to shut it up.
|
|
|
|
*/
|
|
|
|
void test_silent_logger( const char *log_domain, GLogLevelFlags log_level,
|
2011-08-22 02:10:56 -05:00
|
|
|
const gchar *msg, gpointer user_data );
|
2011-07-14 12:06:13 -05:00
|
|
|
/**
|
|
|
|
* Call this from a mock object to indicate that the mock has in fact
|
|
|
|
* been called
|
|
|
|
*/
|
|
|
|
void test_set_called( const gboolean val );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructively tests (meaning that it resets called to FALSE) and
|
|
|
|
* returns the value of called.
|
|
|
|
*/
|
2011-07-17 16:22:11 -05:00
|
|
|
const gboolean test_reset_called( void );
|
2011-07-14 12:06:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the test data pointer with the what you expect your mock to be
|
|
|
|
* called with.
|
|
|
|
*/
|
|
|
|
void test_set_data( gpointer data );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructively retrieves the test data pointer. Call from your mock
|
|
|
|
* to ensure that it received the expected data.
|
|
|
|
*/
|
|
|
|
const gpointer test_reset_data( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A handy function to use to free memory from lists of simple
|
|
|
|
* pointers. Call g_list_free_full(list, (GDestroyNotify)*test_free).
|
|
|
|
*/
|
|
|
|
void test_free( gpointer data );
|
|
|
|
|
2001-08-17 17:49:17 -05:00
|
|
|
/* Privately used to indicate a test result. You may use these if you
|
|
|
|
* wish, but it's easier to use the do_test macro above.
|
|
|
|
*/
|
2001-09-28 04:04:43 -05:00
|
|
|
gboolean do_test_call(
|
2009-12-29 14:12:48 -06:00
|
|
|
gboolean result,
|
|
|
|
const char* test_title,
|
|
|
|
const char* filename,
|
|
|
|
int line );
|
2001-10-15 19:50:01 -05:00
|
|
|
gboolean do_test_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
gboolean result,
|
|
|
|
const char* test_title,
|
|
|
|
const char* filename,
|
|
|
|
int line,
|
|
|
|
const char* format, ... );
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints out the number of tests passed and failed.
|
|
|
|
*/
|
|
|
|
void print_test_results(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this to set whether successful tests
|
|
|
|
* should print a message.
|
|
|
|
* Default is false.
|
|
|
|
* Successful test messages are useful while initally constructing the
|
|
|
|
* test suite, but when it's completed, no news is good news.
|
|
|
|
* A successful test run will be indicated by the message
|
|
|
|
* from print_test_results().
|
|
|
|
*/
|
|
|
|
void set_success_print( gboolean in_should_print );
|
|
|
|
|
|
|
|
/* Value to return from main. Set to 1 if there were any fails, 0 otherwise. */
|
|
|
|
int get_rv(void);
|
|
|
|
|
|
|
|
/** Testing primitives.
|
|
|
|
* Sometimes you just have to put the results of
|
|
|
|
* a test into different forks of the code.
|
|
|
|
*/
|
|
|
|
void success_call(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line );
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
void success_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
... );
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
void failure_call(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line);
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
void failure_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
... );
|
2001-08-17 17:49:17 -05:00
|
|
|
|
2001-08-17 19:13:45 -05:00
|
|
|
gboolean get_random_boolean(void);
|
|
|
|
gint get_random_int_in_range(int start, int end);
|
2001-10-11 03:22:44 -05:00
|
|
|
void random_character_include_funky_chars (gboolean use_funky_chars);
|
2001-08-17 19:13:45 -05:00
|
|
|
gchar get_random_character(void);
|
|
|
|
gchar* get_random_string(void);
|
2006-03-30 19:43:16 -06:00
|
|
|
gchar * get_random_string_length_in_range(int minlen, int maxlen);
|
2001-10-12 05:50:35 -05:00
|
|
|
gchar* get_random_string_without(const char *exclude_chars);
|
2001-08-17 19:13:45 -05:00
|
|
|
gint64 get_random_gint64(void);
|
|
|
|
double get_random_double(void);
|
|
|
|
const char* get_random_string_in_array(const char* str_list[]);
|
|
|
|
|
2001-08-17 17:49:17 -05:00
|
|
|
#endif /* TEST_STUFF_H */
|