From a934ee69c4509cdba9add48ffe9fdd929330b0b2 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Wed, 8 Apr 2015 13:58:41 +0100 Subject: [PATCH] [error_state] add a sneaky little stream operator to simplify combining error_states --- base/error_state.h | 5 +++++ unit-tests/Makefile.in | 1 + unit-tests/error_state_t.cc | 31 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 unit-tests/error_state_t.cc diff --git a/base/error_state.h b/base/error_state.h index 9864f6a..079fed9 100644 --- a/base/error_state.h +++ b/base/error_state.h @@ -11,6 +11,11 @@ namespace base { }; error_state combine_errors(error_state lhs, error_state rhs); + + inline error_state &operator <<(error_state &err, error_state rhs) { + err = combine_errors(err, rhs); + return err; + } } //---------------------------------------------------------------- diff --git a/unit-tests/Makefile.in b/unit-tests/Makefile.in index d57f4ce..9307e5f 100644 --- a/unit-tests/Makefile.in +++ b/unit-tests/Makefile.in @@ -57,6 +57,7 @@ TEST_SOURCE=\ unit-tests/cache_superblock_t.cc \ unit-tests/damage_tracker_t.cc \ unit-tests/endian_t.cc \ + unit-tests/error_state_t.cc \ unit-tests/rmap_visitor_t.cc \ unit-tests/run_set_t.cc \ unit-tests/space_map_t.cc \ diff --git a/unit-tests/error_state_t.cc b/unit-tests/error_state_t.cc new file mode 100644 index 0000000..55e096c --- /dev/null +++ b/unit-tests/error_state_t.cc @@ -0,0 +1,31 @@ +#include "gmock/gmock.h" +#include "base/error_state.h" + +using namespace base; +using namespace std; +using namespace testing; + +//---------------------------------------------------------------- + +TEST(ErrorStateTests, stream_operator) +{ + { + error_state err = NO_ERROR; + err << NO_ERROR << FATAL; + ASSERT_THAT(err, Eq(FATAL)); + } + + { + error_state err = NO_ERROR; + err << NO_ERROR << NON_FATAL; + ASSERT_THAT(err, Eq(NON_FATAL)); + } + + { + error_state err = NO_ERROR; + err << NO_ERROR << FATAL << NO_ERROR << NON_FATAL; + ASSERT_THAT(err, Eq(FATAL)); + } +} + +//----------------------------------------------------------------