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