Kaydet (Commit) 26259e0c authored tarafından Tobias Lippert's avatar Tobias Lippert Kaydeden (comit) Caolán McNamara

Unittest generation of page number placeholders in table of contents

Conflicts:
	sw/inc/ToxTextGenerator.hxx
	sw/source/core/tox/ToxTextGenerator.cxx

Change-Id: I15c963b6e1a8823a1fdafd2c123d18ba3dc9f134
Reviewed-on: https://gerrit.libreoffice.org/9611Reviewed-by: 's avatarCaolán McNamara <caolanm@redhat.com>
Tested-by: 's avatarCaolán McNamara <caolanm@redhat.com>
üst 2ac07bb5
......@@ -15,6 +15,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,sw_tox_test))
$(eval $(call gb_CppunitTest_add_exception_objects,sw_tox_test, \
sw/qa/cppunit/tox/test_ToxWhitespaceStripper \
sw/qa/cppunit/tox/test_ToxLinkProcessor \
sw/qa/cppunit/tox/test_ToxTextGenerator \
))
$(eval $(call gb_CppunitTest_use_libraries,sw_tox_test, \
......
......@@ -23,6 +23,7 @@
#include "rtl/ustring.hxx"
#include "sal/types.h"
#include "swdllapi.h"
#include <boost/shared_ptr.hpp>
#include <vector>
......@@ -35,6 +36,7 @@ class SwPageDesc;
class SwTxtAttr;
class SwTxtNode;
struct SwTOXSortTabBase;
class ToxTextGeneratorTest;
namespace sw {
......@@ -82,6 +84,19 @@ private:
static void
ApplyHandledTextToken(const HandledTextToken& htt, SwTxtNode& targetNode);
/** Handle a page number token.
*
* Will return a string of @p numberOfToxSources concatenated '@' signs, separated by commas, and
* finished by a '~'.
* (The '@' sign is the magic character C_NUM_REPL, the '~' sign is the magic character C_END_PAGE_NUM.
*
* @internal
* The count of similar entries, i.e., nodes in aTOXSources of SwTOXSortTabBase gives the PagerNumber
* pattern.
*/
static OUString
ConstructPageNumberPlaceholder(size_t numberOfToxSources);
/** Collect the attributes of a hint that shall be copied over to the TOX.
*
* Some text attributes are used in the TOX entries. This method defines which attributes are used.
......@@ -91,6 +106,19 @@ private:
*/
static boost::shared_ptr<SfxItemSet>
CollectAttributesForTox(const SwTxtAttr& hint, SwAttrPool& pool);
/** This method will call GetNumStringOfFirstNode() of the first node in the provided SwTOXSortTabBase.
*
* The parameters @p bUsePrefix and @p nLevel are passed to SwTxtNode::GetNumString()
*
* @internal
* The method is only called if several preconditions for @p rBase are true. Check the implementation
* for details.
*/
static OUString
GetNumStringOfFirstNode(const SwTOXSortTabBase& rBase, bool bUsePrefix, sal_uInt8 nLevel);
friend class ::ToxTextGeneratorTest;
};
}
......
/* -*- 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 "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();
CPPUNIT_TEST_SUITE(ToxTextGeneratorTest);
CPPUNIT_TEST(EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems);
CPPUNIT_TEST(OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem);
CPPUNIT_TEST(TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem);
CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet);
CPPUNIT_TEST_SUITE_END();
};
struct MockedSortTab : public SwTOXSortTabBase {
MockedSortTab()
: SwTOXSortTabBase(TOX_SORT_INDEX,0,0,0) {;}
/*virtual*/ TextAndReading GetText_Impl() const {
return TextAndReading();
}
/*virtual*/ sal_uInt16 GetLevel() const {
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);
}
// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -116,7 +116,7 @@ public:
};
// Beschreibung: Klassen fuer die Sortierung der Verzeichnisse
struct SwTOXSortTabBase
struct SAL_DLLPUBLIC_EXPORT SwTOXSortTabBase
{
SwTOXSources aTOXSources;
::com::sun::star::lang::Locale aLocale;
......
......@@ -47,27 +47,37 @@
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
/// Generate String according to the Form and remove the
/// special characters 0-31 and 255.
static OUString lcl_GetNumString( const SwTOXSortTabBase& rBase, bool bUsePrefix, sal_uInt8 nLevel )
namespace sw {
OUString
ToxTextGenerator::GetNumStringOfFirstNode( const SwTOXSortTabBase& rBase, bool bUsePrefix, sal_uInt8 nLevel )
{
OUString sRet;
if (!rBase.pTxtMark) { // only if it's not a Mark
return sRet;
}
if( !rBase.pTxtMark && !rBase.aTOXSources.empty() )
{ // only if it's not a Mark
const SwTxtNode* pNd = rBase.aTOXSources[0].pNd->GetTxtNode();
if( pNd )
{
const SwNumRule* pRule = pNd->GetNumRule();
if (rBase.aTOXSources.empty()) {
return sRet;
}
if( pRule && pNd->GetActualListLevel() < MAXLEVEL )
sRet = pNd->GetNumString(bUsePrefix, nLevel);
}
const SwTxtNode* pNd = rBase.aTOXSources[0].pNd->GetTxtNode();
if (!pNd) {
return sRet;
}
const SwNumRule* pRule = pNd->GetNumRule();
if (!pRule) {
return sRet;
}
if (pNd->GetActualListLevel() < MAXLEVEL) {
sRet = pNd->GetNumString(bUsePrefix, nLevel);
}
return sRet;
}
namespace sw {
ToxTextGenerator::ToxTextGenerator(const SwForm& toxForm)
:mToxForm(toxForm),
......@@ -111,7 +121,7 @@ void ToxTextGenerator::GenerateText(SwDoc* pDoc, const std::vector<SwTOXSortTabB
{
case TOKEN_ENTRY_NO:
// for TOC numbering
rTxt += lcl_GetNumString( rBase, aToken.nChapterFormat == CF_NUMBER, static_cast<sal_uInt8>(aToken.nOutlineLevel - 1) ) ;
rTxt += GetNumStringOfFirstNode( rBase, aToken.nChapterFormat == CF_NUMBER, static_cast<sal_uInt8>(aToken.nOutlineLevel - 1) ) ;
break;
case TOKEN_ENTRY_TEXT: {
......@@ -123,7 +133,7 @@ void ToxTextGenerator::GenerateText(SwDoc* pDoc, const std::vector<SwTOXSortTabB
case TOKEN_ENTRY:
{
// for TOC numbering
rTxt += lcl_GetNumString( rBase, true, MAXLEVEL );
rTxt += GetNumStringOfFirstNode( rBase, true, MAXLEVEL );
SwIndex aIdx( pTOXNd, rTxt.getLength() );
ToxWhitespaceStripper stripper(rBase.GetTxt().sText);
pTOXNd->InsertText(stripper.GetStrippedString(), aIdx);
......@@ -210,22 +220,7 @@ void ToxTextGenerator::GenerateText(SwDoc* pDoc, const std::vector<SwTOXSortTabB
break;
case TOKEN_PAGE_NUMS:
// Place holder for the PageNumber; we only respect the first one
{
// The count of similar entries gives the PagerNumber pattern
size_t nSize = rBase.aTOXSources.size();
if (nSize > 0)
{
OUString aInsStr = OUString(C_NUM_REPL);
for (size_t i = 1; i < nSize; ++i)
{
aInsStr += S_PAGE_DELI;
aInsStr += OUString(C_NUM_REPL);
}
aInsStr += OUString(C_END_PAGE_NUM);
rTxt += aInsStr;
}
}
rTxt += ConstructPageNumberPlaceholder(rBase.aTOXSources.size());
break;
case TOKEN_CHAPTER_INFO:
......@@ -380,6 +375,23 @@ ToxTextGenerator::ApplyHandledTextToken(const HandledTextToken& htt, SwTxtNode&
}
}
OUString
ToxTextGenerator::ConstructPageNumberPlaceholder(size_t numberOfToxSources)
{
OUString retval;
if (numberOfToxSources == 0) {
return retval;
}
// Place holder for the PageNumber; we only respect the first one
retval += OUString(C_NUM_REPL);
for (size_t i = 1; i < numberOfToxSources; ++i) {
retval += S_PAGE_DELI;
retval += OUString(C_NUM_REPL);
}
retval += OUString(C_END_PAGE_NUM);
return retval;
}
} // end namespace sw
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment