Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sw / qa / core / test_ToxWhitespaceStripper.cxx
blobdc3b23b828aa6c5933fafe4037719780c147d8de
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <stdexcept>
12 #include <sal/types.h>
14 #include <rtl/ustring.hxx>
16 #include <ToxWhitespaceStripper.hxx>
18 #include <cppunit/TestAssert.h>
19 #include <cppunit/TestFixture.h>
20 #include <cppunit/extensions/HelperMacros.h>
22 using namespace sw;
24 class ToxWhitespaceStripperTest : public CppUnit::TestFixture
26 void
27 MappingCharactersToVariousStrippedStringsWorks();
29 void
30 StrippingWhitespacesFromVariousStringsWorks();
32 void
33 PositionAfterStringCanBeRequested();
35 void
36 InvalidPositionIsMappedToLastEntry();
38 CPPUNIT_TEST_SUITE(ToxWhitespaceStripperTest);
39 CPPUNIT_TEST(MappingCharactersToVariousStrippedStringsWorks);
40 CPPUNIT_TEST(StrippingWhitespacesFromVariousStringsWorks);
41 CPPUNIT_TEST(PositionAfterStringCanBeRequested);
42 CPPUNIT_TEST(InvalidPositionIsMappedToLastEntry);
44 CPPUNIT_TEST_SUITE_END();
48 void
49 ToxWhitespaceStripperTest::MappingCharactersToVariousStrippedStringsWorks()
52 OUString const test("abc\n");
53 ToxWhitespaceStripper sut(test);
54 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
55 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
56 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
57 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
60 OUString const test("abc\n\n");
61 ToxWhitespaceStripper sut(test);
62 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
63 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
64 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
65 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
66 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
69 OUString const test("abc\ndef");
70 ToxWhitespaceStripper sut(test);
71 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
72 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
73 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
74 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
75 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(4));
76 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(5));
77 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(6));
80 // 012345 6789
81 OUString const test(" abc \ndef");
82 // 01234567
83 // " abc def"
84 ToxWhitespaceStripper sut(test);
85 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
86 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(1));
87 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(2));
88 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(3));
89 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
90 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(5));
91 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(6));
92 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(7));
93 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(8));
94 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(7), sut.GetPositionInStrippedString(9));
98 void
99 ToxWhitespaceStripperTest::StrippingWhitespacesFromVariousStringsWorks()
102 OUString const test("abc\n");
103 OUString const expected("abc");
104 ToxWhitespaceStripper sut(test);
105 CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
108 OUString const test("abc\n\n");
109 OUString const expected("abc");
110 ToxWhitespaceStripper sut(test);
111 CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
114 OUString const test("abc\ndef");
115 OUString const expected("abc def");
116 ToxWhitespaceStripper sut(test);
117 CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
120 OUString const test(" abc \ndef");
121 OUString const expected(" abc def");
122 ToxWhitespaceStripper sut(test);
123 CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
126 OUString const test(" ");
127 OUString const expected("");
128 ToxWhitespaceStripper sut(test);
129 CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
132 OUString const test("d ");
133 OUString const expected("d");
134 ToxWhitespaceStripper sut(test);
135 CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
139 void
140 ToxWhitespaceStripperTest::PositionAfterStringCanBeRequested()
142 OUString const test("abc");
143 ToxWhitespaceStripper sut(test);
144 sal_Int32 expected = test.getLength();
145 CPPUNIT_ASSERT_EQUAL(expected, sut.GetPositionInStrippedString(test.getLength()));
148 void
149 ToxWhitespaceStripperTest::InvalidPositionIsMappedToLastEntry()
151 OUString const test("ab c");
152 ToxWhitespaceStripper sut(test);
153 sal_Int32 const expected = 4; // the length of the string after merging the two whitespaces
154 sal_Int32 result = sut.GetPositionInStrippedString(40); // a value past the original string length
155 CPPUNIT_ASSERT_EQUAL(expected, result);
158 // Put the test suite in the registry
159 CPPUNIT_TEST_SUITE_REGISTRATION(ToxWhitespaceStripperTest);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */