]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 New Dream Network | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include "gtest/gtest.h" | |
16 | #include "common/Formatter.h" | |
17 | #include "common/HTMLFormatter.h" | |
18 | ||
19 | #include <sstream> | |
20 | #include <string> | |
21 | ||
22 | using namespace ceph; | |
23 | using std::ostringstream; | |
24 | ||
25 | TEST(JsonFormatter, Simple1) { | |
26 | ostringstream oss; | |
27 | JSONFormatter fmt(false); | |
28 | fmt.open_object_section("foo"); | |
29 | fmt.dump_int("a", 1); | |
30 | fmt.dump_int("b", 2); | |
31 | fmt.dump_int("c", 3); | |
32 | fmt.close_section(); | |
33 | fmt.flush(oss); | |
34 | ASSERT_EQ(oss.str(), "{\"a\":1,\"b\":2,\"c\":3}"); | |
35 | } | |
36 | ||
37 | TEST(JsonFormatter, Simple2) { | |
38 | ostringstream oss; | |
39 | JSONFormatter fmt(false); | |
40 | fmt.open_object_section("foo"); | |
41 | fmt.open_object_section("bar"); | |
42 | fmt.dump_int("int", 0xf00000000000ll); | |
43 | fmt.dump_unsigned("unsigned", 0x8000000000000001llu); | |
44 | fmt.dump_float("float", 1.234); | |
45 | fmt.close_section(); | |
46 | fmt.dump_string("string", "str"); | |
47 | fmt.close_section(); | |
48 | fmt.flush(oss); | |
49 | ASSERT_EQ(oss.str(), "{\"bar\":{\"int\":263882790666240,\ | |
11fdf7f2 | 50 | \"unsigned\":9223372036854775809,\"float\":1.234},\ |
7c673cae FG |
51 | \"string\":\"str\"}"); |
52 | } | |
53 | ||
11fdf7f2 TL |
54 | TEST(JsonFormatter, CunningFloats) { |
55 | ostringstream oss; | |
56 | JSONFormatter fmt(false); | |
57 | fmt.open_object_section("foo"); | |
58 | fmt.dump_float("long", 1.0 / 7); | |
59 | fmt.dump_float("big", 12345678901234567890.0); | |
60 | fmt.close_section(); | |
61 | fmt.flush(oss); | |
62 | ASSERT_EQ(oss.str(), "{\"long\":0.14285714285714285,\"big\":1.2345678901234567e+19}"); | |
63 | } | |
64 | ||
7c673cae FG |
65 | TEST(JsonFormatter, Empty) { |
66 | ostringstream oss; | |
67 | JSONFormatter fmt(false); | |
68 | fmt.flush(oss); | |
69 | ASSERT_EQ(oss.str(), ""); | |
70 | } | |
71 | ||
72 | TEST(XmlFormatter, Simple1) { | |
73 | ostringstream oss; | |
74 | XMLFormatter fmt(false); | |
75 | fmt.open_object_section("foo"); | |
76 | fmt.dump_int("a", 1); | |
77 | fmt.dump_int("b", 2); | |
78 | fmt.dump_int("c", 3); | |
79 | fmt.close_section(); | |
80 | fmt.flush(oss); | |
81 | ASSERT_EQ(oss.str(), "<foo><a>1</a><b>2</b><c>3</c></foo>"); | |
82 | } | |
83 | ||
84 | TEST(XmlFormatter, Simple2) { | |
85 | ostringstream oss; | |
86 | XMLFormatter fmt(false); | |
87 | fmt.open_object_section("foo"); | |
88 | fmt.open_object_section("bar"); | |
89 | fmt.dump_int("int", 0xf00000000000ll); | |
90 | fmt.dump_unsigned("unsigned", 0x8000000000000001llu); | |
91 | fmt.dump_float("float", 1.234); | |
92 | fmt.close_section(); | |
93 | fmt.dump_string("string", "str"); | |
94 | fmt.close_section(); | |
95 | fmt.flush(oss); | |
96 | ASSERT_EQ(oss.str(), "<foo><bar>\ | |
97 | <int>263882790666240</int>\ | |
98 | <unsigned>9223372036854775809</unsigned>\ | |
99 | <float>1.234</float>\ | |
100 | </bar><string>str</string>\ | |
101 | </foo>"); | |
102 | } | |
103 | ||
104 | TEST(XmlFormatter, Empty) { | |
105 | ostringstream oss; | |
106 | XMLFormatter fmt(false); | |
107 | fmt.flush(oss); | |
108 | ASSERT_EQ(oss.str(), ""); | |
109 | } | |
110 | ||
111 | TEST(XmlFormatter, DumpStream1) { | |
112 | ostringstream oss; | |
113 | XMLFormatter fmt(false); | |
114 | fmt.dump_stream("blah") << "hithere"; | |
115 | fmt.flush(oss); | |
116 | ASSERT_EQ(oss.str(), "<blah>hithere</blah>"); | |
117 | } | |
118 | ||
119 | TEST(XmlFormatter, DumpStream2) { | |
120 | ostringstream oss; | |
121 | XMLFormatter fmt(false); | |
122 | ||
123 | fmt.open_array_section("foo"); | |
124 | fmt.dump_stream("blah") << "hithere"; | |
125 | fmt.close_section(); | |
126 | fmt.flush(oss); | |
127 | ASSERT_EQ(oss.str(), "<foo><blah>hithere</blah></foo>"); | |
128 | } | |
129 | ||
130 | TEST(XmlFormatter, DumpStream3) { | |
131 | ostringstream oss; | |
132 | XMLFormatter fmt(false); | |
133 | ||
134 | fmt.open_array_section("foo"); | |
135 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 136 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
137 | fmt.close_section(); |
138 | fmt.flush(oss); | |
11fdf7f2 | 139 | ASSERT_EQ(oss.str(), "<foo><blah>hithere</blah><pi>0.128</pi></foo>"); |
7c673cae FG |
140 | } |
141 | ||
142 | TEST(XmlFormatter, DTD) { | |
143 | ostringstream oss; | |
144 | XMLFormatter fmt(false); | |
145 | ||
146 | fmt.output_header(); | |
147 | fmt.open_array_section("foo"); | |
148 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 149 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
150 | fmt.close_section(); |
151 | fmt.flush(oss); | |
152 | ASSERT_EQ(oss.str(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
11fdf7f2 | 153 | "<foo><blah>hithere</blah><pi>0.128</pi></foo>"); |
7c673cae FG |
154 | } |
155 | ||
156 | TEST(XmlFormatter, Clear) { | |
157 | ostringstream oss; | |
158 | XMLFormatter fmt(false); | |
159 | ||
160 | fmt.output_header(); | |
161 | fmt.open_array_section("foo"); | |
162 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 163 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
164 | fmt.close_section(); |
165 | fmt.flush(oss); | |
166 | ASSERT_EQ(oss.str(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
11fdf7f2 | 167 | "<foo><blah>hithere</blah><pi>0.128</pi></foo>"); |
7c673cae FG |
168 | |
169 | ostringstream oss2; | |
170 | fmt.flush(oss2); | |
171 | ASSERT_EQ(oss2.str(), ""); | |
172 | ||
173 | ostringstream oss3; | |
174 | fmt.reset(); | |
175 | fmt.flush(oss3); | |
176 | ASSERT_EQ(oss3.str(), ""); | |
177 | } | |
178 | ||
179 | TEST(XmlFormatter, NamespaceTest) { | |
180 | ostringstream oss; | |
181 | XMLFormatter fmt(false); | |
182 | ||
183 | fmt.output_header(); | |
184 | fmt.open_array_section_in_ns("foo", | |
185 | "http://s3.amazonaws.com/doc/2006-03-01/"); | |
186 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 187 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
188 | fmt.close_section(); |
189 | fmt.flush(oss); | |
190 | ASSERT_EQ(oss.str(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
191 | "<foo xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" | |
11fdf7f2 | 192 | "<blah>hithere</blah><pi>0.128</pi></foo>"); |
7c673cae FG |
193 | } |
194 | ||
195 | TEST(XmlFormatter, DumpFormatNameSpaceTest) { | |
196 | ostringstream oss1; | |
197 | XMLFormatter fmt(false); | |
198 | ||
199 | fmt.dump_format_ns("foo", | |
200 | "http://s3.amazonaws.com/doc/2006-03-01/", | |
201 | "%s","bar"); | |
202 | fmt.flush(oss1); | |
203 | ASSERT_EQ(oss1.str(), | |
204 | "<foo xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">bar</foo>"); | |
205 | ||
206 | // Testing with a null ns..should be same as dump format | |
207 | ostringstream oss2; | |
208 | fmt.reset(); | |
209 | fmt.dump_format_ns("foo",NULL,"%s","bar"); | |
210 | fmt.flush(oss2); | |
211 | ASSERT_EQ(oss2.str(),"<foo>bar</foo>"); | |
212 | } | |
213 | ||
214 | TEST(HtmlFormatter, Simple1) { | |
215 | ostringstream oss; | |
216 | HTMLFormatter fmt(false); | |
217 | fmt.open_object_section("foo"); | |
218 | fmt.dump_int("a", 1); | |
219 | fmt.dump_int("b", 2); | |
220 | fmt.dump_int("c", 3); | |
221 | fmt.close_section(); | |
222 | fmt.flush(oss); | |
223 | ASSERT_EQ(oss.str(), "<foo><li>a: 1</li><li>b: 2</li><li>c: 3</li></foo>"); | |
224 | } | |
225 | ||
226 | TEST(HtmlFormatter, Simple2) { | |
227 | ostringstream oss; | |
228 | HTMLFormatter fmt(false); | |
229 | fmt.open_object_section("foo"); | |
230 | fmt.open_object_section("bar"); | |
231 | fmt.dump_int("int", 0xf00000000000ll); | |
232 | fmt.dump_unsigned("unsigned", 0x8000000000000001llu); | |
233 | fmt.dump_float("float", 1.234); | |
234 | fmt.close_section(); | |
235 | fmt.dump_string("string", "str"); | |
236 | fmt.close_section(); | |
237 | fmt.flush(oss); | |
238 | ASSERT_EQ(oss.str(), "<foo><bar>\ | |
239 | <li>int: 263882790666240</li>\ | |
240 | <li>unsigned: 9223372036854775809</li>\ | |
241 | <li>float: 1.234</li>\ | |
242 | </bar><li>string: str</li>\ | |
243 | </foo>"); | |
244 | } | |
245 | ||
246 | TEST(HtmlFormatter, Empty) { | |
247 | ostringstream oss; | |
248 | HTMLFormatter fmt(false); | |
249 | fmt.flush(oss); | |
250 | ASSERT_EQ(oss.str(), ""); | |
251 | } | |
252 | ||
253 | TEST(HtmlFormatter, DumpStream1) { | |
254 | ostringstream oss; | |
255 | HTMLFormatter fmt(false); | |
256 | fmt.dump_stream("blah") << "hithere"; | |
257 | fmt.flush(oss); | |
258 | ASSERT_EQ(oss.str(), "<li>blah: hithere</li>"); | |
259 | } | |
260 | ||
261 | TEST(HtmlFormatter, DumpStream2) { | |
262 | ostringstream oss; | |
263 | HTMLFormatter fmt(false); | |
264 | ||
265 | fmt.open_array_section("foo"); | |
266 | fmt.dump_stream("blah") << "hithere"; | |
267 | fmt.close_section(); | |
268 | fmt.flush(oss); | |
269 | ASSERT_EQ(oss.str(), "<foo><li>blah: hithere</li></foo>"); | |
270 | } | |
271 | ||
272 | TEST(HtmlFormatter, DumpStream3) { | |
273 | ostringstream oss; | |
274 | HTMLFormatter fmt(false); | |
275 | ||
276 | fmt.open_array_section("foo"); | |
277 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 278 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
279 | fmt.close_section(); |
280 | fmt.flush(oss); | |
11fdf7f2 | 281 | ASSERT_EQ(oss.str(), "<foo><li>blah: hithere</li><li>pi: 0.128</li></foo>"); |
7c673cae FG |
282 | } |
283 | ||
284 | TEST(HtmlFormatter, DTD) { | |
285 | ostringstream oss; | |
286 | HTMLFormatter fmt(false); | |
287 | ||
288 | fmt.write_raw_data(HTMLFormatter::XML_1_DTD); | |
289 | fmt.open_array_section("foo"); | |
290 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 291 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
292 | fmt.close_section(); |
293 | fmt.flush(oss); | |
294 | ASSERT_EQ(oss.str(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
11fdf7f2 | 295 | "<foo><li>blah: hithere</li><li>pi: 0.128</li></foo>"); |
7c673cae FG |
296 | } |
297 | ||
298 | TEST(HtmlFormatter, Clear) { | |
299 | ostringstream oss; | |
300 | HTMLFormatter fmt(false); | |
301 | ||
302 | fmt.write_raw_data(HTMLFormatter::XML_1_DTD); | |
303 | fmt.open_array_section("foo"); | |
304 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 305 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
306 | fmt.close_section(); |
307 | fmt.flush(oss); | |
308 | ASSERT_EQ(oss.str(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
11fdf7f2 | 309 | "<foo><li>blah: hithere</li><li>pi: 0.128</li></foo>"); |
7c673cae FG |
310 | |
311 | ostringstream oss2; | |
312 | fmt.flush(oss2); | |
313 | ASSERT_EQ(oss2.str(), ""); | |
314 | ||
315 | ostringstream oss3; | |
316 | fmt.reset(); | |
317 | fmt.flush(oss3); | |
318 | ASSERT_EQ(oss3.str(), ""); | |
319 | } | |
320 | ||
321 | TEST(HtmlFormatter, NamespaceTest) { | |
322 | ostringstream oss; | |
323 | HTMLFormatter fmt(false); | |
324 | ||
325 | fmt.write_raw_data(HTMLFormatter::XML_1_DTD); | |
326 | fmt.open_array_section_in_ns("foo", | |
327 | "http://s3.amazonaws.com/doc/2006-03-01/"); | |
328 | fmt.dump_stream("blah") << "hithere"; | |
11fdf7f2 | 329 | fmt.dump_float("pi", 0.128); |
7c673cae FG |
330 | fmt.close_section(); |
331 | fmt.flush(oss); | |
332 | ASSERT_EQ(oss.str(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
333 | "<foo xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" | |
11fdf7f2 | 334 | "<li>blah: hithere</li><li>pi: 0.128</li></foo>"); |
7c673cae FG |
335 | } |
336 | ||
337 | TEST(HtmlFormatter, DumpFormatNameSpaceTest) { | |
338 | ostringstream oss1; | |
339 | HTMLFormatter fmt(false); | |
340 | ||
341 | fmt.dump_format_ns("foo", | |
342 | "http://s3.amazonaws.com/doc/2006-03-01/", | |
343 | "%s","bar"); | |
344 | fmt.flush(oss1); | |
345 | ASSERT_EQ(oss1.str(), | |
346 | "<li xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">foo: bar</li>"); | |
347 | ||
348 | // Testing with a null ns..should be same as dump format | |
349 | ostringstream oss2; | |
350 | fmt.reset(); | |
351 | fmt.dump_format_ns("foo",NULL,"%s","bar"); | |
352 | fmt.flush(oss2); | |
353 | ASSERT_EQ(oss2.str(),"<li>foo: bar</li>"); | |
354 | } |