changeset 18852:3275eee3613b

lib-test: let tests invoke i_warning/i_error behaviour 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>
author Phil Carmody <phil@dovecot.fi>
date Mon, 15 Jun 2015 14:31:19 +0300
parents b5608c537daa
children 8739d201898c
files src/lib-test/test-common.c src/lib-test/test-common.h
diffstat 2 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-test/test-common.c	Mon Jun 15 14:31:19 2015 +0300
+++ b/src/lib-test/test-common.c	Mon Jun 15 14:31:19 2015 +0300
@@ -19,6 +19,7 @@
 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 @@
 	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 @@
 		return;
 	}
 #endif
+	if (expected_errors > 0) {
+		expected_errors--;
+		return;
+	}
 	test_success = FALSE;
 }
 
--- a/src/lib-test/test-common.h	Mon Jun 15 14:31:19 2015 +0300
+++ b/src/lib-test/test-common.h	Mon Jun 15 14:31:19 2015 +0300
@@ -20,6 +20,9 @@
 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);