1 | // |
2 | // NameTest.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 "NameTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/XML/Name.h" |
15 | |
16 | |
17 | using Poco::XML::Name; |
18 | |
19 | |
20 | NameTest::NameTest(const std::string& name): CppUnit::TestCase(name) |
21 | { |
22 | } |
23 | |
24 | |
25 | NameTest::~NameTest() |
26 | { |
27 | } |
28 | |
29 | |
30 | void NameTest::testSplit() |
31 | { |
32 | std::string qname = "name" ; |
33 | std::string prefix; |
34 | std::string local; |
35 | Name::split(qname, prefix, local); |
36 | assertTrue (prefix.empty()); |
37 | assertTrue (local == "name" ); |
38 | |
39 | qname = "p:l" ; |
40 | Name::split(qname, prefix, local); |
41 | assertTrue (prefix == "p" ); |
42 | assertTrue (local == "l" ); |
43 | |
44 | qname = "pre:local" ; |
45 | Name::split(qname, prefix, local); |
46 | assertTrue (prefix == "pre" ); |
47 | assertTrue (local == "local" ); |
48 | } |
49 | |
50 | |
51 | void NameTest::testLocalName() |
52 | { |
53 | std::string qname = "name" ; |
54 | std::string local = Name::localName(qname); |
55 | assertTrue (local == "name" ); |
56 | qname = "p:l" ; |
57 | local = Name::localName(qname); |
58 | assertTrue (local == "l" ); |
59 | qname = "pre:local" ; |
60 | local = Name::localName(qname); |
61 | assertTrue (local == "local" ); |
62 | } |
63 | |
64 | |
65 | void NameTest::testPrefix() |
66 | { |
67 | std::string qname = "name" ; |
68 | std::string prefix = Name::prefix(qname); |
69 | assertTrue (prefix.empty()); |
70 | qname = "p:l" ; |
71 | prefix = Name::prefix(qname); |
72 | assertTrue (prefix == "p" ); |
73 | qname = "pre:local" ; |
74 | prefix = Name::prefix(qname); |
75 | assertTrue (prefix == "pre" ); |
76 | } |
77 | |
78 | |
79 | void NameTest::testName() |
80 | { |
81 | std::string qname = "name" ; |
82 | Name name(qname); |
83 | assertTrue (name.qname() == "name" ); |
84 | assertTrue (name.prefix().empty()); |
85 | assertTrue (name.namespaceURI().empty()); |
86 | assertTrue (name.localName().empty()); |
87 | |
88 | qname.clear(); |
89 | name.assign(qname, "http://www.appinf.com/" , "local" ); |
90 | assertTrue (name.qname().empty()); |
91 | assertTrue (name.prefix().empty()); |
92 | assertTrue (name.namespaceURI() == "http://www.appinf.com/" ); |
93 | assertTrue (name.localName() == "local" ); |
94 | |
95 | Name name2("pre:local" , "http://www.appinf.com/" ); |
96 | assertTrue (name2.qname() == "pre:local" ); |
97 | assertTrue (name2.prefix() == "pre" ); |
98 | assertTrue (name2.namespaceURI() == "http://www.appinf.com/" ); |
99 | assertTrue (name2.localName() == "local" ); |
100 | |
101 | name2.assign("PRE:Local" , "http://www.appinf.com/" ); |
102 | assertTrue (name2.qname() == "PRE:Local" ); |
103 | assertTrue (name2.prefix() == "PRE" ); |
104 | assertTrue (name2.namespaceURI() == "http://www.appinf.com/" ); |
105 | assertTrue (name2.localName() == "Local" ); |
106 | } |
107 | |
108 | |
109 | void NameTest::testCompare() |
110 | { |
111 | Name n1("pre:local" ); |
112 | Name n2(n1); |
113 | Name n3("pre:local2" ); |
114 | |
115 | assertTrue (n1.equals(n2)); |
116 | assertTrue (!n1.equals(n3)); |
117 | |
118 | n1.assign("pre:local" , "http://www.appinf.com" , "local" ); |
119 | n2.assign("pre:local" , "http://www.appinf.com" , "local" ); |
120 | n3.assign("pre:local2" , "http://www.appinf.com" , "local2" ); |
121 | |
122 | assertTrue (n1.equals(n2)); |
123 | assertTrue (!n1.equals(n3)); |
124 | |
125 | assertTrue (n1.equals("pre:local" , "http://www.appinf.com" , "local" )); |
126 | assertTrue (!n1.equals("pre:local" , "" , "" )); |
127 | assertTrue (n1.equalsWeakly("pre:local" , "" , "" )); |
128 | assertTrue (!n1.equalsWeakly("pre:local2" , "" , "" )); |
129 | assertTrue (!n1.equals("" , "http://www.appinf.com" , "local" )); |
130 | assertTrue (n1.equalsWeakly("" , "http://www.appinf.com" , "local" )); |
131 | assertTrue (!n1.equalsWeakly("" , "http://www.appinf.com" , "local2" )); |
132 | } |
133 | |
134 | |
135 | void NameTest::testSwap() |
136 | { |
137 | Name n1("ns:name1" , "http://www.appinf.com" ); |
138 | Name n2("ns:name2" , "http://www.foobar.com" ); |
139 | n1.swap(n2); |
140 | assertTrue (n1.qname() == "ns:name2" ); |
141 | assertTrue (n1.namespaceURI() == "http://www.foobar.com" ); |
142 | assertTrue (n1.localName() == "name2" ); |
143 | assertTrue (n2.qname() == "ns:name1" ); |
144 | assertTrue (n2.namespaceURI() == "http://www.appinf.com" ); |
145 | assertTrue (n2.localName() == "name1" ); |
146 | } |
147 | |
148 | |
149 | void NameTest::setUp() |
150 | { |
151 | } |
152 | |
153 | |
154 | void NameTest::tearDown() |
155 | { |
156 | } |
157 | |
158 | |
159 | CppUnit::Test* NameTest::suite() |
160 | { |
161 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NameTest" ); |
162 | |
163 | CppUnit_addTest(pSuite, NameTest, testSplit); |
164 | CppUnit_addTest(pSuite, NameTest, testLocalName); |
165 | CppUnit_addTest(pSuite, NameTest, testPrefix); |
166 | CppUnit_addTest(pSuite, NameTest, testName); |
167 | CppUnit_addTest(pSuite, NameTest, testCompare); |
168 | CppUnit_addTest(pSuite, NameTest, testSwap); |
169 | |
170 | return pSuite; |
171 | } |
172 | |