summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_xmlformatter.cc
blob: 9ac6dde456e831d2e20e5217579ed1b6d93a9065 (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
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
#include "gtest/gtest.h"

#include "common/Formatter.h"
#include <sstream>
#include <string>

using namespace ceph;


TEST(xmlformatter, oneline)
{

  std::stringstream sout;
  XMLFormatter formatter;
  formatter.dump_int("integer", 10);
  formatter.dump_float("float", 10.0);
  formatter.dump_string("string", "string");
  formatter.flush(sout);
  std::string cmp = "<integer>10</integer><float>10</float><string>string</string>";
  EXPECT_EQ(cmp, sout.str());
}

TEST(xmlformatter, multiline)
{
  std::stringstream sout;
  XMLFormatter formatter;
  formatter.dump_int("integer", 10);
  formatter.dump_float("float", 10.0);
  formatter.dump_string("string", "string");
  formatter.dump_int("integer", 20);
  formatter.dump_float("float", 20.0);
  formatter.dump_string("string", "string");

  std::string cmp = ""
    "<integer>10</integer><float>10</float><string>string</string>"
    "<integer>20</integer><float>20</float><string>string</string>";

  formatter.flush(sout);
  EXPECT_EQ(cmp, sout.str());
}

TEST(xmlformatter, multiflush)
{
  std::stringstream sout1;
  std::stringstream sout2;
  XMLFormatter formatter;
  formatter.dump_int("integer", 10);
  formatter.dump_float("float", 10.0);
  formatter.dump_string("string", "string");
  formatter.flush(sout1);

  std::string cmp = ""
    "<integer>10</integer>"
    "<float>10</float>"
    "<string>string</string>";

  EXPECT_EQ(cmp, sout1.str());

  formatter.dump_int("integer", 20);
  formatter.dump_float("float", 20.0);
  formatter.dump_string("string", "string");
  formatter.flush(sout2);

  cmp = ""
    "<integer>20</integer>"
    "<float>20</float>"
    "<string>string</string>";

  EXPECT_EQ(cmp, sout2.str());
}

TEST(xmlformatter, pretty)
{
  std::stringstream sout;
  XMLFormatter formatter(
      true,   // pretty
      false,   // lowercased
      false); // underscored
  formatter.open_object_section("xml");
  formatter.dump_int("Integer", 10);
  formatter.dump_float("Float", 10.0);
  formatter.dump_string("String", "String");
  formatter.close_section();
  formatter.flush(sout);
  std::string cmp = ""
    "<xml>\n"
    " <Integer>10</Integer>\n"
    " <Float>10</Float>\n"
    " <String>String</String>\n"
    "</xml>\n\n";
  EXPECT_EQ(cmp, sout.str());
}

TEST(xmlformatter, lowercased)
{
  std::stringstream sout;
  XMLFormatter formatter(
      false,  // pretty
      true,   // lowercased
      false); // underscored
  formatter.dump_int("Integer", 10);
  formatter.dump_float("Float", 10.0);
  formatter.dump_string("String", "String");
  formatter.flush(sout);
  std::string cmp = ""
    "<integer>10</integer>"
    "<float>10</float>"
    "<string>String</string>";
  EXPECT_EQ(cmp, sout.str());
}

TEST(xmlformatter, underscored)
{
  std::stringstream sout;
  XMLFormatter formatter(
      false,  // pretty
      false,   // lowercased
      true); // underscored
  formatter.dump_int("Integer Item", 10);
  formatter.dump_float("Float Item", 10.0);
  formatter.dump_string("String Item", "String");
  formatter.flush(sout);
  std::string cmp = ""
    "<Integer_Item>10</Integer_Item>"
    "<Float_Item>10</Float_Item>"
    "<String_Item>String</String_Item>";

  EXPECT_EQ(cmp, sout.str());
}

TEST(xmlformatter, lowercased_underscored)
{
  std::stringstream sout;
  XMLFormatter formatter(
      false,  // pretty
      true,   // lowercased
      true); // underscored
  formatter.dump_int("Integer Item", 10);
  formatter.dump_float("Float Item", 10.0);
  formatter.dump_string("String Item", "String");
  formatter.flush(sout);
  std::string cmp = ""
    "<integer_item>10</integer_item>"
    "<float_item>10</float_item>"
    "<string_item>String</string_item>";
  EXPECT_EQ(cmp, sout.str());
}

TEST(xmlformatter, pretty_lowercased_underscored)
{
  std::stringstream sout;
  XMLFormatter formatter(
      true,  // pretty
      true,   // lowercased
      true); // underscored
  formatter.dump_int("Integer Item", 10);
  formatter.dump_float("Float Item", 10.0);
  formatter.dump_string("String Item", "String");
  formatter.flush(sout);
  std::string cmp = ""
    "<integer_item>10</integer_item>\n"
    "<float_item>10</float_item>\n"
    "<string_item>String</string_item>\n\n";
  EXPECT_EQ(cmp, sout.str());
}