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(u
""_ustr
, actual
);
78 ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
80 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(1);
81 CPPUNIT_ASSERT_EQUAL(u
"@~"_ustr
, actual
);
85 ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
87 OUString actual
= ToxTextGenerator::ConstructPageNumberPlaceholder(2);
88 CPPUNIT_ASSERT_EQUAL(u
"@, @~"_ustr
, actual
);
92 ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet()
94 MockedSortTab sortTab
;
95 sortTab
.pTextMark
= nullptr;
97 OUString actual
= ToxTextGenerator::GetNumStringOfFirstNode(sortTab
, false, 0, nullptr);
98 CPPUNIT_ASSERT_EQUAL(u
""_ustr
, 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(u
""_ustr
, actual
);
113 class MockedToxTabStopTokenHandler
: public ToxTabStopTokenHandler
{
115 virtual HandledTabStopToken
116 HandleTabStopToken(const SwFormToken
&, const SwTextNode
&) const override
118 return HandledTabStopToken();
121 auto CalcEndStop(SwTextNode
const&, SwRootFrame
const*) const -> tools::Long override
127 class ToxTextGeneratorWithMockedChapterField
: public ToxTextGenerator
{
129 explicit ToxTextGeneratorWithMockedChapterField(SwForm
const &form
)
130 : ToxTextGenerator(form
, std::make_shared
<MockedToxTabStopTokenHandler
>()),
131 mChapterFieldType(), mChapterField(&mChapterFieldType
) {}
135 return mChapterField
;
140 ObtainChapterField(SwChapterFieldType
*, const SwFormToken
*,
141 const SwContentFrame
*, const SwContentNode
*) const override
{
142 return mChapterField
;
145 SwChapterFieldType mChapterFieldType
;
146 SwChapterField mChapterField
;
152 ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
155 ToxTextGeneratorWithMockedChapterField
ttg(form
);
156 // set all values to make sure they are not used
157 ttg
.GetChapterField().m_State
.sNumber
= "1";
158 ttg
.GetChapterField().m_State
.sPre
= "PRE";
159 ttg
.GetChapterField().m_State
.sPost
= "POST";
160 ttg
.GetChapterField().m_State
.sTitle
= "TITLE";
162 SwFormToken
token(TOKEN_CHAPTER_INFO
);
163 token
.nChapterFormat
= CF_NUM_NOPREPST_TITLE
;
165 OUString
expected(u
"1"_ustr
);
166 OUString actual
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
167 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
169 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
170 // sTitle are used and hope that the pre- and suffix addition works.
171 token
.nChapterFormat
= CF_NUMBER
;
172 expected
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
173 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
178 ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
181 ToxTextGeneratorWithMockedChapterField
ttg(form
);
182 // set all values to make sure they are not used
183 ttg
.GetChapterField().m_State
.sNumber
= "5";
184 ttg
.GetChapterField().m_State
.sPre
= "PRE";
185 ttg
.GetChapterField().m_State
.sPost
= "POST";
186 ttg
.GetChapterField().m_State
.sTitle
= "myTitle";
188 SwFormToken
token(TOKEN_CHAPTER_INFO
);
189 token
.nChapterFormat
= CF_NUMBER_NOPREPST
;
191 OUString
expected(u
"5 myTitle"_ustr
);
192 OUString actual
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
193 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
195 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
196 // sTitle are used and hope that the pre- and suffix addition works.
197 token
.nChapterFormat
= CF_NUM_TITLE
;
198 expected
= ttg
.GenerateTextForChapterToken(token
, nullptr, nullptr, nullptr);
199 CPPUNIT_ASSERT_EQUAL(expected
, actual
);
202 // Put the test suite in the registry
203 CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest
);
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */