1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "gn/xml_element_writer.h"
6 
7 #include <sstream>
8 
9 #include "util/test/test.h"
10 
11 namespace {
12 
13 class MockValueWriter {
14  public:
MockValueWriter(const std::string & value)15   explicit MockValueWriter(const std::string& value) : value_(value) {}
operator ()(std::ostream & out) const16   void operator()(std::ostream& out) const { out << value_; }
17 
18  private:
19   std::string value_;
20 };
21 
22 }  // namespace
23 
TEST(XmlElementWriter,EmptyElement)24 TEST(XmlElementWriter, EmptyElement) {
25   std::ostringstream out;
26   { XmlElementWriter writer(out, "foo", XmlAttributes()); }
27   EXPECT_EQ("<foo />\n", out.str());
28 
29   std::ostringstream out_attr;
30   {
31     XmlElementWriter writer(out_attr, "foo",
32                             XmlAttributes("bar", "abc").add("baz", "123"));
33   }
34   EXPECT_EQ("<foo bar=\"abc\" baz=\"123\" />\n", out_attr.str());
35 
36   std::ostringstream out_indent;
37   {
38     XmlElementWriter writer(out_indent, "foo", XmlAttributes("bar", "baz"), 2);
39   }
40   EXPECT_EQ("  <foo bar=\"baz\" />\n", out_indent.str());
41 
42   std::ostringstream out_writer;
43   {
44     XmlElementWriter writer(out_writer, "foo", "bar", MockValueWriter("baz"),
45                             2);
46   }
47   EXPECT_EQ("  <foo bar=\"baz\" />\n", out_writer.str());
48 }
49 
TEST(XmlElementWriter,ElementWithText)50 TEST(XmlElementWriter, ElementWithText) {
51   std::ostringstream out;
52   {
53     XmlElementWriter writer(out, "foo", XmlAttributes("bar", "baz"));
54     writer.Text("Hello world!");
55   }
56   EXPECT_EQ("<foo bar=\"baz\">Hello world!</foo>\n", out.str());
57 }
58 
TEST(XmlElementWriter,SubElements)59 TEST(XmlElementWriter, SubElements) {
60   std::ostringstream out;
61   {
62     XmlElementWriter writer(out, "root", XmlAttributes("aaa", "000"));
63     writer.SubElement("foo", XmlAttributes());
64     writer.SubElement("bar", XmlAttributes("bbb", "111"))->Text("hello");
65     writer.SubElement("baz", "ccc", MockValueWriter("222"))
66         ->SubElement("grandchild");
67   }
68   std::string expected =
69       "<root aaa=\"000\">\n"
70       "  <foo />\n"
71       "  <bar bbb=\"111\">hello</bar>\n"
72       "  <baz ccc=\"222\">\n"
73       "    <grandchild />\n"
74       "  </baz>\n"
75       "</root>\n";
76   EXPECT_EQ(expected, out.str());
77 }
78 
TEST(XmlElementWriter,StartContent)79 TEST(XmlElementWriter, StartContent) {
80   std::ostringstream out;
81   {
82     XmlElementWriter writer(out, "foo", XmlAttributes("bar", "baz"));
83     writer.StartContent(false) << "Hello world!";
84   }
85   EXPECT_EQ("<foo bar=\"baz\">Hello world!</foo>\n", out.str());
86 }
87 
TEST(XmlElementWriter,TestXmlEscape)88 TEST(XmlElementWriter, TestXmlEscape) {
89   std::string input = "\r \n \t & < > \"";
90   std::string output = XmlEscape(input);
91   std::string expected = "&#13; &#10; &#9; &amp; &lt; &gt; &quot;";
92   EXPECT_EQ(expected, output);
93 }
94