add test_sock_push, and filter argument to test main()
authorTero Marttila <terom@fixme.fi>
Fri, 13 Mar 2009 15:22:46 +0200
changeset 52 97604efda1ce
parent 51 cc61eaa841ef
child 53 12d806823775
add test_sock_push, and filter argument to test main()
src/test.c
--- a/src/test.c	Fri Mar 13 15:09:53 2009 +0200
+++ b/src/test.c	Fri Mar 13 15:22:46 2009 +0200
@@ -28,11 +28,15 @@
 /**
  * This re-formats the given string to escape values, and returns a pointer to an internal static buffer.
  *
- * If len is given as >= 0, only the given number of chars will be dumped from str
+ * If len is given as >= 0, only the given number of chars will be dumped from str.
  *
- * The buffer cycles a bit, so you pointers remain valid across DUMP_STR_COUNT calls.
+ * The buffer cycles a bit, so the returned pointers remain valid across DUMP_STR_COUNT calls.
  *
- * The resulting strings are truncated to (DUMP_STR_BUF - DUMP_STR_TAIL) bytes, not including the ending "...'\0"
+ * The resulting string is truncated to (DUMP_STR_BUF - DUMP_STR_TAIL) bytes, not including the ending "...'\0".
+ *
+ * @param str the string to dump, should be NUL-terminated unless len is given
+ * @param len if negative, ignored, otherwise, only this many bytes are dumped from str
+ * @param return a pointer to a static buffer that remains valid across DUMP_STR_COUNT calls to this function
  */
 const char *dump_strn (const char *str, ssize_t len)
 {
@@ -81,7 +85,7 @@
         } else if (isprint(*str)) {
             // normal char
             *buf++ = *str;
-
+        
         } else {
             // something more special
             switch (*str) {
@@ -120,19 +124,19 @@
 
 void assert_strcmp (const char *is, const char *should_be)
 {
-    if (strcmp(is, should_be))
+    if (!is || strcmp(is, should_be))
         FATAL("%s != %s", dump_str(is), dump_str(should_be));
 }
 
 void assert_strncmp (const char *is, const char *should_be, size_t n)
 {   
-    if (strncmp(is, should_be, n))
+    if (!is || strncmp(is, should_be, n))
         FATAL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n));
 }
 
 void assert_strlen (const char *str, size_t n)
 {
-    if (strlen(str) != n)
+    if (!str || strlen(str) != n)
         FATAL("strlen(%s) != %u", dump_str(str), (unsigned) n);
 }
 
@@ -146,7 +150,6 @@
 {
     if (err != SUCCESS)
         FATAL("error: %s", error_name(err));
-
 }
 
 void assert_err (err_t err, err_t target)
@@ -209,6 +212,14 @@
     free(buf);
 }
 
+/**
+ * Nicer name for test_sock_add_recv_str
+ */
+void test_sock_push (struct sock_test *sock, const char *str)
+{
+    return sock_test_add_recv_str(sock, str);
+}
+
 void test_dump_str (void)
 {
     log_info("dumping example strings on stdout:");
@@ -267,7 +278,6 @@
     assert_success(line_proto_recv(lp, &line_buf));
 
     if (line_str) {
-        assert(line_buf != NULL);
         assert_strcmp(line_buf, line_str);
 
     } else {
@@ -439,7 +449,7 @@
  
     // test on_register callback    
     if (noisy) log_info("test irc_conn_callbacks.on_register");
-    sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n");
+    test_sock_push(sock, "001 mynick :Blaa blaa blaa\r\n");
     if (ctx) assert(ctx->on_registered);
     assert_strcmp(conn->nickname, "mynick");
    
@@ -464,12 +474,12 @@
 
     // test on_TEST handler
     log_info("test irc_conn.handlers");
-    sock_test_add_recv_str(sock, ":foobar-prefix TEST arg0\r\n");
+    test_sock_push(sock, ":foobar-prefix TEST arg0\r\n");
     assert(ctx.on_TEST);
 
     // test PING/PONG
     log_info("test PING/PONG");
-    sock_test_add_recv_str(sock, "PING foo\r\n");
+    test_sock_push(sock, "PING foo\r\n");
     assert_sock_data(sock, "PONG foo\r\n");
 
     // quit nicely
@@ -478,7 +488,7 @@
     assert_sock_data(sock, "QUIT :bye now\r\n");
     assert(conn->quitting);
 
-    sock_test_add_recv_str(sock, "ERROR :Closing Link: Quit\r\n");
+    test_sock_push(sock, "ERROR :Closing Link: Quit\r\n");
     sock_test_set_recv_eof(sock);
     assert(conn->quit && !conn->quitting && !conn->registered);
     assert(ctx.on_quit);
@@ -544,7 +554,7 @@
     
     // registration reply
     log_info("test irc_conn_on_RPL_WELCOME");
-    sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n");
+    test_sock_push(sock, "001 mynick :Blaa blaa blaa\r\n");
     assert(net->conn->registered);
     assert_strcmp(net->conn->nickname, "mynick");
     
@@ -555,7 +565,7 @@
 
     // JOIN reply
     log_info("test irc_chan_on_JOIN");
-    sock_test_add_recv_str(sock, ":mynick!user@host JOIN #test\r\n");
+    test_sock_push(sock, ":mynick!user@host JOIN #test\r\n");
     assert(!chan->joining && chan->joined);
     assert(ctx.on_chan_self_join);
 
@@ -589,15 +599,28 @@
 
 int main (int argc, char **argv)
 {
+    const char *filter;
     struct test *test;
+    
+    if (argc == 1) {
+        // no arguments
+        filter = NULL;
 
-    (void) argv;
-    
-    // no arguments
-    assert(argc == 1);
+    } else if (argc == 2) {
+        // filter
+        filter = argv[1];
+        
+        log_info("only running tests: %s", filter);
+
+    } else {
+        FATAL("too many arguments");
+    }
 
     // run tests
     for (test = _tests; test->name; test++) {
+        if (filter && strcmp(test->name, filter))
+            continue;
+
         log_info("Running test: %s", test->name);
 
         test->func();