Allow outputting const_string to std::ostream

This commit is contained in:
Sebastian Messmer 2015-09-18 22:08:56 +02:00
parent 0fc5731f18
commit 819e48b446
2 changed files with 28 additions and 4 deletions

View File

@ -3,6 +3,7 @@
#include <cstring>
#include <string>
#include <iostream>
#include "impl/digit_parser.h"
namespace cpputils {
@ -71,6 +72,11 @@ namespace cpputils {
const char *_str;
unsigned int _size;
};
std::ostream &operator<<(std::ostream &stream, const const_string &str) {
stream << str.toStdString();
return stream;
}
}
#endif

View File

@ -2,6 +2,8 @@
#include <google/gtest/gtest.h>
using namespace cpputils;
using std::ostringstream;
using std::string;
// ----------------------------------------------
// size()
@ -118,8 +120,24 @@ static_assert(const_string("") == const_string("").dropUIntPrefix(), "\"\" shoul
// toStdString()
// ----------------------------------------------
TEST(const_string_test, toStdString) {
EXPECT_EQ("", const_string("").toStdString());
EXPECT_EQ("a", const_string("a").toStdString());
EXPECT_EQ("abc", const_string("abc").toStdString());
EXPECT_EQ("abc", const_string("prefix_abc_suffix").substr(7,3).toStdString());
EXPECT_EQ("", const_string("").toStdString());
EXPECT_EQ("a", const_string("a").toStdString());
EXPECT_EQ("abc", const_string("abc").toStdString());
EXPECT_EQ("abc", const_string("prefix_abc_suffix").substr(7,3).toStdString());
}
// ----------------------------------------------
// operator<<
// ----------------------------------------------
void EXPECT_OUTPUTS(const string &expected, const const_string &testObj) {
ostringstream stream;
stream << testObj;
EXPECT_EQ(expected, stream.str());
}
TEST(const_string_test, OutputOperator) {
EXPECT_OUTPUTS("", const_string(""));
EXPECT_OUTPUTS("a", const_string("a"));
EXPECT_OUTPUTS("abc", const_string("abc"));
EXPECT_OUTPUTS("abc", const_string("prefix_abc_suffix").substr(7,3));
}