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 : #include <cppunit/plugin/TestPlugIn.h>
22 :
23 : using namespace sw;
24 :
25 18 : class ToxWhitespaceStripperTest : public CppUnit::TestFixture
26 : {
27 : void
28 : MappingCharactersToVariousStrippedStringsWorks();
29 :
30 : void
31 : StrippingWhitespacesFromVariousStringsWorks();
32 :
33 : void
34 : PositionAfterStringCanBeRequested();
35 :
36 4 : CPPUNIT_TEST_SUITE(ToxWhitespaceStripperTest);
37 2 : CPPUNIT_TEST(MappingCharactersToVariousStrippedStringsWorks);
38 2 : CPPUNIT_TEST(StrippingWhitespacesFromVariousStringsWorks);
39 2 : CPPUNIT_TEST(PositionAfterStringCanBeRequested);
40 :
41 4 : CPPUNIT_TEST_SUITE_END();
42 :
43 : };
44 :
45 : void
46 2 : ToxWhitespaceStripperTest::MappingCharactersToVariousStrippedStringsWorks()
47 : {
48 : {
49 2 : OUString test("abc\n");
50 4 : ToxWhitespaceStripper sut(test);
51 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
52 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
53 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
54 4 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
55 : }
56 : {
57 2 : OUString test("abc\n\n");
58 4 : ToxWhitespaceStripper sut(test);
59 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
60 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
61 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
62 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
63 4 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
64 : }
65 : {
66 2 : OUString test("abc\ndef");
67 4 : ToxWhitespaceStripper sut(test);
68 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
69 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
70 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
71 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
72 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(4));
73 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(5));
74 4 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(6));
75 : }
76 : {
77 : // 012345 6789
78 2 : OUString test(" abc \ndef");
79 : // 01234567
80 : // " abc def"
81 4 : ToxWhitespaceStripper sut(test);
82 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
83 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(1));
84 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(2));
85 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(3));
86 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
87 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(5));
88 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(6));
89 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(7));
90 2 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(8));
91 4 : CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(7), sut.GetPositionInStrippedString(9));
92 : }
93 2 : }
94 :
95 : void
96 2 : ToxWhitespaceStripperTest::StrippingWhitespacesFromVariousStringsWorks()
97 : {
98 : {
99 2 : OUString test("abc\n");
100 4 : OUString expected("abc");
101 4 : ToxWhitespaceStripper sut(test);
102 4 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
103 : }
104 : {
105 2 : OUString test("abc\n\n");
106 4 : OUString expected("abc");
107 4 : ToxWhitespaceStripper sut(test);
108 4 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
109 : }
110 : {
111 2 : OUString test("abc\ndef");
112 4 : OUString expected("abc def");
113 4 : ToxWhitespaceStripper sut(test);
114 4 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
115 : }
116 : {
117 2 : OUString test(" abc \ndef");
118 4 : OUString expected(" abc def");
119 4 : ToxWhitespaceStripper sut(test);
120 4 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
121 : }
122 : {
123 2 : OUString test(" ");
124 4 : OUString expected("");
125 4 : ToxWhitespaceStripper sut(test);
126 4 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
127 : }
128 : {
129 2 : OUString test("d ");
130 4 : OUString expected("d");
131 4 : ToxWhitespaceStripper sut(test);
132 4 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
133 : }
134 2 : }
135 :
136 : void
137 2 : ToxWhitespaceStripperTest::PositionAfterStringCanBeRequested()
138 : {
139 2 : OUString test("abc");
140 4 : ToxWhitespaceStripper sut(test);
141 2 : sal_Int32 expected = test.getLength();
142 4 : CPPUNIT_ASSERT_EQUAL(expected, sut.GetPositionInStrippedString(test.getLength()));
143 2 : }
144 :
145 : // Put the test suite in the registry
146 2 : CPPUNIT_TEST_SUITE_REGISTRATION(ToxWhitespaceStripperTest);
147 :
148 8 : CPPUNIT_PLUGIN_IMPLEMENT();
149 :
150 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|