1//
2// LineEndingConverterTest.cpp
3//
4// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "LineEndingConverterTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/LineEndingConverter.h"
15#include "Poco/StreamCopier.h"
16#include <sstream>
17
18
19using Poco::LineEnding;
20using Poco::InputLineEndingConverter;
21using Poco::OutputLineEndingConverter;
22using Poco::StreamCopier;
23
24
25LineEndingConverterTest::LineEndingConverterTest(const std::string& rName): CppUnit::TestCase(rName)
26{
27}
28
29
30LineEndingConverterTest::~LineEndingConverterTest()
31{
32}
33
34
35void LineEndingConverterTest::testInputDosToUnix()
36{
37 std::istringstream input("line1\r\nline2\r\nline3\r\n");
38 std::ostringstream output;
39 InputLineEndingConverter conv(input, LineEnding::NEWLINE_LF);
40 StreamCopier::copyStream(conv, output);
41 std::string result = output.str();
42 assertTrue (result == "line1\nline2\nline3\n");
43}
44
45
46void LineEndingConverterTest::testInputUnixToDos()
47{
48 std::istringstream input("line1\nline2\nline3\n");
49 std::ostringstream output;
50 InputLineEndingConverter conv(input, LineEnding::NEWLINE_CRLF);
51 StreamCopier::copyStream(conv, output);
52 std::string result = output.str();
53 assertTrue (result == "line1\r\nline2\r\nline3\r\n");
54}
55
56
57void LineEndingConverterTest::testInputMacToUnix()
58{
59 std::istringstream input("line1\rline2\rline3\r");
60 std::ostringstream output;
61 InputLineEndingConverter conv(input, LineEnding::NEWLINE_LF);
62 StreamCopier::copyStream(conv, output);
63 std::string result = output.str();
64 assertTrue (result == "line1\nline2\nline3\n");
65}
66
67
68void LineEndingConverterTest::testInputRemove()
69{
70 std::istringstream input("line1\r\nline2\rline3\n");
71 std::ostringstream output;
72 InputLineEndingConverter conv(input, "");
73 StreamCopier::copyStream(conv, output);
74 std::string result = output.str();
75 assertTrue (result == "line1line2line3");
76}
77
78
79void LineEndingConverterTest::testOutputDosToUnix()
80{
81 std::ostringstream output;
82 OutputLineEndingConverter conv(output, LineEnding::NEWLINE_LF);
83 conv << "line1\r\nline2\r\nline3\r\n" << std::flush;
84 std::string result = output.str();
85 assertTrue (result == "line1\nline2\nline3\n");
86}
87
88
89void LineEndingConverterTest::testOutputUnixToDos()
90{
91 std::ostringstream output;
92 OutputLineEndingConverter conv(output, LineEnding::NEWLINE_CRLF);
93 conv << "line1\nline2\nline3\n" << std::flush;
94 std::string result = output.str();
95 assertTrue (result == "line1\r\nline2\r\nline3\r\n");
96}
97
98
99void LineEndingConverterTest::testOutputMacToUnix()
100{
101 std::ostringstream output;
102 OutputLineEndingConverter conv(output, LineEnding::NEWLINE_LF);
103 conv << "line1\rline2\rline3\r" << std::flush;
104 std::string result = output.str();
105 assertTrue (result == "line1\nline2\nline3\n");
106}
107
108
109void LineEndingConverterTest::testOutputRemove()
110{
111 std::ostringstream output;
112 OutputLineEndingConverter conv(output, "");
113 conv << "line1\r\nline2\rline3\n" << std::flush;
114 std::string result = output.str();
115 assertTrue (result == "line1line2line3");
116}
117
118
119void LineEndingConverterTest::setUp()
120{
121}
122
123
124void LineEndingConverterTest::tearDown()
125{
126}
127
128
129CppUnit::Test* LineEndingConverterTest::suite()
130{
131 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LineEndingConverterTest");
132
133 CppUnit_addTest(pSuite, LineEndingConverterTest, testInputDosToUnix);
134 CppUnit_addTest(pSuite, LineEndingConverterTest, testInputUnixToDos);
135 CppUnit_addTest(pSuite, LineEndingConverterTest, testInputMacToUnix);
136 CppUnit_addTest(pSuite, LineEndingConverterTest, testInputRemove);
137 CppUnit_addTest(pSuite, LineEndingConverterTest, testOutputDosToUnix);
138 CppUnit_addTest(pSuite, LineEndingConverterTest, testOutputUnixToDos);
139 CppUnit_addTest(pSuite, LineEndingConverterTest, testOutputMacToUnix);
140 CppUnit_addTest(pSuite, LineEndingConverterTest, testOutputRemove);
141
142 return pSuite;
143}
144