[Libreoffice-commits] core.git: sw/CppunitTest_sw_tox.mk sw/inc sw/qa sw/source

Tobias Lippert drtl at fastmail.fm
Fri Jun 6 01:08:30 PDT 2014


 sw/CppunitTest_sw_tox.mk                    |    1 
 sw/inc/ToxTextGenerator.hxx                 |   28 +++++++
 sw/qa/cppunit/tox/test_ToxTextGenerator.cxx |   99 ++++++++++++++++++++++++++++
 sw/source/core/inc/txmsrt.hxx               |    2 
 sw/source/core/tox/ToxTextGenerator.cxx     |   74 ++++++++++++--------
 5 files changed, 172 insertions(+), 32 deletions(-)

New commits:
commit 26259e0cca3bc4d8f45337b0cafea2eed8eb32f9
Author: Tobias Lippert <drtl at fastmail.fm>
Date:   Sun Jun 1 14:22:48 2014 +0200

    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/9611
    Reviewed-by: Caolán McNamara <caolanm at redhat.com>
    Tested-by: Caolán McNamara <caolanm at redhat.com>

diff --git a/sw/CppunitTest_sw_tox.mk b/sw/CppunitTest_sw_tox.mk
index 7911de3..e45f597 100644
--- a/sw/CppunitTest_sw_tox.mk
+++ b/sw/CppunitTest_sw_tox.mk
@@ -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, \
diff --git a/sw/inc/ToxTextGenerator.hxx b/sw/inc/ToxTextGenerator.hxx
index 8536114..ba79177 100644
--- a/sw/inc/ToxTextGenerator.hxx
+++ b/sw/inc/ToxTextGenerator.hxx
@@ -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;
 };
 
 }
diff --git a/sw/qa/cppunit/tox/test_ToxTextGenerator.cxx b/sw/qa/cppunit/tox/test_ToxTextGenerator.cxx
new file mode 100644
index 0000000..41f4bd0
--- /dev/null
+++ b/sw/qa/cppunit/tox/test_ToxTextGenerator.cxx
@@ -0,0 +1,99 @@
+/* -*- 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: */
diff --git a/sw/source/core/inc/txmsrt.hxx b/sw/source/core/inc/txmsrt.hxx
index 53dda8b..26ce41b 100644
--- a/sw/source/core/inc/txmsrt.hxx
+++ b/sw/source/core/inc/txmsrt.hxx
@@ -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;
diff --git a/sw/source/core/tox/ToxTextGenerator.cxx b/sw/source/core/tox/ToxTextGenerator.cxx
index 721df78..10a3e2f 100644
--- a/sw/source/core/tox/ToxTextGenerator.cxx
+++ b/sw/source/core/tox/ToxTextGenerator.cxx
@@ -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: */


More information about the Libreoffice-commits mailing list