Files
libreoffice/sw/qa/cppunit/tox/test_ToxTextGenerator.cxx
Tobias Lippert 0a1b1511fa Unittest generation of text for chapter entries in the TOC
Conflicts:
	sw/inc/ToxTextGenerator.hxx
	sw/qa/cppunit/tox/test_ToxTextGenerator.cxx
	sw/source/core/tox/ToxTextGenerator.cxx

Change-Id: I343958f85fb6718215a0caa456a825d72f168a57
Reviewed-on: https://gerrit.libreoffice.org/9612
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
2014-06-06 12:19:12 +00:00

181 lines
6.1 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "rtl/ustring.hxx"
#include "chpfld.hxx"
#include "tox.hxx"
#include "txmsrt.hxx"
#include "ToxTextGenerator.hxx"
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>
using sw::ToxTextGenerator;
class ToxTextGeneratorTest : public CppUnit::TestFixture {
public:
void EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems();
void OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem();
void TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem();
void EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet();
void EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty();
void ChapterNumberWithoutTextIsGeneratedForNoprepstTitle();
void ChapterNumberWithTitleIsGeneratedForNumberNoPrepst();
CPPUNIT_TEST_SUITE(ToxTextGeneratorTest);
CPPUNIT_TEST(EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems);
CPPUNIT_TEST(OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem);
CPPUNIT_TEST(TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem);
CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet);
CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty);
CPPUNIT_TEST(ChapterNumberWithoutTextIsGeneratedForNoprepstTitle);
CPPUNIT_TEST(ChapterNumberWithTitleIsGeneratedForNumberNoPrepst);
CPPUNIT_TEST_SUITE_END();
};
struct MockedSortTab : public SwTOXSortTabBase {
MockedSortTab()
: SwTOXSortTabBase(TOX_SORT_INDEX,0,0,0) {;}
virtual TextAndReading GetText_Impl() const SAL_OVERRIDE {
return TextAndReading();
}
virtual sal_uInt16 GetLevel() const SAL_OVERRIDE {
return 0;
}
};
void
ToxTextGeneratorTest::EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems()
{
OUString expected("");
OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(0);
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
void
ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
{
OUString expected("@~");
OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(1);
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
void
ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
{
OUString expected("@, @~");
OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(2);
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
void
ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet()
{
MockedSortTab sortTab;
sortTab.pTxtMark = NULL;
OUString expected("");
OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
void
ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty()
{
MockedSortTab sortTab;
sortTab.pTxtMark = reinterpret_cast<SwTxtTOXMark*>(1);
OUString expected("");
OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
class ToxTextGeneratorWithMockedChapterField : public ToxTextGenerator {
public:
ToxTextGeneratorWithMockedChapterField(SwForm &form)
: ToxTextGenerator(form), mChapterFieldType(), mChapterField(&mChapterFieldType) {;}
SwChapterField&
GetChapterField() {
return mChapterField;
}
private:
SwChapterField
ObtainChapterField(SwChapterFieldType* chapterFieldType, const SwFormToken* chapterToken,
const SwCntntFrm* contentFrame, const SwCntntNode *contentNode) const {
// get rid of 'unused-parameters' warnings
(void)(chapterFieldType);(void)(chapterToken);(void)(contentFrame);(void)(contentNode);
return mChapterField;
}
SwChapterFieldType mChapterFieldType;
SwChapterField mChapterField;
};
void
ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
{
SwForm form;
ToxTextGeneratorWithMockedChapterField ttg(form);
// set all values to make sure they are not used
ttg.GetChapterField().sNumber = "1";
ttg.GetChapterField().sPre = "PRE";
ttg.GetChapterField().sPost = "POST";
ttg.GetChapterField().sTitle = "TITLE";
SwFormToken token(TOKEN_CHAPTER_INFO);
token.nChapterFormat = CF_NUM_NOPREPST_TITLE;
OUString expected("1");
OUString actual = ttg.GenerateTextForChapterToken(token, NULL, NULL);
CPPUNIT_ASSERT_EQUAL(expected, actual);
// we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
// sTitle are used and hope that the pre- and suffix addition works.
token.nChapterFormat = CF_NUMBER;
expected = ttg.GenerateTextForChapterToken(token, NULL, NULL);
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
void
ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
{
SwForm form;
ToxTextGeneratorWithMockedChapterField ttg(form);
// set all values to make sure they are not used
ttg.GetChapterField().sNumber = "5";
ttg.GetChapterField().sPre = "PRE";
ttg.GetChapterField().sPost = "POST";
ttg.GetChapterField().sTitle = "myTitle";
SwFormToken token(TOKEN_CHAPTER_INFO);
token.nChapterFormat = CF_NUMBER_NOPREPST;
OUString expected("5 myTitle");
OUString actual = ttg.GenerateTextForChapterToken(token, NULL, NULL);
CPPUNIT_ASSERT_EQUAL(expected, actual);
// we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
// sTitle are used and hope that the pre- and suffix addition works.
token.nChapterFormat = CF_NUM_TITLE;
expected = ttg.GenerateTextForChapterToken(token, NULL, NULL);
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */