diff --git a/moo/moo-tests-utils.h b/moo/moo-tests-utils.h index b5e1c0be..71073ce9 100644 --- a/moo/moo-tests-utils.h +++ b/moo/moo-tests-utils.h @@ -80,7 +80,7 @@ G_STMT_START { \ #define TEST_ASSERT_STR_NEQ(actual,expected) \ TEST_ASSERT_CMP (const char *, actual, expected, \ - TEST_STR_NEQ, =, TEST_FMT_STR) + TEST_STR_NEQ, !=, TEST_FMT_STR) #define TEST_ASSERT_STR_EQ_MSG(actual,expected,format,...) \ TEST_ASSERT_CMP_MSG (const char *, actual, expected, \ @@ -173,6 +173,16 @@ TEST_FMT_STRV (char **array) #define TEST_FMT_INT(a) test_string_stack_add__ (g_strdup_printf ("%d", (int) a)) #define TEST_FMT_UINT(a) test_string_stack_add__ (g_strdup_printf ("%u", (guint) a)) +#define TEST_G_ASSERT(expr) \ +G_STMT_START { \ + if (G_UNLIKELY (!(expr))) \ + g_assert_warning (G_LOG_DOMAIN, \ + __FILE__, \ + __LINE__, \ + G_STRFUNC, \ + #expr); \ +} G_STMT_END + G_GNUC_UNUSED struct TestWarningsInfo { int count; int line; @@ -186,12 +196,19 @@ test_log_handler (const gchar *log_domain, const gchar *message, gpointer data) { - g_assert (data == test_warnings_info); + TEST_G_ASSERT (data == test_warnings_info); - test_warnings_info->count -= 1; + if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)) + { + test_warnings_info->count -= 1; - if (test_warnings_info->count < 0) + if (test_warnings_info->count < 0) + g_log_default_handler (log_domain, log_level, message, NULL); + } + else + { g_log_default_handler (log_domain, log_level, message, NULL); + } } static void @@ -201,8 +218,8 @@ TEST_EXPECT_WARNINGV_ (int howmany, const char *fmt, va_list args) { - g_assert (test_warnings_info == NULL); - g_assert (howmany >= 0); + TEST_G_ASSERT (test_warnings_info == NULL); + TEST_G_ASSERT (howmany >= 0); test_warnings_info = g_new0 (struct TestWarningsInfo, 1); test_warnings_info->count = howmany; @@ -237,7 +254,7 @@ TEST_EXPECT_WARNING_ (int howmany, G_GNUC_UNUSED static void TEST_CHECK_WARNING (void) { - g_assert (test_warnings_info != NULL); + TEST_G_ASSERT (test_warnings_info != NULL); TEST_PASSED_OR_FAILED (test_warnings_info->count == 0, test_warnings_info->line, diff --git a/moo/moo-tests.h b/moo/moo-tests.h index 29b38f94..d5672615 100644 --- a/moo/moo-tests.h +++ b/moo/moo-tests.h @@ -6,6 +6,7 @@ G_BEGIN_DECLS +void moo_test_mooaccel (void); void moo_test_mooutils_fs (void); #ifdef __WIN32__ diff --git a/moo/mooutils/mooaccel.c b/moo/mooutils/mooaccel.c index bb19e84d..9ad9662b 100644 --- a/moo/mooutils/mooaccel.c +++ b/moo/mooutils/mooaccel.c @@ -257,7 +257,8 @@ _moo_accel_register (const char *accel_path, { char *freeme = NULL; - g_return_if_fail (accel_path != NULL && default_accel != NULL); + g_return_if_fail (accel_path && accel_path[0]); + g_return_if_fail (default_accel != NULL); init_accel_map (); @@ -266,8 +267,8 @@ _moo_accel_register (const char *accel_path, if (default_accel[0]) { - freeme = _moo_accel_normalize (default_accel); - g_return_if_fail (freeme != NULL); + if (!(freeme = _moo_accel_normalize (default_accel))) + return; default_accel = freeme; } @@ -336,22 +337,18 @@ _moo_modify_accel (const char *accel_path, char * _moo_get_accel_label (const char *accel) { - guint key; - GdkModifierType mods; + guint key = 0; + GdkModifierType mods = 0; g_return_val_if_fail (accel != NULL, g_strdup ("")); - init_accel_map (); - if (*accel) - { gtk_accelerator_parse (accel, &key, &mods); + + if (key) return gtk_accelerator_get_label (key, mods); - } else - { return g_strdup (""); - } } @@ -561,3 +558,246 @@ _moo_accel_normalize (const char *accel) return NULL; } } + + +#ifdef MOO_ENABLE_TESTS + +#include +#include + +void _moo_accel_register (const char *accel_path, + const char *default_accel); + +const char *_moo_get_accel (const char *accel_path); +const char *_moo_get_default_accel (const char *accel_path); + +void _moo_modify_accel (const char *accel_path, + const char *new_accel); + +static void +test_moo_accel_register (void) +{ + typedef struct { + const char *path; + const char *accel; + const char *second_accel; + const char *third_accel; + } PA; + + guint i; + + PA bad_cases[] = { + { NULL, "", NULL }, { "", "", NULL }, { "", NULL, NULL }, + }; + + PA bad_accels[] = { + { "/Test1/a", NULL, "" }, + { "/Test1/b", "/Foobar/a", "", "", "a" }, + { "/Foobar/b", "", "a", "" }, + { "/Foobar/c", "b", "", "plus" }, + { "/Foobar/d", "c", "F4", "F4" }, + { "/Foobar/e", "F4", "a", "F4" }, + }; + + for (i = 0; i < G_N_ELEMENTS (bad_cases); ++i) + { + TEST_EXPECT_WARNING (1, "_moo_accel_register(%s, %s)", + TEST_FMT_STR (bad_cases[i].path), + TEST_FMT_STR (bad_cases[i].accel)); + _moo_accel_register (bad_cases[i].path, bad_cases[i].accel); + TEST_CHECK_WARNING (); + } + + for (i = 0; i < G_N_ELEMENTS (bad_accels); ++i) + { + TEST_EXPECT_WARNING (1, "_moo_accel_register(%s, %s)", + TEST_FMT_STR (bad_accels[i].path), + TEST_FMT_STR (bad_accels[i].accel)); + _moo_accel_register (bad_accels[i].path, bad_accels[i].accel); + TEST_CHECK_WARNING (); + + TEST_EXPECT_WARNING (2, "_moo_get_default_accel(%s) after " + "invalid _moo_accel_register(%s, %s)", + TEST_FMT_STR (bad_accels[i].path), + TEST_FMT_STR (bad_accels[i].path), + TEST_FMT_STR (bad_accels[i].accel)); + TEST_ASSERT_STR_EQ_MSG (_moo_get_default_accel (bad_accels[i].path), "", + "_moo_get_default_accel(%s) after " + "invalid _moo_accel_register(%s, %s)", + TEST_FMT_STR (bad_accels[i].path), + TEST_FMT_STR (bad_accels[i].path), + TEST_FMT_STR (bad_accels[i].accel)); + TEST_ASSERT_STR_EQ_MSG (_moo_get_accel (bad_accels[i].path), "", + "_moo_get_accel(%s) after " + "invalid _moo_accel_register(%s, %s)", + TEST_FMT_STR (bad_accels[i].path), + TEST_FMT_STR (bad_accels[i].path), + TEST_FMT_STR (bad_accels[i].accel)); + TEST_CHECK_WARNING (); + } + + for (i = 0; i < G_N_ELEMENTS (cases); ++i) + { + const char *path = cases[i].path; + const char *accel = cases[i].accel; + const char *second_accel = cases[i].second_accel; + const char *third_accel = cases[i].third_accel; + guint key = 0; + GdkModifierType mods = 0; + + TEST_EXPECT_WARNING (0, "_moo_accel_register and friends for path %s", path); + + _moo_accel_register (path, accel); + TEST_ASSERT_STR_EQ_MSG (_moo_get_default_accel (path), accel, + "_moo_get_default_accel(%s) after _moo_accel_register(%s, %s)", + path, path, accel); + TEST_ASSERT_STR_EQ_MSG (_moo_get_accel (path), accel, + "_moo_get_accel(%s) after _moo_accel_register(%s, %s)", + path, path, accel); + + _moo_accel_register (path, second_accel); + TEST_ASSERT_STR_EQ_MSG (_moo_get_default_accel (path), accel, + "_moo_get_default_accel(%s) after second _moo_accel_register(%s, %s)", + path, path, second_accel); + TEST_ASSERT_STR_EQ_MSG (_moo_get_accel (path), accel, + "_moo_get_accel(%s) after second _moo_accel_register(%s, %s)", + path, path, second_accel); + + _moo_modify_accel (path, second_accel); + TEST_ASSERT_STR_EQ_MSG (_moo_get_default_accel (path), accel, + "_moo_get_default_accel(%s) after _moo_modify_accel(%s, %s)", + path, path, second_accel); + TEST_ASSERT_STR_EQ_MSG (_moo_get_accel (path), second_accel, + "_moo_get_accel(%s) after _moo_modify_accel(%s, %s)", + path, path, second_accel); + + if (*third_accel) + gtk_accelerator_parse (third_accel, &key, &mods); + gtk_accel_map_change_entry (path, key, mods, FALSE); + TEST_ASSERT_STR_EQ_MSG (_moo_get_default_accel (path), accel, + "_moo_get_default_accel(%s) after gtk_accel_map_change_entry(%s, %s)", + path, path, third_accel); + TEST_ASSERT_STR_EQ_MSG (_moo_get_accel (path), third_accel, + "_moo_get_accel(%s) after gtk_accel_map_change_entry(%s, %s)", + path, path, third_accel); + + TEST_CHECK_WARNING (); + } +} + +static void +test_moo_accel_normalize (void) +{ + guint i; + + struct { + const char *input; + const char *result; + } cases[] = { + { NULL, NULL }, { "", NULL }, { "some nonsense", NULL }, { "foobar", NULL }, + { "<", NULL }, { "Moo", NULL }, { "", NULL }, + { "a+", NULL }, { "a-", NULL }, { "a+b", NULL }, { "Ctrl+Shift", NULL }, + + { "Tab", "Tab" }, { "Tab", "Tab" }, + { "a", "a" }, { "b", "b" }, { "c", "c" }, + { "d", "d" }, { "e", "e" }, + { "f", "f" }, { "g", "g" }, + { "F8", "F8" }, { "F12", "F12" }, { "z", "z" }, { "X", "x" }, { "S", "s" }, + + { "shift+Tab", "Tab" }, + { "Control+a", "a" }, { "Ctl+b", "b" }, { "Ctrl+c", "c" }, + { "ctl+d", "d" }, { "control+e", "e" }, + { "ctl+shift+f", "f" }, { "shift+ctrl+G", "g" }, + { "F8", "F8" }, { "F12", "F12" }, { "z", "z" }, { "X", "x" }, { "shift+S", "s" }, + + { "shift-Tab", "Tab" }, + { "Control-a", "a" }, { "Ctl-b", "b" }, { "Ctrl-c", "c" }, + + { "shift-+", "plus" }, { "shift+-", "minus" }, + { "shift-plus", "plus" }, { "shift+plus", "plus" }, + }; + + setlocale (LC_ALL, "C"); + + for (i = 0; i < G_N_ELEMENTS (cases); ++i) + { + char *result; + + TEST_EXPECT_WARNING (!cases[i].result && + (cases[i].input && cases[i].input[0]), + "_moo_accel_normalize(%s)", + TEST_FMT_STR (cases[i].input)); + + result = _moo_accel_normalize (cases[i].input); + + TEST_CHECK_WARNING (); + + TEST_ASSERT_STR_EQ_MSG (result, cases[i].result, + "_moo_accel_normalize(%s)", + TEST_FMT_STR (cases[i].input)); + + g_free (result); + } + + setlocale (LC_ALL, ""); +} + +static void +test_moo_get_accel_label (void) +{ + guint i; + + struct { + const char *input; + const char *result; + } cases[] = { + { NULL, "" }, { "", "" }, { "some nonsense", "" }, { "foobar", "" }, + { "<", "" }, { "Moo", "" }, { "", "" }, + + { "Tab", "Tab" }, { "Tab", "Shift+Tab" }, + { "a", "Ctrl+A" }, { "b", "Ctrl+B" }, { "c", "Ctrl+C" }, + { "d", "Ctrl+D" }, { "e", "Ctrl+E" }, + { "f", "Shift+Ctrl+F" }, { "g", "Shift+Ctrl+G" }, + { "F8", "F8" }, { "F12", "F12" }, { "z", "Z" }, { "X", "X" }, { "S", "Shift+S" } + }; + + setlocale (LC_ALL, "C"); + + for (i = 0; i < G_N_ELEMENTS (cases); ++i) + { + char *result; + + TEST_EXPECT_WARNING (!cases[i].input, "_moo_get_accel_label(%s)", + TEST_FMT_STR (cases[i].input)); + + result = _moo_get_accel_label (cases[i].input); + + TEST_CHECK_WARNING (); + + TEST_ASSERT_STR_EQ_MSG (result, cases[i].result, + "_moo_get_accel_label(%s)", + TEST_FMT_STR (cases[i].input)); + + g_free (result); + } + + setlocale (LC_ALL, ""); +} + +void +moo_test_mooaccel (void) +{ + CU_pSuite suite; + + suite = CU_add_suite ("mooutils/mooaccel.c", NULL, NULL); + + CU_add_test (suite, "test of _moo_get_accel_label()", test_moo_get_accel_label); + CU_add_test (suite, "test of _moo_accel_normalize()", test_moo_accel_normalize); + CU_add_test (suite, "test of _moo_accel_register() and friends", test_moo_accel_register); +} + +#endif diff --git a/tests/run-mingw.sh b/tests/run-mingw.sh new file mode 100755 index 00000000..ebdc81d3 --- /dev/null +++ b/tests/run-mingw.sh @@ -0,0 +1,3 @@ +#! /bin/sh +cd /usr/local/win/gtk/bin +exec wine /home/muntyan/projects/moo/build/mingw/tests/run-tests.exe diff --git a/tests/run-tests.c b/tests/run-tests.c index 96faf53e..68a31614 100644 --- a/tests/run-tests.c +++ b/tests/run-tests.c @@ -29,6 +29,7 @@ main (int argc, char *argv[]) // return CU_get_error(); // } + moo_test_mooaccel (); moo_test_mooutils_fs (); #ifdef __WIN32__