Use CUnit for testing

This commit is contained in:
Yevgen Muntyan 2008-01-19 01:34:21 -06:00
parent 06094e3fc3
commit 7092585eba
7 changed files with 175 additions and 17 deletions

View File

@ -8,7 +8,7 @@
<debug>
<build_dir>build/debug</build_dir>
<configure>
<args>--enable-debug=full --enable-all-warnings --enable-project</args>
<args>--enable-debug=full --enable-all-warnings --enable-project --enable-tests</args>
<vars>
<var name="CFLAGS">-g</var>
<var name="OBJCFLAGS">-g</var>

View File

@ -102,9 +102,13 @@ endif
libmoo_la_LIBADD = $(libs) $(MOO_LIBS) $(MOO_OBJC_LIBS)
libmoo_la_LDFLAGS = $(libmoo_res_ldflag) -no-undefined -avoid-version $(export_flags)
libmoo_la_SOURCES = $(moo_include_headers)
libmoo_la_SOURCES = $(moo_include_headers) moo-tests.h
nodist_libmoo_la_SOURCES += $(nodist_moo_include_headers)
if MOO_ENABLE_TESTS
libmoo_la_LIBADD += -lcunit
endif
if MOO_PYTHON_BUILTIN
if !MOO_BUILD_MOO_MODULE
libmoo_la_LIBADD += $(PYTHON_LIBS) $(PYTHON_EXTRA_LIBS) moopython/libmoopython.la

118
moo/moo-tests.h Normal file
View File

@ -0,0 +1,118 @@
#ifndef MOO_TESTS_H
#define MOO_TESTS_H
#include <CUnit/CUnit.h>
#include <glib.h>
#include <string.h>
#include <stdarg.h>
G_BEGIN_DECLS
inline static void
moo_test_passed_or_failed (gboolean passed,
int line,
const char *file,
const char *format,
va_list args)
{
const char *msg = NULL;
char *freeme = NULL;
if (format)
msg = freeme = g_strdup_vprintf (format, args);
if (!msg)
msg = passed ? "Passed" : "Failed";
CU_assertImplementation (passed, line, msg, file, "", FALSE);
g_free (freeme);
}
inline static void
moo_test_passed (int line,
const char *file,
const char *format,
...)
{
va_list args;
va_start (args, format);
moo_test_passed_or_failed (TRUE, line, file, format, args);
va_end (args);
}
inline static void
moo_test_failed (int line,
const char *file,
const char *format,
...)
{
va_list args;
va_start (args, format);
moo_test_passed_or_failed (FALSE, line, file, format, args);
va_end (args);
}
inline static gboolean
moo_test_str_equal (const char *s1, const char *s2)
{
if (!s1 || !s2)
return s1 == s2;
else
return strcmp (s1, s2) == 0;
}
static char *
moo_test_string_stack_add (char *string)
{
#define STACK_LEN 100
static char *stack[STACK_LEN];
static guint ptr = STACK_LEN;
if (ptr == STACK_LEN)
ptr = 0;
g_free (stack[ptr]);
stack[ptr++] = string;
return string;
#undef STACK_LEN
}
inline static const char *
moo_test_str_format (const char *s)
{
if (s)
return moo_test_string_stack_add (g_strdup_printf ("\"%s\"", s));
else
return "<null>";
}
#define MOO_ASSERT_STRING_EQUAL(actual, expected) \
G_STMT_START { \
const char *a__ = actual; \
const char *e__ = expected; \
if (!moo_test_str_equal (a__, e__)) \
{ \
char *as__ = moo_test_str_format (a__); \
char *es__ = moo_test_str_format (e__); \
moo_test_failed (__LINE__, __FILE__, \
"MOO_ASSERT_STRING_EQUAL (actual = %s, expected = %s)", \
moo_test_str_format (a__), moo_test_str_format (e__)); \
g_free (es__); \
g_free (as__); \
} \
else \
{ \
CU_assertImplementation (TRUE, __LINE__, "", __FILE__, "", FALSE); \
} \
} G_STMT_END
void moo_test_mooutils_fs (void);
G_END_DECLS
#endif /* MOO_TESTS_H */

