summaryrefslogtreecommitdiffstats
path: root/tools/gn/xml_element_writer_unittest.cc
blob: 93dfd47585130b5df9fa0e443519f37da7a33458 (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/gn/xml_element_writer.h"

#include <sstream>

#include "testing/gtest/include/gtest/gtest.h"

namespace {

class MockValueWriter {
 public:
  explicit MockValueWriter(const std::string& value) : value_(value) {}
  void operator()(std::ostream& out) const { out << value_; }

 private:
  std::string value_;
};

}  // namespace

TEST(XmlElementWriter, EmptyElement) {
  std::ostringstream out;
  { XmlElementWriter writer(out, "foo", XmlAttributes()); }
  EXPECT_EQ("<foo />\n", out.str());

  std::ostringstream out_attr;
  {
    XmlElementWriter writer(out_attr, "foo",
                            XmlAttributes("bar", "abc").add("baz", "123"));
  }
  EXPECT_EQ("<foo bar=\"abc\" baz=\"123\" />\n", out_attr.str());

  std::ostringstream out_indent;
  {
    XmlElementWriter writer(out_indent, "foo", XmlAttributes("bar", "baz"), 2);
  }
  EXPECT_EQ("  <foo bar=\"baz\" />\n", out_indent.str());

  std::ostringstream out_writer;
  {
    XmlElementWriter writer(out_writer, "foo", "bar", MockValueWriter("baz"),
                            2);
  }
  EXPECT_EQ("  <foo bar=\"baz\" />\n", out_writer.str());
}

TEST(XmlElementWriter, ElementWithText) {
  std::ostringstream out;
  {
    XmlElementWriter writer(out, "foo", XmlAttributes("bar", "baz"));
    writer.Text("Hello world!");
  }
  EXPECT_EQ("<foo bar=\"baz\">Hello world!</foo>\n", out.str());
}

TEST(XmlElementWriter, SubElements) {
  std::ostringstream out;
  {
    XmlElementWriter writer(out, "root", XmlAttributes("aaa", "000"));
    writer.SubElement("foo", XmlAttributes());
    writer.SubElement("bar", XmlAttributes("bbb", "111"))->Text("hello");
    writer.SubElement("baz", "ccc", MockValueWriter("222"))
        ->SubElement("grandchild");
  }
  std::string expected =
      "<root aaa=\"000\">\n"
      "  <foo />\n"
      "  <bar bbb=\"111\">hello</bar>\n"
      "  <baz ccc=\"222\">\n"
      "    <grandchild />\n"
      "  </baz>\n"
      "</root>\n";
  EXPECT_EQ(expected, out.str());
}

TEST(XmlElementWriter, StartContent) {
  std::ostringstream out;
  {
    XmlElementWriter writer(out, "foo", XmlAttributes("bar", "baz"));
    writer.StartContent(false) << "Hello world!";
  }
  EXPECT_EQ("<foo bar=\"baz\">Hello world!</foo>\n", out.str());
}