Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / sw / qa / core / test_ToxTextGenerator.cxx
blob2a41c9844201c49534bdf46985e4a5036c159735
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include "rtl/ustring.hxx"
11 #include "chpfld.hxx"
12 #include "ndtxt.hxx"
13 #include "tox.hxx"
14 #include "txmsrt.hxx"
15 #include "ToxTextGenerator.hxx"
16 #include "ToxTabStopTokenHandler.hxx"
18 #include <memory>
20 #include <cppunit/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <cppunit/plugin/TestPlugIn.h>
25 using namespace sw;
27 class ToxTextGeneratorTest : public CppUnit::TestFixture {
28 public:
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 {
50 MockedSortTab()
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 {
57 return 0;
61 void
62 ToxTextGeneratorTest::EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems()
64 OUString expected("");
65 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(0);
66 CPPUNIT_ASSERT_EQUAL(expected, actual);
69 void
70 ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
72 OUString expected("@~");
73 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(1);
74 CPPUNIT_ASSERT_EQUAL(expected, actual);
77 void
78 ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
80 OUString expected("@, @~");
81 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(2);
82 CPPUNIT_ASSERT_EQUAL(expected, actual);
85 void
86 ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet()
88 MockedSortTab sortTab;
89 sortTab.pTextMark = nullptr;
91 OUString expected("");
92 OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
93 CPPUNIT_ASSERT_EQUAL(expected, actual);
96 void
97 ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty()
99 MockedSortTab sortTab;
100 sortTab.pTextMark = reinterpret_cast<SwTextTOXMark*>(1);
102 OUString expected("");
103 OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
104 CPPUNIT_ASSERT_EQUAL(expected, actual);
107 class MockedToxTabStopTokenHandler : public ToxTabStopTokenHandler {
108 public:
109 virtual HandledTabStopToken
110 HandleTabStopToken(const SwFormToken& aToken, const SwTextNode& targetNode,
111 const SwRootFrame *currentLayout) const override {
112 (void)(aToken); (void)(targetNode); (void)(currentLayout); // avoid unused warnings.
113 return HandledTabStopToken();
117 class ToxTextGeneratorWithMockedChapterField : public ToxTextGenerator {
118 public:
119 explicit ToxTextGeneratorWithMockedChapterField(SwForm &form)
120 : ToxTextGenerator(form, std::make_shared<MockedToxTabStopTokenHandler>()),
121 mChapterFieldType(), mChapterField(&mChapterFieldType) {}
123 SwChapterField&
124 GetChapterField() {
125 return mChapterField;
128 private:
129 SwChapterField
130 ObtainChapterField(SwChapterFieldType* chapterFieldType, const SwFormToken* chapterToken,
131 const SwContentFrame* contentFrame, const SwContentNode *contentNode) const override {
132 // get rid of 'unused-parameters' warnings
133 (void)(chapterFieldType);(void)(chapterToken);(void)(contentFrame);(void)(contentNode);
134 return mChapterField;
137 SwChapterFieldType mChapterFieldType;
138 SwChapterField mChapterField;
141 void
142 ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
144 SwForm form;
145 ToxTextGeneratorWithMockedChapterField ttg(form);
146 // set all values to make sure they are not used
147 ttg.GetChapterField().sNumber = "1";
148 ttg.GetChapterField().sPre = "PRE";
149 ttg.GetChapterField().sPost = "POST";
150 ttg.GetChapterField().sTitle = "TITLE";
152 SwFormToken token(TOKEN_CHAPTER_INFO);
153 token.nChapterFormat = CF_NUM_NOPREPST_TITLE;
155 OUString expected("1");
156 OUString actual = ttg.GenerateTextForChapterToken(token, nullptr, nullptr);
157 CPPUNIT_ASSERT_EQUAL(expected, actual);
159 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
160 // sTitle are used and hope that the pre- and suffix addition works.
161 token.nChapterFormat = CF_NUMBER;
162 expected = ttg.GenerateTextForChapterToken(token, nullptr, nullptr);
163 CPPUNIT_ASSERT_EQUAL(expected, actual);
167 void
168 ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
170 SwForm form;
171 ToxTextGeneratorWithMockedChapterField ttg(form);
172 // set all values to make sure they are not used
173 ttg.GetChapterField().sNumber = "5";
174 ttg.GetChapterField().sPre = "PRE";
175 ttg.GetChapterField().sPost = "POST";
176 ttg.GetChapterField().sTitle = "myTitle";
178 SwFormToken token(TOKEN_CHAPTER_INFO);
179 token.nChapterFormat = CF_NUMBER_NOPREPST;
181 OUString expected("5 myTitle");
182 OUString actual = ttg.GenerateTextForChapterToken(token, nullptr, nullptr);
183 CPPUNIT_ASSERT_EQUAL(expected, actual);
185 // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
186 // sTitle are used and hope that the pre- and suffix addition works.
187 token.nChapterFormat = CF_NUM_TITLE;
188 expected = ttg.GenerateTextForChapterToken(token, nullptr, nullptr);
189 CPPUNIT_ASSERT_EQUAL(expected, actual);
192 // Put the test suite in the registry
193 CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */