From 9bf3a0fa5fd4fad7c8b44e6159f0c00b059266a4 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Mon, 27 Mar 2006 18:28:07 +0000 Subject: [PATCH] Add test suite for GBookmarkFile. 2006-03-27 Emmanuele Bassi * tests/Makefile.am: * tests/bookmarkfile-test.c: * tests/run-bookmark-test.sh: * tests/bookmarks/*.xbel: Add test suite for GBookmarkFile. --- ChangeLog | 7 + ChangeLog.pre-2-12 | 7 + tests/Makefile.am | 2 + tests/bookmarkfile-test.c | 240 ++++++++++++++++++++++++++++++++++ tests/bookmarks/fail-01.xbel | 0 tests/bookmarks/fail-02.xbel | 2 + tests/bookmarks/fail-03.xbel | 18 +++ tests/bookmarks/fail-04.xbel | 21 +++ tests/bookmarks/fail-05.xbel | 21 +++ tests/bookmarks/fail-06.xbel | 19 +++ tests/bookmarks/fail-07.xbel | 21 +++ tests/bookmarks/fail-08.xbel | 18 +++ tests/bookmarks/fail-09.xbel | 20 +++ tests/bookmarks/fail-10.xbel | 21 +++ tests/bookmarks/fail-11.xbel | 22 ++++ tests/bookmarks/fail-12.xbel | 22 ++++ tests/bookmarks/fail-13.xbel | 22 ++++ tests/bookmarks/fail-14.xbel | 24 ++++ tests/bookmarks/fail-15.xbel | 23 ++++ tests/bookmarks/fail-16.xbel | 24 ++++ tests/bookmarks/fail-17.xbel | 22 ++++ tests/bookmarks/valid-01.xbel | 21 +++ tests/bookmarks/valid-02.xbel | 15 +++ tests/run-bookmark-test.sh | 34 +++++ 24 files changed, 646 insertions(+) create mode 100644 tests/bookmarkfile-test.c create mode 100644 tests/bookmarks/fail-01.xbel create mode 100644 tests/bookmarks/fail-02.xbel create mode 100644 tests/bookmarks/fail-03.xbel create mode 100644 tests/bookmarks/fail-04.xbel create mode 100644 tests/bookmarks/fail-05.xbel create mode 100644 tests/bookmarks/fail-06.xbel create mode 100644 tests/bookmarks/fail-07.xbel create mode 100644 tests/bookmarks/fail-08.xbel create mode 100644 tests/bookmarks/fail-09.xbel create mode 100644 tests/bookmarks/fail-10.xbel create mode 100644 tests/bookmarks/fail-11.xbel create mode 100644 tests/bookmarks/fail-12.xbel create mode 100644 tests/bookmarks/fail-13.xbel create mode 100644 tests/bookmarks/fail-14.xbel create mode 100644 tests/bookmarks/fail-15.xbel create mode 100644 tests/bookmarks/fail-16.xbel create mode 100644 tests/bookmarks/fail-17.xbel create mode 100644 tests/bookmarks/valid-01.xbel create mode 100644 tests/bookmarks/valid-02.xbel create mode 100755 tests/run-bookmark-test.sh diff --git a/ChangeLog b/ChangeLog index 66b330d79..902fe4413 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2006-03-27 Emmanuele Bassi + + * tests/Makefile.am: + * tests/bookmarkfile-test.c: + * tests/run-bookmark-test.sh: + * tests/bookmarks/*.xbel: Add test suite for GBookmarkFile. + 2006-03-27 Emmanuele Bassi * docs/reference/glib/glib-docs.sgml: diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 66b330d79..902fe4413 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,10 @@ +2006-03-27 Emmanuele Bassi + + * tests/Makefile.am: + * tests/bookmarkfile-test.c: + * tests/run-bookmark-test.sh: + * tests/bookmarks/*.xbel: Add test suite for GBookmarkFile. + 2006-03-27 Emmanuele Bassi * docs/reference/glib/glib-docs.sgml: diff --git a/tests/Makefile.am b/tests/Makefile.am index 9347751a3..9c14f85cb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -63,6 +63,7 @@ endif test_programs = \ atomic-test \ array-test \ + bookmarkfile-test \ $(CXX_TEST) \ child-test \ completion-test \ @@ -126,6 +127,7 @@ module_ldadd = $(libgmodule) $(G_MODULE_LIBS) $(progs_ldadd) atomic_test_LDADD = $(progs_ldadd) array_test_LDADD = $(progs_ldadd) +bookmarkfile_test_LDADD = $(progs_ldadd) child_test_LDADD = $(thread_ldadd) completion_test_LDADD = $(progs_ldadd) convert_test_LDADD = $(progs_ldadd) diff --git a/tests/bookmarkfile-test.c b/tests/bookmarkfile-test.c new file mode 100644 index 000000000..efb8b2778 --- /dev/null +++ b/tests/bookmarkfile-test.c @@ -0,0 +1,240 @@ +#undef G_DISABLE_ASSERT + +#include +#include +#include +#include +#include +#include + +#define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz" +#define TEST_URI_1 "file:///test/uri/1" +#define TEST_URI_2 "file:///test/uri/2" + +#define TEST_MIME "text/plain" + +#define TEST_APP_NAME "bookmarkfile-test" +#define TEST_APP_EXEC "bookmarkfile-test %f" + +static void +test_assert_empty_error (GError **error) +{ + if (*error != NULL) + { + g_warning ("Unexpected error (d: %s, c: %d): %s\n", + g_quark_to_string ((*error)->domain), + (*error)->code, + (*error)->message); + g_error_free (*error); + + g_assert_not_reached (); + } +} + +static void +test_assert_not_empty_error (GError **error, + GQuark domain, + gint code) +{ + if (*error == NULL) + { + g_warning ("Unexpected success (%s domain expected)\n", + g_quark_to_string (domain)); + + g_assert_not_reached (); + } + + if ((*error)->domain != domain) + { + g_warning ("Unexpected domain %s (%s domain expected)\n", + g_quark_to_string ((*error)->domain), + g_quark_to_string (domain)); + + g_assert_not_reached (); + } + + if ((*error)->code != code) + { + g_warning ("Unexpected code %d (%d code expected)\n", + (*error)->code, + code); + + g_assert_not_reached (); + } + + g_error_free (*error); + *error = NULL; +} + +static void +test_assert_str_equal (const gchar *str, + const gchar *cmp) +{ + if (strcmp (str, cmp) != 0) + { + g_warning ("Unexpected string '%s' ('%s' expected)\n", str, cmp); + + g_assert_not_reached (); + } +} + +static gboolean +test_load (GBookmarkFile *bookmark, + const gchar *filename) +{ + GError *error = NULL; + gboolean res; + + res = g_bookmark_file_load_from_file (bookmark, filename, &error); + if (error) + { + g_print ("Load error: %s\n", error->message); + g_error_free (error); + } + + return res; +} + +static gboolean +test_query (GBookmarkFile *bookmark) +{ + gint size; + gchar **uris; + gsize uris_len, i; + gboolean res = TRUE; + + size = g_bookmark_file_get_size (bookmark); + uris = g_bookmark_file_get_uris (bookmark, &uris_len); + + if (uris_len != size) + { + g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size); + + res = FALSE; + } + + for (i = 0; i < uris_len; i++) + if (!g_bookmark_file_has_item (bookmark, uris[i])) + { + g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]); + + res = FALSE; + } + + g_strfreev (uris); + + return res; +} + +static gboolean +test_modify (GBookmarkFile *bookmark) +{ + gchar *text; + guint count; + time_t stamp; + GError *error = NULL; + + g_print ("\t=> check global title/description..."); + g_bookmark_file_set_title (bookmark, NULL, "a file"); + g_bookmark_file_set_description (bookmark, NULL, "a bookmark file"); + + text = g_bookmark_file_get_title (bookmark, NULL, &error); + test_assert_empty_error (&error); + test_assert_str_equal (text, "a file"); + g_free (text); + + text = g_bookmark_file_get_description (bookmark, NULL, &error); + test_assert_empty_error (&error); + test_assert_str_equal (text, "a bookmark file"); + g_free (text); + g_print ("ok\n"); + + g_print ("\t=> check bookmark title/description..."); + g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title"); + g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description"); + + text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error); + test_assert_empty_error (&error); + test_assert_str_equal (text, "a title"); + g_free (text); + g_print ("ok\n"); + + g_print ("\t=> check non existing bookmark..."); + g_bookmark_file_get_description (bookmark, TEST_URI_1, &error); + test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); + g_print ("ok\n"); + + g_print ("\t=> check application..."); + g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME); + g_bookmark_file_add_application (bookmark, TEST_URI_0, + TEST_APP_NAME, + TEST_APP_EXEC); + g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL) == TRUE); + g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME, + &text, + &count, + &stamp, + &error); + test_assert_empty_error (&error); + g_assert (count == 1); + g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL)); + g_free (text); + + g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail", + &text, + &count, + &stamp, + &error); + test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED); + g_print ("ok\n"); + + g_print ("\t=> check groups..."); + g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test"); + g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL) == TRUE); + g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL) == FALSE); + g_print ("ok\n"); + + return TRUE; +} + +static gint +test_file (const gchar *filename) +{ + GBookmarkFile *bookmark_file; + gboolean success; + + g_return_val_if_fail (filename != NULL, 1); + + g_print ("checking GBookmarkFile...\n"); + + bookmark_file = g_bookmark_file_new (); + g_assert (bookmark_file != NULL); + + success = test_load (bookmark_file, filename); + + if (success) + { + success = test_query (bookmark_file); + success = test_modify (bookmark_file); + } + + g_bookmark_file_free (bookmark_file); + + g_print ("ok\n"); + + return (success == TRUE ? 0 : 1); +} + +int +main (int argc, + char *argv[]) +{ + if (argc > 1) + return test_file (argv[1]); + else + { + fprintf (stderr, "Usage: bookmarkfile-test \n"); + + return 1; + } +} diff --git a/tests/bookmarks/fail-01.xbel b/tests/bookmarks/fail-01.xbel new file mode 100644 index 000000000..e69de29bb diff --git a/tests/bookmarks/fail-02.xbel b/tests/bookmarks/fail-02.xbel new file mode 100644 index 000000000..39f0094b3 --- /dev/null +++ b/tests/bookmarks/fail-02.xbel @@ -0,0 +1,2 @@ + + diff --git a/tests/bookmarks/fail-03.xbel b/tests/bookmarks/fail-03.xbel new file mode 100644 index 000000000..563dd5408 --- /dev/null +++ b/tests/bookmarks/fail-03.xbel @@ -0,0 +1,18 @@ + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + diff --git a/tests/bookmarks/fail-04.xbel b/tests/bookmarks/fail-04.xbel new file mode 100644 index 000000000..86f1e9012 --- /dev/null +++ b/tests/bookmarks/fail-04.xbel @@ -0,0 +1,21 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + diff --git a/tests/bookmarks/fail-05.xbel b/tests/bookmarks/fail-05.xbel new file mode 100644 index 000000000..355f172de --- /dev/null +++ b/tests/bookmarks/fail-05.xbel @@ -0,0 +1,21 @@ + + + + Singleton + A file & containing a single bookmark element + + + + + + + + + + + diff --git a/tests/bookmarks/fail-06.xbel b/tests/bookmarks/fail-06.xbel new file mode 100644 index 000000000..d1e288bd0 --- /dev/null +++ b/tests/bookmarks/fail-06.xbel @@ -0,0 +1,19 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + diff --git a/tests/bookmarks/fail-07.xbel b/tests/bookmarks/fail-07.xbel new file mode 100644 index 000000000..8e24c64cb --- /dev/null +++ b/tests/bookmarks/fail-07.xbel @@ -0,0 +1,21 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + diff --git a/tests/bookmarks/fail-08.xbel b/tests/bookmarks/fail-08.xbel new file mode 100644 index 000000000..d26f75276 --- /dev/null +++ b/tests/bookmarks/fail-08.xbel @@ -0,0 +1,18 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + diff --git a/tests/bookmarks/fail-09.xbel b/tests/bookmarks/fail-09.xbel new file mode 100644 index 000000000..5e1ce649a --- /dev/null +++ b/tests/bookmarks/fail-09.xbel @@ -0,0 +1,20 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + diff --git a/tests/bookmarks/fail-10.xbel b/tests/bookmarks/fail-10.xbel new file mode 100644 index 000000000..f4eef12c6 --- /dev/null +++ b/tests/bookmarks/fail-10.xbel @@ -0,0 +1,21 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + diff --git a/tests/bookmarks/fail-11.xbel b/tests/bookmarks/fail-11.xbel new file mode 100644 index 000000000..520693be0 --- /dev/null +++ b/tests/bookmarks/fail-11.xbel @@ -0,0 +1,22 @@ + + + + Singleton + A file containing a single bookmark element + + & + + + + + + + + + + diff --git a/tests/bookmarks/fail-12.xbel b/tests/bookmarks/fail-12.xbel new file mode 100644 index 000000000..9ba280e89 --- /dev/null +++ b/tests/bookmarks/fail-12.xbel @@ -0,0 +1,22 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + + diff --git a/tests/bookmarks/fail-13.xbel b/tests/bookmarks/fail-13.xbel new file mode 100644 index 000000000..cdd1affcb --- /dev/null +++ b/tests/bookmarks/fail-13.xbel @@ -0,0 +1,22 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + + diff --git a/tests/bookmarks/fail-14.xbel b/tests/bookmarks/fail-14.xbel new file mode 100644 index 000000000..2161632f0 --- /dev/null +++ b/tests/bookmarks/fail-14.xbel @@ -0,0 +1,24 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + Test + + + + + diff --git a/tests/bookmarks/fail-15.xbel b/tests/bookmarks/fail-15.xbel new file mode 100644 index 000000000..9a2ac1299 --- /dev/null +++ b/tests/bookmarks/fail-15.xbel @@ -0,0 +1,23 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + Test + + + + diff --git a/tests/bookmarks/fail-16.xbel b/tests/bookmarks/fail-16.xbel new file mode 100644 index 000000000..18f55b321 --- /dev/null +++ b/tests/bookmarks/fail-16.xbel @@ -0,0 +1,24 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + Test + + + + + diff --git a/tests/bookmarks/fail-17.xbel b/tests/bookmarks/fail-17.xbel new file mode 100644 index 000000000..9ad97bd60 --- /dev/null +++ b/tests/bookmarks/fail-17.xbel @@ -0,0 +1,22 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + + diff --git a/tests/bookmarks/valid-01.xbel b/tests/bookmarks/valid-01.xbel new file mode 100644 index 000000000..6ab98ba35 --- /dev/null +++ b/tests/bookmarks/valid-01.xbel @@ -0,0 +1,21 @@ + + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + diff --git a/tests/bookmarks/valid-02.xbel b/tests/bookmarks/valid-02.xbel new file mode 100644 index 000000000..85a84a077 --- /dev/null +++ b/tests/bookmarks/valid-02.xbel @@ -0,0 +1,15 @@ + + + Singleton + A file containing a single bookmark element + + + + + + + + + + + diff --git a/tests/run-bookmark-test.sh b/tests/run-bookmark-test.sh new file mode 100755 index 000000000..3bed44d22 --- /dev/null +++ b/tests/run-bookmark-test.sh @@ -0,0 +1,34 @@ +#! /bin/sh + +fail () +{ + echo "Test failed: $*" + exit 1 +} + +echo_v () +{ + if [ "$verbose" = "1" ]; then + echo "$*" + fi +} + +error_out=/dev/null +if [ "$1" = "-v" ]; then + verbose=1 + error_out=/dev/stderr +fi +for I in ${srcdir:-.}/bookmarks/fail-*.xbel; do + echo_v "Parsing $I, should fail" + ./bookmarkfile-test $I > /dev/null 2> $error_out && fail "failed to generate error on $I" + if test "$?" != "1"; then + fail "unexpected error on $I" + fi +done + +for I in ${srcdir:-.}/bookmarks/valid-*.xbel; do + echo_v "Parsing $I, should succeed" + ./bookmarkfile-test $I > /dev/null 2> $error_out || fail "failed on $I" +done + +echo_v "All tests passed."