Added operator<<(ostream, Either)

This commit is contained in:
Sebastian Messmer 2015-06-26 02:17:08 +02:00
parent e8c7efe626
commit f3222e361b
2 changed files with 24 additions and 3 deletions

View File

@ -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<class Left, class Right>
std::ostream &operator<<(std::ostream &stream, const Either<Left, Right> &value) {
if (value.is_left()) {
stream << "Left(" << value.left() << ")";
} else {
stream << "Right(" << value.right() << ")";
}
return stream;
}
//TODO Test make_either<>
//TODO make_either<>
}

View File

@ -3,9 +3,11 @@
#include <boost/optional/optional_io.hpp>
#include "../either.h"
#include "../macros.h"
#include <sstream>
//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<string, int>("mystring");
EXPECT_EQ("Left(mystring)", str.str());
}
TEST_F(EitherTest, OutputRight) {
ostringstream str;
str << Either<int, string>("mystring");
EXPECT_EQ("Right(mystring)", str.str());
}
class DestructorCallback {
public:
MOCK_CONST_METHOD0(call, void());