aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/xml/document/xml-document_ut.cpp
blob: a8cfb45117d9f48825b60ea68a8e5b7961eb7ebc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <library/cpp/testing/unittest/registar.h>
#include <util/generic/map.h>
 
#include "xml-document.h" 
 
Y_UNIT_TEST_SUITE(TestXmlDocument) {
    Y_UNIT_TEST(Iteration) {
        NXml::TDocument xml(
            "<?xml version=\"1.0\"?>\n"
            "<root>qq<a><b></b></a>ww<c></c></root>",
            NXml::TDocument::String);

        NXml::TConstNode root = xml.Root();
        UNIT_ASSERT_EQUAL(root.Name(), "root");
        NXml::TConstNode n = root.FirstChild().NextSibling();
        UNIT_ASSERT_EQUAL(n.Name(), "a");
        n = n.NextSibling().NextSibling();
        UNIT_ASSERT_EQUAL(n.Name(), "c");
    }

    Y_UNIT_TEST(ParseString) {
        NXml::TDocument xml( 
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n"
            "<text>Некоторый текст</text>\n"
            "</root>",
            NXml::TDocument::String);
 
        NXml::TConstNode root = xml.Root(); 
        NXml::TConstNode b = root.Node("a/b"); 
        UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15); 
        UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true); 
 
        NXml::TConstNode text = root.Node("text"); 
        UNIT_ASSERT_EQUAL(text.Value<TString>(), "Некоторый текст");
    } 
    Y_UNIT_TEST(SerializeString) {
        NXml::TDocument xml("frob", NXml::TDocument::RootName); 
        xml.Root().SetAttr("xyzzy", "Frobozz"); 
        xml.Root().SetAttr("kulness", 0.3); 
        xml.Root().SetAttr("timelimit", 3); 
 
        NXml::TNode authors = xml.Root().AddChild("authors"); 
        authors.AddChild("graham").SetAttr("name", "Nelson"); 
        authors.AddChild("zarf").SetValue("Andrew Plotkin"); 
        authors.AddChild("emshort", "Emily Short"); 
 
        TString data = xml.ToString("utf-8");
        UNIT_ASSERT_EQUAL(data, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
                                "<frob xyzzy=\"Frobozz\" kulness=\"0.3\" timelimit=\"3\">\n" 
                                "  <authors>\n"
                                "    <graham name=\"Nelson\"/>\n"
                                "    <zarf>Andrew Plotkin</zarf>\n"
                                "    <emshort>Emily Short</emshort>\n" 
                                "  </authors>\n"
                                "</frob>\n");
        // check default utf8 output with ru
        {
            NXml::TDocument xml2("frob", NXml::TDocument::RootName);
            xml2.Root().SetAttr("xyzzy", "привет =)");
            UNIT_ASSERT_VALUES_EQUAL(xml2.ToString(), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                                                      "<frob xyzzy=\"привет =)\"/>\n");
        }
    } 
    Y_UNIT_TEST(XPathNs) {
        using namespace NXml;
        TDocument xml(
            "<?xml version=\"1.0\"?>\n"
            "<root xmlns='http://hello.com/hello'>\n"
            "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n"
            "<text>Некоторый текст</text>\n"
            "</root>",
            TDocument::String);

        TNamespacesForXPath nss;
        TNamespaceForXPath ns = {"h", "http://hello.com/hello"};
        nss.push_back(ns);

        TConstNode root = xml.Root();
        TConstNode b = root.Node("h:a/h:b", false, nss);
        UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15);
        UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true);

        TConstNode text = root.Node("h:text", false, nss);
        UNIT_ASSERT_EQUAL(text.Value<TString>(), "Некоторый текст");

        // For performance you can create xpath context once using nss and pass it.
        TXPathContextPtr ctxt = root.CreateXPathContext(nss);
        UNIT_ASSERT(root.Node("text", true, *ctxt).IsNull());
        UNIT_ASSERT_EXCEPTION(root.Node("text", false, *ctxt), yexception);
        UNIT_ASSERT_EQUAL(root.Node("h:text", false, *ctxt).Value<TString>(), "Некоторый текст");
    }
    Y_UNIT_TEST(XmlNodes) {
        using namespace NXml;
        TDocument xml("<?xml version=\"1.0\"?>\n"
                      "<root>qq<a><b>asdfg</b></a>ww<c></c></root>",
                      NXml::TDocument::String);
        TNode root = xml.Root();
        UNIT_ASSERT_EQUAL(root.Value<TString>(), "qqasdfgww");
        TConstNode node = root.FirstChild();
        UNIT_ASSERT_EQUAL(node.IsText(), true);
        UNIT_ASSERT_EQUAL(node.Value<TString>(), "qq");
        node = node.NextSibling();
        UNIT_ASSERT_EQUAL(node.IsText(), false);
        UNIT_ASSERT_EQUAL(node.Name(), "a");
        UNIT_ASSERT_EQUAL(node.Value<TString>(), "asdfg");
        node = node.NextSibling();
        UNIT_ASSERT_EQUAL(node.IsText(), true);
        UNIT_ASSERT_EQUAL(node.Value<TString>(), "ww");
        node = node.NextSibling();
        UNIT_ASSERT_EQUAL(node.IsText(), false);
        UNIT_ASSERT_EQUAL(node.Name(), "c");
        UNIT_ASSERT_EQUAL(node.Value<TString>(), "");
        node = node.NextSibling();
        UNIT_ASSERT_EQUAL(node.IsNull(), true);
        TStringStream iterLog; 
        for (const auto& node2 : root.Nodes("/root/*")) {
            iterLog << node2.Name() << ';';
        } 
        UNIT_ASSERT_STRINGS_EQUAL(iterLog.Str(), "a;c;"); 

        // get only element nodes, ignore text nodes with empty "name" param
        node = root.FirstChild(TString());
        UNIT_ASSERT_EQUAL(node.IsText(), false);
        UNIT_ASSERT_EQUAL(node.Name(), "a");
        node = node.NextSibling(TString());
        UNIT_ASSERT_EQUAL(node.IsText(), false);
        UNIT_ASSERT_EQUAL(node.Name(), "c");

        // use exact "name" to retrieve children and siblings
        node = root.FirstChild("a");
        UNIT_ASSERT_EQUAL(node.IsNull(), false);
        UNIT_ASSERT_EQUAL(node.Name(), "a");
        node = node.NextSibling("c");
        UNIT_ASSERT_EQUAL(node.IsNull(), false);
        UNIT_ASSERT_EQUAL(node.Name(), "c");
        node = root.FirstChild("c"); // skip "a"
        UNIT_ASSERT_EQUAL(node.IsNull(), false);
        UNIT_ASSERT_EQUAL(node.Name(), "c");

        // node not found: no exceptions, null nodes are returned
        node = root.FirstChild("b"); // b is not direct child of root
        UNIT_ASSERT_EQUAL(node.IsNull(), true);
        node = root.FirstChild("nosuchnode");
        UNIT_ASSERT_EQUAL(node.IsNull(), true);
        node = root.FirstChild();
        node = root.NextSibling("unknownnode");
        UNIT_ASSERT_EQUAL(node.IsNull(), true);
        UNIT_ASSERT_EXCEPTION(node.Name(), yexception);
        UNIT_ASSERT_EXCEPTION(node.Value<TString>(), yexception);
        UNIT_ASSERT_EXCEPTION(node.IsText(), yexception);
    }
    Y_UNIT_TEST(DefVal) {
        using namespace NXml; 
        TDocument xml("<?xml version=\"1.0\"?>\n" 
                      "<root><a></a></root>",
                      NXml::TDocument::String);
        UNIT_ASSERT_EQUAL(xml.Root().Node("a", true).Node("b", true).Value<int>(3), 3); 
    } 
    Y_UNIT_TEST(NodesVsXPath) {
        using namespace NXml; 
        TDocument xml("<?xml version=\"1.0\"?>\n" 
                      "<root><a x=\"y\"></a></root>",
                      NXml::TDocument::String);
        UNIT_ASSERT_EXCEPTION(xml.Root().Nodes("/root/a/@x"), yexception); 
        UNIT_ASSERT_VALUES_EQUAL(xml.Root().XPath("/root/a/@x").Size(), 1); 
    } 
    Y_UNIT_TEST(NodeIsFirst) {
        using namespace NXml; 
        TDocument xml("<?xml version=\"1.0\"?>\n" 
                      "<root><a x=\"y\">first</a>" 
                      "<a>second</a></root>",
                      NXml::TDocument::String);
        UNIT_ASSERT_EXCEPTION(xml.Root().Node("/root/a/@x"), yexception); 
        UNIT_ASSERT_STRINGS_EQUAL(xml.Root().Node("/root/a").Value<TString>(), "first");
    } 
    Y_UNIT_TEST(CopyNode) {
        using namespace NXml;
        // default-construct empty node
        TNode empty;
        // put to container
        TMap<int, TNode> nmap;
        nmap[2];

        // do copy
        TDocument xml("<?xml version=\"1.0\"?>\n"
                      "<root><a></a></root>",
                      TDocument::String);

        TDocument xml2("<?xml version=\"1.0\"?>\n"
                       "<root><node><b>bold</b><i>ita</i></node></root>",
                       TDocument::String);

        TNode node = xml2.Root().Node("//node");
        TNode place = xml.Root().Node("//a");

        place.AddChild(node);

        TStringStream s;
        xml.Save(s, "", false);
        UNIT_ASSERT_VALUES_EQUAL(s.Str(),
                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                                 "<root><a><node><b>bold</b><i>ita</i></node></a></root>\n");
    }

    Y_UNIT_TEST(RenderNode) {
        using namespace NXml;
        {
            // no namespaces
            TDocument xml(
                "<?xml version=\"1.0\"?>\n"
                "<root>\n"
                "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n"
                "<text>Некоторый текст</text>\n"
                "</root>",
                TDocument::String);
            TNode n = xml.Root().Node("//a");
            UNIT_ASSERT_VALUES_EQUAL(n.ToString(), "<a><b len=\"15\" correct=\"1\">hello world</b></a>");
        }
        {
            // namespaces
            TDocument xml(
                "<?xml version=\"1.0\"?>\n"
                "<root xmlns='http://hello.com/hello'>\n"
                "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n"
                "<text>Некоторый текст</text>\n"
                "</root>",
                TDocument::String);
            TNamespacesForXPath nss;
            TNamespaceForXPath ns = {"h", "http://hello.com/hello"};
            nss.push_back(ns);

            TNode n = xml.Root().Node("//h:a", false, nss);
            UNIT_ASSERT_VALUES_EQUAL(n.ToString(), "<a><b len=\"15\" correct=\"1\">hello world</b></a>");
        }
    }

    Y_UNIT_TEST(ReuseXPathContext) {
        using namespace NXml;

        TDocument xml(
            "<?xml version=\"1.0\"?>\n"
            "<root>\n"
            "<a><b><c>Hello, world!</c></b></a>\n"
            "<text x=\"10\">First</text>\n"
            "<text y=\"20\">Second</text>\n"
            "</root>",
            TDocument::String);

        TXPathContextPtr rootCtxt = xml.Root().CreateXPathContext();

        // Check Node()
        TConstNode b = xml.Root().Node("a/b", false, *rootCtxt);

        // We can use root node context for xpath evaluation in any node
        TConstNode c1 = b.Node("c", false, *rootCtxt);
        UNIT_ASSERT_EQUAL(c1.Value<TString>(), "Hello, world!");

        TXPathContextPtr bCtxt = b.CreateXPathContext();
        TConstNode c2 = b.Node("c", false, *bCtxt);
        UNIT_ASSERT_EQUAL(c2.Value<TString>(), "Hello, world!");

        // Mixing contexts from different documents is forbidden
        TDocument otherXml("<root></root>", TDocument::String);
        TXPathContextPtr otherCtxt = otherXml.Root().CreateXPathContext();
        UNIT_ASSERT_EXCEPTION(b.Node("c", false, *otherCtxt), yexception);

        // Check Nodes()
        TConstNodes texts = xml.Root().Nodes("text", true, *rootCtxt);
        UNIT_ASSERT_EQUAL(texts.Size(), 2);

        // Nodes() does't work for non-element nodes
        UNIT_ASSERT_EXCEPTION(xml.Root().Nodes("text/@x", true, *rootCtxt), yexception);

        // Check XPath()
        TConstNodes ys = xml.Root().XPath("text/@y", true, *rootCtxt);
        UNIT_ASSERT_EQUAL(ys.Size(), 1);
        UNIT_ASSERT_EQUAL(ys[0].Value<int>(), 20);
    }

    Y_UNIT_TEST(Html) {
        using namespace NXml;

        TDocument htmlChunk("video", TDocument::RootName);
        TNode videoNode = htmlChunk.Root();

        videoNode.SetAttr("controls");

        TStringStream ss;
        videoNode.SaveAsHtml(ss);
        UNIT_ASSERT_EQUAL(ss.Str(), "<video controls></video>");
    }

    Y_UNIT_TEST(Move) {
        using namespace NXml;

        TDocument xml1("foo", TDocument::RootName);
        xml1.Root().AddChild("bar");

        UNIT_ASSERT_VALUES_EQUAL(xml1.Root().ToString(), "<foo><bar/></foo>");

        TDocument xml2 = std::move(xml1);
        UNIT_ASSERT_EXCEPTION(xml1.Root(), yexception);
        UNIT_ASSERT_VALUES_EQUAL(xml2.Root().ToString(), "<foo><bar/></foo>");
    }
 
    Y_UNIT_TEST(StringConversion) { 
        using namespace NXml; 
        TDocument xml("foo", TDocument::RootName); 
        auto root = xml.Root(); 
        const TStringBuf stringBuf = "bar"; 
        root.SetAttr("bar", stringBuf); 
        const TString tString = "baz"; 
        root.SetAttr("baz", tString); 
        root.SetAttr("quux", "literal"); 
        root.SetAttr("frob", 500); 
    } 
}