about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-03-02 01:51:40 +0000
committerJames Booth <boothj5@gmail.com>2012-03-02 01:51:40 +0000
commita05a65febe83c9def5c5d6818b3a31b1b01599e0 (patch)
tree0cbe44538769f43df672adfa5f1123ef5e6a0f32
parentc641ebb8a461fa6742dae60bd9f075837e04aae2 (diff)
downloadprofani-tty-a05a65febe83c9def5c5d6818b3a31b1b01599e0.tar.gz
Added head_unit tests
-rw-r--r--.gitignore1
-rw-r--r--Makefile13
-rw-r--r--history.c5
-rw-r--r--test_history.c130
-rw-r--r--testsuite.c9
-rw-r--r--testsuite.h6
6 files changed, 164 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 7ad97ab2..0b327371 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,4 @@ roster
 *.o
 *.log
 *.swp
+testsuite
diff --git a/Makefile b/Makefile
index aa25b3a4..e6304e78 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,12 @@
 CC = gcc
 WARNS = -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-variable
 LIBS = -lxml2 -lexpat -lssl -lresolv -lncurses -L ~/lib -lstrophe
+TESTLIB = -L ~/lib -l headunit
+CPPLIB = -lstdc++
 CFLAGS = -I ~/include -O3 $(WARNS) $(LIBS)
 OBJS = log.o windows.o title_bar.o status_bar.o input_win.o jabber.o \
        profanity.o util.o command.o history.o main.o
+TESTOBJS = test_history.o history.o
 
 profanity: $(OBJS)
 	$(CC) -o profanity $(OBJS) $(LIBS)
@@ -20,8 +23,18 @@ command.o: command.h util.h history.h
 history.o: history.h
 main.o: profanity.h
 
+test_history.o: history.h
+
+testsuite: testsuite.h $(TESTOBJS)
+	$(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB)
+
+.PHONY: test
+test: testsuite
+	./testsuite
+
 .PHONY: clean
 clean:
 	rm -f profanity
 	rm -f profanity.log
 	rm -f *.o
+	rm -f testsuite
diff --git a/history.c b/history.c
index d47b5d07..9053b54f 100644
--- a/history.c
+++ b/history.c
@@ -20,6 +20,7 @@
  *
  */
 
+#include <stdlib.h>
 #include <string.h>
 
 #define MAX_HISTORY 100
@@ -30,6 +31,10 @@ static int _pos;
 
 void history_init(void)
 {
+    int i;
+    for (i = 0; i < _size; i++)
+        free(_history[i]);
+    
     _size = 0;
     _pos = -1;
 }
diff --git a/test_history.c b/test_history.c
new file mode 100644
index 00000000..14fbe564
--- /dev/null
+++ b/test_history.c
@@ -0,0 +1,130 @@
+#include <stdlib.h>
+#include <head-unit.h>
+#include "history.h"
+
+static void beforetest(void)
+{
+    history_init();
+}
+
+static void previous_returns_null_after_init(void)
+{
+    char *prev = history_previous();
+    assert_true(prev == NULL);
+}
+
+static void next_returns_null_after_init(void)
+{
+    char *next = history_next();
+    assert_true(next == NULL);
+}
+
+static void append_after_init_doesnt_fail(void)
+{
+    history_append("try append");
+    assert_true(1);
+}
+
+static void append_then_previous_returns_appended(void)
+{
+    history_append("try append");
+    char *prev = history_previous();
+    assert_string_equals(prev, "try append"); 
+}
+
+static void append_then_next_returns_null(void)
+{
+    history_append("try append");
+    char *next = history_next();
+    assert_true(next == NULL);
+}
+
+static void hits_null_at_top(void)
+{
+    history_append("cmd1");
+    history_append("cmd2");
+    history_previous(); // cmd2
+    history_previous(); // cmd1
+    char *prev = history_previous();
+    assert_true(prev == NULL);
+}
+
+static void navigate_to_correct_item(void)
+{
+    history_append("cmd1");
+    history_append("cmd2");
+    history_append("cmd3");
+    history_append("cmd4");
+    history_append("cmd5");
+    history_append("cmd6");
+
+    history_previous(); // cmd6
+    history_previous(); // cmd5
+    history_previous(); // cmd4
+    history_previous(); // cmd3
+    history_next(); // cmd4
+    history_previous(); // cmd3
+    history_previous(); // cmd2
+    char *str = history_next(); // cmd3
+
+    assert_string_equals(str, "cmd3");
+}
+
+static void append_previous_item(void)
+{
+    history_append("cmd1");
+    history_append("cmd2");
+    history_append("cmd3");
+    history_append("cmd4");
+    history_append("cmd5");
+    history_append("cmd6");
+
+    history_previous(); // cmd6
+    history_previous(); // cmd5
+    history_previous(); // cmd4
+    history_previous(); // cmd3
+    history_next(); // cmd4
+    history_previous(); // cmd3
+    history_previous(); // cmd2
+    char *str = history_next(); // cmd3
+
+    history_append(str);
+
+    char *cmd3_1 = history_previous();
+    assert_string_equals(cmd3_1, "cmd3");
+
+    char *cmd6 = history_previous();
+    assert_string_equals(cmd6, "cmd6");
+
+    char *cmd5 = history_previous();
+    assert_string_equals(cmd5, "cmd5");
+
+    char *cmd4 = history_previous();
+    assert_string_equals(cmd4, "cmd4");
+
+    char *cmd3 = history_previous();
+    assert_string_equals(cmd3, "cmd3");
+
+    char *cmd2 = history_previous();
+    assert_string_equals(cmd2, "cmd2");
+
+    char *cmd1 = history_previous();
+    assert_string_equals(cmd1, "cmd1");
+
+    char *end = history_previous();
+    assert_true(end == NULL);
+}
+
+void register_history_tests(void)
+{
+    TEST_MODULE("history tests");
+    BEFORETEST(beforetest);
+    TEST(previous_returns_null_after_init);
+    TEST(next_returns_null_after_init);
+    TEST(append_after_init_doesnt_fail);
+    TEST(append_then_previous_returns_appended);
+    TEST(append_then_next_returns_null);
+    TEST(hits_null_at_top);
+    TEST(navigate_to_correct_item);
+    TEST(append_previous_item);
+}
diff --git a/testsuite.c b/testsuite.c
new file mode 100644
index 00000000..af9b6c73
--- /dev/null
+++ b/testsuite.c
@@ -0,0 +1,9 @@
+#include <head-unit.h>
+#include "testsuite.h"
+
+int main(void)
+{
+    register_history_tests();
+    run_suite();
+    return 0;
+}
diff --git a/testsuite.h b/testsuite.h
new file mode 100644
index 00000000..0e67be19
--- /dev/null
+++ b/testsuite.h
@@ -0,0 +1,6 @@
+#ifndef TESTSUITE_H
+#define TESTSUITE_H
+
+void register_history_tests(void);
+
+#endif