commit: cb6872cc9790ee2cd04f54ee762c23f5e48d9f4b
parent edbe278f8dcf822504e25f055dc89dcfcd9e3656
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 13 Mar 2022 00:15:35 +0100
bookmark_test: Add basic test
Diffstat:
4 files changed, 71 insertions(+), 13 deletions(-)
diff --git a/Makefile b/Makefile
@@ -7,9 +7,9 @@ include config.mk
SRCS = bookmarks.c fmt.c fmt_test.c uri.c uri_test.c keybindings.c downloads.c badwolf.c
OBJS = bookmarks.o fmt.o uri.o keybindings.o downloads.o badwolf.o
-OBJS_test = fmt_test.o uri_test.o
+OBJS_test = fmt_test.o uri_test.o bookmarks_test.o
EXE = badwolf
-EXE_test = fmt_test uri_test
+EXE_test = fmt_test uri_test bookmarks_test
TRANS = fr.mo pt_BR.mo tr.mo de.mo vi.mo
DOCS = usr.bin.badwolf README.md KnowledgeBase.md interface.txt
@@ -54,10 +54,15 @@ uri_test: uri.o uri_test.o
fmt_test: fmt.o fmt_test.o
$(CC) -std=c11 -o $@ fmt.o fmt_test.o $(LDFLAGS) $(LIBS)
+bookmarks_test: bookmarks.o bookmarks_test.o
+ $(CC) -std=c11 -o $@ bookmarks.o bookmarks_test.o $(LDFLAGS) $(LIBS)
+
+
.PHONY: test
test: $(EXE_test)
$(DBG) ./uri_test
$(DBG) ./fmt_test
+ $(DBG) ./bookmarks_test
.PHONY: lint
lint:
diff --git a/bookmarks.c b/bookmarks.c
@@ -69,18 +69,19 @@ load_xpath_results(GtkListStore *list_store, xmlNodeSetPtr nodes)
GtkTreeModel *
bookmarks_completion_init()
{
- const xmlChar *xpathExpr = (const xmlChar *)"//bookmark/@href";
- char *filename = g_build_filename(g_get_user_data_dir(), "badwolf", "bookmarks.xbel", NULL);
- xmlDocPtr doc = NULL;
+ xmlDocPtr doc = NULL;
xmlXPathContextPtr xpathCtx = NULL;
xmlXPathObjectPtr xpathObj = NULL;
- GtkListStore *list_store = gtk_list_store_new(1, G_TYPE_STRING);
+
+ const xmlChar *xpathExpr = (const xmlChar *)"//bookmark/@href";
+ char *filename = g_build_filename(g_get_user_data_dir(), "badwolf", "bookmarks.xbel", NULL);
+ GtkListStore *list_store = gtk_list_store_new(1, G_TYPE_STRING);
/* flawfinder: ignore, just a presence check */
if(access(filename, R_OK) != 0)
{
g_fprintf(stderr, _("Bookmarks: No loadable file found at %s\n"), filename);
- return NULL;
+ goto failure;
}
g_fprintf(stderr, _("Bookmarks: loading at %s\n"), filename);
@@ -88,8 +89,7 @@ bookmarks_completion_init()
if(doc == NULL)
{
g_fprintf(stderr, _("Bookmarks: unable to parse file \"%s\"\n"), filename);
- location_completion_cleanup(xpathObj, xpathCtx, doc);
- return NULL;
+ goto failure;
}
xmlXIncludeProcess(doc);
@@ -98,16 +98,14 @@ bookmarks_completion_init()
if(xpathCtx == NULL)
{
g_fprintf(stderr, _("Bookmarks: unable to create new XPath context\n"));
- location_completion_cleanup(xpathObj, xpathCtx, doc);
- return NULL;
+ goto failure;
}
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
if(xpathObj == NULL)
{
g_fprintf(stderr, _("Bookmarks: unable to evaluate XPath expression \"%s\"\n"), xpathExpr);
- location_completion_cleanup(xpathObj, xpathCtx, doc);
- return NULL;
+ goto failure;
}
load_xpath_results(list_store, xpathObj->nodesetval);
@@ -117,4 +115,9 @@ bookmarks_completion_init()
g_fprintf(stderr, _("Bookmarks: Done.\n"));
return GTK_TREE_MODEL(list_store);
+
+failure:
+ location_completion_cleanup(xpathObj, xpathCtx, doc);
+ g_free(filename);
+ return NULL;
}
diff --git a/bookmarks_test.c b/bookmarks_test.c
@@ -0,0 +1,34 @@
+// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
+// Copyright © 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+// SPDX-License-Identifier: BSD-3-Clause
+
+#include "bookmarks.h"
+
+#include <glib.h>
+#include <gtk/gtk.h>
+#include <stdlib.h> /* setenv */
+
+static void
+bookmarks_completion_init_test(void)
+{
+ GtkTreeModel *result;
+
+ char *filename = g_build_filename(g_get_current_dir(), "bookmarks_test.fixtures/full/", NULL);
+ g_assert_true(setenv("XDG_DATA_HOME", filename, 1) == 0);
+
+ result = bookmarks_completion_init();
+
+ g_assert_true(result != NULL);
+ g_free(filename);
+ g_assert_true(unsetenv("XDG_DATA_HOME") == 0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/bookmarks_completion_init/test", bookmarks_completion_init_test);
+
+ return g_test_run();
+}
diff --git a/bookmarks_test.fixtures/full/badwolf/bookmarks.xbel b/bookmarks_test.fixtures/full/badwolf/bookmarks.xbel
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="bookmarks.xsl"?>
+<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML"
+ "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">
+
+<xbel>
+ <folder>
+ <title>Operating Systems</title>
+ <bookmark href="http://9front.org/">
+ <title>9Front</title>
+ <desc>Fork of Plan 9, a Unix successor by Bell Labs</desc>
+ </bookmark>
+ </folder>
+ <bookmark href="https://litepub.social/"><title>LitePub</title><desc>Profile of ActivityPub used by Pleroma et al.</desc></bookmark>
+ <!-- comment -->
+</xbel>