LCOV - code coverage report
Current view: top level - svtools/qa/unit - testHtmlWriter.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 160 160 100.0 %
Date: 2014-11-03 Functions: 18 19 94.7 %
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             : 
      11             : #include "cppunit/TestCase.h"
      12             : #include "cppunit/TestFixture.h"
      13             : #include "cppunit/TestSuite.h"
      14             : #include "cppunit/extensions/HelperMacros.h"
      15             : #include "cppunit/plugin/TestPlugIn.h"
      16             : 
      17             : #include <tools/stream.hxx>
      18             : #include <svtools/HtmlWriter.hxx>
      19             : 
      20             : namespace
      21             : {
      22             : 
      23          30 : OString extractFromStream(SvMemoryStream& rStream)
      24             : {
      25          30 :     rStream.WriteChar('\0');
      26          30 :     rStream.Flush();
      27          30 :     rStream.Seek(STREAM_SEEK_TO_BEGIN);
      28          30 :     return OString((const sal_Char*)rStream.GetBuffer());
      29             : }
      30             : 
      31             : }
      32             : 
      33          42 : class Test: public CppUnit::TestFixture
      34             : {
      35             : 
      36             : public:
      37             :     virtual void setUp() SAL_OVERRIDE;
      38             :     void testSingleElement();
      39             :     void testSingleElementWithAttributes();
      40             :     void testSingleElementWithContent();
      41             :     void testSingleElementWithContentAndAttributes();
      42             :     void testNested();
      43             :     void testAttributeValues();
      44             :     void testFlushStack();
      45             : 
      46           4 :     CPPUNIT_TEST_SUITE(Test);
      47           2 :     CPPUNIT_TEST(testSingleElement);
      48           2 :     CPPUNIT_TEST(testSingleElementWithAttributes);
      49           2 :     CPPUNIT_TEST(testSingleElementWithContent);
      50           2 :     CPPUNIT_TEST(testSingleElementWithContentAndAttributes);
      51           2 :     CPPUNIT_TEST(testNested);
      52           2 :     CPPUNIT_TEST(testAttributeValues);
      53           2 :     CPPUNIT_TEST(testFlushStack);
      54             : 
      55           4 :     CPPUNIT_TEST_SUITE_END();
      56             : };
      57             : 
      58          14 : void Test::setUp()
      59          14 : {}
      60             : 
      61           2 : void Test::testSingleElement()
      62             : {
      63             :     {
      64           2 :         SvMemoryStream aStream;
      65             : 
      66           4 :         HtmlWriter aHtml(aStream);
      67           2 :         aHtml.prettyPrint(false);
      68           2 :         aHtml.start("abc");
      69           2 :         aHtml.end();
      70             : 
      71           4 :         OString aString = extractFromStream(aStream);
      72           4 :         CPPUNIT_ASSERT_EQUAL(aString, OString("<abc/>"));
      73             :     }
      74             : 
      75             :     {
      76           2 :         SvMemoryStream aStream;
      77             : 
      78           4 :         HtmlWriter aHtml(aStream);
      79           2 :         aHtml.prettyPrint(false);
      80           2 :         aHtml.single("abc");
      81             : 
      82           4 :         OString aString = extractFromStream(aStream);
      83             : 
      84           4 :         CPPUNIT_ASSERT_EQUAL(aString, OString("<abc/>"));
      85             :     }
      86           2 : }
      87             : 
      88           2 : void Test::testSingleElementWithAttributes()
      89             : {
      90             :     {
      91           2 :         SvMemoryStream aStream;
      92             : 
      93           4 :         HtmlWriter aHtml(aStream);
      94           2 :         aHtml.prettyPrint(false);
      95           2 :         aHtml.start("abc");
      96           2 :         aHtml.attribute("x", "y");
      97           2 :         aHtml.end();
      98             : 
      99           4 :         OString aString = extractFromStream(aStream);
     100             : 
     101           4 :         CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\"/>"));
     102             :     }
     103             : 
     104             :     {
     105           2 :         SvMemoryStream aStream;
     106             : 
     107           4 :         HtmlWriter aHtml(aStream);
     108           2 :         aHtml.prettyPrint(false);
     109           2 :         aHtml.start("abc");
     110           2 :         aHtml.attribute("x", "y");
     111           2 :         aHtml.attribute("q", "w");
     112           2 :         aHtml.end();
     113             : 
     114           4 :         OString aString = extractFromStream(aStream);
     115             : 
     116           4 :         CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\" q=\"w\"/>"));
     117             :     }
     118           2 : }
     119             : 
     120           2 : void Test::testSingleElementWithContent()
     121             : {
     122           2 :     SvMemoryStream aStream;
     123             : 
     124           4 :     HtmlWriter aHtml(aStream);
     125           2 :     aHtml.prettyPrint(false);
     126           2 :     aHtml.start("abc");
     127           2 :     aHtml.write("xxxx");
     128           2 :     aHtml.end();
     129             : 
     130           4 :     OString aString = extractFromStream(aStream);
     131             : 
     132           4 :     CPPUNIT_ASSERT_EQUAL(aString, OString("<abc>xxxx</abc>"));
     133           2 : }
     134             : 
     135           2 : void Test::testSingleElementWithContentAndAttributes()
     136             : {
     137           2 :     SvMemoryStream aStream;
     138             : 
     139           4 :     HtmlWriter aHtml(aStream);
     140           2 :     aHtml.prettyPrint(false);
     141           2 :     aHtml.start("abc");
     142           2 :     aHtml.attribute("x", "y");
     143           2 :     aHtml.attribute("q", "w");
     144           2 :     aHtml.write("xxxx");
     145           2 :     aHtml.end();
     146             : 
     147           4 :     OString aString = extractFromStream(aStream);
     148             : 
     149           4 :     CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\" q=\"w\">xxxx</abc>"));
     150           2 : }
     151             : 
     152           2 : void Test::testNested()
     153             : {
     154           2 :     SvMemoryStream aStream;
     155             : 
     156           4 :     HtmlWriter aHtml(aStream);
     157           2 :     aHtml.prettyPrint(false);
     158           2 :     aHtml.start("abc");
     159           2 :         aHtml.start("xyz");
     160           2 :         aHtml.write("xxx");
     161           2 :         aHtml.end();
     162           2 :     aHtml.end();
     163             : 
     164           4 :     OString aString = extractFromStream(aStream);
     165             : 
     166           4 :     CPPUNIT_ASSERT_EQUAL(OString("<abc><xyz>xxx</xyz></abc>"), aString);
     167           2 : }
     168             : 
     169           2 : void Test::testAttributeValues()
     170             : {
     171           2 :     SvMemoryStream aStream;
     172             : 
     173           4 :     HtmlWriter aHtml(aStream);
     174           2 :     aHtml.prettyPrint(false);
     175           2 :     aHtml.start("abc");
     176           2 :     aHtml.attribute("one", OString("one"));
     177           2 :     aHtml.attribute("two", OUString("two"));
     178           2 :     aHtml.attribute("three", sal_Int32(12));
     179           2 :     aHtml.end();
     180             : 
     181           4 :     OString aString = extractFromStream(aStream);
     182             : 
     183           4 :     CPPUNIT_ASSERT_EQUAL(OString("<abc one=\"one\" two=\"two\" three=\"12\"/>"), aString);
     184           2 : }
     185             : 
     186           2 : void Test::testFlushStack()
     187             : {
     188             :     {
     189           2 :         SvMemoryStream aStream;
     190             : 
     191           4 :         HtmlWriter aHtml(aStream);
     192           2 :         aHtml.prettyPrint(false);
     193           2 :         aHtml.start("a");
     194           2 :         aHtml.flushStack("a"); // simple ,end element "a" = like end()
     195             : 
     196           4 :         OString aString = extractFromStream(aStream);
     197             : 
     198           4 :         CPPUNIT_ASSERT_EQUAL(OString("<a/>"), aString);
     199             :     }
     200             : 
     201             :     {
     202           2 :         SvMemoryStream aStream;
     203             : 
     204           4 :         HtmlWriter aHtml(aStream);
     205           2 :         aHtml.prettyPrint(false);
     206           2 :         aHtml.start("a");
     207           2 :         aHtml.start("b");
     208           2 :         aHtml.flushStack("b"); // end at first element "b", don't output "a" yet
     209             : 
     210           4 :         OString aString = extractFromStream(aStream);
     211             : 
     212           4 :         CPPUNIT_ASSERT_EQUAL(OString("<a><b/>"), aString);
     213             :     }
     214             : 
     215             :     {
     216           2 :         SvMemoryStream aStream;
     217             : 
     218           4 :         HtmlWriter aHtml(aStream);
     219           2 :         aHtml.prettyPrint(false);
     220           2 :         aHtml.start("a");
     221           2 :         aHtml.start("b");
     222           2 :         aHtml.flushStack("a"); // end at first element "a"
     223             : 
     224           4 :         OString aString = extractFromStream(aStream);
     225             : 
     226           4 :         CPPUNIT_ASSERT_EQUAL(OString("<a><b/></a>"), aString);
     227             :     }
     228             : 
     229             :     {
     230           2 :         SvMemoryStream aStream;
     231             : 
     232           4 :         HtmlWriter aHtml(aStream);
     233           2 :         aHtml.prettyPrint(false);
     234           2 :         aHtml.start("a");
     235           2 :         aHtml.start("b");
     236           2 :         aHtml.start("c");
     237           2 :         aHtml.flushStack("a"); // end at first element "a"
     238             : 
     239           4 :         OString aString = extractFromStream(aStream);
     240             : 
     241           4 :         CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b></a>"), aString);
     242             :     }
     243             : 
     244             :     {
     245           2 :         SvMemoryStream aStream;
     246             : 
     247           4 :         HtmlWriter aHtml(aStream);
     248           2 :         aHtml.prettyPrint(false);
     249           2 :         aHtml.start("a");
     250           2 :         aHtml.start("b");
     251           2 :         aHtml.start("c");
     252           2 :         aHtml.flushStack("b"); // end at first element "b"
     253             : 
     254           4 :         OString aString = extractFromStream(aStream);
     255             : 
     256           4 :         CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b>"), aString);
     257             :     }
     258             : 
     259             :     {
     260           2 :         SvMemoryStream aStream;
     261             : 
     262           4 :         HtmlWriter aHtml(aStream);
     263           2 :         aHtml.prettyPrint(false);
     264           2 :         aHtml.start("a");
     265           2 :         aHtml.start("b");
     266           2 :         aHtml.start("c");
     267           2 :         aHtml.flushStack("x"); // no known element - ends when stack is empty
     268             : 
     269           4 :         OString aString = extractFromStream(aStream);
     270             : 
     271           4 :         CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b></a>"), aString);
     272             :     }
     273             : 
     274             :     {
     275           2 :         SvMemoryStream aStream;
     276             : 
     277           4 :         HtmlWriter aHtml(aStream);
     278           2 :         aHtml.prettyPrint(false);
     279           2 :         aHtml.start("a");
     280           2 :         aHtml.start("b");
     281           2 :         aHtml.start("c");
     282           2 :         aHtml.flushStack(); // flush the whole stack
     283             : 
     284           4 :         OString aString = extractFromStream(aStream);
     285             : 
     286           4 :         CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b></a>"), aString);
     287             :     }
     288           2 : }
     289             : 
     290           2 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
     291           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     292             : 
     293             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10