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 7 : OString extractFromStream(SvMemoryStream& rStream)
24 : {
25 7 : rStream.WriteChar('\0');
26 7 : rStream.Flush();
27 7 : rStream.Seek(STREAM_SEEK_TO_BEGIN);
28 7 : return OString((const sal_Char*)rStream.GetBuffer());
29 : }
30 :
31 : }
32 :
33 15 : 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 :
44 2 : CPPUNIT_TEST_SUITE(Test);
45 1 : CPPUNIT_TEST(testSingleElement);
46 1 : CPPUNIT_TEST(testSingleElementWithAttributes);
47 1 : CPPUNIT_TEST(testSingleElementWithContent);
48 1 : CPPUNIT_TEST(testSingleElementWithContentAndAttributes);
49 1 : CPPUNIT_TEST(testNested);
50 :
51 2 : CPPUNIT_TEST_SUITE_END();
52 : };
53 :
54 5 : void Test::setUp()
55 5 : {}
56 :
57 1 : void Test::testSingleElement()
58 : {
59 : {
60 1 : SvMemoryStream aStream;
61 :
62 2 : HtmlWriter aHtml(aStream);
63 1 : aHtml.prettyPrint(false);
64 1 : aHtml.start("abc");
65 1 : aHtml.end();
66 :
67 2 : OString aString = extractFromStream(aStream);
68 2 : CPPUNIT_ASSERT_EQUAL(aString, OString("<abc/>"));
69 : }
70 :
71 : {
72 1 : SvMemoryStream aStream;
73 :
74 2 : HtmlWriter aHtml(aStream);
75 1 : aHtml.prettyPrint(false);
76 1 : aHtml.single("abc");
77 :
78 2 : OString aString = extractFromStream(aStream);
79 :
80 2 : CPPUNIT_ASSERT_EQUAL(aString, OString("<abc/>"));
81 : }
82 1 : }
83 :
84 1 : void Test::testSingleElementWithAttributes()
85 : {
86 : {
87 1 : SvMemoryStream aStream;
88 :
89 2 : HtmlWriter aHtml(aStream);
90 1 : aHtml.prettyPrint(false);
91 1 : aHtml.start("abc");
92 1 : aHtml.attribute("x", "y");
93 1 : aHtml.end();
94 :
95 2 : OString aString = extractFromStream(aStream);
96 :
97 2 : CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\"/>"));
98 : }
99 :
100 : {
101 1 : SvMemoryStream aStream;
102 :
103 2 : HtmlWriter aHtml(aStream);
104 1 : aHtml.prettyPrint(false);
105 1 : aHtml.start("abc");
106 1 : aHtml.attribute("x", "y");
107 1 : aHtml.attribute("q", "w");
108 1 : aHtml.end();
109 :
110 2 : OString aString = extractFromStream(aStream);
111 :
112 2 : CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\" q=\"w\"/>"));
113 : }
114 1 : }
115 :
116 1 : void Test::testSingleElementWithContent()
117 : {
118 1 : SvMemoryStream aStream;
119 :
120 2 : HtmlWriter aHtml(aStream);
121 1 : aHtml.prettyPrint(false);
122 1 : aHtml.start("abc");
123 1 : aHtml.write("xxxx");
124 1 : aHtml.end();
125 :
126 2 : OString aString = extractFromStream(aStream);
127 :
128 2 : CPPUNIT_ASSERT_EQUAL(aString, OString("<abc>xxxx</abc>"));
129 1 : }
130 :
131 1 : void Test::testSingleElementWithContentAndAttributes()
132 : {
133 1 : SvMemoryStream aStream;
134 :
135 2 : HtmlWriter aHtml(aStream);
136 1 : aHtml.prettyPrint(false);
137 1 : aHtml.start("abc");
138 1 : aHtml.attribute("x", "y");
139 1 : aHtml.attribute("q", "w");
140 1 : aHtml.write("xxxx");
141 1 : aHtml.end();
142 :
143 2 : OString aString = extractFromStream(aStream);
144 :
145 2 : CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\" q=\"w\">xxxx</abc>"));
146 1 : }
147 :
148 1 : void Test::testNested()
149 : {
150 1 : SvMemoryStream aStream;
151 :
152 2 : HtmlWriter aHtml(aStream);
153 1 : aHtml.prettyPrint(false);
154 1 : aHtml.start("abc");
155 1 : aHtml.start("xyz");
156 1 : aHtml.write("xxx");
157 1 : aHtml.end();
158 1 : aHtml.end();
159 :
160 2 : OString aString = extractFromStream(aStream);
161 :
162 2 : CPPUNIT_ASSERT_EQUAL(OString("<abc><xyz>xxx</xyz></abc>"), aString);
163 1 : }
164 :
165 :
166 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
167 4 : CPPUNIT_PLUGIN_IMPLEMENT();
168 :
169 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|