LCOV - code coverage report
Current view: top level - sw/qa/core - test_ToxTextGenerator.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 83 89 93.3 %
Date: 2015-06-13 12:38:46 Functions: 26 32 81.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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             :  */
       9             : 
      10             : #include "rtl/ustring.hxx"
      11             : #include "chpfld.hxx"
      12             : #include "ndtxt.hxx"
      13             : #include "tox.hxx"
      14             : #include "txmsrt.hxx"
      15             : #include "ToxTextGenerator.hxx"
      16             : #include "ToxTabStopTokenHandler.hxx"
      17             : 
      18             : #include <memory>
      19             : 
      20             : #include <cppunit/TestAssert.h>
      21             : #include <cppunit/TestFixture.h>
      22             : #include <cppunit/extensions/HelperMacros.h>
      23             : #include <cppunit/plugin/TestPlugIn.h>
      24             : 
      25             : using namespace sw;
      26             : 
      27          21 : class ToxTextGeneratorTest : public CppUnit::TestFixture {
      28             : public:
      29             :     void EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems();
      30             :     void OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem();
      31             :     void TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem();
      32             :     void EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet();
      33             :     void EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty();
      34             :     void ChapterNumberWithoutTextIsGeneratedForNoprepstTitle();
      35             :     void ChapterNumberWithTitleIsGeneratedForNumberNoPrepst();
      36             : 
      37           2 :     CPPUNIT_TEST_SUITE(ToxTextGeneratorTest);
      38           1 :     CPPUNIT_TEST(EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems);
      39           1 :     CPPUNIT_TEST(OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem);
      40           1 :     CPPUNIT_TEST(TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem);
      41           1 :     CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet);
      42           1 :     CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty);
      43           1 :     CPPUNIT_TEST(ChapterNumberWithoutTextIsGeneratedForNoprepstTitle);
      44           1 :     CPPUNIT_TEST(ChapterNumberWithTitleIsGeneratedForNumberNoPrepst);
      45           5 :     CPPUNIT_TEST_SUITE_END();
      46             : 
      47             : };
      48             : 
      49           2 : struct MockedSortTab : public SwTOXSortTabBase {
      50           2 :     MockedSortTab()
      51           2 :     : SwTOXSortTabBase(TOX_SORT_INDEX,0,0,0) {;}
      52             : 
      53           0 :     virtual TextAndReading GetText_Impl() const SAL_OVERRIDE {
      54           0 :         return TextAndReading();
      55             :     }
      56           0 :     virtual sal_uInt16  GetLevel() const SAL_OVERRIDE {
      57           0 :         return 0;
      58             :     }
      59             : };
      60             : 
      61             : void
      62           1 : ToxTextGeneratorTest::EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems()
      63             : {
      64           1 :     OUString expected("");
      65           2 :     OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(0);
      66           2 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
      67           1 : }
      68             : 
      69             : void
      70           1 : ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
      71             : {
      72           1 :     OUString expected("@~");
      73           2 :     OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(1);
      74           2 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
      75           1 : }
      76             : 
      77             : void
      78           1 : ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
      79             : {
      80           1 :     OUString expected("@, @~");
      81           2 :     OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(2);
      82           2 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
      83           1 : }
      84             : 
      85             : void
      86           1 : ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet()
      87             : {
      88           1 :     MockedSortTab sortTab;
      89           1 :     sortTab.pTextMark = NULL;
      90             : 
      91           2 :     OUString expected("");
      92           2 :     OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
      93           2 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
      94           1 : }
      95             : 
      96             : void
      97           1 : ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty()
      98             : {
      99           1 :     MockedSortTab sortTab;
     100           1 :     sortTab.pTextMark = reinterpret_cast<SwTextTOXMark*>(1);
     101             : 
     102           2 :     OUString expected("");
     103           2 :     OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
     104           2 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
     105           1 : }
     106             : 
     107           4 : class MockedToxTabStopTokenHandler : public ToxTabStopTokenHandler {
     108             : public:
     109             :     virtual HandledTabStopToken
     110           0 :     HandleTabStopToken(const SwFormToken& aToken, const SwTextNode& targetNode,
     111             :             const SwRootFrm *currentLayout) const SAL_OVERRIDE {
     112             :         (void)(aToken); (void)(targetNode); (void)(currentLayout); // avoid unused warnings.
     113           0 :         return HandledTabStopToken();
     114             :     }
     115             : };
     116             : 
     117           2 : class ToxTextGeneratorWithMockedChapterField : public ToxTextGenerator {
     118             : public:
     119           2 :     explicit ToxTextGeneratorWithMockedChapterField(SwForm &form)
     120             :     : ToxTextGenerator(form, std::make_shared<MockedToxTabStopTokenHandler>()),
     121           2 :       mChapterFieldType(), mChapterField(&mChapterFieldType) {;}
     122             : 
     123             :     SwChapterField&
     124           8 :     GetChapterField() {
     125           8 :         return mChapterField;
     126             :     }
     127             : 
     128             : private:
     129             :     SwChapterField
     130           4 :     ObtainChapterField(SwChapterFieldType* chapterFieldType, const SwFormToken* chapterToken,
     131             :             const SwContentFrm* contentFrame, const SwContentNode *contentNode) const SAL_OVERRIDE {
     132             :         // get rid of 'unused-parameters' warnings
     133             :         (void)(chapterFieldType);(void)(chapterToken);(void)(contentFrame);(void)(contentNode);
     134           4 :         return mChapterField;
     135             :     }
     136             : 
     137             :     SwChapterFieldType mChapterFieldType;
     138             :     SwChapterField mChapterField;
     139             : };
     140             : 
     141             : void
     142           1 : ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
     143             : {
     144           1 :     SwForm form;
     145           2 :     ToxTextGeneratorWithMockedChapterField ttg(form);
     146             :     // set all values to make sure they are not used
     147           1 :     ttg.GetChapterField().sNumber = "1";
     148           1 :     ttg.GetChapterField().sPre = "PRE";
     149           1 :     ttg.GetChapterField().sPost = "POST";
     150           1 :     ttg.GetChapterField().sTitle = "TITLE";
     151             : 
     152           2 :     SwFormToken token(TOKEN_CHAPTER_INFO);
     153           1 :     token.nChapterFormat = CF_NUM_NOPREPST_TITLE;
     154             : 
     155           2 :     OUString expected("1");
     156           2 :     OUString actual = ttg.GenerateTextForChapterToken(token, NULL, NULL);
     157           1 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
     158             : 
     159             :     // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
     160             :     // sTitle are used and hope that the pre- and suffix addition works.
     161           1 :     token.nChapterFormat = CF_NUMBER;
     162           1 :     expected = ttg.GenerateTextForChapterToken(token, NULL, NULL);
     163           2 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
     164           1 : }
     165             : 
     166             : 
     167             : void
     168           1 : ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
     169             : {
     170           1 :     SwForm form;
     171           2 :     ToxTextGeneratorWithMockedChapterField ttg(form);
     172             :     // set all values to make sure they are not used
     173           1 :     ttg.GetChapterField().sNumber = "5";
     174           1 :     ttg.GetChapterField().sPre = "PRE";
     175           1 :     ttg.GetChapterField().sPost = "POST";
     176           1 :     ttg.GetChapterField().sTitle = "myTitle";
     177             : 
     178           2 :     SwFormToken token(TOKEN_CHAPTER_INFO);
     179           1 :     token.nChapterFormat = CF_NUMBER_NOPREPST;
     180             : 
     181           2 :     OUString expected("5 myTitle");
     182           2 :     OUString actual = ttg.GenerateTextForChapterToken(token, NULL, NULL);
     183           1 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
     184             : 
     185             :     // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
     186             :     // sTitle are used and hope that the pre- and suffix addition works.
     187           1 :     token.nChapterFormat = CF_NUM_TITLE;
     188           1 :     expected = ttg.GenerateTextForChapterToken(token, NULL, NULL);
     189           2 :     CPPUNIT_ASSERT_EQUAL(expected, actual);
     190           1 : }
     191             : 
     192             : // Put the test suite in the registry
     193           3 : CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest);
     194             : 
     195             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11