3
* Copyright 2004, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
31
#include "talk/base/common.h"
32
#include "talk/base/gunit.h"
33
#include "talk/xmllite/xmlbuilder.h"
34
#include "talk/xmllite/xmlelement.h"
35
#include "talk/xmllite/xmlparser.h"
37
using buzz::XmlBuilder;
38
using buzz::XmlElement;
39
using buzz::XmlParser;
41
TEST(XmlBuilderTest, TestTrivial) {
43
XmlParser::ParseXml(&builder, "<testing/>");
44
EXPECT_EQ("<testing/>", builder.BuiltElement()->Str());
47
TEST(XmlBuilderTest, TestAttributes1) {
49
XmlParser::ParseXml(&builder, "<testing a='b'/>");
50
EXPECT_EQ("<testing a=\"b\"/>", builder.BuiltElement()->Str());
53
TEST(XmlBuilderTest, TestAttributes2) {
55
XmlParser::ParseXml(&builder, "<testing e='' long='some text'/>");
56
EXPECT_EQ("<testing e=\"\" long=\"some text\"/>",
57
builder.BuiltElement()->Str());
60
TEST(XmlBuilderTest, TestNesting1) {
62
XmlParser::ParseXml(&builder,
63
"<top><first/><second><third></third></second></top>");
64
EXPECT_EQ("<top><first/><second><third/></second></top>",
65
builder.BuiltElement()->Str());
68
TEST(XmlBuilderTest, TestNesting2) {
70
XmlParser::ParseXml(&builder,
71
"<top><fifth><deeper><and><deeper/></and><sibling><leaf/>"
72
"</sibling></deeper></fifth><first/><second><third></third>"
74
EXPECT_EQ("<top><fifth><deeper><and><deeper/></and><sibling><leaf/>"
75
"</sibling></deeper></fifth><first/><second><third/>"
76
"</second></top>", builder.BuiltElement()->Str());
79
TEST(XmlBuilderTest, TestQuoting1) {
81
XmlParser::ParseXml(&builder, "<testing a='>'/>");
82
EXPECT_EQ("<testing a=\">\"/>", builder.BuiltElement()->Str());
85
TEST(XmlBuilderTest, TestQuoting2) {
87
XmlParser::ParseXml(&builder, "<testing a='<>&"'/>");
88
EXPECT_EQ("<testing a=\"<>&"\"/>",
89
builder.BuiltElement()->Str());
92
TEST(XmlBuilderTest, TestQuoting3) {
94
XmlParser::ParseXml(&builder, "<testing a='so "important"'/>");
95
EXPECT_EQ("<testing a=\"so "important"\"/>",
96
builder.BuiltElement()->Str());
99
TEST(XmlBuilderTest, TestQuoting4) {
101
XmlParser::ParseXml(&builder, "<testing a='"important", yes'/>");
102
EXPECT_EQ("<testing a=\""important", yes\"/>",
103
builder.BuiltElement()->Str());
106
TEST(XmlBuilderTest, TestQuoting5) {
108
XmlParser::ParseXml(&builder,
109
"<testing a='<what is "important">'/>");
110
EXPECT_EQ("<testing a=\"<what is "important">\"/>",
111
builder.BuiltElement()->Str());
114
TEST(XmlBuilderTest, TestText1) {
116
XmlParser::ParseXml(&builder, "<testing>></testing>");
117
EXPECT_EQ("<testing>></testing>", builder.BuiltElement()->Str());
120
TEST(XmlBuilderTest, TestText2) {
122
XmlParser::ParseXml(&builder, "<testing><>&"</testing>");
123
EXPECT_EQ("<testing><>&\"</testing>",
124
builder.BuiltElement()->Str());
127
TEST(XmlBuilderTest, TestText3) {
129
XmlParser::ParseXml(&builder, "<testing>so <important></testing>");
130
EXPECT_EQ("<testing>so <important></testing>",
131
builder.BuiltElement()->Str());
134
TEST(XmlBuilderTest, TestText4) {
136
XmlParser::ParseXml(&builder, "<testing><important>, yes</testing>");
137
EXPECT_EQ("<testing><important>, yes</testing>",
138
builder.BuiltElement()->Str());
141
TEST(XmlBuilderTest, TestText5) {
143
XmlParser::ParseXml(&builder,
144
"<testing>importance &<important>&</testing>");
145
EXPECT_EQ("<testing>importance &<important>&</testing>",
146
builder.BuiltElement()->Str());
149
TEST(XmlBuilderTest, TestNamespace1) {
151
XmlParser::ParseXml(&builder, "<testing xmlns='foo'/>");
152
EXPECT_EQ("<testing xmlns=\"foo\"/>", builder.BuiltElement()->Str());
155
TEST(XmlBuilderTest, TestNamespace2) {
157
XmlParser::ParseXml(&builder, "<testing xmlns:a='foo' a:b='c'/>");
158
EXPECT_EQ("<testing xmlns:a=\"foo\" a:b=\"c\"/>",
159
builder.BuiltElement()->Str());
162
TEST(XmlBuilderTest, TestNamespace3) {
164
XmlParser::ParseXml(&builder, "<testing xmlns:a=''/>");
165
EXPECT_TRUE(NULL == builder.BuiltElement());
168
TEST(XmlBuilderTest, TestNamespace4) {
170
XmlParser::ParseXml(&builder, "<testing a:b='c'/>");
171
EXPECT_TRUE(NULL == builder.BuiltElement());
174
TEST(XmlBuilderTest, TestAttrCollision1) {
176
XmlParser::ParseXml(&builder, "<testing a='first' a='second'/>");
177
EXPECT_TRUE(NULL == builder.BuiltElement());
180
TEST(XmlBuilderTest, TestAttrCollision2) {
182
XmlParser::ParseXml(&builder,
183
"<testing xmlns:a='foo' xmlns:b='foo' a:x='c' b:x='d'/>");
184
EXPECT_TRUE(NULL == builder.BuiltElement());
187
TEST(XmlBuilderTest, TestAttrCollision3) {
189
XmlParser::ParseXml(&builder,
190
"<testing xmlns:a='foo'><nested xmlns:b='foo' a:x='c' b:x='d'/>"
192
EXPECT_TRUE(NULL == builder.BuiltElement());