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