More help tests.
authorJelmer Vernooij <jelmer@samba.org>
Sat, 17 Feb 2007 19:20:04 +0000 (20:20 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Sat, 17 Feb 2007 19:20:04 +0000 (20:20 +0100)
src/help.c
src/help.h
src/main.c
testsuite/test-help.c

index c55702bbc732db5c17a707ddb2cf3d159996b127..14a88f97177c69c300e8ca7ef32291d7d10878d3 100644 (file)
@@ -33,13 +33,45 @@ void help_free(help_t *h)
        g_free(h);
 }
 
-help_t *help_init( const char *helpfile )
+GHashTable *help_build_hash(char *data, gsize len)
+{
+       GHashTable *h = g_hash_table_new(g_str_hash, g_str_equal);
+       gsize i, k;
+       char *p;
+
+       i = 0;
+       while (i < len) {
+               if (data[i] == '?') {
+                       /* Key starts here */
+                       k = i+1;
+                       p = g_strstr_len(data+k, len-k, "\n%\n");
+                       if (p == NULL) {
+                               log_global(LOG_WARNING, "Error parsing help file");
+                               g_hash_table_destroy(h);
+                               return NULL;
+                       }
+                       p[1] = 0;
+                       i+=strlen(data+i)+2;
+                       p = g_strstr_len(data+k, len-k, "\n");
+                       p[0] = 0;
+                       g_hash_table_insert(h, data+k, p+1);
+               } else {
+                       log_global(LOG_WARNING, "Unknown character '0x%02x' in help file", 
+                                          data[i]);
+                       g_hash_table_destroy(h);
+                       return NULL;
+               }
+       }
+       
+       return h;
+}
+
+help_t *help_load_file( const char *helpfile )
 {
        help_t *h;
        char *data;
        GError *error = NULL;
-       gsize len, i, k;
-       char *p;
+       gsize len;
        
        h = g_new0 (help_t, 1);
        
@@ -56,36 +88,12 @@ help_t *help_init( const char *helpfile )
                return NULL;
        }
 
-       h->entries = g_hash_table_new(g_str_hash, g_str_equal);
-       i = 0;
-       while (i < len) {
-               if (data[i] == '?') {
-                       /* Key starts here */
-                       k = i+1;
-                       p = g_strstr_len(data+k, len-k, "\n%\n");
-                       if (p == NULL) {
-                               log_global(LOG_WARNING, "Error parsing help file");
-                               goto error;
-                       }
-                       p[1] = 0;
-                       i+=strlen(data+i+3);
-                       p = g_strstr_len(data+k, len-k, "\n");
-                       p[0] = 0;
-                       g_hash_table_insert(h->entries, data+k, p+1);
-               } else {
-                       log_global(LOG_WARNING, "Unknown character '%c' in help file", 
-                                          data[i]);
-                       i++;
-               }
+       h->entries = help_build_hash(data, len);
+       if (h->entries == NULL) {
+               g_mapped_file_free(h->file);
+               g_free(h);
        }
-
        return h;
-
-error:
-       g_mapped_file_free(h->file);
-       g_hash_table_destroy(h->entries);
-       g_free(h);
-       return NULL;
 }
 
 const char *help_get(help_t *h, const char *string)
index 9528a84c69386ac964ffb5156764940feee9aeb7..6d456cb1823ae5f4fce79d3489c0389224993893 100644 (file)
@@ -32,7 +32,7 @@ typedef struct help
        GHashTable *entries;
 } help_t;
 
-help_t *help_init( const char *helpfile );
+help_t *help_load_file(const char *helpfile);
 const char *help_get( help_t *help, const char *string );
 void help_free(help_t *h);
 
index add454aebf924906d15b23acdcf380192ca5a9c7..45a20a5a0abba6bb74e2677bb6a9bfd05ff9f03e 100644 (file)
@@ -263,7 +263,7 @@ int main(int argc, char **argv)
 
        init_admin();
        init_nickserv();
