Disallow writing a wide string to a char stream and vice versa, because there is no good way to implement these function without knowing the encodings and what std::ostream doesn't make any sense.

This commit is contained in:
Victor Zverovich
2013-09-09 22:21:40 -07:00
parent 1733198bf5
commit b605b3980c
3 changed files with 105 additions and 45 deletions
+52 -12
View File
@@ -89,22 +89,50 @@ using fmt::pad;
FORMAT_TEST_THROW_(statement, expected_exception, expected_message, \
GTEST_NONFATAL_FAILURE_)
struct WriteChecker {
// Checks if writing value to BasicWriter<Char> produces the same result
// as writing it to std::basic_ostringstream<Char>.
template <typename Char, typename T>
static ::testing::AssertionResult CheckWrite(const T &value, const char *type) {
std::basic_ostringstream<Char> os;
os << value;
std::basic_string<Char> expected = os.str();
std::basic_string<Char> actual = str(BasicWriter<Char>() << value);
if (expected == actual)
return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< "Value of: str(Writer<" << type << ">() << value)\n"
<< " Actual: " << actual << "\n"
<< "Expected: " << expected << "\n";
}
struct AnyWriteChecker {
template <typename T>
::testing::AssertionResult operator()(const char *, const T &value) const {
std::ostringstream os;
os << value;
std::string expected = os.str(), actual = str(Writer() << value);
if (expected == actual)
return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< "Value of: str(Writer() << value)\n"
<< " Actual: " << actual << "\n"
<< "Expected: " << expected << "\n";
::testing::AssertionResult result = CheckWrite<char>(value, "char");
return result ? CheckWrite<wchar_t>(value, "wchar_t") : result;
}
};
#define CHECK_WRITE(value) ASSERT_PRED_FORMAT1(WriteChecker(), value)
struct CharWriteChecker {
template <typename T>
::testing::AssertionResult operator()(const char *, const T &value) const {
return CheckWrite<char>(value, "char");
}
};
struct WCharWriteChecker {
template <typename T>
::testing::AssertionResult operator()(const char *, const T &value) const {
return CheckWrite<wchar_t>(value, "char");
}
};
// Checks if writing value to BasicWriter produces the same result
// as writing it to std::ostringstream both for char and wchar_t.
#define CHECK_WRITE(value) ASSERT_PRED_FORMAT1(AnyWriteChecker(), value)
#define CHECK_WRITE_CHAR(value) ASSERT_PRED_FORMAT1(CharWriteChecker(), value)
#define CHECK_WRITE_WCHAR(value) ASSERT_PRED_FORMAT1(WCharWriteChecker(), value)
// Increment a number in a string.
void Increment(char *s) {
@@ -279,8 +307,20 @@ TEST(WriterTest, WriteDouble) {
CHECK_WRITE(4.2l);
}
TEST(WriterTest, WriteChar) {
CHECK_WRITE('a');
}
TEST(WriterTest, WriteString) {
CHECK_WRITE("abc");
CHECK_WRITE_CHAR("abc");
// The following line shouldn't compile:
//fmt::Writer() << L"abc";
}
TEST(WriterTest, WriteWideString) {
CHECK_WRITE_WCHAR(L"abc");
// The following line shouldn't compile:
//fmt::WWriter() << "abc";
}
TEST(WriterTest, oct) {