tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / sw / qa / core / test_ToxTextGenerator.cxx
blobd64830a770acc6ee3515cc9797025e975ca5f315
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(u""_ustr, actual);
77 void
78 ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
80 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(1);
81 CPPUNIT_ASSERT_EQUAL(u"@~"_ustr, actual);
84 void
85 ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
87 OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(2);
88 CPPUNIT_ASSERT_EQUAL(u"@, @~"_ustr, 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(u""_ustr, 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(u""_ustr, actual);
111 namespace {
113 class MockedToxTabStopTokenHandler : public ToxTabStopTokenHandler {
114 public:
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
123 return 0;
127 class ToxTextGeneratorWithMockedChapterField : public ToxTextGenerator {
128 public:
129 explicit ToxTextGeneratorWithMockedChapterField(SwForm const &form)
130 : ToxTextGenerator(form, std::make_shared<MockedToxTabStopTokenHandler>()),
131 mChapterFieldType(), mChapterField(&mChapterFieldType) {}
133 SwChapterField&
134 GetChapterField() {
135 return mChapterField;
138 private:
139 SwChapterField
140 ObtainChapterField(SwChapterFieldType*, const SwFormToken*,
141 const SwContentFrame*, const SwContentNode *) const override {
142 return mChapterField;
145 SwChapterFieldType mChapterFieldType;
146 SwChapterField mChapterField;
151 void
152 ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
154 SwForm form;
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);
177 void
178 ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
180 SwForm form;
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: */