]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-test: let tests invoke i_warning/i_error behaviour
authorPhil Carmody <phil@dovecot.fi>
Mon, 15 Jun 2015 11:31:19 +0000 (14:31 +0300)
committerPhil Carmody <phil@dovecot.fi>
Mon, 15 Jun 2015 11:31:19 +0000 (14:31 +0300)
Error-handling paths should be testable too. Permit a test case to register
that a known number of warnings/errors are to be expected, and to verify
that those warnings did occur afterwards. Too many messages will fail the
test exactly as it did in the past, they're unexpected messages. However,
too few messages will also cause the test case to fail.

Signed-off-by: Phil Carmody <phil@dovecot.fi>
src/lib-test/test-common.c
src/lib-test/test-common.h

index 45037bda39c693b286de3f893c7868e10db6d0af..72038a29dbe155694a0efa3a84a4fe7a9517765e 100644 (file)
@@ -19,6 +19,7 @@ static char *test_prefix;
 static bool test_success;
 static unsigned int failure_count;
 static unsigned int total_count;
+static unsigned int expected_errors;
 
 struct test_istream {
        struct istream_private istream;
@@ -256,6 +257,19 @@ void test_out_reason(const char *name, bool success, const char *reason)
        total_count++;
 }
 
+void
+test_expect_errors(unsigned int expected)
+{
+       i_assert(expected_errors == 0);
+       expected_errors = expected;
+}
+void
+test_expect_no_more_errors(void)
+{
+       test_assert(expected_errors == 0);
+       expected_errors = 0;
+}
+
 static void ATTR_FORMAT(2, 0)
 test_error_handler(const struct failure_context *ctx,
                   const char *format, va_list args)
@@ -270,6 +284,10 @@ test_error_handler(const struct failure_context *ctx,
                return;
        }
 #endif
+       if (expected_errors > 0) {
+               expected_errors--;
+               return;
+       }
        test_success = FALSE;
 }
 
index 66da66a2b58dac1b6dfe7774a82ff605d0adf4f6..0e6f85390782065160f82ebfa6d2aef470c41847 100644 (file)
@@ -20,6 +20,9 @@ void test_begin(const char *name);
 void test_assert_failed(const char *code, const char *file, unsigned int line);
 void test_assert_failed_idx(const char *code, const char *file, unsigned int line, long long i);
 bool test_has_failed(void);
+/* If you're testing nasty cases which you want to warn, surround the noisy op with these */
+void test_expect_errors(unsigned int expected);
+void test_expect_no_more_errors(void);
 void test_end(void);
 
 void test_out(const char *name, bool success);