summaryrefslogtreecommitdiff
path: root/sw/qa/core/test_ToxWhitespaceStripper.cxx
blob: dc3b23b828aa6c5933fafe4037719780c147d8de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <stdexcept>

#include <sal/types.h>

#include <rtl/ustring.hxx>

#include <ToxWhitespaceStripper.hxx>

#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

using namespace sw;

class ToxWhitespaceStripperTest : public CppUnit::TestFixture
{
    void
    MappingCharactersToVariousStrippedStringsWorks();

    void
    StrippingWhitespacesFromVariousStringsWorks();

    void
    PositionAfterStringCanBeRequested();

    void
    InvalidPositionIsMappedToLastEntry();

    CPPUNIT_TEST_SUITE(ToxWhitespaceStripperTest);
    CPPUNIT_TEST(MappingCharactersToVariousStrippedStringsWorks);
    CPPUNIT_TEST(StrippingWhitespacesFromVariousStringsWorks);
    CPPUNIT_TEST(PositionAfterStringCanBeRequested);
    CPPUNIT_TEST(InvalidPositionIsMappedToLastEntry);

    CPPUNIT_TEST_SUITE_END();

};

void
ToxWhitespaceStripperTest::MappingCharactersToVariousStrippedStringsWorks()
{
    {
        OUString const test("abc\n");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
    }
    {
        OUString const test("abc\n\n");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
    }
    {
        OUString const test("abc\ndef");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(1));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(2));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(3));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(4));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(5));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(6));
    }
    {
        //             012345 6789
        OUString const test("  abc \ndef");
        //             01234567
        //            " abc def"
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(0));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), sut.GetPositionInStrippedString(1));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), sut.GetPositionInStrippedString(2));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), sut.GetPositionInStrippedString(3));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sut.GetPositionInStrippedString(4));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(5));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), sut.GetPositionInStrippedString(6));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), sut.GetPositionInStrippedString(7));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), sut.GetPositionInStrippedString(8));
        CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(7), sut.GetPositionInStrippedString(9));
    }
}

void
ToxWhitespaceStripperTest::StrippingWhitespacesFromVariousStringsWorks()
{
    {
        OUString const test("abc\n");
        OUString const expected("abc");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
    }
    {
        OUString const test("abc\n\n");
        OUString const expected("abc");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
    }
    {
        OUString const test("abc\ndef");
        OUString const expected("abc def");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
    }
    {
        OUString const test("  abc \ndef");
        OUString const expected(" abc def");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
    }
    {
        OUString const test("  ");
        OUString const expected("");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
    }
    {
        OUString const test("d  ");
        OUString const expected("d");
        ToxWhitespaceStripper sut(test);
        CPPUNIT_ASSERT_EQUAL(expected, sut.GetStrippedString());
    }
}

void
ToxWhitespaceStripperTest::PositionAfterStringCanBeRequested()
{
    OUString const test("abc");
    ToxWhitespaceStripper sut(test);
    sal_Int32 expected = test.getLength();
    CPPUNIT_ASSERT_EQUAL(expected, sut.GetPositionInStrippedString(test.getLength()));
}

void
ToxWhitespaceStripperTest::InvalidPositionIsMappedToLastEntry()
{
    OUString const test("ab  c");
    ToxWhitespaceStripper sut(test);
    sal_Int32 const expected = 4; // the length of the string after merging the two whitespaces
    sal_Int32 result = sut.GetPositionInStrippedString(40); // a value past the original string length
    CPPUNIT_ASSERT_EQUAL(expected, result);
}

// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION(ToxWhitespaceStripperTest);

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */