[devhelp] util: add dh_util_get_possible_index_files()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp] util: add dh_util_get_possible_index_files()
- Date: Mon, 25 Dec 2017 20:19:21 +0000 (UTC)
commit 72f8e5f607a25726901a79d69e48da6c9f4e050c
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Dec 23 19:22:38 2017 +0100
util: add dh_util_get_possible_index_files()
This will be useful in DhBookManager, but is written in dh-util so that
it can be unit-tested.
Add also my copyright to dh-util.h, in 2015 I've also added a util
function.
src/dh-util.c | 44 ++++++++++++++++++++++
src/dh-util.h | 3 +
unit-tests/Makefile.am | 3 +
unit-tests/test-util.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 147 insertions(+), 0 deletions(-)
---
diff --git a/src/dh-util.c b/src/dh-util.c
index bcb7f92..81fd93c 100644
--- a/src/dh-util.c
+++ b/src/dh-util.c
@@ -304,3 +304,47 @@ _dh_util_free_book_tree (GNode *book_tree)
g_node_destroy (book_tree);
}
+
+/* Returns: (transfer full) (element-type GFile): the list of possible Devhelp
+ * index files in @book_directory, in order of preference.
+ */
+GSList *
+dh_util_get_possible_index_files (GFile *book_directory)
+{
+ const gchar *extensions[] = {
+ ".devhelp2",
+ ".devhelp2.gz",
+ ".devhelp",
+ ".devhelp.gz",
+ NULL
+ };
+ gchar *directory_name;
+ GSList *list = NULL;
+ gint i;
+
+ g_return_val_if_fail (G_IS_FILE (book_directory), NULL);
+
+ directory_name = g_file_get_basename (book_directory);
+ g_return_val_if_fail (directory_name != NULL, NULL);
+
+ for (i = 0; extensions[i] != NULL; i++) {
+ const gchar *cur_extension = extensions[i];
+ gchar *index_file_name;
+ GFile *index_file;
+
+ /* The name of the directory the index file is in and the name
+ * of the index file (minus the extension) must match.
+ */
+ index_file_name = g_strconcat (directory_name, cur_extension, NULL);
+
+ index_file = g_file_get_child (book_directory, index_file_name);
+ list = g_slist_prepend (list, index_file);
+
+ g_free (index_file_name);
+ }
+
+ list = g_slist_reverse (list);
+
+ g_free (directory_name);
+ return list;
+}
diff --git a/src/dh-util.h b/src/dh-util.h
index b34cc0f..7eaa335 100644
--- a/src/dh-util.h
+++ b/src/dh-util.h
@@ -2,6 +2,7 @@
/*
* Copyright (C) 2001-2002 Mikael Hallendal <micke imendio com>
* Copyright (C) 2004,2008 Imendio AB
+ * Copyright (C) 2015, 2017 Sébastien Wilmet <swilmet gnome org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -50,6 +51,8 @@ void dh_util_queue_concat (GQueue *q1,
G_GNUC_INTERNAL
void _dh_util_free_book_tree (GNode *book_tree);
+GSList * dh_util_get_possible_index_files (GFile *book_directory);
+
G_END_DECLS
#endif /* DH_UTIL_H */
diff --git a/unit-tests/Makefile.am b/unit-tests/Makefile.am
index e57e34e..66c58c2 100644
--- a/unit-tests/Makefile.am
+++ b/unit-tests/Makefile.am
@@ -15,6 +15,9 @@ UNIT_TEST_PROGS =
UNIT_TEST_PROGS += test-link
test_link_SOURCES = test-link.c
+UNIT_TEST_PROGS += test-util
+test_util_SOURCES = test-util.c
+
noinst_PROGRAMS = $(UNIT_TEST_PROGS)
TESTS = $(UNIT_TEST_PROGS)
diff --git a/unit-tests/test-util.c b/unit-tests/test-util.c
new file mode 100644
index 0000000..0f9b895
--- /dev/null
+++ b/unit-tests/test-util.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2017 Sébastien Wilmet <swilmet gnome org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "dh-util.h"
+
+static void
+check_get_possible_index_files (const gchar *book_directory_path,
+ const gchar *book_basename)
+{
+ GFile *book_directory;
+ GSList *list;
+ GSList *l;
+ gint i;
+
+ book_directory = g_file_new_for_path (book_directory_path);
+ list = dh_util_get_possible_index_files (book_directory);
+
+ g_assert_cmpint (g_slist_length (list), ==, 4);
+
+ for (l = list, i = 0; l != NULL; l = l->next, i++) {
+ GFile *index_file = G_FILE (l->data);
+ gchar *expected_basename;
+ gchar *basename;
+ gchar *expected_path;
+ gchar *path;
+
+ switch (i) {
+ case 0:
+ expected_basename = g_strconcat (book_basename, ".devhelp2", NULL);
+ break;
+
+ case 1:
+ expected_basename = g_strconcat (book_basename, ".devhelp2.gz", NULL);
+ break;
+
+ case 2:
+ expected_basename = g_strconcat (book_basename, ".devhelp", NULL);
+ break;
+
+ case 3:
+ expected_basename = g_strconcat (book_basename, ".devhelp.gz", NULL);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ basename = g_file_get_basename (index_file);
+ g_assert_cmpstr (basename, ==, expected_basename);
+
+ expected_path = g_build_filename (book_directory_path, expected_basename, NULL);
+ path = g_file_get_path (index_file);
+ g_assert_cmpstr (path, ==, expected_path);
+
+ g_free (expected_basename);
+ g_free (basename);
+ g_free (expected_path);
+ g_free (path);
+ }
+
+ g_object_unref (book_directory);
+ g_slist_free_full (list, g_object_unref);
+}
+
+static void
+test_get_possible_index_files (void)
+{
+ check_get_possible_index_files ("/usr/share/gtk-doc/html/glib", "glib");
+ check_get_possible_index_files ("/usr/share/gtk-doc/html/glib/", "glib");
+ check_get_possible_index_files ("/usr/share/gtk-doc/html/gtk3", "gtk3");
+ check_get_possible_index_files ("/usr/share/gtk-doc/html/gtk3/", "gtk3");
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/util/get_possible_index_files", test_get_possible_index_files);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]