about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-04-26 23:13:10 +0100
committerJames Booth <boothj5@gmail.com>2012-04-26 23:13:10 +0100
commit308d7fa945a51bb28d07ea97a437b310ab297c5f (patch)
tree63a55f744aacce949298708647f78186296bc8e9
parent04e021eb363798e4ab01777ac0bc725994d8bd3a (diff)
downloadprofani-tty-308d7fa945a51bb28d07ea97a437b310ab297c5f.tar.gz
Added trim tests
-rw-r--r--test_util.c65
-rw-r--r--util.c3
2 files changed, 68 insertions, 0 deletions
diff --git a/test_util.c b/test_util.c
index ae936d9a..d29a139b 100644
--- a/test_util.c
+++ b/test_util.c
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <string.h>
 #include <head-unit.h>
 #include "util.h"
 
@@ -145,6 +146,64 @@ void replace_when_new_null(void)
     assert_string_equals("hello", result);
 }
 
+void trim_when_no_whitespace_returns_same(void)
+{
+    char *str = malloc((strlen("hi there") + 1) * sizeof(char));
+    strcpy(str, "hi there");
+    char *result = trim(str);
+
+    assert_string_equals("hi there", result);
+    free(str);
+}
+
+void trim_when_space_at_start(void)
+{
+    char *str = malloc((strlen("  hi there") + 1) * sizeof(char));
+    strcpy(str, "  hi there");
+    char *result = trim(str);
+    
+    assert_string_equals("hi there", result);
+    free(str);
+}
+
+void trim_when_space_at_end(void)
+{
+    char *str = malloc((strlen("hi there  ") + 1) * sizeof(char));
+    strcpy(str, "hi there  ");
+    char *result = trim(str);
+
+    assert_string_equals("hi there", result);
+    free(str);
+}
+
+void trim_when_space_at_start_and_end(void)
+{
+    char *str = malloc((strlen("   hi there  ") + 1) * sizeof(char));
+    strcpy(str, "   hi there  ");
+    char *result = trim(str);
+
+    assert_string_equals("hi there", result);
+    free(str);
+}
+
+void trim_when_empty(void)
+{
+    char *str = malloc((strlen("") + 1) * sizeof(char));
+    strcpy(str, "");
+    char *result = trim(str);
+
+    assert_string_equals("", result);
+    free(str);
+}
+
+void trim_when_null(void)
+{
+    char *str = NULL;
+    trim(str);
+
+    assert_is_null(str);
+}
+
 void register_util_tests(void)
 {
     TEST_MODULE("util tests");
@@ -161,4 +220,10 @@ void register_util_tests(void)
     TEST(replace_when_sub_null);
     TEST(replace_when_new_empty);
     TEST(replace_when_new_null);
+    TEST(trim_when_no_whitespace_returns_same);
+    TEST(trim_when_space_at_start);
+    TEST(trim_when_space_at_end);
+    TEST(trim_when_space_at_start_and_end);
+    TEST(trim_when_empty);
+    TEST(trim_when_null);
 }
diff --git a/util.c b/util.c
index bfe21263..b4c152a4 100644
--- a/util.c
+++ b/util.c
@@ -38,6 +38,9 @@ void get_time(char *thetime)
 
 char *trim(char *str)
 {
+    if (str == NULL)
+        return NULL;
+
     char *end;
 
     while (isspace(*str))