File size: 3,221 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | // SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#ifdef _MSC_VER
# pragma warning(disable : 4996)
#endif
#include "Base/Stream.h"
class TextOutputStreamTest: public ::testing::Test
{
protected:
void SetUp() override
{}
void TearDown() override
{}
};
TEST_F(TextOutputStreamTest, singleLineCharStar)
{
// Arrange
const std::string testString("Single line const char *");
std::ostringstream ss;
Base::TextOutputStream tos(ss);
// Act
tos << testString;
// Assert - the number of newlines in the string, a colon, the string, a newline
EXPECT_EQ(std::string("0:") + testString + "\n", ss.str());
}
TEST_F(TextOutputStreamTest, multiLineCharStar)
{
// Arrange
const std::string testString("Multi-line\nconst char *");
std::ostringstream ss;
Base::TextOutputStream tos(ss);
// Act
tos << testString; // Testing it with a string instead of a const char * -- failing right now
// Assert - the number of newlines in the string, a colon, the string, a newline
EXPECT_EQ(std::string("1:") + testString + "\n", ss.str());
}
TEST_F(TextOutputStreamTest, singleLineCharStarWithCarriageReturns)
{
// Arrange
const std::string testString("Single-line\rconst char *");
std::ostringstream ss;
Base::TextOutputStream tos(ss);
// Act
tos << testString;
// Assert - the number of newlines in the string, a colon, the string, a newline. Carriage
// returns are left alone because they aren't followed by a newline
EXPECT_EQ(std::string("0:") + testString + "\n", ss.str());
}
TEST_F(TextOutputStreamTest, multiLineCharStarWithCarriageReturnsAndNewlines)
{
// Arrange
const std::string testString("Multi-line\r\nconst char *");
const std::string testStringWithoutCR("Multi-line\nconst char *");
std::ostringstream ss;
Base::TextOutputStream tos(ss);
// Act
tos << testString;
// Assert - the number of newlines in the string, a colon, the string, a newline, but the string
// has been stripped of the carriage returns.
EXPECT_EQ(std::string("1:") + testStringWithoutCR + "\n", ss.str());
}
class TextStreamIntegrationTest: public ::testing::Test
{
protected:
void SetUp() override
{}
void TearDown() override
{}
};
TEST_F(TextStreamIntegrationTest, OutputThenInputSimpleMultiLine)
{
// Arrange
std::string multiLineString("One\nTwo\nThree");
// Act
std::ostringstream ssO;
Base::TextOutputStream tos(ssO);
tos << multiLineString;
std::istringstream ssI(ssO.str());
Base::TextInputStream tis(ssI);
std::string result;
tis >> result;
// Assert
EXPECT_EQ(multiLineString, result);
}
TEST_F(TextStreamIntegrationTest, OutputThenInputMultiLineWithCarriageReturns)
{
// Arrange
std::string multiLineString("One\r\nTwo\r\nThree");
std::string multiLineStringResult("One\nTwo\nThree");
// Act
std::ostringstream ssO;
Base::TextOutputStream tos(ssO);
tos << multiLineString;
std::istringstream ssI(ssO.str());
Base::TextInputStream tis(ssI);
std::string result;
tis >> result;
// Assert
EXPECT_EQ(multiLineStringResult, result);
}
|