Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #include <stdexcept>
11 :
12 : #include <sal/types.h>
13 :
14 : #include <rtl/ustring.hxx>
15 :
16 : #include <ToxWhitespaceStripper.hxx>
17 :
18 : #include <cppunit/TestAssert.h>
19 : #include <cppunit/TestFixture.h>
20 : #include <cppunit/extensions/HelperMacros.h>
21 :
22 : using namespace sw;
23 :
24 12 : class ToxWhitespaceStripperTest : public CppUnit::TestFixture
25 : {
26 : void
27 : MappingCharactersToVariousStrippedStringsWorks();
28 :
29 : void
30 : StrippingWhitespacesFromVariousStringsWorks();
31 :
32 : void
33 : PositionAfterStringCanBeRequested();
34 :
35 : void
36 : InvalidPositionIsMappedToLastEntry();
37 :
38 2 : CPPUNIT_TEST_SUITE(ToxWhitespaceStripperTest);
39 1 : CPPUNIT_TEST(MappingCharactersToVariousStrippedStringsWorks);
40 1 : CPPUNIT_TEST(StrippingWhitespacesFromVariousStringsWorks);
41 1 : CPPUNIT_TEST(PositionAfterStringCanBeRequested);
42 1 : CPPUNIT_TEST(InvalidPositionIsMappedToLastEntry);
43 :
44 5 : CPPUNIT_TEST_SUITE_END();
45 :
46 : };
47 :
48 : void
49 1 : ToxWhitespaceStripperTest::MappingCharactersToVariousStrippedStringsWorks()
50 : {
51 : {
52 1 : OUString test("abc\n");
53 2 : ToxWhitespaceStripper sut(test);
54 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
55 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
56 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
57 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
58 : }
59 : {
60 1 : OUString test("abc\n\n");
61 2 : ToxWhitespaceStripper sut(test);
62 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
63 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
64 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
65 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
66 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
67 : }
68 : {
69 1 : OUString test("abc\ndef");
70 2 : ToxWhitespaceStripper sut(test);
71 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
72 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
73 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
74 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
75 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(4));
76 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(5));
77 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(6));
78 : }
79 : {
80 : // 012345 6789
81 1 : OUString test(" abc \ndef");
82 : // 01234567
83 : // " abc def"
84 2 : ToxWhitespaceStripper sut(test);
85 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
86 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(1));
87 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(2));
88 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(3));
89 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
90 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(5));
91 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(6));
92 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(7));
93 1 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(8));
94 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(7), sut.GetPositionInStrippedString(9));
95 : }
96 1 : }
97 :
98 : void
99 1 : ToxWhitespaceStripperTest::StrippingWhitespacesFromVariousStringsWorks()
100 : {
101 : {
102 1 : OUString test("abc\n");
103 2 : OUString expected("abc");
104 2 : ToxWhitespaceStripper sut(test);
105 2 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
106 : }
107 : {
108 1 : OUString test("abc\n\n");
109 2 : OUString expected("abc");
110 2 : ToxWhitespaceStripper sut(test);
111 2 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
112 : }
113 : {
114 1 : OUString test("abc\ndef");
115 2 : OUString expected("abc def");
116 2 : ToxWhitespaceStripper sut(test);
117 2 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
118 : }
119 : {
120 1 : OUString test(" abc \ndef");
121 2 : OUString expected(" abc def");
122 2 : ToxWhitespaceStripper sut(test);
123 2 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
124 : }
125 : {
126 1 : OUString test(" ");
127 2 : OUString expected("");
128 2 : ToxWhitespaceStripper sut(test);
129 2 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
130 : }
131 : {
132 1 : OUString test("d ");
133 2 : OUString expected("d");
134 2 : ToxWhitespaceStripper sut(test);
135 2 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
136 : }
137 1 : }
138 :
139 : void
140 1 : ToxWhitespaceStripperTest::PositionAfterStringCanBeRequested()
141 : {
142 1 : OUString test("abc");
143 2 : ToxWhitespaceStripper sut(test);
144 1 : sal_Int32 expected = test.getLength();
145 2 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetPositionInStrippedString(test.getLength()));
146 1 : }
147 :
148 : void
149 1 : ToxWhitespaceStripperTest::InvalidPositionIsMappedToLastEntry()
150 : {
151 1 : OUString test("ab c");
152 2 : ToxWhitespaceStripper sut(test);
153 1 : sal_Int32 expected = 4; // the length of the string after merging the two whitespaces
154 1 : sal_Int32 result = sut.GetPositionInStrippedString(40); // a value past the original string length
155 2 : CPPUNIT_ASSERT_EQUAL(expected, result);
156 1 : }
157 :
158 : // Put the test suite in the registry
159 3 : CPPUNIT_TEST_SUITE_REGISTRATION(ToxWhitespaceStripperTest);
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|