diff options
author | onqtam <vik.kirilov@gmail.com> | 2017-04-13 18:55:44 +0300 |
---|---|---|
committer | onqtam <vik.kirilov@gmail.com> | 2017-05-16 00:22:19 +0300 |
commit | 2751b5609a7addf978252063d75c1fbb95066348 (patch) | |
tree | 6d379afb019fe029fafa92a2cea39e8b62e557a6 | |
parent | a9675f50d94c425376cacd18e9c06499ab06841f (diff) |
simplified exception translation a bit - relates #12
-rw-r--r-- | doc/markdown/roadmap.md | 4 | ||||
-rw-r--r-- | doctest/doctest.h | 48 | ||||
-rw-r--r-- | doctest/parts/doctest_fwd.h | 20 | ||||
-rw-r--r-- | doctest/parts/doctest_impl.h | 28 | ||||
-rw-r--r-- | scripts/how_exception_translators_work/main.cpp | 52 | ||||
-rw-r--r-- | scripts/random_dev_notes.md | 5 |
6 files changed, 69 insertions, 88 deletions
diff --git a/doc/markdown/roadmap.md b/doc/markdown/roadmap.md index 0a00bd6..1434970 100644 --- a/doc/markdown/roadmap.md +++ b/doc/markdown/roadmap.md @@ -40,14 +40,14 @@ Planned features for future releases - order changes constantly... - redo the compile time ones - also look into CATCH_CONFIG_FAST_COMPILE - remove old benchmarks for doctest 1.0 - add runtime benchmarks +- resolve pull/60 (merge in dev, move cmake folder in scripts, maybe change/remove some of the 2 new SKIP options) - rework the examples folder - so the test runner is compiled only a few times - CI builds take a ton of time! - change docs a bit - mainly what is in the landing page (add link to overload) - address the coverage issue... look at how this project does it: https://github.com/rollbear/trompeloeil -- resolve pull/60 (merge in dev, move cmake folder in scripts, maybe change/remove some of the 2 new SKIP options) +- builds with GCC 7 when it is released (should be in late April) - also by then the precise (ubuntu 12.04) ppa for clang 4.0 should be whitelisted by travis ### For 1.3: -- builds with GCC 7 when it is released (should be in late April) - also by then the precise (ubuntu 12.04) ppa for clang 4.0 should be whitelisted by travis - reporters - output to file - a system for writing custom reporters diff --git a/doctest/doctest.h b/doctest/doctest.h index 4092c4c..8d6110e 100644 --- a/doctest/doctest.h +++ b/doctest/doctest.h @@ -1530,19 +1530,10 @@ namespace detail return res; } - struct ExceptionTranslatorResult - { - bool success; - String result; - - ExceptionTranslatorResult() - : success(false) {} - }; - struct DOCTEST_INTERFACE IExceptionTranslator { virtual ~IExceptionTranslator(); - virtual ExceptionTranslatorResult translate() const = 0; + virtual bool translate(String&) const = 0; }; template <typename T> @@ -1552,17 +1543,16 @@ namespace detail ExceptionTranslator(String (*translateFunction)(T)) : m_translateFunction(translateFunction) {} - ExceptionTranslatorResult translate() const { - ExceptionTranslatorResult res; + bool translate(String& res) const { #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS try { throw; } catch(T ex) { - res.result = m_translateFunction(ex); - res.success = true; + res = m_translateFunction(ex); + return true; } catch(...) {} #endif // DOCTEST_CONFIG_NO_EXCEPTIONS - return res; + return false; } protected: @@ -3887,20 +3877,24 @@ namespace detail String translateActiveException() { #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS - ExceptionTranslatorResult res; + String res; std::vector<const IExceptionTranslator*>& translators = getExceptionTranslators(); - for(size_t i = 0; i < translators.size() && res.success == false; ++i) - res = translators[i]->translate(); - if(res.success == false) { - try { - throw; - } catch(TestFailureException&) { throw; } catch(std::exception& ex) { - res.result = ex.what(); - } catch(std::string& msg) { res.result = msg.c_str(); } catch(const char* msg) { - res.result = msg; - } catch(...) { res.result = "unknown exception"; } + for(size_t i = 0; i < translators.size(); ++i) + if(translators[i]->translate(res)) + return res; + // clang-format off + try { + throw; + } catch(std::exception& ex) { + return ex.what(); + } catch(std::string& msg) { + return msg.c_str(); + } catch(const char* msg) { + return msg; + } catch(...) { + return "unknown exception"; } - return res.result; +// clang-format on #else // DOCTEST_CONFIG_NO_EXCEPTIONS return ""; #endif // DOCTEST_CONFIG_NO_EXCEPTIONS diff --git a/doctest/parts/doctest_fwd.h b/doctest/parts/doctest_fwd.h index 1cd9c4d..4b769cd 100644 --- a/doctest/parts/doctest_fwd.h +++ b/doctest/parts/doctest_fwd.h @@ -1527,19 +1527,10 @@ namespace detail return res; } - struct ExceptionTranslatorResult - { - bool success; - String result; - - ExceptionTranslatorResult() - : success(false) {} - }; - struct DOCTEST_INTERFACE IExceptionTranslator { virtual ~IExceptionTranslator(); - virtual ExceptionTranslatorResult translate() const = 0; + virtual bool translate(String&) const = 0; }; template <typename T> @@ -1549,17 +1540,16 @@ namespace detail ExceptionTranslator(String (*translateFunction)(T)) : m_translateFunction(translateFunction) {} - ExceptionTranslatorResult translate() const { - ExceptionTranslatorResult res; + bool translate(String& res) const { #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS try { throw; } catch(T ex) { - res.result = m_translateFunction(ex); - res.success = true; + res = m_translateFunction(ex); + return true; } catch(...) {} #endif // DOCTEST_CONFIG_NO_EXCEPTIONS - return res; + return false; } protected: diff --git a/doctest/parts/doctest_impl.h b/doctest/parts/doctest_impl.h index f67ee88..3e9d735 100644 --- a/doctest/parts/doctest_impl.h +++ b/doctest/parts/doctest_impl.h @@ -996,20 +996,24 @@ namespace detail String translateActiveException() { #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS - ExceptionTranslatorResult res; + String res; std::vector<const IExceptionTranslator*>& translators = getExceptionTranslators(); - for(size_t i = 0; i < translators.size() && res.success == false; ++i) - res = translators[i]->translate(); - if(res.success == false) { - try { - throw; - } catch(TestFailureException&) { throw; } catch(std::exception& ex) { - res.result = ex.what(); - } catch(std::string& msg) { res.result = msg.c_str(); } catch(const char* msg) { - res.result = msg; - } catch(...) { res.result = "unknown exception"; } + for(size_t i = 0; i < translators.size(); ++i) + if(translators[i]->translate(res)) + return res; + // clang-format off + try { + throw; + } catch(std::exception& ex) { + return ex.what(); + } catch(std::string& msg) { + return msg.c_str(); + } catch(const char* msg) { + return msg; + } catch(...) { + return "unknown exception"; } - return res.result; +// clang-format on #else // DOCTEST_CONFIG_NO_EXCEPTIONS return ""; #endif // DOCTEST_CONFIG_NO_EXCEPTIONS diff --git a/scripts/how_exception_translators_work/main.cpp b/scripts/how_exception_translators_work/main.cpp index 692d0bb..faf5e29 100644 --- a/scripts/how_exception_translators_work/main.cpp +++ b/scripts/how_exception_translators_work/main.cpp @@ -1,5 +1,4 @@ #include <vector> -#include <utility> #include <iostream> #include <stdexcept> using namespace std; @@ -9,32 +8,33 @@ using namespace std; #define ANONYMOUS(x) CAT(x, __COUNTER__) struct ITranslator { - virtual pair<bool, string> translate() const = 0; + virtual bool translate(string&) = 0; }; template<typename T> struct Translator : ITranslator { Translator(string(*func)(T)) : m_func(func) {} - pair<bool, string> translate() const { + bool translate(string& res) { try { throw; } catch(T ex) { - return pair<bool, string>(true, m_func(ex)); + res = m_func(ex); + return true; } catch(...) { - return pair<bool, string>(false, ""); + return false; } } string(*m_func)(T); }; -void regTranslatorImpl(const ITranslator* translator); // fwd decl +void regTranslatorImpl(ITranslator* t); // fwd decl template<typename T> int regTranslator(string(*func)(T)) { - static Translator<T> Translator(func); - regTranslatorImpl(&Translator); + static Translator<T> t(func); + regTranslatorImpl(&t); return 0; } @@ -47,32 +47,30 @@ int regTranslator(string(*func)(T)) { // impl -vector<const ITranslator*> translators; +vector<ITranslator*> translators; -void regTranslatorImpl(const ITranslator* translator) { - translators.push_back(translator); +void regTranslatorImpl(ITranslator* t) { + translators.push_back(t); } string translate() { - pair<bool, string> res(false, ""); // try translators - for(size_t i = 0; i < translators.size() && res.first == false; ++i) - res = translators[i]->translate(); + string res; + for(size_t i = 0; i < translators.size(); ++i) + if(translators[i]->translate(res)) + return res; // proceed with default translation - if(res.first == false) { - try { - throw; - } catch(exception& ex) { - res.second = ex.what(); - } catch(string& msg) { - res.second = msg; - } catch(const char* msg) { - res.second = msg; - } catch(...) { - res.second = "Unknown exception!"; - } + try { + throw; + } catch(exception& ex) { + return ex.what(); + } catch(string& msg) { + return msg; + } catch(const char* msg) { + return msg; + } catch(...) { + return "Unknown exception!"; } - return res.second; } // usage diff --git a/scripts/random_dev_notes.md b/scripts/random_dev_notes.md index d6ac0d8..799954b 100644 --- a/scripts/random_dev_notes.md +++ b/scripts/random_dev_notes.md @@ -1,11 +1,6 @@ - - -rewrite color printing stuff to use streams and not printf functions - - look at boost test again: http://www.boost.org/doc/libs/1_63_0/libs/test/doc/html/index.html |