Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / core / test_ToxTextGenerator.cxx
blobc0b667a27334d951f2c7821ff9fa00a7002a6e4a
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 <tox.hxx>
13 #include <txmsrt.hxx>
14 #include <ToxTextGenerator.hxx>
15 #include <ToxTabStopTokenHandler.hxx>
17 #include <memory>
19 #include <cppunit/TestAssert.h>
20 #include <cppunit/TestFixture.h>
21 #include <cppunit/extensions/HelperMacros.h>
23 #include <swdll.hxx>
25 using namespace sw;
27 class ToxTextGeneratorTest : public CppUnit::TestFixture {
28 public:
29 virtual void setUp() override
31 SwGlobals::ensure();
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();
54 namespace {
56 struct MockedSortTab : public SwTOXSortTabBase {
57 MockedSortTab()
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 {
64 return 0;
70 void
71 ToxTextGeneratorTest::EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems()
73 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(0);
74 CPPUNIT_ASSERT_EQUAL(OUString(""), actual);
77 void
78 ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
80 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(1);
81 CPPUNIT_ASSERT_EQUAL(OUString("@~"), actual);
84 void
85 ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
87 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(2);
88 CPPUNIT_ASSERT_EQUAL(OUString("@, @~"), actual);
91 void
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);
101 void
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);
111 namespace {
113 class MockedToxTabStopTokenHandler : public ToxTabStopTokenHandler {
114 public:
115 virtual HandledTabStopToken
116 HandleTabStopToken(const SwFormToken&, const SwTextNode&,
117 const SwRootFrame *) const override {
118 return HandledTabStopToken();
122 class ToxTextGeneratorWithMockedChapterField : public ToxTextGenerator {
123 public:
124 explicit ToxTextGeneratorWithMockedChapterField(SwForm const &form)
125 : ToxTextGenerator(form, std::make_shared<MockedToxTabStopTokenHandler>()),
126 mChapterFieldType(), mChapterField(&mChapterFieldType) {}
128 SwChapterField&
129 GetChapterField() {
130 return mChapterField;
133 private:
134 SwChapterField
135 ObtainChapterField(SwChapterFieldType*, const SwFormToken*,
136 const SwContentFrame*, const SwContentNode *) const override {
137 return mChapterField;
140 SwChapterFieldType mChapterFieldType;
141 SwChapterField mChapterField;
146 void
147 ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
149 SwForm form;
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);
172 void
173 ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
175 SwForm form;
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: */