1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
23 OString
extractFromStream(SvMemoryStream
& rStream
)
25 rStream
.WriteChar('\0');
27 rStream
.Seek(STREAM_SEEK_TO_BEGIN
);
28 return OString(static_cast<const sal_Char
*>(rStream
.GetBuffer()));
33 class Test
: public CppUnit::TestFixture
37 virtual void setUp() SAL_OVERRIDE
;
38 void testSingleElement();
39 void testSingleElementWithAttributes();
40 void testSingleElementWithContent();
41 void testSingleElementWithContentAndAttributes();
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();
61 void Test::testSingleElement()
64 SvMemoryStream aStream
;
66 HtmlWriter
aHtml(aStream
);
67 aHtml
.prettyPrint(false);
71 OString aString
= extractFromStream(aStream
);
72 CPPUNIT_ASSERT_EQUAL(aString
, OString("<abc/>"));
76 SvMemoryStream aStream
;
78 HtmlWriter
aHtml(aStream
);
79 aHtml
.prettyPrint(false);
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);
96 aHtml
.attribute("x", "y");
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);
110 aHtml
.attribute("x", "y");
111 aHtml
.attribute("q", "w");
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);
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);
142 aHtml
.attribute("x", "y");
143 aHtml
.attribute("q", "w");
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);
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);
176 aHtml
.attribute("one", OString("one"));
177 aHtml
.attribute("two", OUString("two"));
178 aHtml
.attribute("three", sal_Int32(12));
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);
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);
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);
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);
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);
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);
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);
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: */