View File

@ -5,7 +5,9 @@ AM_CFLAGS = \
$(MOO_DEBUG_CFLAGS)
AM_OBJCFLAGS = $(AM_CFLAGS)
noinst_PROGRAMS =
noinst_PROGRAMS = run-tests
# noinst_PROGRAMS =
test_programs = \
markup testpaned miniglade \
testobject testthreads
@ -15,11 +17,6 @@ test_programs += \
testtext testfileview \
testpanedfileview testfold \
testkeyfile
if MOO_OS_UNIX
if MOO_USE_OBJC
test_programs += ms
endif
endif
endif
if MOO_BUILD_TERM
test_programs += mterm termbuffer
@ -30,9 +27,9 @@ endif
EXTRA_PROGRAMS = $(test_programs)
if MOO_ENABLE_TESTS
noinst_PROGRAMS += $(test_programs)
endif
# if MOO_ENABLE_TESTS
# noinst_PROGRAMS += $(test_programs)
# endif
all-tests: $(EXTRA_PROGRAMS)
@ -66,6 +63,9 @@ if MOO_OS_MINGW
ldflags += -mwindows
endif MOO_OS_MINGW
run_tests_LDFLAGS = $(ldflags)
run_tests_LDADD = $(ldadd) -lcunit
testfold_LDFLAGS = $(ldflags)
testfold_LDADD = $(ldadd)
@ -85,10 +85,6 @@ testthreads_LDFLAGS = $(ldflags)
testthreads_LDADD = $(ldadd) $(GTHREAD_LIBS)
testthreads_SOURCES = testthreads.c
ms_LDFLAGS = $(ldflags)
ms_LDADD = $(ldadd) -lreadline -lobjc
ms_SOURCES = mscript.m
testhtml_LDFLAGS = $(ldflags)
testhtml_LDADD = $(ldadd)
testhtml_SOURCES = testhtml.c

39
tests/run-tests.c Normal file
View File

@ -0,0 +1,39 @@
#include <moo.h>
#include <moo-tests.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
int
main (int argc, char *argv[])
{
// CU_pSuite pSuite;
gtk_init (&argc, &argv);
if (CU_initialize_registry () != CUE_SUCCESS)
g_error ("CU_initialize_registry() failed");
// /* add a suite to the registry */
// pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
// if (NULL == pSuite) {
// CU_cleanup_registry();
// return CU_get_error();
// }
//
// /* add the tests to the suite */
// /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
// if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) ||
// (NULL == CU_add_test(pSuite, "test of fread()", testFREAD)))
// {
// CU_cleanup_registry();
// return CU_get_error();
// }
moo_test_mooutils_fs ();
CU_basic_set_mode (CU_BRM_VERBOSE);
CU_basic_run_tests ();
CU_cleanup_registry ();
return CU_get_error ();
}

View File

@ -15,14 +15,14 @@ static void add_panes (GtkWidget *paned, MooPanePosition pane_position)
gtk_text_buffer_insert_at_cursor (buffer, "Hi there. Hi there. "
"Hi there. Hi there. Hi there. Hi there. Hi there. ", -1);
label = moo_pane_label_new (GTK_STOCK_OK, NULL, "TextView", "TextView");
moo_big_paned_insert_pane (MOO_BIG_PANED (paned), textview, label,
moo_big_paned_insert_pane (MOO_BIG_PANED (paned), textview, NULL, label,
pane_position, -1);
moo_pane_label_free (label);
label = moo_pane_label_new (GTK_STOCK_CANCEL, NULL, "File Selector", "File Selector");
moo_big_paned_insert_pane (MOO_BIG_PANED (paned),
g_object_new (MOO_TYPE_FILE_VIEW, NULL),
label, pane_position, -1);
NULL, label, pane_position, -1);
moo_pane_label_free (label);
}

View File

@ -12,6 +12,7 @@
#include "mooutils/mooutils-thread.h"
#include "mooutils/mooutils-misc.h"
#include "mooutils/mooutils-debug.h"
#include <stdio.h>