summaryrefslogtreecommitdiff
path: root/src/mongo/unittest
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/unittest')
-rw-r--r--src/mongo/unittest/unittest.h32
-rw-r--r--src/mongo/unittest/unittest_main.cpp2
2 files changed, 31 insertions, 3 deletions
diff --git a/src/mongo/unittest/unittest.h b/src/mongo/unittest/unittest.h
index 29a6c76f5b9..17e9c9416b3 100644
--- a/src/mongo/unittest/unittest.h
+++ b/src/mongo/unittest/unittest.h
@@ -153,13 +153,43 @@
}))
/**
+ * This internal helper is used to ignore warnings about unused results. Some unit tests which test
+ * `ASSERT_THROWS` and its variations are used on functions which both throw and return `Status` or
+ * `StatusWith` objects. Although such function designs are undesirable, they do exist, presently.
+ * Therefore this internal helper macro is used by `ASSERT_THROWS` and its variations to silence
+ * such warnings without forcing the caller to invoke `.ignore()` on the called function.
+ *
+ * NOTE: This macro should NOT be used inside regular unit test code to ignore unchecked `Status` or
+ * `StatusWith` instances -- if a `Status` or `StatusWith` result is to be ignored, please use the
+ * normal `.ignore()` code. This macro exists only to make using `ASSERT_THROWS` less inconvenient
+ * on functions which both throw and return `Status` or `StatusWith`.
+ */
+//#define UNIT_TEST_INTERNALS_IGNORE_UNUSED_RESULT_WARNINGS(STATEMENT)
+#ifdef __GNUC__
+// The `(void) 0`s are to permit more readable formatting of these in-macro pragma statements.
+#define UNIT_TEST_INTERNALS_IGNORE_UNUSED_RESULT_WARNINGS(STATEMENT) \
+ do { \
+ _Pragma("GCC diagnostic push")(void) 0; \
+ _Pragma("GCC diagnostic ignored \"-Wunused\"")(void) 0; \
+ _Pragma("GCC diagnostic ignored \"-Wunused-result\"")(void) 0; \
+ STATEMENT; \
+ _Pragma("GCC diagnostic pop")(void) 0; \
+ } while (false)
+#else
+#define UNIT_TEST_INTERNALS_IGNORE_UNUSED_RESULT_WARNINGS(STATEMENT) \
+ do { \
+ STATEMENT; \
+ } while (false)
+#endif
+
+/**
* Behaves like ASSERT_THROWS, above, but also calls CHECK(caughtException) which may contain
* additional assertions.
*/
#define ASSERT_THROWS_WITH_CHECK(STATEMENT, EXCEPTION_TYPE, CHECK) \
do { \
try { \
- STATEMENT; \
+ UNIT_TEST_INTERNALS_IGNORE_UNUSED_RESULT_WARNINGS(STATEMENT); \
FAIL("Expected statement " #STATEMENT " to throw " #EXCEPTION_TYPE \
" but it threw nothing."); \
} catch (const EXCEPTION_TYPE& ex) { \
diff --git a/src/mongo/unittest/unittest_main.cpp b/src/mongo/unittest/unittest_main.cpp
index 33e99099020..a5910a5b50c 100644
--- a/src/mongo/unittest/unittest_main.cpp
+++ b/src/mongo/unittest/unittest_main.cpp
@@ -1,5 +1,3 @@
-// mongo/unittest/unittest_main.cpp
-
/* Copyright 2010 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify