Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/talk/xmllite/xmlnsstack_unittest.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 10.2 KB
Line 
1/*
2 * libjingle
3 * Copyright 2004, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/xmllite/xmlnsstack.h"
29
30#include <string>
31#include <sstream>
32#include <iostream>
33
34#include "talk/base/common.h"
35#include "talk/base/gunit.h"
36#include "talk/xmllite/xmlconstants.h"
37
38using buzz::NS_XML;
39using buzz::NS_XMLNS;
40using buzz::QName;
41using buzz::XmlnsStack;
42
43TEST(XmlnsStackTest, TestBuiltin) {
44  XmlnsStack stack;
45
46  EXPECT_EQ(std::string(NS_XML), stack.NsForPrefix("xml").first);
47  EXPECT_EQ(std::string(NS_XMLNS), stack.NsForPrefix("xmlns").first);
48  EXPECT_EQ("", stack.NsForPrefix("").first);
49
50  EXPECT_EQ("xml", stack.PrefixForNs(NS_XML, false).first);
51  EXPECT_EQ("xmlns", stack.PrefixForNs(NS_XMLNS, false).first);
52  EXPECT_EQ("", stack.PrefixForNs("", false).first);
53  EXPECT_EQ("", stack.PrefixForNs("", true).first);
54}
55
56TEST(XmlnsStackTest, TestNsForPrefix) {
57 XmlnsStack stack;
58  stack.AddXmlns("pre1", "ns1");
59  stack.AddXmlns("pre2", "ns2");
60  stack.AddXmlns("pre1", "ns3");
61  stack.AddXmlns("", "ns4");
62
63  EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first);
64  EXPECT_TRUE(stack.NsForPrefix("pre1").second);
65  EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first);
66  EXPECT_EQ("ns4", stack.NsForPrefix("").first);
67  EXPECT_EQ("", stack.NsForPrefix("pre3").first);
68  EXPECT_FALSE(stack.NsForPrefix("pre3").second);
69}
70
71TEST(XmlnsStackTest, TestPrefixForNs) {
72  XmlnsStack stack;
73  stack.AddXmlns("pre1", "ns1");
74  stack.AddXmlns("pre2", "ns2");
75  stack.AddXmlns("pre1", "ns3");
76  stack.AddXmlns("pre3", "ns2");
77  stack.AddXmlns("pre4", "ns4");
78  stack.AddXmlns("", "ns4");
79
80  EXPECT_EQ("", stack.PrefixForNs("ns1", false).first);
81  EXPECT_FALSE(stack.PrefixForNs("ns1", false).second);
82  EXPECT_EQ("", stack.PrefixForNs("ns1", true).first);
83  EXPECT_FALSE(stack.PrefixForNs("ns1", true).second);
84  EXPECT_EQ("pre3", stack.PrefixForNs("ns2", false).first);
85  EXPECT_TRUE(stack.PrefixForNs("ns2", false).second);
86  EXPECT_EQ("pre3", stack.PrefixForNs("ns2", true).first);
87  EXPECT_TRUE(stack.PrefixForNs("ns2", true).second);
88  EXPECT_EQ("pre1", stack.PrefixForNs("ns3", false).first);
89  EXPECT_EQ("pre1", stack.PrefixForNs("ns3", true).first);
90  EXPECT_EQ("", stack.PrefixForNs("ns4", false).first);
91  EXPECT_TRUE(stack.PrefixForNs("ns4", false).second);
92  EXPECT_EQ("pre4", stack.PrefixForNs("ns4", true).first);
93  EXPECT_EQ("", stack.PrefixForNs("ns5", false).first);
94  EXPECT_FALSE(stack.PrefixForNs("ns5", false).second);
95  EXPECT_EQ("", stack.PrefixForNs("ns5", true).first);
96  EXPECT_EQ("", stack.PrefixForNs("", false).first);
97  EXPECT_EQ("", stack.PrefixForNs("", true).first);
98
99  stack.AddXmlns("", "ns6");
100  EXPECT_EQ("", stack.PrefixForNs("ns6", false).first);
101  EXPECT_TRUE(stack.PrefixForNs("ns6", false).second);
102  EXPECT_EQ("", stack.PrefixForNs("ns6", true).first);
103  EXPECT_FALSE(stack.PrefixForNs("ns6", true).second);
104}
105
106TEST(XmlnsStackTest, TestFrames) {
107  XmlnsStack stack;
108  stack.PushFrame();
109  stack.AddXmlns("pre1", "ns1");
110  stack.AddXmlns("pre2", "ns2");
111
112  stack.PushFrame();
113  stack.AddXmlns("pre1", "ns3");
114  stack.AddXmlns("pre3", "ns2");
115  stack.AddXmlns("pre4", "ns4");
116
117  stack.PushFrame();
118  stack.PushFrame();
119  stack.AddXmlns("", "ns4");
120
121  // basic test
122  EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first);
123  EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first);
124  EXPECT_EQ("ns2", stack.NsForPrefix("pre3").first);
125  EXPECT_EQ("ns4", stack.NsForPrefix("pre4").first);
126  EXPECT_EQ("", stack.NsForPrefix("pre5").first);
127  EXPECT_FALSE(stack.NsForPrefix("pre5").second);
128  EXPECT_EQ("ns4", stack.NsForPrefix("").first);
129  EXPECT_TRUE(stack.NsForPrefix("").second);
130
131  // pop the default xmlns definition
132  stack.PopFrame();
133  EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first);
134  EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first);
135  EXPECT_EQ("ns2", stack.NsForPrefix("pre3").first);
136  EXPECT_EQ("ns4", stack.NsForPrefix("pre4").first);
137  EXPECT_EQ("", stack.NsForPrefix("pre5").first);
138  EXPECT_FALSE(stack.NsForPrefix("pre5").second);
139  EXPECT_EQ("", stack.NsForPrefix("").first);
140  EXPECT_TRUE(stack.NsForPrefix("").second);
141
142  // pop empty frame (nop)
143  stack.PopFrame();
144  EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first);
145  EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first);
146  EXPECT_EQ("ns2", stack.NsForPrefix("pre3").first);
147  EXPECT_EQ("ns4", stack.NsForPrefix("pre4").first);
148  EXPECT_EQ("", stack.NsForPrefix("pre5").first);
149  EXPECT_FALSE(stack.NsForPrefix("pre5").second);
150  EXPECT_EQ("", stack.NsForPrefix("").first);
151  EXPECT_TRUE(stack.NsForPrefix("").second);
152
153  // pop frame with three defs
154  stack.PopFrame();
155  EXPECT_EQ("ns1", stack.NsForPrefix("pre1").first);
156  EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first);
157  EXPECT_EQ("", stack.NsForPrefix("pre3").first);
158  EXPECT_FALSE(stack.NsForPrefix("pre3").second);
159  EXPECT_EQ("", stack.NsForPrefix("pre4").first);
160  EXPECT_FALSE(stack.NsForPrefix("pre4").second);
161  EXPECT_EQ("", stack.NsForPrefix("pre5").first);
162  EXPECT_FALSE(stack.NsForPrefix("pre5").second);
163  EXPECT_EQ("", stack.NsForPrefix("").first);
164  EXPECT_TRUE(stack.NsForPrefix("").second);
165
166  // pop frame with last two defs
167  stack.PopFrame();
168  EXPECT_FALSE(stack.NsForPrefix("pre1").second);
169  EXPECT_FALSE(stack.NsForPrefix("pre2").second);
170  EXPECT_FALSE(stack.NsForPrefix("pre3").second);
171  EXPECT_FALSE(stack.NsForPrefix("pre4").second);
172  EXPECT_FALSE(stack.NsForPrefix("pre5").second);
173  EXPECT_TRUE(stack.NsForPrefix("").second);
174  EXPECT_EQ("", stack.NsForPrefix("pre1").first);
175  EXPECT_EQ("", stack.NsForPrefix("pre2").first);
176  EXPECT_EQ("", stack.NsForPrefix("pre3").first);
177  EXPECT_EQ("", stack.NsForPrefix("pre4").first);
178  EXPECT_EQ("", stack.NsForPrefix("pre5").first);
179  EXPECT_EQ("", stack.NsForPrefix("").first);
180}
181
182TEST(XmlnsStackTest, TestAddNewPrefix) {
183  XmlnsStack stack;
184
185  // builtin namespaces cannot be added
186  EXPECT_FALSE(stack.AddNewPrefix("", true).second);
187  EXPECT_FALSE(stack.AddNewPrefix("", false).second);
188  EXPECT_FALSE(stack.AddNewPrefix(NS_XML, true).second);
189  EXPECT_FALSE(stack.AddNewPrefix(NS_XML, false).second);
190  EXPECT_FALSE(stack.AddNewPrefix(NS_XMLNS, true).second);
191  EXPECT_FALSE(stack.AddNewPrefix(NS_XMLNS, false).second);
192
193  // namespaces already added cannot be added again.
194  EXPECT_EQ("foo", stack.AddNewPrefix("http://a.b.com/foo.htm", true).first);
195  EXPECT_EQ("bare", stack.AddNewPrefix("http://a.b.com/bare", false).first);
196  EXPECT_EQ("z", stack.AddNewPrefix("z", false).first);
197  EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/foo.htm", true).second);
198  EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/bare", true).second);
199  EXPECT_FALSE(stack.AddNewPrefix("z", true).second);
200  EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/foo.htm", false).second);
201  EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/bare", false).second);
202  EXPECT_FALSE(stack.AddNewPrefix("z", false).second);
203
204  // default namespace usable by non-attributes only
205  stack.AddXmlns("", "http://my/default");
206  EXPECT_FALSE(stack.AddNewPrefix("http://my/default", false).second);
207  EXPECT_EQ("def", stack.AddNewPrefix("http://my/default", true).first);
208
209  // namespace cannot start with 'xml'
210  EXPECT_EQ("ns", stack.AddNewPrefix("http://a.b.com/xmltest", true).first);
211  EXPECT_EQ("ns2", stack.AddNewPrefix("xmlagain", false).first);
212
213  // verify added namespaces are still defined
214  EXPECT_EQ("http://a.b.com/foo.htm", stack.NsForPrefix("foo").first);
215  EXPECT_TRUE(stack.NsForPrefix("foo").second);
216  EXPECT_EQ("http://a.b.com/bare", stack.NsForPrefix("bare").first);
217  EXPECT_TRUE(stack.NsForPrefix("bare").second);
218  EXPECT_EQ("z", stack.NsForPrefix("z").first);
219  EXPECT_TRUE(stack.NsForPrefix("z").second);
220  EXPECT_EQ("http://my/default", stack.NsForPrefix("").first);
221  EXPECT_TRUE(stack.NsForPrefix("").second);
222  EXPECT_EQ("http://my/default", stack.NsForPrefix("def").first);
223  EXPECT_TRUE(stack.NsForPrefix("def").second);
224  EXPECT_EQ("http://a.b.com/xmltest", stack.NsForPrefix("ns").first);
225  EXPECT_TRUE(stack.NsForPrefix("ns").second);
226  EXPECT_EQ("xmlagain", stack.NsForPrefix("ns2").first);
227  EXPECT_TRUE(stack.NsForPrefix("ns2").second);
228}
229
230TEST(XmlnsStackTest, TestFormatQName) {
231  XmlnsStack stack;
232  stack.AddXmlns("pre1", "ns1");
233  stack.AddXmlns("pre2", "ns2");
234  stack.AddXmlns("pre1", "ns3");
235  stack.AddXmlns("", "ns4");
236
237  EXPECT_EQ("zip",
238      stack.FormatQName(QName("ns1", "zip"), false));  // no match
239  EXPECT_EQ("pre2:abracadabra",
240      stack.FormatQName(QName("ns2", "abracadabra"), false));
241  EXPECT_EQ("pre1:a",
242      stack.FormatQName(QName("ns3", "a"), false));
243  EXPECT_EQ("simple",
244      stack.FormatQName(QName("ns4", "simple"), false));
245  EXPECT_EQ("root",
246      stack.FormatQName(QName("", "root"), false));  // no match
247
248  EXPECT_EQ("zip",
249      stack.FormatQName(QName("ns1", "zip"), true));  // no match
250  EXPECT_EQ("pre2:abracadabra",
251      stack.FormatQName(QName("ns2", "abracadabra"), true));
252  EXPECT_EQ("pre1:a",
253      stack.FormatQName(QName("ns3", "a"), true));
254  EXPECT_EQ("simple",
255      stack.FormatQName(QName("ns4", "simple"), true));  // no match
256  EXPECT_EQ("root",
257      stack.FormatQName(QName("", "root"), true));
258}
Note: See TracBrowser for help on using the repository browser.