4
// $Id: //poco/1.2/XML/testsuite/src/TextTest.cpp#1 $
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/DOM/Text.h"
37
#include "Poco/DOM/CDATASection.h"
38
#include "Poco/DOM/Element.h"
39
#include "Poco/DOM/Document.h"
40
#include "Poco/DOM/AutoPtr.h"
43
using Poco::XML::Text;
44
using Poco::XML::CDATASection;
45
using Poco::XML::Element;
46
using Poco::XML::Document;
47
using Poco::XML::AutoPtr;
48
using Poco::XML::XMLString;
51
TextTest::TextTest(const std::string& name): CppUnit::TestCase(name)
61
void TextTest::testLength()
63
AutoPtr<Document> pDoc = new Document;
64
AutoPtr<Text> pText1 = pDoc->createTextNode("");
65
assert (pText1->length() == 0);
66
AutoPtr<Text> pText2 = pDoc->createTextNode("foo bar");
67
assert (pText2->length() == 7);
71
void TextTest::testSubstring()
73
AutoPtr<Document> pDoc = new Document;
74
AutoPtr<Text> pText1 = pDoc->createTextNode("foo bar");
75
XMLString str = pText1->substringData(0, 3);
76
assert (str == "foo");
77
str = pText1->substringData(4, 3);
78
assert (str == "bar");
79
str = pText1->substringData(3, 0);
84
void TextTest::testAppend()
86
AutoPtr<Document> pDoc = new Document;
87
AutoPtr<Text> pText1 = pDoc->createTextNode("foo");
88
pText1->appendData("bar");
89
assert (pText1->data() == "foobar");
93
void TextTest::testInsert()
95
AutoPtr<Document> pDoc = new Document;
96
AutoPtr<Text> pText1 = pDoc->createTextNode("bar");
97
pText1->insertData(0, "foo");
98
assert (pText1->data() == "foobar");
99
pText1->insertData(pText1->length(), "!");
100
assert (pText1->data() == "foobar!");
101
pText1->insertData(3, " ");
102
assert (pText1->data() == "foo bar!");
106
void TextTest::testDelete()
108
AutoPtr<Document> pDoc = new Document;
109
AutoPtr<Text> pText1 = pDoc->createTextNode("foo bar");
110
pText1->deleteData(3, 1);
111
assert (pText1->data() == "foobar");
112
pText1->deleteData(0, 3);
113
assert (pText1->data() == "bar");
114
pText1->deleteData(1, 0);
115
assert (pText1->data() == "bar");
116
pText1->deleteData(0, pText1->length());
117
assert (pText1->data() == "");
121
void TextTest::testReplace()
123
AutoPtr<Document> pDoc = new Document;
124
AutoPtr<Text> pText1 = pDoc->createTextNode("foo bar");
125
pText1->replaceData(0, 3, "FOO");
126
assert (pText1->data() == "FOO bar");
127
pText1->replaceData(4, 3, "BAR!!!");
128
assert (pText1->data() == "FOO BAR!!!");
129
pText1->replaceData(3, 1, "-");
130
assert (pText1->data() == "FOO-BAR!!!");
131
pText1->replaceData(3, 1, "---");
132
assert (pText1->data() == "FOO---BAR!!!");
133
pText1->replaceData(3, 3, " ");
134
assert (pText1->data() == "FOO BAR!!!");
135
pText1->replaceData(0, pText1->length(), "foo bar");
136
assert (pText1->data() == "foo bar");
140
void TextTest::testSplit()
142
AutoPtr<Document> pDoc = new Document;
143
AutoPtr<Element> pElem = pDoc->createElement("elem");
144
AutoPtr<Text> pText1 = pDoc->createCDATASection("foobar");
145
pElem->appendChild(pText1);
146
pText1->splitText(3);
147
assert (pElem->firstChild()->nodeValue() == "foo");
148
assert (pElem->lastChild()->nodeValue() == "bar");
152
void TextTest::testSplitCDATA()
154
AutoPtr<Document> pDoc = new Document;
155
AutoPtr<Element> pElem = pDoc->createElement("elem");
156
AutoPtr<Text> pText1 = pDoc->createTextNode("foobar");
157
pElem->appendChild(pText1);
158
pText1->splitText(3);
159
assert (pElem->firstChild()->nodeValue() == "foo");
160
assert (pElem->lastChild()->nodeValue() == "bar");
165
void TextTest::setUp()
170
void TextTest::tearDown()
175
CppUnit::Test* TextTest::suite()
177
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TextTest");
179
CppUnit_addTest(pSuite, TextTest, testLength);
180
CppUnit_addTest(pSuite, TextTest, testSubstring);
181
CppUnit_addTest(pSuite, TextTest, testAppend);
182
CppUnit_addTest(pSuite, TextTest, testInsert);
183
CppUnit_addTest(pSuite, TextTest, testDelete);
184
CppUnit_addTest(pSuite, TextTest, testReplace);
185
CppUnit_addTest(pSuite, TextTest, testSplit);
186
CppUnit_addTest(pSuite, TextTest, testSplitCDATA);