Allow outputting const_string to std::ostream
This commit is contained in:
parent
0fc5731f18
commit
819e48b446
@ -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
|
||||
|
@ -2,6 +2,8 @@
|
||||
#include <google/gtest/gtest.h>
|
||||
|
||||
using namespace cpputils;
|
||||
using std::ostringstream;
|
||||
using std::string;
|
||||
|
||||
// ----------------------------------------------
|
||||
// size()
|
||||
@ -123,3 +125,19 @@ TEST(const_string_test, 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));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user