-       help_init(&help, HELPFILE);
+       help = help_load_file(HELPFILE);
 
        /* Determine correct modules directory */
        init_plugins(getenv("CTRLPROXY_MODULESDIR")?getenv("CTRLPROXY_MODULESDIR"):MODULESDIR);
index 7e5283e3833a17a4d3099ff5ffa902ab960e8b88..829f6fbd93b13daecc03d1448a3ad911cf619e71 100644 (file)
@@ -6,25 +6,81 @@
 #include <stdio.h>
 #include "help.h"
 
+GHashTable *help_build_hash(char *data, gsize len);
+
+static GHashTable *build_hash(const char *data)
+{
+       return help_build_hash(g_strdup(data), strlen(data));
+}
+
+START_TEST(test_help_hash)
+       GHashTable *h = build_hash("?foo\nbar\n%\n");
+       fail_unless(g_hash_table_lookup(h, "foo") != NULL);
+       fail_unless(strcmp(g_hash_table_lookup(h, "foo"), "bar\n") == 0);
+END_TEST
+
+START_TEST(test_help_hash_nopercent)
+       GHashTable *h = build_hash("?foo\nbar\n");
+       fail_unless(h == NULL);
+END_TEST
+
+START_TEST(test_help_hash_multiple)
+       GHashTable *h = build_hash("?foo\nbar\n%\n?bla\nbloe\nblie\n%\n");
+       fail_unless(g_hash_table_lookup(h, "foo") != NULL);
+       fail_unless(strcmp(g_hash_table_lookup(h, "foo"), "bar\n") == 0);
+       fail_unless(strcmp(g_hash_table_lookup(h, "bla"), "bloe\nblie\n") == 0);
+END_TEST
+
+START_TEST(test_help_hash_empty)
+       GHashTable *h = build_hash("?foo\nbar\n%\n?\nbloe\nblie\n%\n");
+       fail_unless(g_hash_table_lookup(h, "foo") != NULL);
+       fail_unless(strcmp(g_hash_table_lookup(h, "foo"), "bar\n") == 0);
+       fail_unless(strcmp(g_hash_table_lookup(h, ""), "bloe\nblie\n") == 0);
+END_TEST
+
+START_TEST(test_help_hash_strange)
+       GHashTable *h = build_hash("barbla");
+       fail_unless(h == NULL);
+END_TEST
+
 START_TEST(test_help_none)
-       help_t *h, *r;
-       r = help_init(&h, "/dev/null");
-       fail_if(r == NULL);
-       fail_if(r != h);
+       help_t *h;
+       h = help_load_file("/dev/null");
+       fail_if(h == NULL);
+END_TEST
+
+START_TEST(test_help_nonexistant)
+       help_t *h;
+       h = help_load_file("IDONTEXIST");
+       fail_unless(h == NULL);
+END_TEST
+
+START_TEST(test_help_free)
+       help_t *h;
+       h = help_load_file("/dev/null");
+       fail_if(h == NULL);
+       help_free(h);
 END_TEST
 
 START_TEST(test_help_nonexistent)
-       help_t *h, *r;
-       r = help_init(&h, "/dev/null");
-       fail_unless(help_get(&h, "nonexistent") == NULL);
+       help_t *h;
+       h = help_load_file("/dev/null");
+       fail_unless(help_get(h, "nonexistent") == NULL);
 END_TEST
 
 Suite *help_suite (void)
 {
-       Suite *s = suite_create("Help");
+       Suite *s = suite_create("help");
        TCase *tc_core = tcase_create("Core");
        suite_add_tcase (s, tc_core);
        tcase_add_test (tc_core, test_help_none);
+       tcase_add_test (tc_core, test_help_hash);
+       tcase_add_test (tc_core, test_help_hash_empty);
+       tcase_add_test (tc_core, test_help_hash_multiple);
+       tcase_add_test (tc_core, test_help_hash_nopercent);
+       tcase_add_test (tc_core, test_help_hash_strange);
        tcase_add_test (tc_core, test_help_nonexistent);
+       tcase_add_test (tc_core, test_help_free);
+       tcase_add_test (tc_core, test_help_nonexistant);
        return s;
 }