Bump version to 5.0-14
[LibreOffice.git] / svtools / qa / unit / testHtmlWriter.cxx
blob5ad7c0d9247fda9491f09288758a80124c40d477
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 */
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"
17 #include <tools/stream.hxx>
18 #include <svtools/HtmlWriter.hxx>
20 namespace
23 OString extractFromStream(SvMemoryStream& rStream)
25 rStream.WriteChar('\0');
26 rStream.Flush();
27 rStream.Seek(STREAM_SEEK_TO_BEGIN);
28 return OString(static_cast<const sal_Char*>(rStream.GetBuffer()));
33 class Test: public CppUnit::TestFixture
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();
46 CPPUNIT_TEST_SUITE(Test);
47 CPPUNIT_TEST(testSingleElement);
48 CPPUNIT_TEST(testSingleElementWithAttributes);
49 CPPUNIT_TEST(testSingleElementWithContent);
50 CPPUNIT_TEST(testSingleElementWithContentAndAttributes);
51 CPPUNIT_TEST(testNested);
52 CPPUNIT_TEST(testAttributeValues);
53 CPPUNIT_TEST(testFlushStack);
55 CPPUNIT_TEST_SUITE_END();
58 void Test::setUp()
61 void Test::testSingleElement()
64 SvMemoryStream aStream;
66 HtmlWriter aHtml(aStream);
67 aHtml.prettyPrint(false);
68 aHtml.start("abc");
69 aHtml.end();
71 OString aString = extractFromStream(aStream);
72 CPPUNIT_ASSERT_EQUAL(aString, OString("<abc/>"));
76 SvMemoryStream aStream;
78 HtmlWriter aHtml(aStream);
79 aHtml.prettyPrint(false);
80 aHtml.single("abc");
82 OString aString = extractFromStream(aStream);
84 CPPUNIT_ASSERT_EQUAL(aString, OString("<abc/>"));
88 void Test::testSingleElementWithAttributes()
91 SvMemoryStream aStream;
93 HtmlWriter aHtml(aStream);
94 aHtml.prettyPrint(false);
95 aHtml.start("abc");
96 aHtml.attribute("x", "y");
97 aHtml.end();
99 OString aString = extractFromStream(aStream);
101 CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\"/>"));
105 SvMemoryStream aStream;
107 HtmlWriter aHtml(aStream);
108 aHtml.prettyPrint(false);
109 aHtml.start("abc");
110 aHtml.attribute("x", "y");
111 aHtml.attribute("q", "w");
112 aHtml.end();
114 OString aString = extractFromStream(aStream);
116 CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\" q=\"w\"/>"));
120 void Test::testSingleElementWithContent()
122 SvMemoryStream aStream;
124 HtmlWriter aHtml(aStream);
125 aHtml.prettyPrint(false);
126 aHtml.start("abc");
127 aHtml.write("xxxx");
128 aHtml.end();
130 OString aString = extractFromStream(aStream);
132 CPPUNIT_ASSERT_EQUAL(aString, OString("<abc>xxxx</abc>"));
135 void Test::testSingleElementWithContentAndAttributes()
137 SvMemoryStream aStream;
139 HtmlWriter aHtml(aStream);
140 aHtml.prettyPrint(false);
141 aHtml.start("abc");
142 aHtml.attribute("x", "y");
143 aHtml.attribute("q", "w");
144 aHtml.write("xxxx");
145 aHtml.end();
147 OString aString = extractFromStream(aStream);
149 CPPUNIT_ASSERT_EQUAL(aString, OString("<abc x=\"y\" q=\"w\">xxxx</abc>"));
152 void Test::testNested()
154 SvMemoryStream aStream;
156 HtmlWriter aHtml(aStream);
157 aHtml.prettyPrint(false);
158 aHtml.start("abc");
159 aHtml.start("xyz");
160 aHtml.write("xxx");
161 aHtml.end();
162 aHtml.end();
164 OString aString = extractFromStream(aStream);
166 CPPUNIT_ASSERT_EQUAL(OString("<abc><xyz>xxx</xyz></abc>"), aString);
169 void Test::testAttributeValues()
171 SvMemoryStream aStream;
173 HtmlWriter aHtml(aStream);
174 aHtml.prettyPrint(false);
175 aHtml.start("abc");
176 aHtml.attribute("one", OString("one"));
177 aHtml.attribute("two", OUString("two"));
178 aHtml.attribute("three", sal_Int32(12));
179 aHtml.end();
181 OString aString = extractFromStream(aStream);
183 CPPUNIT_ASSERT_EQUAL(OString("<abc one=\"one\" two=\"two\" three=\"12\"/>"), aString);
186 void Test::testFlushStack()
189 SvMemoryStream aStream;
191 HtmlWriter aHtml(aStream);
192 aHtml.prettyPrint(false);
193 aHtml.start("a");
194 aHtml.flushStack("a"); // simple ,end element "a" = like end()
196 OString aString = extractFromStream(aStream);
198 CPPUNIT_ASSERT_EQUAL(OString("<a/>"), aString);
202 SvMemoryStream aStream;
204 HtmlWriter aHtml(aStream);
205 aHtml.prettyPrint(false);
206 aHtml.start("a");
207 aHtml.start("b");
208 aHtml.flushStack("b"); // end at first element "b", don't output "a" yet
210 OString aString = extractFromStream(aStream);
212 CPPUNIT_ASSERT_EQUAL(OString("<a><b/>"), aString);
216 SvMemoryStream aStream;
218 HtmlWriter aHtml(aStream);
219 aHtml.prettyPrint(false);
220 aHtml.start("a");
221 aHtml.start("b");
222 aHtml.flushStack("a"); // end at first element "a"
224 OString aString = extractFromStream(aStream);
226 CPPUNIT_ASSERT_EQUAL(OString("<a><b/></a>"), aString);
230 SvMemoryStream aStream;
232 HtmlWriter aHtml(aStream);
233 aHtml.prettyPrint(false);
234 aHtml.start("a");
235 aHtml.start("b");
236 aHtml.start("c");
237 aHtml.flushStack("a"); // end at first element "a"
239 OString aString = extractFromStream(aStream);
241 CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b></a>"), aString);
245 SvMemoryStream aStream;
247 HtmlWriter aHtml(aStream);
248 aHtml.prettyPrint(false);
249 aHtml.start("a");
250 aHtml.start("b");
251 aHtml.start("c");
252 aHtml.flushStack("b"); // end at first element "b"
254 OString aString = extractFromStream(aStream);
256 CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b>"), aString);
260 SvMemoryStream aStream;
262 HtmlWriter aHtml(aStream);
263 aHtml.prettyPrint(false);
264 aHtml.start("a");
265 aHtml.start("b");
266 aHtml.start("c");
267 aHtml.flushStack("x"); // no known element - ends when stack is empty
269 OString aString = extractFromStream(aStream);
271 CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b></a>"), aString);
275 SvMemoryStream aStream;
277 HtmlWriter aHtml(aStream);
278 aHtml.prettyPrint(false);
279 aHtml.start("a");
280 aHtml.start("b");
281 aHtml.start("c");
282 aHtml.flushStack(); // flush the whole stack
284 OString aString = extractFromStream(aStream);
286 CPPUNIT_ASSERT_EQUAL(OString("<a><b><c/></b></a>"), aString);
290 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
291 CPPUNIT_PLUGIN_IMPLEMENT();
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */