Toponaming/Base: Add ASCII stream output class (#13209)
* Toponaming/Base: Add ASCII stream output class * Remove the ref from std::string * Update based on review comments
This commit is contained in:
@@ -16,6 +16,7 @@ target_sources(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Quantity.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Reader.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Rotation.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Stream.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TimeInfo.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Tools.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Tools2D.cpp
|
||||
|
||||
127
tests/src/Base/Stream.cpp
Normal file
127
tests/src/Base/Stream.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user