From f3222e361bbc28d3c3922f99c22a44d44c6914c0 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 26 Jun 2015 02:17:08 +0200 Subject: [PATCH] Added operator<<(ostream, Either) --- either.h | 13 ++++++++++--- test/EitherTest.cpp | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/either.h b/either.h index 87ae08c4..bc07297c 100644 --- a/either.h +++ b/either.h @@ -67,9 +67,6 @@ namespace cpputils { //TODO fold, map_left, map_right, left_or_else(val), right_or_else(val), left_or_else(func), right_or_else(func) - //TODO Test operator<< - //TODO operator<<(ostream) - bool is_left() const { return _side == Side::left; } @@ -167,6 +164,16 @@ namespace cpputils { return !operator==(lhs, rhs); } + template + std::ostream &operator<<(std::ostream &stream, const Either &value) { + if (value.is_left()) { + stream << "Left(" << value.left() << ")"; + } else { + stream << "Right(" << value.right() << ")"; + } + return stream; + } + //TODO Test make_either<> //TODO make_either<> } diff --git a/test/EitherTest.cpp b/test/EitherTest.cpp index 91470547..2b0697b4 100644 --- a/test/EitherTest.cpp +++ b/test/EitherTest.cpp @@ -3,9 +3,11 @@ #include #include "../either.h" #include "../macros.h" +#include //TODO Go through all test cases and think about whether it makes sense to add the same test case but with primitive types. +using std::ostringstream; using std::string; using std::vector; using namespace cpputils; @@ -295,6 +297,18 @@ TEST_F(EitherTest, LeftNotEqualsRight) { EXPECT_FALSE(val2 == val1); } +TEST_F(EitherTest, OutputLeft) { + ostringstream str; + str << Either("mystring"); + EXPECT_EQ("Left(mystring)", str.str()); +} + +TEST_F(EitherTest, OutputRight) { + ostringstream str; + str << Either("mystring"); + EXPECT_EQ("Right(mystring)", str.str()); +} + class DestructorCallback { public: MOCK_CONST_METHOD0(call, void());