improved logger tests

Tue, 06 May 2014 14:22:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 06 May 2014 14:22:08 +0200
changeset 170
5fbb9efebe4a
parent 169
279dd3ca7a77
child 171
49cebb8eceff

improved logger tests

test/logging_tests.c file | annotate | diff | comparison | revisions
test/logging_tests.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
ucx/logging.h file | annotate | diff | comparison | revisions
--- a/test/logging_tests.c	Tue May 06 12:30:12 2014 +0200
+++ b/test/logging_tests.c	Tue May 06 14:22:08 2014 +0200
@@ -27,27 +27,79 @@
  */
 
 #include "logging_tests.h"
+#include <time.h>
 
-UCX_TEST(test_ucx_logger_log) {
-    char buffer[100];
+UCX_TEST(test_ucx_logger_new) {
+    
     FILE *stream = tmpfile();
-
     UcxLogger *logger = ucx_logger_new(stream,
             UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
     
     UCX_TEST_BEGIN
-    ucx_logger_info(logger, "allright");
+    UCX_TEST_ASSERT(logger->stream == stream, "stream not set");
+    UCX_TEST_ASSERT(logger->mask == (UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL),
+        "mask not set");
+    UCX_TEST_ASSERT(logger->level == UCX_LOGGER_INFO,
+        "level not set");
+    UCX_TEST_ASSERT(logger->writer == (write_func)fwrite,
+        "writer not set to fwrite");
+    UCX_TEST_ASSERT(strcmp(logger->dateformat, "%F %T %z ") == 0,
+        "date format not set to \"%F %T %z\"");
+    
+    UCX_TEST_ASSERT(logger->levels->count == 4,
+        "incorrect number of registered log levels");
+
+    int level = UCX_LOGGER_ERROR;
+    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
+        "[ERROR]") == 0, "invalid error level");
+    level = UCX_LOGGER_WARN;
+    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
+        "[WARNING]") == 0, "invalid warning level");
+    level = UCX_LOGGER_INFO;
+    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
+        "[INFO]") == 0, "invalid info level");
+    level = UCX_LOGGER_TRACE;
+    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
+        "[TRACE]") == 0, "invalid trace level");
+
+    UCX_TEST_END
+    
+    fclose(stream);
+    ucx_logger_free(logger);
+}
+
+UCX_TEST(test_ucx_logger_log) {
+    char buffer[100];
+    
+    FILE *stream = tmpfile();
+
+    UcxLogger *logger = ucx_logger_new(stream,
+            UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
+    logger->dateformat = "%F:";
+    
+    UCX_TEST_BEGIN
+    const uint line1 = __LINE__; ucx_logger_info(logger, "allright");
+    
     ucx_logger_trace(logger, "dont log this!");
-    ucx_logger_error(logger, "error %d!", 42);
+    
+    logger->mask |= UCX_LOGGER_TIMESTAMP;
+    time_t now = time(NULL);
+    char timestr[13];
+    strftime(timestr, 12, "%F:", localtime(&now));
+    const uint line2 = __LINE__; ucx_logger_error(logger, "error %d!", 42);
+    
     fseek(stream, 0, SEEK_SET);
     size_t r = fread(buffer, 1, 100, stream);
     
-    // TODO: completely rewrite this test
-    
-    size_t expected_length = 76;
-    UCX_TEST_ASSERT(r == expected_length && strncmp(buffer,
-            "[INFO] logging_tests.c:39 - allright\n"
-            "[ERROR] logging_tests.c:41 - error 42!\n", expected_length) == 0, "incorrect logs");
+    const size_t expected_length = 87;
+    char expected[expected_length+1];
+    snprintf(expected, expected_length+1,
+        "[INFO] logging_tests.c:%u - allright\n"
+        "[ERROR] %slogging_tests.c:%u - error 42!\n", line1, timestr, line2);
+
+    UCX_TEST_ASSERT(r == expected_length, "incorrect log length");
+    UCX_TEST_ASSERT(strncmp(buffer, expected, expected_length) == 0,
+        "incorrect logs");
 
     UCX_TEST_END
 
--- a/test/logging_tests.h	Tue May 06 12:30:12 2014 +0200
+++ b/test/logging_tests.h	Tue May 06 14:22:08 2014 +0200
@@ -36,6 +36,7 @@
 extern "C" {
 #endif
 
+UCX_TEST(test_ucx_logger_new);
 UCX_TEST(test_ucx_logger_log);
 
 #ifdef	__cplusplus
--- a/test/main.c	Tue May 06 12:30:12 2014 +0200
+++ b/test/main.c	Tue May 06 14:22:08 2014 +0200
@@ -127,6 +127,7 @@
         ucx_test_register(suite, test_sstrprefixsuffix);
         
         /* UcxLogger Tests */
+        ucx_test_register(suite, test_ucx_logger_new);
         ucx_test_register(suite, test_ucx_logger_log);
         
         /* UcxList Tests */
--- a/ucx/logging.h	Tue May 06 12:30:12 2014 +0200
+++ b/ucx/logging.h	Tue May 06 14:22:08 2014 +0200
@@ -97,7 +97,7 @@
     write_func writer;
 
     /**
-     * The date format for timestamp outputs
+     * The date format for timestamp outputs including the delimiter
      * (default: <code>"%F %T %z "</code>).
      * @see UCX_LOGGER_TIMESTAMP
      */

mercurial