~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to XML/testsuite/src/TextTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// TextTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/XML/testsuite/src/TextTest.cpp#1 $
 
5
//
 
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
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:
 
15
// 
 
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.
 
22
// 
 
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.
 
30
//
 
31
 
 
32
 
 
33
#include "TextTest.h"
 
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"
 
41
 
 
42
 
 
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;
 
49
 
 
50
 
 
51
TextTest::TextTest(const std::string& name): CppUnit::TestCase(name)
 
52
{
 
53
}
 
54
 
 
55
 
 
56
TextTest::~TextTest()
 
57
{
 
58
}
 
59
 
 
60
 
 
61
void TextTest::testLength()
 
62
{
 
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);
 
68
}
 
69
 
 
70
 
 
71
void TextTest::testSubstring()
 
72
{
 
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);
 
80
        assert (str == "");
 
81
}
 
82
 
 
83
 
 
84
void TextTest::testAppend()
 
85
{
 
86
        AutoPtr<Document> pDoc = new Document;
 
87
        AutoPtr<Text> pText1 = pDoc->createTextNode("foo");
 
88
        pText1->appendData("bar");
 
89
        assert (pText1->data() == "foobar");
 
90
}
 
91
 
 
92
 
 
93
void TextTest::testInsert()
 
94
{
 
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!");
 
103
}
 
104
 
 
105
 
 
106
void TextTest::testDelete()
 
107
{
 
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() == "");
 
118
}
 
119
 
 
120
 
 
121
void TextTest::testReplace()
 
122
{
 
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");
 
137
}
 
138
 
 
139
 
 
140
void TextTest::testSplit()
 
141
{
 
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");
 
149
}
 
150
 
 
151
 
 
152
void TextTest::testSplitCDATA()
 
153
{
 
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");
 
161
 
 
162
}
 
163
 
 
164
 
 
165
void TextTest::setUp()
 
166
{
 
167
}
 
168
 
 
169
 
 
170
void TextTest::tearDown()
 
171
{
 
172
}
 
173
 
 
174
 
 
175
CppUnit::Test* TextTest::suite()
 
176
{
 
177
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TextTest");
 
178
 
 
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);
 
187
 
 
188
        return pSuite;
 
189
}