summaryrefslogtreecommitdiff
path: root/indra/test/catch_and_store_what_in.h
diff options
context:
space:
mode:
Diffstat (limited to 'indra/test/catch_and_store_what_in.h')
-rw-r--r--indra/test/catch_and_store_what_in.h26
1 files changed, 25 insertions, 1 deletions
diff --git a/indra/test/catch_and_store_what_in.h b/indra/test/catch_and_store_what_in.h
index 59f8cc0085..5beba06024 100644
--- a/indra/test/catch_and_store_what_in.h
+++ b/indra/test/catch_and_store_what_in.h
@@ -2,7 +2,7 @@
* @file catch_and_store_what_in.h
* @author Nat Goodspeed
* @date 2012-02-15
- * @brief CATCH_AND_STORE_WHAT_IN() macro
+ * @brief catch_what() template function, CATCH_AND_STORE_WHAT_IN() macro
*
* $LicenseInfo:firstyear=2012&license=viewerlgpl$
* Copyright (c) 2012, Linden Research, Inc.
@@ -13,6 +13,30 @@
#define LL_CATCH_AND_STORE_WHAT_IN_H
/**
+ * In the brave new world of lambdas, we can use a nicer C++ idiom for testing
+ * exceptions than CATCH_AND_STORE_WHAT_IN() below, e.g.:
+ *
+ * @code
+ * std::string threw = catch_what<std::runtime_error>(
+ * [](){ throw std::runtime_error("badness"); });
+ * ensure_equals(threw, "badness");
+ * @endcode
+ */
+template <typename EXCEPTION, typename FUNC>
+std::string catch_what(FUNC func)
+{
+ try
+ {
+ func();
+ return {};
+ }
+ catch (const EXCEPTION& err)
+ {
+ return err.what();
+ }
+}
+
+/**
* Idiom useful for test programs: catch an expected exception, store its
* what() string in a specified std::string variable. From there the caller
* can do things like: