1 | // |
2 | // TextTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "TextTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/DOM/Text.h" |
15 | #include "Poco/DOM/CDATASection.h" |
16 | #include "Poco/DOM/Element.h" |
17 | #include "Poco/DOM/Document.h" |
18 | #include "Poco/DOM/AutoPtr.h" |
19 | |
20 | |
21 | using Poco::XML::Text; |
22 | using Poco::XML::CDATASection; |
23 | using Poco::XML::Element; |
24 | using Poco::XML::Document; |
25 | using Poco::XML::AutoPtr; |
26 | using Poco::XML::XMLString; |
27 | |
28 | |
29 | TextTest::TextTest(const std::string& name): CppUnit::TestCase(name) |
30 | { |
31 | } |
32 | |
33 | |
34 | TextTest::~TextTest() |
35 | { |
36 | } |
37 | |
38 | |
39 | void TextTest::testLength() |
40 | { |
41 | AutoPtr<Document> pDoc = new Document; |
42 | AutoPtr<Text> pText1 = pDoc->createTextNode("" ); |
43 | assertTrue (pText1->length() == 0); |
44 | AutoPtr<Text> pText2 = pDoc->createTextNode("foo bar" ); |
45 | assertTrue (pText2->length() == 7); |
46 | } |
47 | |
48 | |
49 | void TextTest::testSubstring() |
50 | { |
51 | AutoPtr<Document> pDoc = new Document; |
52 | AutoPtr<Text> pText1 = pDoc->createTextNode("foo bar" ); |
53 | XMLString str = pText1->substringData(0, 3); |
54 | assertTrue (str == "foo" ); |
55 | str = pText1->substringData(4, 3); |
56 | assertTrue (str == "bar" ); |
57 | str = pText1->substringData(3, 0); |
58 | assertTrue (str == "" ); |
59 | } |
60 | |
61 | |
62 | void TextTest::testAppend() |
63 | { |
64 | AutoPtr<Document> pDoc = new Document; |
65 | AutoPtr<Text> pText1 = pDoc->createTextNode("foo" ); |
66 | pText1->appendData("bar" ); |
67 | assertTrue (pText1->data() == "foobar" ); |
68 | } |
69 | |
70 | |
71 | void TextTest::testInsert() |
72 | { |
73 | AutoPtr<Document> pDoc = new Document; |
74 | AutoPtr<Text> pText1 = pDoc->createTextNode("bar" ); |
75 | pText1->insertData(0, "foo" ); |
76 | assertTrue (pText1->data() == "foobar" ); |
77 | pText1->insertData(pText1->length(), "!" ); |
78 | assertTrue (pText1->data() == "foobar!" ); |
79 | pText1->insertData(3, " " ); |
80 | assertTrue (pText1->data() == "foo bar!" ); |
81 | } |
82 | |
83 | |
84 | void TextTest::testDelete() |
85 | { |
86 | AutoPtr<Document> pDoc = new Document; |
87 | AutoPtr<Text> pText1 = pDoc->createTextNode("foo bar" ); |
88 | pText1->deleteData(3, 1); |
89 | assertTrue (pText1->data() == "foobar" ); |
90 | pText1->deleteData(0, 3); |
91 | assertTrue (pText1->data() == "bar" ); |
92 | pText1->deleteData(1, 0); |
93 | assertTrue (pText1->data() == "bar" ); |
94 | pText1->deleteData(0, pText1->length()); |
95 | assertTrue (pText1->data() == "" ); |
96 | } |
97 | |
98 | |
99 | void TextTest::testReplace() |
100 | { |
101 | AutoPtr<Document> pDoc = new Document; |
102 | AutoPtr<Text> pText1 = pDoc->createTextNode("foo bar" ); |
103 | pText1->replaceData(0, 3, "FOO" ); |
104 | assertTrue (pText1->data() == "FOO bar" ); |
105 | pText1->replaceData(4, 3, "BAR!!!" ); |
106 | assertTrue (pText1->data() == "FOO BAR!!!" ); |
107 | pText1->replaceData(3, 1, "-" ); |
108 | assertTrue (pText1->data() == "FOO-BAR!!!" ); |
109 | pText1->replaceData(3, 1, "---" ); |
110 | assertTrue (pText1->data() == "FOO---BAR!!!" ); |
111 | pText1->replaceData(3, 3, " " ); |
112 | assertTrue (pText1->data() == "FOO BAR!!!" ); |
113 | pText1->replaceData(0, pText1->length(), "foo bar" ); |
114 | assertTrue (pText1->data() == "foo bar" ); |
115 | } |
116 | |
117 | |
118 | void TextTest::testSplit() |
119 | { |
120 | AutoPtr<Document> pDoc = new Document; |
121 | AutoPtr<Element> pElem = pDoc->createElement("elem" ); |
122 | AutoPtr<Text> pText1 = pDoc->createCDATASection("foobar" ); |
123 | pElem->appendChild(pText1); |
124 | pText1->splitText(3); |
125 | assertTrue (pElem->firstChild()->nodeValue() == "foo" ); |
126 | assertTrue (pElem->lastChild()->nodeValue() == "bar" ); |
127 | } |
128 | |
129 | |
130 | void TextTest::testSplitCDATA() |
131 | { |
132 | AutoPtr<Document> pDoc = new Document; |
133 | AutoPtr<Element> pElem = pDoc->createElement("elem" ); |
134 | AutoPtr<Text> pText1 = pDoc->createTextNode("foobar" ); |
135 | pElem->appendChild(pText1); |
136 | pText1->splitText(3); |
137 | assertTrue (pElem->firstChild()->nodeValue() == "foo" ); |
138 | assertTrue (pElem->lastChild()->nodeValue() == "bar" ); |
139 | |
140 | } |
141 | |
142 | |
143 | void TextTest::setUp() |
144 | { |
145 | } |
146 | |
147 | |
148 | void TextTest::tearDown() |
149 | { |
150 | } |
151 | |
152 | |
153 | CppUnit::Test* TextTest::suite() |
154 | { |
155 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TextTest" ); |
156 | |
157 | CppUnit_addTest(pSuite, TextTest, testLength); |
158 | CppUnit_addTest(pSuite, TextTest, testSubstring); |
159 | CppUnit_addTest(pSuite, TextTest, testAppend); |
160 | CppUnit_addTest(pSuite, TextTest, testInsert); |
161 | CppUnit_addTest(pSuite, TextTest, testDelete); |
162 | CppUnit_addTest(pSuite, TextTest, testReplace); |
163 | CppUnit_addTest(pSuite, TextTest, testSplit); |
164 | CppUnit_addTest(pSuite, TextTest, testSplitCDATA); |
165 | |
166 | return pSuite; |
167 | } |
168 | |