From 99ba8e2b06ecce6d4fb161a3addf46a57da6c471 Mon Sep 17 00:00:00 2001 From: Martin Marinov <Martin.Marinov@autodesk.com> Date: Fri, 25 Feb 2022 15:20:43 +0000 Subject: [PATCH] MTBR-915 Improve baseline records for unexpected ourcomes (#76) --- Debug/DebTime.hh | 3 +-- Test/TestOutcome.cc | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Debug/DebTime.hh b/Debug/DebTime.hh index 3743027..dec65c4 100644 --- a/Debug/DebTime.hh +++ b/Debug/DebTime.hh @@ -28,9 +28,8 @@ public: ~StopWatchSessionT() NOEXCEPT(false) { - const auto dvsr = std::max(ONE, dvsr_); // TODO: implement "prettier" DEB out if seconds turn into minutes/hours/etc - DEB_line_if(dvsr_ == ONE, deb_lvl_, + DEB_line_if(dvsr_ <= ONE, deb_lvl_, sssn_name_ << " took " << sw_.stop() / 1000.0 << " s."); DEB_line_if(dvsr_ > ONE, deb_lvl_, dvsr_ << " x " << sssn_name_ << " took " << sw_.stop() / 1000.0 / dvsr_ diff --git a/Test/TestOutcome.cc b/Test/TestOutcome.cc index 5f38b50..ec5e5cd 100755 --- a/Test/TestOutcome.cc +++ b/Test/TestOutcome.cc @@ -20,17 +20,26 @@ namespace Test namespace { -void record_and_handle_unexpected( - const char* const _call, const Outcome& _oc, const bool _unexpected) +void record_and_handle_unexpected(const char* const _call, const Outcome& _oc, + const bool _unexpected, const char* const _expctd_what) { - TEST(outcome, record(_call, _oc.ok(), _oc.error_description().c_str())); - if (_unexpected) + { + Base::OStringStream strm; + strm << "Unexpected outcome: Expected " << _expctd_what + << " but got " << _oc.error_description().c_str() << " instead!"; + TEST(outcome, record(_call, _oc.ok(), strm.str.c_str())); _oc.unexpected_handler(); + } + else + TEST(outcome, record(_call, _oc.ok(), _oc.error_description().c_str())); } } // namespace -Outcome::Outcome() : ok_(true), error_code_("0"), error_message_("Success") {} +const char SUCCESS[] = "Success"; +const Outcome FAILURE(false, "FAILURE", "Failure"); + +Outcome::Outcome() : ok_(true), error_code_(SUCCESS), error_message_(SUCCESS) {} Outcome::Outcome( bool _ok, const std::string& _error_code, const std::string& _error_message) @@ -58,21 +67,22 @@ Outcome::UnexpectedHandler Outcome::unexpected_handler = [] const Outcome& record_outcome(const char* const _call, const Outcome& _oc) { // Never call unexpected-outcome handler - record_and_handle_unexpected(_call, _oc, false); + record_and_handle_unexpected(_call, _oc, false, nullptr); return _oc; } void expect_success(const char* const _call, const Outcome& _oc) { // Call unexpected-outcome handler if the call *failed* - record_and_handle_unexpected(_call, _oc, !_oc.ok()); + record_and_handle_unexpected(_call, _oc, !_oc.ok(), SUCCESS); } void expect_outcome( const char* const _call, const Outcome& _oc, const char* const _error_code) { // Call unexpected-outcome handler if the error code is not correct - record_and_handle_unexpected(_call, _oc, _oc.error_code() != _error_code); + record_and_handle_unexpected( + _call, _oc, _oc.error_code() != _error_code, _error_code); } void expect_outcome( @@ -84,10 +94,11 @@ void expect_outcome( void expect_failure(const char* const _call, const Outcome& _oc) { // If _oc represents a failure, replace it with a generic failure outcome - auto oc = _oc.ok() ? _oc : Outcome(_oc.ok(), "FAILURE", "Failure"); + auto oc = _oc.ok() ? _oc : FAILURE; // Call unexpected-outcome handler if the call *succeeds* - record_and_handle_unexpected(_call, oc, oc.ok()); + record_and_handle_unexpected( + _call, oc, oc.ok(), FAILURE.error_code().c_str()); } void ignore_outcome(const char* const _call, const char* const _reason) -- GitLab