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>
14 #include <ToxTextGenerator.hxx>
15 #include <ToxTabStopTokenHandler.hxx>
19 #include <cppunit/TestAssert.h>
20 #include <cppunit/TestFixture.h>
21 #include <cppunit/extensions/HelperMacros.h>
27 class ToxTextGeneratorTest
: public CppUnit::TestFixture
{
29 virtual void setUp() override
34 void EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems();
35 void OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem();
36 void TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem();
37 void EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet();
38 void EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty();
39 void ChapterNumberWithoutTextIsGeneratedForNoprepstTitle();
40 void ChapterNumberWithTitleIsGeneratedForNumberNoPrepst();
42 CPPUNIT_TEST_SUITE(ToxTextGeneratorTest
);
43 CPPUNIT_TEST(EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems
);
44 CPPUNIT_TEST(OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem
);
45 CPPUNIT_TEST(TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem
);
46 CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet
);
47 CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty
);
48 CPPUNIT_TEST(ChapterNumberWithoutTextIsGeneratedForNoprepstTitle
);
49 CPPUNIT_TEST(ChapterNumberWithTitleIsGeneratedForNumberNoPrepst
);
50 CPPUNIT_TEST_SUITE_END();
56 struct MockedSortTab
: public SwTOXSortTabBase
{
58 : SwTOXSortTabBase(TOX_SORT_INDEX
,nullptr,nullptr,nullptr) {}
60 virtual TextAndReading
GetText_Impl(SwRootFrame
const*) const override
{
61 return TextAndReading();
63 virtual sal_uInt16
GetLevel() const override
{
71 ToxTextGeneratorTest::EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems()
73 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(0);
74 CPPUNIT_ASSERT_EQUAL(OUString(""), actual
);
78 ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
80 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(1);
81 CPPUNIT_ASSERT_EQUAL(OUString("@~"), actual
);
85 ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
87 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(2);
88 CPPUNIT_ASSERT_EQUAL(OUString("@, @~"), actual
);
92 ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet()
94 MockedSortTab sortTab
;
95 sortTab
.pTextMark
= nullptr;
97 OUString actual
= ToxTextGenerator::GetNumStringOfFirstNode(sortTab
, false, 0, nullptr);
98 CPPUNIT_ASSERT_EQUAL(OUString(""), actual
);
102 ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty()
104 MockedSortTab sortTab
;
105 sortTab
.pTextMark
= reinterpret_cast<SwTextTOXMark
*>(1);
107 OUString actual
= ToxTextGenerator::GetNumStringOfFirstNode(sortTab
, false, 0, nullptr);
108 CPPUNIT_ASSERT_EQUAL(OUString(""), actual
);
113 class MockedToxTabStopTokenHandler
: public ToxTabStopTokenHandler
{
115 virtual HandledTabStopToken
116 HandleTabStopToken(const SwFormToken
&, const SwTextNode
&,
117 const SwRootFrame
*) const override
{
118 return HandledTabStopToken();
122 class ToxTextGeneratorWithMockedChapterField
: public ToxTextGenerator
{
124 explicit ToxTextGeneratorWithMockedChapterField(SwForm
const &form
)
125 : ToxTextGenerator(form
, std::make_shared
<MockedToxTabStopTokenHandler
>()),
126 mChapterFieldType(), mChapterField(&mChapterFieldType
) {}
130 return mChapterField
;
135 ObtainChapterField(SwChapterFieldType
*, const SwFormToken
*,
136 const SwContentFrame
*, const SwContentNode
*) const override
{
137 return mChapterField
;
140 SwChapterFieldType mChapterFieldType
;
141 SwChapterField mChapterField
;
147 ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
150 ToxTextGeneratorWithMockedChapterField
ttg(form
);
151 // set all values to make sure they are not used
152 ttg
.GetChapterField().m_State
.sNumber
= "1";
153 ttg
.GetChapterField().m_State
.sPre
= "PRE";
154 ttg
.GetChapterField().m_State
.sPost
= "POST";
155 ttg
.GetChapterField().m_State
.sTitle
= "TITLE";
157 SwFormToken
token(TOKEN_CHAPTER_INFO
);
158 token
.nChapterFormat
= CF_NUM_NOPREPST_TITLE
;
160 OUString
expected("1");
161 OUString actual
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
162 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
164 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
165 // sTitle are used and hope that the pre- and suffix addition works.
166 token
.nChapterFormat
= CF_NUMBER
;
167 expected
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
168 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
173 ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
176 ToxTextGeneratorWithMockedChapterField
ttg(form
);
177 // set all values to make sure they are not used
178 ttg
.GetChapterField().m_State
.sNumber
= "5";
179 ttg
.GetChapterField().m_State
.sPre
= "PRE";
180 ttg
.GetChapterField().m_State
.sPost
= "POST";
181 ttg
.GetChapterField().m_State
.sTitle
= "myTitle";
183 SwFormToken
token(TOKEN_CHAPTER_INFO
);
184 token
.nChapterFormat
= CF_NUMBER_NOPREPST
;
186 OUString
expected("5 myTitle");
187 OUString actual
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
188 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
190 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
191 // sTitle are used and hope that the pre- and suffix addition works.
192 token
.nChapterFormat
= CF_NUM_TITLE
;
193 expected
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
194 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
197 // Put the test suite in the registry
198 CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest
);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */