1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <sal/types.h>
12 #include <rtl/ustring.hxx>
14 #include <ToxWhitespaceStripper.hxx>
16 #include <cppunit/TestAssert.h>
17 #include <cppunit/TestFixture.h>
18 #include <cppunit/extensions/HelperMacros.h>
22 class ToxWhitespaceStripperTest
: public CppUnit::TestFixture
24 void MappingCharactersToVariousStrippedStringsWorks();
26 void StrippingWhitespacesFromVariousStringsWorks();
28 void PositionAfterStringCanBeRequested();
30 void InvalidPositionIsMappedToLastEntry();
32 CPPUNIT_TEST_SUITE(ToxWhitespaceStripperTest
);
33 CPPUNIT_TEST(MappingCharactersToVariousStrippedStringsWorks
);
34 CPPUNIT_TEST(StrippingWhitespacesFromVariousStringsWorks
);
35 CPPUNIT_TEST(PositionAfterStringCanBeRequested
);
36 CPPUNIT_TEST(InvalidPositionIsMappedToLastEntry
);
38 CPPUNIT_TEST_SUITE_END();
41 void ToxWhitespaceStripperTest::MappingCharactersToVariousStrippedStringsWorks()
44 ToxWhitespaceStripper
sut(u
"abc\n");
45 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), sut
.GetPositionInStrippedString(0));
46 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), sut
.GetPositionInStrippedString(1));
47 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), sut
.GetPositionInStrippedString(2));
48 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(3), sut
.GetPositionInStrippedString(3));
51 ToxWhitespaceStripper
sut(u
"abc\n\n");
52 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), sut
.GetPositionInStrippedString(0));
53 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), sut
.GetPositionInStrippedString(1));
54 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), sut
.GetPositionInStrippedString(2));
55 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(3), sut
.GetPositionInStrippedString(3));
56 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(3), sut
.GetPositionInStrippedString(4));
59 ToxWhitespaceStripper
sut(u
"abc\ndef");
60 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), sut
.GetPositionInStrippedString(0));
61 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), sut
.GetPositionInStrippedString(1));
62 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), sut
.GetPositionInStrippedString(2));
63 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(3), sut
.GetPositionInStrippedString(3));
64 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(4), sut
.GetPositionInStrippedString(4));
65 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(5), sut
.GetPositionInStrippedString(5));
66 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(6), sut
.GetPositionInStrippedString(6));
72 ToxWhitespaceStripper
sut(u
" abc \ndef");
73 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), sut
.GetPositionInStrippedString(0));
74 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), sut
.GetPositionInStrippedString(1));
75 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), sut
.GetPositionInStrippedString(2));
76 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), sut
.GetPositionInStrippedString(3));
77 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(3), sut
.GetPositionInStrippedString(4));
78 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(4), sut
.GetPositionInStrippedString(5));
79 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(4), sut
.GetPositionInStrippedString(6));
80 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(5), sut
.GetPositionInStrippedString(7));
81 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(6), sut
.GetPositionInStrippedString(8));
82 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(7), sut
.GetPositionInStrippedString(9));
86 void ToxWhitespaceStripperTest::StrippingWhitespacesFromVariousStringsWorks()
89 ToxWhitespaceStripper
sut(u
"abc\n");
90 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, sut
.GetStrippedString());
93 ToxWhitespaceStripper
sut(u
"abc\n\n");
94 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, sut
.GetStrippedString());
97 ToxWhitespaceStripper
sut(u
"abc\ndef");
98 CPPUNIT_ASSERT_EQUAL(u
"abc def"_ustr
, sut
.GetStrippedString());
101 ToxWhitespaceStripper
sut(u
" abc \ndef");
102 CPPUNIT_ASSERT_EQUAL(u
" abc def"_ustr
, sut
.GetStrippedString());
105 ToxWhitespaceStripper
sut(u
" ");
106 CPPUNIT_ASSERT_EQUAL(u
""_ustr
, sut
.GetStrippedString());
109 ToxWhitespaceStripper
sut(u
"d ");
110 CPPUNIT_ASSERT_EQUAL(u
"d"_ustr
, sut
.GetStrippedString());
114 void ToxWhitespaceStripperTest::PositionAfterStringCanBeRequested()
116 static OUString
constexpr test(u
"abc"_ustr
);
117 ToxWhitespaceStripper
sut(test
);
118 sal_Int32 expected
= test
.getLength();
119 CPPUNIT_ASSERT_EQUAL(expected
, sut
.GetPositionInStrippedString(test
.getLength()));
122 void ToxWhitespaceStripperTest::InvalidPositionIsMappedToLastEntry()
124 ToxWhitespaceStripper
sut(u
"ab c");
125 sal_Int32
const expected
= 4; // the length of the string after merging the two whitespaces
127 = sut
.GetPositionInStrippedString(40); // a value past the original string length
128 CPPUNIT_ASSERT_EQUAL(expected
, result
);
131 // Put the test suite in the registry
132 CPPUNIT_TEST_SUITE_REGISTRATION(ToxWhitespaceStripperTest
);
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */