Added first unit tests.
authorDaniel Carl <danielcarl@gmx.de>
Thu, 5 Jun 2014 23:49:47 +0000 (01:49 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Thu, 5 Jun 2014 23:49:47 +0000 (01:49 +0200)
.gitignore
Makefile
config.mk
tests/Makefile [new file with mode: 0644]
tests/test-util.c [new file with mode: 0644]

index 32d4c57..7439153 100644 (file)
@@ -1,7 +1,10 @@
 *.[oa]
-*.do
+*.[ld]o
 vimb
 vimb_dbg
 *.tar.gz
 src/hints.js.h
 src/config.h
+tests/*
+!tests/Makefile
+!tests/*.c
index d391263..7c4ab04 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,8 +2,10 @@ include config.mk
 
 -include $(DEPS)
 
-all: $(TARGET)
+all:   $(TARGET)
 debug: $(DTARGET)
+test:  $(LIBTARGET)
+       @$(MAKE) $(MFLAGS) -s -C tests
 
 options:
        @echo "$(PROJECT) build options:"
@@ -12,57 +14,71 @@ options:
        @echo "LDFLAGS = $(LDFLAGS)"
        @echo "CC      = $(CC)"
 
-src/hints.o: src/hints.js.h
+install: $(TARGET) doc/$(MAN1)
+       install -d $(DESTDIR)$(BINDIR)
+       install -d $(DESTDIR)$(MANDIR1)
+       install -m 755 $(TARGET) $(DESTDIR)$(BINDIR)/$(TARGET)
+       @echo "install -m 644 src/$(MAN1) $(DESTDIR)$(MANDIR1)/$(MAN1)"
+       @sed -e "s/VERSION/$(VERSION)/g" \
+               -e "s/DATE/`date +'%m %Y'`/g" < doc/$(MAN1) > $(DESTDIR)$(MANDIR1)/$(MAN1)
+       @chmod 644 $(DESTDIR)$(MANDIR1)/$(MAN1)
+
+uninstall:
+       $(RM) $(DESTDIR)$(BINDIR)/$(TARGET)
+       $(RM) $(DESTDIR)$(MANDIR1)/$(MAN1)
+
+clean: test-clean
+       $(RM) src/*.o src/*.do src/*.lo src/hints.js.h
+       $(RM) tests/$(LIBTARGET) $(TARGET) $(DTARGET)
+
+test-clean:
+       @$(MAKE) $(MFLAGS) -C tests clean
+
+dist: dist-clean
+       @echo "Creating tarball."
+       @git archive --format tar -o $(DIST_FILE) HEAD
+
+dist-clean:
+       $(RM) $(DIST_FILE)
+
+src/hints.o:  src/hints.js.h
 src/hints.do: src/hints.js.h
+src/hints.lo: src/hints.js.h
 
 src/hints.js.h: src/hints.js
        @echo "minify $<"
        @cat $< | src/js2h.sh > $@
 
-$(OBJ): src/config.h config.mk
+$(OBJ):  src/config.h config.mk
 $(DOBJ): src/config.h config.mk
+$(LOBJ): src/config.h config.mk
 
 $(TARGET): $(OBJ)
        @echo "$(CC) $@"
-       @$(CC) $(OBJ) -o $(TARGET) $(LDFLAGS)
+       @$(CC) $(OBJ) -o $@ $(LDFLAGS)
 
 $(DTARGET): $(DOBJ)
        @echo "$(CC) $@"
-       @$(CC) $(DFLAGS) $(DOBJ) -o $(DTARGET) $(DLDFLAGS)
+       @$(CC) $(DOBJ) -o $@ $(DLDFLAGS)
+
+$(LIBTARGET): $(LOBJ)
+       @echo "$(CC) tests/$@"
+       @$(CC) -shared ${LOBJ} -o ./tests/$(LIBTARGET)
 
 src/config.h:
        @echo create $@ from src/config.def.h
        @cp src/config.def.h $@
 
 %.o: %.c %.h
-       @echo "${CC} $<"
-       @$(CC) -c -o $@ $< $(CFLAGS)
+       @echo "${CC} $@"
+       @$(CC) $(CFLAGS) -c -o $@ $<
 
 %.do: %.c %.h
-       @echo "${CC} $<"
-       @$(CC) -c -o $@ $< $(DFLAGS)
-
-install: $(TARGET) doc/$(MAN1)
-       install -d $(DESTDIR)$(BINDIR)
-       install -d $(DESTDIR)$(MANDIR1)
-       install -m 755 $(TARGET) $(DESTDIR)$(BINDIR)/$(TARGET)
-       @echo "install -m 644 src/$(MAN1) $(DESTDIR)$(MANDIR1)/$(MAN1)"
-       @sed -e "s/VERSION/$(VERSION)/g" \
-               -e "s/DATE/`date +'%m %Y'`/g" < doc/$(MAN1) > $(DESTDIR)$(MANDIR1)/$(MAN1)
-       @chmod 644 $(DESTDIR)$(MANDIR1)/$(MAN1)
+       @echo "${CC} $@"
+       @$(CC) $(DFLAGS) -c -o $@ $<
 
-uninstall:
-       $(RM) $(DESTDIR)$(BINDIR)/$(TARGET)
-       $(RM) $(DESTDIR)$(MANDIR1)/$(MAN1)
-
-clean:
-       $(RM) src/*.o src/*.do src/hints.js.h $(TARGET) $(DTARGET)
-
-dist: distclean
-       @echo "Creating tarball."
-       @git archive --format tar -o $(DIST_FILE) HEAD
-
-distclean:
-       $(RM) $(DIST_FILE)
+%.lo: %.c %.h
+       @echo "${CC} $@"
+       @$(CC) $(CFLAGS) -fPIC -c -o $@ $<
 
-.PHONY: clean debug all install uninstall options dist
+.PHONY: clean debug all install uninstall options dist test
index 4baee69..eaf6a00 100644 (file)
--- a/config.mk
+++ b/config.mk
@@ -46,15 +46,19 @@ CFLAGS  += ${CPPFLAGS}
 LDFLAGS += ${LIBFLAGS}
 
 # compiler flags for the debug target
-DFLAGS   += $(CFLAGS) -ggdb -g
+DFLAGS   += $(CFLAGS) -ggdb -g -O0
 DLDFLAGS += ${LIBFLAGS}
 
 OBJ       = $(patsubst %.c, %.o, $(wildcard src/*.c))
 DOBJ      = $(patsubst %.c, %.do, $(wildcard src/*.c))
+LOBJ      = $(patsubst %.c, %.lo, $(wildcard src/*.c))
 DEPS      = $(OBJ:%.o=%.d)
 
 TARGET    = $(PROJECT)
 DTARGET   = $(TARGET)_dbg
+LIBTARGET = lib$(PROJECT).so
 DIST_FILE = $(PROJECT)_$(VERSION).tar.gz
 MANDIR1   = $(MANDIR)/man1
 MAN1      = $(PROJECT).1
+
+MFLAGS    =
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644 (file)
index 0000000..21fdd72
--- /dev/null
@@ -0,0 +1,14 @@
+include ../config.mk
+
+CPPFLAGS += -I ../
+CFLAGS   += -fPIC
+
+TEST_PROGS = test-util
+
+all: $(TEST_PROGS)
+       LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):." gtester --verbose $(TEST_PROGS)
+
+${TEST_PROGS}: $(LIBTARGET)
+
+clean:
+       rm -f $(TEST_PROGS)
diff --git a/tests/test-util.c b/tests/test-util.c
new file mode 100644 (file)
index 0000000..e4c9337
--- /dev/null
@@ -0,0 +1,151 @@
+/**
+ * vimb - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012-2014 Daniel Carl
+ *
+ * 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 3 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 <gtk/gtk.h>
+#include <src/util.h>
+
+extern VbCore vb;
+
+#define EXPAND(in, out) { \
+    char *value = util_expand(in, UTIL_EXP_DOLLAR|UTIL_EXP_TILDE|UTIL_EXP_SPECIAL); \
+    g_assert_cmpstr(value, ==, out); \
+    g_free(value); \
+}
+
+static void test_expand_evn(void)
+{
+    /* set environment var for testing expansion */
+    g_setenv("VIMB_VAR", "value", true);
+
+    EXPAND("$VIMB_VAR", "value");
+    EXPAND("$VIMB_VAR", "value");
+    EXPAND("$VIMB_VAR$VIMB_VAR", "valuevalue");
+    EXPAND("${VIMB_VAR}", "value");
+    EXPAND("my$VIMB_VAR", "myvalue");
+    EXPAND("'$VIMB_VAR'", "'value'");
+    EXPAND("${VIMB_VAR}s ", "values ");
+
+    g_unsetenv("UNKNOWN");
+
+    EXPAND("$UNKNOWN", "");
+    EXPAND("${UNKNOWN}", "");
+    EXPAND("'$UNKNOWN'", "''");
+}
+
+static void test_expand_escaped(void)
+{
+    g_setenv("VIMB_VAR", "value", true);
+
+    EXPAND("\\$VIMB_VAR", "$VIMB_VAR");
+    EXPAND("\\${VIMB_VAR}", "${VIMB_VAR}");
+
+    EXPAND("\\~/", "~/");
+    EXPAND("\\~/vimb", "~/vimb");
+    EXPAND("\\~root", "~root");
+
+    EXPAND("\\%", "%");
+
+    EXPAND("\\\\$VIMB_VAR", "\\value");         /* \\$VAR becomes \ExpandedVar */
+    EXPAND("\\\\\\$VIMB_VAR", "\\$VIMB_VAR");   /* \\\$VAR becomes \$VAR */
+}
+
+static void test_expand_tilde_home(void)
+{
+    char *dir;
+    const char *home = util_get_home_dir();
+
+    EXPAND("~", "~");
+    EXPAND("~/", home);
+    EXPAND("foo~/bar", "foo~/bar");
+    EXPAND("~/foo", (dir = g_strdup_printf("%s/foo", home)));
+    g_free(dir);
+
+    EXPAND("foo ~/bar", (dir = g_strdup_printf("foo %s/bar", home)));
+    g_free(dir);
+
+    EXPAND("~/~", (dir = g_strdup_printf("%s/~", home)));
+    g_free(dir);
+
+    EXPAND("~/~/", (dir = g_strdup_printf("%s/~/", home)));
+    g_free(dir);
+}
+
+static void test_expand_tilde_user(void)
+{
+    const char *home = util_get_home_dir();
+    const char *user = g_get_user_name();
+    char *in, *out;
+
+    /* don't expand within words */
+    EXPAND((in = g_strdup_printf("foo~%s/bar", user)), in);
+    g_free(in);
+
+    EXPAND((in = g_strdup_printf("foo ~%s", user)), (out = g_strdup_printf("foo %s", home)));
+    g_free(in);
+    g_free(out);
+
+    EXPAND((in = g_strdup_printf("~%s", user)), home);
+    g_free(in);
+
+    EXPAND((in = g_strdup_printf("~%s/bar", user)), (out = g_strdup_printf("%s/bar", home)));
+    g_free(in);
+    g_free(out);
+}
+
+static void test_expand_speacial(void)
+{
+    vb.state.uri = "http://fanglingsu.github.io/vimb/";
+
+    EXPAND("%", "http://fanglingsu.github.io/vimb/");
+    EXPAND("'%'", "'http://fanglingsu.github.io/vimb/'");
+}
+
+static void test_strcasestr(void)
+{
+    g_assert_nonnull(util_strcasestr("Vim like Browser", "browser"));
+    g_assert_nonnull(util_strcasestr("Vim like Browser", "vim LIKE"));
+}
+
+static void test_str_replace(void)
+{
+    char *value;
+
+    value = util_str_replace("a", "uu", "foo bar baz");
+    g_assert_cmpstr(value, ==, "foo buur buuz");
+    g_free(value);
+
+    value = util_str_replace("$1", "placeholder", "string with $1");
+    g_assert_cmpstr(value, ==, "string with placeholder");
+    g_free(value);
+}
+
+int main(int argc, char *argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/test-util/expand-env", test_expand_evn);
+    g_test_add_func("/test-util/expand-escaped", test_expand_escaped);
+    g_test_add_func("/test-util/expand-tilde-home", test_expand_tilde_home);
+    g_test_add_func("/test-util/expand-tilde-user", test_expand_tilde_user);
+    g_test_add_func("/test-util/expand-spacial", test_expand_speacial);
+    g_test_add_func("/test-util/strcasestr", test_strcasestr);
+    g_test_add_func("/test-util/str_replace", test_str_replace);
+
+    return g_test_run();
+}