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 <rtl/ustring.hxx>
15 #include <ToxTextGenerator.hxx>
16 #include <ToxTabStopTokenHandler.hxx>
20 #include <cppunit/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <cppunit/plugin/TestPlugIn.h>
27 class ToxTextGeneratorTest
: public CppUnit::TestFixture
{
29 void EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems();
30 void OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem();
31 void TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem();
32 void EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet();
33 void EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty();
34 void ChapterNumberWithoutTextIsGeneratedForNoprepstTitle();
35 void ChapterNumberWithTitleIsGeneratedForNumberNoPrepst();
37 CPPUNIT_TEST_SUITE(ToxTextGeneratorTest
);
38 CPPUNIT_TEST(EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems
);
39 CPPUNIT_TEST(OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem
);
40 CPPUNIT_TEST(TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem
);
41 CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet
);
42 CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty
);
43 CPPUNIT_TEST(ChapterNumberWithoutTextIsGeneratedForNoprepstTitle
);
44 CPPUNIT_TEST(ChapterNumberWithTitleIsGeneratedForNumberNoPrepst
);
45 CPPUNIT_TEST_SUITE_END();
49 struct MockedSortTab
: public SwTOXSortTabBase
{
51 : SwTOXSortTabBase(TOX_SORT_INDEX
,nullptr,nullptr,nullptr) {}
53 virtual TextAndReading
GetText_Impl() const override
{
54 return TextAndReading();
56 virtual sal_uInt16
GetLevel() const override
{
62 ToxTextGeneratorTest::EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems()
64 OUString
const expected("");
65 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(0);
66 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
70 ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
72 OUString
const expected("@~");
73 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(1);
74 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
78 ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
80 OUString
const expected("@, @~");
81 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(2);
82 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
86 ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet()
88 MockedSortTab sortTab
;
89 sortTab
.pTextMark
= nullptr;
91 OUString
const expected("");
92 OUString actual
= ToxTextGenerator::GetNumStringOfFirstNode(sortTab
, false, 0);
93 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
97 ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty()
99 MockedSortTab sortTab
;
100 sortTab
.pTextMark
= reinterpret_cast<SwTextTOXMark
*>(1);
102 OUString
const expected("");
103 OUString actual
= ToxTextGenerator::GetNumStringOfFirstNode(sortTab
, false, 0);
104 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
107 class MockedToxTabStopTokenHandler
: public ToxTabStopTokenHandler
{
109 virtual HandledTabStopToken
110 HandleTabStopToken(const SwFormToken
&, const SwTextNode
&,
111 const SwRootFrame
*) const override
{
112 return HandledTabStopToken();
116 class ToxTextGeneratorWithMockedChapterField
: public ToxTextGenerator
{
118 explicit ToxTextGeneratorWithMockedChapterField(SwForm
const &form
)
119 : ToxTextGenerator(form
, std::make_shared
<MockedToxTabStopTokenHandler
>()),
120 mChapterFieldType(), mChapterField(&mChapterFieldType
) {}
124 return mChapterField
;
129 ObtainChapterField(SwChapterFieldType
*, const SwFormToken
*,
130 const SwContentFrame
*, const SwContentNode
*) const override
{
131 return mChapterField
;
134 SwChapterFieldType mChapterFieldType
;
135 SwChapterField mChapterField
;
139 ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
142 ToxTextGeneratorWithMockedChapterField
ttg(form
);
143 // set all values to make sure they are not used
144 ttg
.GetChapterField().sNumber
= "1";
145 ttg
.GetChapterField().sPre
= "PRE";
146 ttg
.GetChapterField().sPost
= "POST";
147 ttg
.GetChapterField().sTitle
= "TITLE";
149 SwFormToken
token(TOKEN_CHAPTER_INFO
);
150 token
.nChapterFormat
= CF_NUM_NOPREPST_TITLE
;
152 OUString
expected("1");
153 OUString actual
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr);
154 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
156 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
157 // sTitle are used and hope that the pre- and suffix addition works.
158 token
.nChapterFormat
= CF_NUMBER
;
159 expected
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr);
160 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
165 ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
168 ToxTextGeneratorWithMockedChapterField
ttg(form
);
169 // set all values to make sure they are not used
170 ttg
.GetChapterField().sNumber
= "5";
171 ttg
.GetChapterField().sPre
= "PRE";
172 ttg
.GetChapterField().sPost
= "POST";
173 ttg
.GetChapterField().sTitle
= "myTitle";
175 SwFormToken
token(TOKEN_CHAPTER_INFO
);
176 token
.nChapterFormat
= CF_NUMBER_NOPREPST
;
178 OUString
expected("5 myTitle");
179 OUString actual
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr);
180 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
182 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
183 // sTitle are used and hope that the pre- and suffix addition works.
184 token
.nChapterFormat
= CF_NUM_TITLE
;
185 expected
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr);
186 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
189 // Put the test suite in the registry
190 CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest
);
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */