Enhance save as template and new from template dialogs
[inkscape.git] / testfiles / src / xml-test.cpp
blob023fd6e62db0d256353a1a17c02ab1ca74da6d09
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3 * @file
4 * Test xml node
5 */
6 /*
7 * Authors:
8 * Ted Gould
10 * Copyright (C) 2020 Authors
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 #include <list>
16 #include <memory>
17 #include <string>
18 #include <gtest/gtest.h>
19 #include "xml/repr.h"
21 TEST(XmlTest, nodeiter)
23 auto testdoc = std::shared_ptr<Inkscape::XML::Document>(sp_repr_read_buf("<svg><g/></svg>", SP_SVG_NS_URI));
24 ASSERT_TRUE(testdoc);
26 auto count = 0;
27 for (auto &child : *testdoc->root()) {
28 ASSERT_STREQ(child.name(), "svg:g");
29 count++;
31 ASSERT_EQ(count, 1);
33 testdoc =
34 std::shared_ptr<Inkscape::XML::Document>(sp_repr_read_buf("<svg><g/><g/><g><g/></g></svg>", SP_SVG_NS_URI));
35 ASSERT_TRUE(testdoc);
37 count = 0;
38 for (auto &child : *testdoc->root()) {
39 ASSERT_STREQ(child.name(), "svg:g");
40 count++;
42 ASSERT_EQ(count, 3);
44 testdoc = std::shared_ptr<Inkscape::XML::Document>(sp_repr_read_buf(R"""(
45 <svg>
46 <g/>
47 <!-- comment -->
48 <g>
49 <circle/>
50 </g>
51 <g>
52 <circle id='a'/>
53 <path id='b'/>
54 <path id='c'/>
55 </g>
56 </svg>
57 )""", SP_SVG_NS_URI));
58 ASSERT_TRUE(testdoc);
60 auto path = std::list<std::string>{"svg:g", "svg:path"};
61 auto found = testdoc->root()->findChildPath(path);
62 ASSERT_NE(found, nullptr);
63 ASSERT_STREQ(found->attribute("id"), "b");
65 // no such second element
66 path = {"svg:g", "svg:g"};
67 ASSERT_EQ(testdoc->root()->findChildPath(path), nullptr);
69 // no such first element
70 path = {"svg:symbol", "svg:path"};
71 ASSERT_EQ(testdoc->root()->findChildPath(path), nullptr);
73 // root with no children
74 testdoc = std::shared_ptr<Inkscape::XML::Document>(sp_repr_read_buf("<svg/>", SP_SVG_NS_URI));
75 ASSERT_EQ(testdoc->root()->findChildPath(path), nullptr);
78 TEST(XmlQuoteTest, nodeiter)
80 auto testdoc = std::shared_ptr<Inkscape::XML::Document>(sp_repr_read_buf("<svg attr='&lt;foo&#10;bar\n&quot;amp&amp;&gt;'>\nTEXT\n&#10;NODE\n<g><![CDATA[TEST&#10;CDATA]]></g></svg>", SP_SVG_NS_URI));
81 ASSERT_STREQ(testdoc->root()->attribute("attr"), "<foo\nbar \"amp&>");
83 for (auto &child : *testdoc->root()) {
84 ASSERT_STREQ(child.content(), "\nTEXT\n\nNODE\n");
85 break;
88 auto content = sp_repr_save_buf(testdoc.get());
89 ASSERT_STREQ(content.c_str(), R"""(<?xml version="1.0" encoding="UTF-8" standalone="no"?>
90 <svg:svg
91 attr="&lt;foo&#10;bar &quot;amp&amp;&gt;"
92 xmlns:svg="http://www.w3.org/2000/svg">
93 TEXT
95 NODE
96 <svg:g><![CDATA[TEST&#10;CDATA]]></svg:g>
97 </svg:svg>
98 )""");
102 Local Variables:
103 mode:c++
104 c-file-style:"stroustrup"
105 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
106 indent-tabs-mode:nil
107 fill-column:99
108 End:
110 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :