blob: 372d7c2684e86d6093092c72aa1c79a82affc067 [file] [log] [blame]
Nick Kledzikf60a9272012-12-12 20:46:15 +00001//===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +000010#include "llvm/ADT/StringRef.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000011#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
Zachary Turner4fbf61d2016-06-07 19:32:09 +000013#include "llvm/Support/Endian.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000014#include "llvm/Support/Format.h"
15#include "llvm/Support/YAMLTraits.h"
16#include "gtest/gtest.h"
17
Nick Kledzikf60a9272012-12-12 20:46:15 +000018using llvm::yaml::Input;
19using llvm::yaml::Output;
20using llvm::yaml::IO;
21using llvm::yaml::MappingTraits;
22using llvm::yaml::MappingNormalization;
23using llvm::yaml::ScalarTraits;
24using llvm::yaml::Hex8;
25using llvm::yaml::Hex16;
26using llvm::yaml::Hex32;
27using llvm::yaml::Hex64;
28
29
Nick Kledzik7cd45f22013-11-21 00:28:07 +000030
31
32static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
33}
34
35
36
Nick Kledzikf60a9272012-12-12 20:46:15 +000037//===----------------------------------------------------------------------===//
38// Test MappingTraits
39//===----------------------------------------------------------------------===//
40
41struct FooBar {
42 int foo;
43 int bar;
44};
45typedef std::vector<FooBar> FooBarSequence;
46
47LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
48
Justin Bogner64d2cdf2015-03-02 17:26:43 +000049struct FooBarContainer {
50 FooBarSequence fbs;
51};
Nick Kledzikf60a9272012-12-12 20:46:15 +000052
53namespace llvm {
54namespace yaml {
55 template <>
56 struct MappingTraits<FooBar> {
57 static void mapping(IO &io, FooBar& fb) {
58 io.mapRequired("foo", fb.foo);
59 io.mapRequired("bar", fb.bar);
60 }
61 };
Justin Bogner64d2cdf2015-03-02 17:26:43 +000062
63 template <> struct MappingTraits<FooBarContainer> {
64 static void mapping(IO &io, FooBarContainer &fb) {
65 io.mapRequired("fbs", fb.fbs);
66 }
67 };
Nick Kledzikf60a9272012-12-12 20:46:15 +000068}
69}
70
71
72//
73// Test the reading of a yaml mapping
74//
75TEST(YAMLIO, TestMapRead) {
76 FooBar doc;
Alexander Kornienko681e37c2013-11-18 15:50:04 +000077 {
78 Input yin("---\nfoo: 3\nbar: 5\n...\n");
79 yin >> doc;
Nick Kledzikf60a9272012-12-12 20:46:15 +000080
Alexander Kornienko681e37c2013-11-18 15:50:04 +000081 EXPECT_FALSE(yin.error());
82 EXPECT_EQ(doc.foo, 3);
83 EXPECT_EQ(doc.bar, 5);
84 }
85
86 {
87 Input yin("{foo: 3, bar: 5}");
88 yin >> doc;
89
90 EXPECT_FALSE(yin.error());
91 EXPECT_EQ(doc.foo, 3);
92 EXPECT_EQ(doc.bar, 5);
93 }
Nick Kledzikf60a9272012-12-12 20:46:15 +000094}
95
Rafael Espindolaa97373f2014-08-08 13:58:00 +000096TEST(YAMLIO, TestMalformedMapRead) {
97 FooBar doc;
98 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
99 yin >> doc;
100 EXPECT_TRUE(!!yin.error());
101}
102
Nick Kledzikf60a9272012-12-12 20:46:15 +0000103//
104// Test the reading of a yaml sequence of mappings
105//
106TEST(YAMLIO, TestSequenceMapRead) {
107 FooBarSequence seq;
108 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
109 yin >> seq;
110
111 EXPECT_FALSE(yin.error());
112 EXPECT_EQ(seq.size(), 2UL);
113 FooBar& map1 = seq[0];
114 FooBar& map2 = seq[1];
115 EXPECT_EQ(map1.foo, 3);
116 EXPECT_EQ(map1.bar, 5);
117 EXPECT_EQ(map2.foo, 7);
118 EXPECT_EQ(map2.bar, 9);
119}
120
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000121//
122// Test the reading of a map containing a yaml sequence of mappings
123//
124TEST(YAMLIO, TestContainerSequenceMapRead) {
125 {
126 FooBarContainer cont;
127 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
128 yin2 >> cont;
129
130 EXPECT_FALSE(yin2.error());
131 EXPECT_EQ(cont.fbs.size(), 2UL);
132 EXPECT_EQ(cont.fbs[0].foo, 3);
133 EXPECT_EQ(cont.fbs[0].bar, 5);
134 EXPECT_EQ(cont.fbs[1].foo, 7);
135 EXPECT_EQ(cont.fbs[1].bar, 9);
136 }
137
138 {
139 FooBarContainer cont;
140 Input yin("---\nfbs:\n...\n");
141 yin >> cont;
142 // Okay: Empty node represents an empty array.
143 EXPECT_FALSE(yin.error());
144 EXPECT_EQ(cont.fbs.size(), 0UL);
145 }
146
147 {
148 FooBarContainer cont;
149 Input yin("---\nfbs: !!null null\n...\n");
150 yin >> cont;
151 // Okay: null represents an empty array.
152 EXPECT_FALSE(yin.error());
153 EXPECT_EQ(cont.fbs.size(), 0UL);
154 }
155
156 {
157 FooBarContainer cont;
158 Input yin("---\nfbs: ~\n...\n");
159 yin >> cont;
160 // Okay: null represents an empty array.
161 EXPECT_FALSE(yin.error());
162 EXPECT_EQ(cont.fbs.size(), 0UL);
163 }
164
165 {
166 FooBarContainer cont;
167 Input yin("---\nfbs: null\n...\n");
168 yin >> cont;
169 // Okay: null represents an empty array.
170 EXPECT_FALSE(yin.error());
171 EXPECT_EQ(cont.fbs.size(), 0UL);
172 }
173}
174
175//
176// Test the reading of a map containing a malformed yaml sequence
177//
178TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
179 {
180 FooBarContainer cont;
181 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
182 suppressErrorMessages);
183 yin >> cont;
184 // Error: fbs is not a sequence.
185 EXPECT_TRUE(!!yin.error());
186 EXPECT_EQ(cont.fbs.size(), 0UL);
187 }
188
189 {
190 FooBarContainer cont;
191 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
192 yin >> cont;
193 // This should be an error.
194 EXPECT_TRUE(!!yin.error());
195 EXPECT_EQ(cont.fbs.size(), 0UL);
196 }
197}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000198
199//
200// Test writing then reading back a sequence of mappings
201//
202TEST(YAMLIO, TestSequenceMapWriteAndRead) {
203 std::string intermediate;
204 {
205 FooBar entry1;
206 entry1.foo = 10;
207 entry1.bar = -3;
208 FooBar entry2;
209 entry2.foo = 257;
210 entry2.bar = 0;
211 FooBarSequence seq;
212 seq.push_back(entry1);
213 seq.push_back(entry2);
214
215 llvm::raw_string_ostream ostr(intermediate);
216 Output yout(ostr);
217 yout << seq;
218 }
219
220 {
221 Input yin(intermediate);
222 FooBarSequence seq2;
223 yin >> seq2;
224
225 EXPECT_FALSE(yin.error());
226 EXPECT_EQ(seq2.size(), 2UL);
227 FooBar& map1 = seq2[0];
228 FooBar& map2 = seq2[1];
229 EXPECT_EQ(map1.foo, 10);
230 EXPECT_EQ(map1.bar, -3);
231 EXPECT_EQ(map2.foo, 257);
232 EXPECT_EQ(map2.bar, 0);
233 }
234}
235
Alex Bradbury16843172017-07-17 11:41:30 +0000236//
237// Test YAML filename handling.
238//
239static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) {
240 EXPECT_EQ(Error.getFilename(), "foo.yaml");
241}
242
243TEST(YAMLIO, TestGivenFilename) {
244 auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
245 Input yin(*Buffer, nullptr, testErrorFilename);
246 FooBar Value;
247 yin >> Value;
248
249 EXPECT_TRUE(!!yin.error());
250}
251
Nick Kledzikf60a9272012-12-12 20:46:15 +0000252
253//===----------------------------------------------------------------------===//
254// Test built-in types
255//===----------------------------------------------------------------------===//
256
257struct BuiltInTypes {
258 llvm::StringRef str;
John Thompson48e018a2013-11-19 17:28:21 +0000259 std::string stdstr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000260 uint64_t u64;
261 uint32_t u32;
262 uint16_t u16;
263 uint8_t u8;
264 bool b;
265 int64_t s64;
266 int32_t s32;
267 int16_t s16;
268 int8_t s8;
269 float f;
270 double d;
271 Hex8 h8;
272 Hex16 h16;
273 Hex32 h32;
274 Hex64 h64;
275};
276
277namespace llvm {
278namespace yaml {
279 template <>
280 struct MappingTraits<BuiltInTypes> {
281 static void mapping(IO &io, BuiltInTypes& bt) {
282 io.mapRequired("str", bt.str);
John Thompson48e018a2013-11-19 17:28:21 +0000283 io.mapRequired("stdstr", bt.stdstr);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000284 io.mapRequired("u64", bt.u64);
285 io.mapRequired("u32", bt.u32);
286 io.mapRequired("u16", bt.u16);
287 io.mapRequired("u8", bt.u8);
288 io.mapRequired("b", bt.b);
289 io.mapRequired("s64", bt.s64);
290 io.mapRequired("s32", bt.s32);
291 io.mapRequired("s16", bt.s16);
292 io.mapRequired("s8", bt.s8);
293 io.mapRequired("f", bt.f);
294 io.mapRequired("d", bt.d);
295 io.mapRequired("h8", bt.h8);
296 io.mapRequired("h16", bt.h16);
297 io.mapRequired("h32", bt.h32);
298 io.mapRequired("h64", bt.h64);
299 }
300 };
301}
302}
303
304
305//
306// Test the reading of all built-in scalar conversions
307//
308TEST(YAMLIO, TestReadBuiltInTypes) {
309 BuiltInTypes map;
310 Input yin("---\n"
311 "str: hello there\n"
John Thompson48e018a2013-11-19 17:28:21 +0000312 "stdstr: hello where?\n"
Nick Kledzikf60a9272012-12-12 20:46:15 +0000313 "u64: 5000000000\n"
314 "u32: 4000000000\n"
315 "u16: 65000\n"
316 "u8: 255\n"
317 "b: false\n"
318 "s64: -5000000000\n"
319 "s32: -2000000000\n"
320 "s16: -32000\n"
321 "s8: -127\n"
322 "f: 137.125\n"
323 "d: -2.8625\n"
324 "h8: 0xFF\n"
325 "h16: 0x8765\n"
326 "h32: 0xFEDCBA98\n"
327 "h64: 0xFEDCBA9876543210\n"
328 "...\n");
329 yin >> map;
330
331 EXPECT_FALSE(yin.error());
332 EXPECT_TRUE(map.str.equals("hello there"));
John Thompson48e018a2013-11-19 17:28:21 +0000333 EXPECT_TRUE(map.stdstr == "hello where?");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000334 EXPECT_EQ(map.u64, 5000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000335 EXPECT_EQ(map.u32, 4000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000336 EXPECT_EQ(map.u16, 65000);
337 EXPECT_EQ(map.u8, 255);
338 EXPECT_EQ(map.b, false);
339 EXPECT_EQ(map.s64, -5000000000LL);
340 EXPECT_EQ(map.s32, -2000000000L);
341 EXPECT_EQ(map.s16, -32000);
342 EXPECT_EQ(map.s8, -127);
343 EXPECT_EQ(map.f, 137.125);
344 EXPECT_EQ(map.d, -2.8625);
345 EXPECT_EQ(map.h8, Hex8(255));
346 EXPECT_EQ(map.h16, Hex16(0x8765));
347 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
348 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
349}
350
351
352//
353// Test writing then reading back all built-in scalar types
354//
355TEST(YAMLIO, TestReadWriteBuiltInTypes) {
356 std::string intermediate;
357 {
358 BuiltInTypes map;
359 map.str = "one two";
John Thompson48e018a2013-11-19 17:28:21 +0000360 map.stdstr = "three four";
Nick Kledzikbed953d2012-12-17 22:11:17 +0000361 map.u64 = 6000000000ULL;
362 map.u32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000363 map.u16 = 50000;
364 map.u8 = 254;
365 map.b = true;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000366 map.s64 = -6000000000LL;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000367 map.s32 = -2000000000;
368 map.s16 = -32000;
369 map.s8 = -128;
370 map.f = 3.25;
371 map.d = -2.8625;
372 map.h8 = 254;
373 map.h16 = 50000;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000374 map.h32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000375 map.h64 = 6000000000LL;
376
377 llvm::raw_string_ostream ostr(intermediate);
378 Output yout(ostr);
379 yout << map;
380 }
381
382 {
383 Input yin(intermediate);
384 BuiltInTypes map;
385 yin >> map;
386
387 EXPECT_FALSE(yin.error());
388 EXPECT_TRUE(map.str.equals("one two"));
John Thompson48e018a2013-11-19 17:28:21 +0000389 EXPECT_TRUE(map.stdstr == "three four");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000390 EXPECT_EQ(map.u64, 6000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000391 EXPECT_EQ(map.u32, 3000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000392 EXPECT_EQ(map.u16, 50000);
393 EXPECT_EQ(map.u8, 254);
394 EXPECT_EQ(map.b, true);
395 EXPECT_EQ(map.s64, -6000000000LL);
396 EXPECT_EQ(map.s32, -2000000000L);
397 EXPECT_EQ(map.s16, -32000);
398 EXPECT_EQ(map.s8, -128);
399 EXPECT_EQ(map.f, 3.25);
400 EXPECT_EQ(map.d, -2.8625);
401 EXPECT_EQ(map.h8, Hex8(254));
402 EXPECT_EQ(map.h16, Hex16(50000));
Nick Kledzikbed953d2012-12-17 22:11:17 +0000403 EXPECT_EQ(map.h32, Hex32(3000000000U));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000404 EXPECT_EQ(map.h64, Hex64(6000000000LL));
405 }
406}
407
Zachary Turner4fbf61d2016-06-07 19:32:09 +0000408//===----------------------------------------------------------------------===//
409// Test endian-aware types
410//===----------------------------------------------------------------------===//
411
412struct EndianTypes {
413 typedef llvm::support::detail::packed_endian_specific_integral<
414 float, llvm::support::little, llvm::support::unaligned>
415 ulittle_float;
416 typedef llvm::support::detail::packed_endian_specific_integral<
417 double, llvm::support::little, llvm::support::unaligned>
418 ulittle_double;
419
420 llvm::support::ulittle64_t u64;
421 llvm::support::ulittle32_t u32;
422 llvm::support::ulittle16_t u16;
423 llvm::support::little64_t s64;
424 llvm::support::little32_t s32;
425 llvm::support::little16_t s16;
426 ulittle_float f;
427 ulittle_double d;
428};
429
430namespace llvm {
431namespace yaml {
432template <> struct MappingTraits<EndianTypes> {
433 static void mapping(IO &io, EndianTypes &et) {
434 io.mapRequired("u64", et.u64);
435 io.mapRequired("u32", et.u32);
436 io.mapRequired("u16", et.u16);
437 io.mapRequired("s64", et.s64);
438 io.mapRequired("s32", et.s32);
439 io.mapRequired("s16", et.s16);
440 io.mapRequired("f", et.f);
441 io.mapRequired("d", et.d);
442 }
443};
444}
445}
446
447//
448// Test the reading of all endian scalar conversions
449//
450TEST(YAMLIO, TestReadEndianTypes) {
451 EndianTypes map;
452 Input yin("---\n"
453 "u64: 5000000000\n"
454 "u32: 4000000000\n"
455 "u16: 65000\n"
456 "s64: -5000000000\n"
457 "s32: -2000000000\n"
458 "s16: -32000\n"
459 "f: 3.25\n"
460 "d: -2.8625\n"
461 "...\n");
462 yin >> map;
463
464 EXPECT_FALSE(yin.error());
465 EXPECT_EQ(map.u64, 5000000000ULL);
466 EXPECT_EQ(map.u32, 4000000000U);
467 EXPECT_EQ(map.u16, 65000);
468 EXPECT_EQ(map.s64, -5000000000LL);
469 EXPECT_EQ(map.s32, -2000000000L);
470 EXPECT_EQ(map.s16, -32000);
471 EXPECT_EQ(map.f, 3.25f);
472 EXPECT_EQ(map.d, -2.8625);
473}
474
475//
476// Test writing then reading back all endian-aware scalar types
477//
478TEST(YAMLIO, TestReadWriteEndianTypes) {
479 std::string intermediate;
480 {
481 EndianTypes map;
482 map.u64 = 6000000000ULL;
483 map.u32 = 3000000000U;
484 map.u16 = 50000;
485 map.s64 = -6000000000LL;
486 map.s32 = -2000000000;
487 map.s16 = -32000;
488 map.f = 3.25f;
489 map.d = -2.8625;
490
491 llvm::raw_string_ostream ostr(intermediate);
492 Output yout(ostr);
493 yout << map;
494 }
495
496 {
497 Input yin(intermediate);
498 EndianTypes map;
499 yin >> map;
500
501 EXPECT_FALSE(yin.error());
502 EXPECT_EQ(map.u64, 6000000000ULL);
503 EXPECT_EQ(map.u32, 3000000000U);
504 EXPECT_EQ(map.u16, 50000);
505 EXPECT_EQ(map.s64, -6000000000LL);
506 EXPECT_EQ(map.s32, -2000000000L);
507 EXPECT_EQ(map.s16, -32000);
508 EXPECT_EQ(map.f, 3.25f);
509 EXPECT_EQ(map.d, -2.8625);
510 }
511}
512
Rui Ueyama106eded2013-09-11 04:00:08 +0000513struct StringTypes {
514 llvm::StringRef str1;
515 llvm::StringRef str2;
516 llvm::StringRef str3;
517 llvm::StringRef str4;
518 llvm::StringRef str5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000519 llvm::StringRef str6;
David Majnemer77880332014-04-10 07:37:33 +0000520 llvm::StringRef str7;
521 llvm::StringRef str8;
522 llvm::StringRef str9;
523 llvm::StringRef str10;
524 llvm::StringRef str11;
John Thompson48e018a2013-11-19 17:28:21 +0000525 std::string stdstr1;
526 std::string stdstr2;
527 std::string stdstr3;
528 std::string stdstr4;
529 std::string stdstr5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000530 std::string stdstr6;
David Majnemer77880332014-04-10 07:37:33 +0000531 std::string stdstr7;
532 std::string stdstr8;
533 std::string stdstr9;
534 std::string stdstr10;
535 std::string stdstr11;
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000536 std::string stdstr12;
Rui Ueyama106eded2013-09-11 04:00:08 +0000537};
Nick Kledzikf60a9272012-12-12 20:46:15 +0000538
Rui Ueyama106eded2013-09-11 04:00:08 +0000539namespace llvm {
540namespace yaml {
541 template <>
542 struct MappingTraits<StringTypes> {
543 static void mapping(IO &io, StringTypes& st) {
544 io.mapRequired("str1", st.str1);
545 io.mapRequired("str2", st.str2);
546 io.mapRequired("str3", st.str3);
547 io.mapRequired("str4", st.str4);
548 io.mapRequired("str5", st.str5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000549 io.mapRequired("str6", st.str6);
David Majnemer77880332014-04-10 07:37:33 +0000550 io.mapRequired("str7", st.str7);
551 io.mapRequired("str8", st.str8);
552 io.mapRequired("str9", st.str9);
553 io.mapRequired("str10", st.str10);
554 io.mapRequired("str11", st.str11);
John Thompson48e018a2013-11-19 17:28:21 +0000555 io.mapRequired("stdstr1", st.stdstr1);
556 io.mapRequired("stdstr2", st.stdstr2);
557 io.mapRequired("stdstr3", st.stdstr3);
558 io.mapRequired("stdstr4", st.stdstr4);
559 io.mapRequired("stdstr5", st.stdstr5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000560 io.mapRequired("stdstr6", st.stdstr6);
David Majnemer77880332014-04-10 07:37:33 +0000561 io.mapRequired("stdstr7", st.stdstr7);
562 io.mapRequired("stdstr8", st.stdstr8);
563 io.mapRequired("stdstr9", st.stdstr9);
564 io.mapRequired("stdstr10", st.stdstr10);
565 io.mapRequired("stdstr11", st.stdstr11);
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000566 io.mapRequired("stdstr12", st.stdstr12);
Rui Ueyama106eded2013-09-11 04:00:08 +0000567 }
568 };
569}
570}
571
572TEST(YAMLIO, TestReadWriteStringTypes) {
573 std::string intermediate;
574 {
575 StringTypes map;
576 map.str1 = "'aaa";
577 map.str2 = "\"bbb";
578 map.str3 = "`ccc";
579 map.str4 = "@ddd";
580 map.str5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000581 map.str6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000582 map.str7 = "true";
583 map.str8 = "FALSE";
584 map.str9 = "~";
585 map.str10 = "0.2e20";
586 map.str11 = "0x30";
John Thompson48e018a2013-11-19 17:28:21 +0000587 map.stdstr1 = "'eee";
588 map.stdstr2 = "\"fff";
589 map.stdstr3 = "`ggg";
590 map.stdstr4 = "@hhh";
591 map.stdstr5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000592 map.stdstr6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000593 map.stdstr7 = "true";
594 map.stdstr8 = "FALSE";
595 map.stdstr9 = "~";
596 map.stdstr10 = "0.2e20";
597 map.stdstr11 = "0x30";
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000598 map.stdstr12 = "- match";
Rui Ueyama106eded2013-09-11 04:00:08 +0000599
600 llvm::raw_string_ostream ostr(intermediate);
601 Output yout(ostr);
602 yout << map;
603 }
604
605 llvm::StringRef flowOut(intermediate);
606 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
607 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
608 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
609 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
610 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000611 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
David Majnemer77880332014-04-10 07:37:33 +0000612 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
613 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
614 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
615 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
616 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000617 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'- match'\n"));
John Thompson48e018a2013-11-19 17:28:21 +0000618 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
619 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
620 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
621 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
622 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000623 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
Rui Ueyama106eded2013-09-11 04:00:08 +0000624
625 {
626 Input yin(intermediate);
627 StringTypes map;
628 yin >> map;
629
630 EXPECT_FALSE(yin.error());
631 EXPECT_TRUE(map.str1.equals("'aaa"));
632 EXPECT_TRUE(map.str2.equals("\"bbb"));
633 EXPECT_TRUE(map.str3.equals("`ccc"));
634 EXPECT_TRUE(map.str4.equals("@ddd"));
635 EXPECT_TRUE(map.str5.equals(""));
David Majnemer97d8ee32014-04-09 17:04:27 +0000636 EXPECT_TRUE(map.str6.equals("0000000004000000"));
John Thompson48e018a2013-11-19 17:28:21 +0000637 EXPECT_TRUE(map.stdstr1 == "'eee");
638 EXPECT_TRUE(map.stdstr2 == "\"fff");
639 EXPECT_TRUE(map.stdstr3 == "`ggg");
640 EXPECT_TRUE(map.stdstr4 == "@hhh");
641 EXPECT_TRUE(map.stdstr5 == "");
David Majnemer97d8ee32014-04-09 17:04:27 +0000642 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
Rui Ueyama106eded2013-09-11 04:00:08 +0000643 }
644}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000645
646//===----------------------------------------------------------------------===//
647// Test ScalarEnumerationTraits
648//===----------------------------------------------------------------------===//
649
650enum Colors {
651 cRed,
652 cBlue,
653 cGreen,
654 cYellow
655};
656
657struct ColorMap {
658 Colors c1;
659 Colors c2;
660 Colors c3;
661 Colors c4;
662 Colors c5;
663 Colors c6;
664};
665
666namespace llvm {
667namespace yaml {
668 template <>
669 struct ScalarEnumerationTraits<Colors> {
670 static void enumeration(IO &io, Colors &value) {
671 io.enumCase(value, "red", cRed);
672 io.enumCase(value, "blue", cBlue);
673 io.enumCase(value, "green", cGreen);
674 io.enumCase(value, "yellow",cYellow);
675 }
676 };
677 template <>
678 struct MappingTraits<ColorMap> {
679 static void mapping(IO &io, ColorMap& c) {
680 io.mapRequired("c1", c.c1);
681 io.mapRequired("c2", c.c2);
682 io.mapRequired("c3", c.c3);
683 io.mapOptional("c4", c.c4, cBlue); // supplies default
684 io.mapOptional("c5", c.c5, cYellow); // supplies default
685 io.mapOptional("c6", c.c6, cRed); // supplies default
686 }
687 };
688}
689}
690
691
692//
693// Test reading enumerated scalars
694//
695TEST(YAMLIO, TestEnumRead) {
696 ColorMap map;
697 Input yin("---\n"
698 "c1: blue\n"
699 "c2: red\n"
700 "c3: green\n"
701 "c5: yellow\n"
702 "...\n");
703 yin >> map;
704
705 EXPECT_FALSE(yin.error());
706 EXPECT_EQ(cBlue, map.c1);
707 EXPECT_EQ(cRed, map.c2);
708 EXPECT_EQ(cGreen, map.c3);
709 EXPECT_EQ(cBlue, map.c4); // tests default
710 EXPECT_EQ(cYellow,map.c5); // tests overridden
711 EXPECT_EQ(cRed, map.c6); // tests default
712}
713
714
715
716//===----------------------------------------------------------------------===//
717// Test ScalarBitSetTraits
718//===----------------------------------------------------------------------===//
719
720enum MyFlags {
721 flagNone = 0,
722 flagBig = 1 << 0,
723 flagFlat = 1 << 1,
724 flagRound = 1 << 2,
725 flagPointy = 1 << 3
726};
727inline MyFlags operator|(MyFlags a, MyFlags b) {
728 return static_cast<MyFlags>(
729 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
730}
731
732struct FlagsMap {
733 MyFlags f1;
734 MyFlags f2;
735 MyFlags f3;
736 MyFlags f4;
737};
738
739
740namespace llvm {
741namespace yaml {
742 template <>
743 struct ScalarBitSetTraits<MyFlags> {
744 static void bitset(IO &io, MyFlags &value) {
745 io.bitSetCase(value, "big", flagBig);
746 io.bitSetCase(value, "flat", flagFlat);
747 io.bitSetCase(value, "round", flagRound);
748 io.bitSetCase(value, "pointy",flagPointy);
749 }
750 };
751 template <>
752 struct MappingTraits<FlagsMap> {
753 static void mapping(IO &io, FlagsMap& c) {
754 io.mapRequired("f1", c.f1);
755 io.mapRequired("f2", c.f2);
756 io.mapRequired("f3", c.f3);
757 io.mapOptional("f4", c.f4, MyFlags(flagRound));
758 }
759 };
760}
761}
762
763
764//
765// Test reading flow sequence representing bit-mask values
766//
767TEST(YAMLIO, TestFlagsRead) {
768 FlagsMap map;
769 Input yin("---\n"
770 "f1: [ big ]\n"
771 "f2: [ round, flat ]\n"
772 "f3: []\n"
773 "...\n");
774 yin >> map;
775
776 EXPECT_FALSE(yin.error());
777 EXPECT_EQ(flagBig, map.f1);
778 EXPECT_EQ(flagRound|flagFlat, map.f2);
779 EXPECT_EQ(flagNone, map.f3); // check empty set
780 EXPECT_EQ(flagRound, map.f4); // check optional key
781}
782
783
784//
785// Test writing then reading back bit-mask values
786//
787TEST(YAMLIO, TestReadWriteFlags) {
788 std::string intermediate;
789 {
790 FlagsMap map;
791 map.f1 = flagBig;
792 map.f2 = flagRound | flagFlat;
793 map.f3 = flagNone;
794 map.f4 = flagNone;
795
796 llvm::raw_string_ostream ostr(intermediate);
797 Output yout(ostr);
798 yout << map;
799 }
800
801 {
802 Input yin(intermediate);
803 FlagsMap map2;
804 yin >> map2;
805
806 EXPECT_FALSE(yin.error());
807 EXPECT_EQ(flagBig, map2.f1);
808 EXPECT_EQ(flagRound|flagFlat, map2.f2);
809 EXPECT_EQ(flagNone, map2.f3);
810 //EXPECT_EQ(flagRound, map2.f4); // check optional key
811 }
812}
813
814
815
816//===----------------------------------------------------------------------===//
817// Test ScalarTraits
818//===----------------------------------------------------------------------===//
819
820struct MyCustomType {
821 int length;
822 int width;
823};
824
825struct MyCustomTypeMap {
826 MyCustomType f1;
827 MyCustomType f2;
828 int f3;
829};
830
831
832namespace llvm {
833namespace yaml {
834 template <>
835 struct MappingTraits<MyCustomTypeMap> {
836 static void mapping(IO &io, MyCustomTypeMap& s) {
837 io.mapRequired("f1", s.f1);
838 io.mapRequired("f2", s.f2);
839 io.mapRequired("f3", s.f3);
840 }
841 };
842 // MyCustomType is formatted as a yaml scalar. A value of
843 // {length=3, width=4} would be represented in yaml as "3 by 4".
844 template<>
845 struct ScalarTraits<MyCustomType> {
846 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
847 out << llvm::format("%d by %d", value.length, value.width);
848 }
849 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
850 size_t byStart = scalar.find("by");
851 if ( byStart != StringRef::npos ) {
852 StringRef lenStr = scalar.slice(0, byStart);
853 lenStr = lenStr.rtrim();
854 if ( lenStr.getAsInteger(0, value.length) ) {
855 return "malformed length";
856 }
857 StringRef widthStr = scalar.drop_front(byStart+2);
858 widthStr = widthStr.ltrim();
859 if ( widthStr.getAsInteger(0, value.width) ) {
860 return "malformed width";
861 }
862 return StringRef();
863 }
864 else {
865 return "malformed by";
866 }
867 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000868 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000869 };
870}
871}
872
873
874//
875// Test writing then reading back custom values
876//
877TEST(YAMLIO, TestReadWriteMyCustomType) {
878 std::string intermediate;
879 {
880 MyCustomTypeMap map;
881 map.f1.length = 1;
882 map.f1.width = 4;
883 map.f2.length = 100;
884 map.f2.width = 400;
885 map.f3 = 10;
886
887 llvm::raw_string_ostream ostr(intermediate);
888 Output yout(ostr);
889 yout << map;
890 }
891
892 {
893 Input yin(intermediate);
894 MyCustomTypeMap map2;
895 yin >> map2;
896
897 EXPECT_FALSE(yin.error());
898 EXPECT_EQ(1, map2.f1.length);
899 EXPECT_EQ(4, map2.f1.width);
900 EXPECT_EQ(100, map2.f2.length);
901 EXPECT_EQ(400, map2.f2.width);
902 EXPECT_EQ(10, map2.f3);
903 }
904}
905
906
907//===----------------------------------------------------------------------===//
Alex Lorenz68e787b2015-05-14 23:08:22 +0000908// Test BlockScalarTraits
909//===----------------------------------------------------------------------===//
910
911struct MultilineStringType {
912 std::string str;
913};
914
915struct MultilineStringTypeMap {
916 MultilineStringType name;
917 MultilineStringType description;
918 MultilineStringType ingredients;
919 MultilineStringType recipes;
920 MultilineStringType warningLabels;
921 MultilineStringType documentation;
922 int price;
923};
924
925namespace llvm {
926namespace yaml {
927 template <>
928 struct MappingTraits<MultilineStringTypeMap> {
929 static void mapping(IO &io, MultilineStringTypeMap& s) {
930 io.mapRequired("name", s.name);
931 io.mapRequired("description", s.description);
932 io.mapRequired("ingredients", s.ingredients);
933 io.mapRequired("recipes", s.recipes);
934 io.mapRequired("warningLabels", s.warningLabels);
935 io.mapRequired("documentation", s.documentation);
936 io.mapRequired("price", s.price);
937 }
938 };
939
940 // MultilineStringType is formatted as a yaml block literal scalar. A value of
941 // "Hello\nWorld" would be represented in yaml as
942 // |
943 // Hello
944 // World
945 template <>
946 struct BlockScalarTraits<MultilineStringType> {
947 static void output(const MultilineStringType &value, void *ctxt,
948 llvm::raw_ostream &out) {
949 out << value.str;
950 }
951 static StringRef input(StringRef scalar, void *ctxt,
952 MultilineStringType &value) {
953 value.str = scalar.str();
954 return StringRef();
955 }
956 };
957}
958}
959
960LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
961
962//
963// Test writing then reading back custom values
964//
965TEST(YAMLIO, TestReadWriteMultilineStringType) {
966 std::string intermediate;
967 {
968 MultilineStringTypeMap map;
969 map.name.str = "An Item";
970 map.description.str = "Hello\nWorld";
971 map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
972 map.recipes.str = "\n\nTest 1\n\n\n";
973 map.warningLabels.str = "";
974 map.documentation.str = "\n\n";
975 map.price = 350;
976
977 llvm::raw_string_ostream ostr(intermediate);
978 Output yout(ostr);
979 yout << map;
980 }
981 {
982 Input yin(intermediate);
983 MultilineStringTypeMap map2;
984 yin >> map2;
985
986 EXPECT_FALSE(yin.error());
987 EXPECT_EQ(map2.name.str, "An Item\n");
988 EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
989 EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
990 EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
991 EXPECT_TRUE(map2.warningLabels.str.empty());
992 EXPECT_TRUE(map2.documentation.str.empty());
993 EXPECT_EQ(map2.price, 350);
994 }
995}
996
997//
998// Test writing then reading back custom values
999//
1000TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
1001 std::string intermediate;
1002 {
1003 std::vector<MultilineStringType> documents;
1004 MultilineStringType doc;
1005 doc.str = "Hello\nWorld";
1006 documents.push_back(doc);
1007
1008 llvm::raw_string_ostream ostr(intermediate);
1009 Output yout(ostr);
1010 yout << documents;
1011
1012 // Verify that the block scalar header was written out on the same line
1013 // as the document marker.
1014 EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
1015 }
1016 {
1017 Input yin(intermediate);
1018 std::vector<MultilineStringType> documents2;
1019 yin >> documents2;
1020
1021 EXPECT_FALSE(yin.error());
1022 EXPECT_EQ(documents2.size(), size_t(1));
1023 EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
1024 }
1025}
1026
1027TEST(YAMLIO, TestReadWriteBlockScalarValue) {
1028 std::string intermediate;
1029 {
1030 MultilineStringType doc;
1031 doc.str = "Just a block\nscalar doc";
1032
1033 llvm::raw_string_ostream ostr(intermediate);
1034 Output yout(ostr);
1035 yout << doc;
1036 }
1037 {
1038 Input yin(intermediate);
1039 MultilineStringType doc;
1040 yin >> doc;
1041
1042 EXPECT_FALSE(yin.error());
1043 EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
1044 }
1045}
1046
1047//===----------------------------------------------------------------------===//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001048// Test flow sequences
1049//===----------------------------------------------------------------------===//
1050
1051LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
1052LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
Richard Smithd0c0c132017-06-30 20:56:57 +00001053LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef, MyString)
1054LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001055
1056namespace llvm {
1057namespace yaml {
1058 template<>
1059 struct ScalarTraits<MyNumber> {
1060 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
1061 out << value;
1062 }
1063
1064 static StringRef input(StringRef scalar, void *, MyNumber &value) {
David Blaikieb088ff62012-12-12 22:14:32 +00001065 long long n;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001066 if ( getAsSignedInteger(scalar, 0, n) )
1067 return "invalid number";
1068 value = n;
1069 return StringRef();
1070 }
David Majnemer77880332014-04-10 07:37:33 +00001071
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00001072 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001073 };
Richard Smithd0c0c132017-06-30 20:56:57 +00001074
1075 template <> struct ScalarTraits<MyString> {
1076 using Impl = ScalarTraits<StringRef>;
1077 static void output(const MyString &V, void *Ctx, raw_ostream &OS) {
1078 Impl::output(V, Ctx, OS);
1079 }
1080 static StringRef input(StringRef S, void *Ctx, MyString &V) {
1081 return Impl::input(S, Ctx, V.value);
1082 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00001083 static QuotingType mustQuote(StringRef S) {
1084 return Impl::mustQuote(S);
1085 }
Richard Smithd0c0c132017-06-30 20:56:57 +00001086 };
Nick Kledzikf60a9272012-12-12 20:46:15 +00001087}
1088}
1089
1090struct NameAndNumbers {
1091 llvm::StringRef name;
Richard Smithd0c0c132017-06-30 20:56:57 +00001092 std::vector<MyString> strings;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001093 std::vector<MyNumber> single;
1094 std::vector<MyNumber> numbers;
1095};
1096
1097namespace llvm {
1098namespace yaml {
1099 template <>
1100 struct MappingTraits<NameAndNumbers> {
1101 static void mapping(IO &io, NameAndNumbers& nn) {
1102 io.mapRequired("name", nn.name);
1103 io.mapRequired("strings", nn.strings);
1104 io.mapRequired("single", nn.single);
1105 io.mapRequired("numbers", nn.numbers);
1106 }
1107 };
1108}
1109}
1110
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001111typedef std::vector<MyNumber> MyNumberFlowSequence;
1112
1113LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1114
1115struct NameAndNumbersFlow {
1116 llvm::StringRef name;
1117 std::vector<MyNumberFlowSequence> sequenceOfNumbers;
1118};
1119
1120namespace llvm {
1121namespace yaml {
1122 template <>
1123 struct MappingTraits<NameAndNumbersFlow> {
1124 static void mapping(IO &io, NameAndNumbersFlow& nn) {
1125 io.mapRequired("name", nn.name);
1126 io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
1127 }
1128 };
1129}
1130}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001131
1132//
1133// Test writing then reading back custom values
1134//
1135TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1136 std::string intermediate;
1137 {
1138 NameAndNumbers map;
1139 map.name = "hello";
1140 map.strings.push_back(llvm::StringRef("one"));
1141 map.strings.push_back(llvm::StringRef("two"));
1142 map.single.push_back(1);
1143 map.numbers.push_back(10);
1144 map.numbers.push_back(-30);
1145 map.numbers.push_back(1024);
1146
1147 llvm::raw_string_ostream ostr(intermediate);
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001148 Output yout(ostr);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001149 yout << map;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001150
Nick Kledzik11964f22013-01-04 19:32:00 +00001151 // Verify sequences were written in flow style
1152 ostr.flush();
1153 llvm::StringRef flowOut(intermediate);
1154 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1155 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001156 }
1157
1158 {
1159 Input yin(intermediate);
1160 NameAndNumbers map2;
1161 yin >> map2;
1162
1163 EXPECT_FALSE(yin.error());
1164 EXPECT_TRUE(map2.name.equals("hello"));
1165 EXPECT_EQ(map2.strings.size(), 2UL);
Richard Smithd0c0c132017-06-30 20:56:57 +00001166 EXPECT_TRUE(map2.strings[0].value.equals("one"));
1167 EXPECT_TRUE(map2.strings[1].value.equals("two"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001168 EXPECT_EQ(map2.single.size(), 1UL);
1169 EXPECT_EQ(1, map2.single[0]);
1170 EXPECT_EQ(map2.numbers.size(), 3UL);
1171 EXPECT_EQ(10, map2.numbers[0]);
1172 EXPECT_EQ(-30, map2.numbers[1]);
1173 EXPECT_EQ(1024, map2.numbers[2]);
1174 }
1175}
1176
1177
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001178//
1179// Test writing then reading back a sequence of flow sequences.
1180//
1181TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1182 std::string intermediate;
1183 {
1184 NameAndNumbersFlow map;
1185 map.name = "hello";
1186 MyNumberFlowSequence single = { 0 };
1187 MyNumberFlowSequence numbers = { 12, 1, -512 };
1188 map.sequenceOfNumbers.push_back(single);
1189 map.sequenceOfNumbers.push_back(numbers);
1190 map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1191
1192 llvm::raw_string_ostream ostr(intermediate);
1193 Output yout(ostr);
1194 yout << map;
1195
1196 // Verify sequences were written in flow style
1197 // and that the parent sequence used '-'.
1198 ostr.flush();
1199 llvm::StringRef flowOut(intermediate);
1200 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1201 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1202 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
1203 }
1204
1205 {
1206 Input yin(intermediate);
1207 NameAndNumbersFlow map2;
1208 yin >> map2;
1209
1210 EXPECT_FALSE(yin.error());
1211 EXPECT_TRUE(map2.name.equals("hello"));
1212 EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1213 EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1214 EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
1215 EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1216 EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
1217 EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
1218 EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1219 EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1220 }
1221}
1222
Nick Kledzikf60a9272012-12-12 20:46:15 +00001223//===----------------------------------------------------------------------===//
1224// Test normalizing/denormalizing
1225//===----------------------------------------------------------------------===//
1226
1227LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1228
1229typedef std::vector<TotalSeconds> SecondsSequence;
1230
Nick Kledzik11964f22013-01-04 19:32:00 +00001231LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001232
1233
1234namespace llvm {
1235namespace yaml {
1236 template <>
1237 struct MappingTraits<TotalSeconds> {
1238
1239 class NormalizedSeconds {
1240 public:
1241 NormalizedSeconds(IO &io)
1242 : hours(0), minutes(0), seconds(0) {
1243 }
1244 NormalizedSeconds(IO &, TotalSeconds &secs)
1245 : hours(secs/3600),
1246 minutes((secs - (hours*3600))/60),
1247 seconds(secs % 60) {
1248 }
1249 TotalSeconds denormalize(IO &) {
1250 return TotalSeconds(hours*3600 + minutes*60 + seconds);
1251 }
1252
1253 uint32_t hours;
1254 uint8_t minutes;
1255 uint8_t seconds;
1256 };
1257
1258 static void mapping(IO &io, TotalSeconds &secs) {
1259 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1260
1261 io.mapOptional("hours", keys->hours, (uint32_t)0);
1262 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
1263 io.mapRequired("seconds", keys->seconds);
1264 }
1265 };
1266}
1267}
1268
1269
1270//
1271// Test the reading of a yaml sequence of mappings
1272//
1273TEST(YAMLIO, TestReadMySecondsSequence) {
1274 SecondsSequence seq;
1275 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1276 yin >> seq;
1277
1278 EXPECT_FALSE(yin.error());
1279 EXPECT_EQ(seq.size(), 2UL);
1280 EXPECT_EQ(seq[0], 3605U);
1281 EXPECT_EQ(seq[1], 59U);
1282}
1283
1284
1285//
1286// Test writing then reading back custom values
1287//
1288TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1289 std::string intermediate;
1290 {
1291 SecondsSequence seq;
1292 seq.push_back(4000);
1293 seq.push_back(500);
1294 seq.push_back(59);
1295
1296 llvm::raw_string_ostream ostr(intermediate);
1297 Output yout(ostr);
1298 yout << seq;
1299 }
1300 {
1301 Input yin(intermediate);
1302 SecondsSequence seq2;
1303 yin >> seq2;
1304
1305 EXPECT_FALSE(yin.error());
1306 EXPECT_EQ(seq2.size(), 3UL);
1307 EXPECT_EQ(seq2[0], 4000U);
1308 EXPECT_EQ(seq2[1], 500U);
1309 EXPECT_EQ(seq2[2], 59U);
1310 }
1311}
1312
1313
1314//===----------------------------------------------------------------------===//
1315// Test dynamic typing
1316//===----------------------------------------------------------------------===//
1317
1318enum AFlags {
1319 a1,
1320 a2,
1321 a3
1322};
1323
1324enum BFlags {
1325 b1,
1326 b2,
1327 b3
1328};
1329
1330enum Kind {
1331 kindA,
1332 kindB
1333};
1334
1335struct KindAndFlags {
1336 KindAndFlags() : kind(kindA), flags(0) { }
1337 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1338 Kind kind;
1339 uint32_t flags;
1340};
1341
1342typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1343
Nick Kledzik11964f22013-01-04 19:32:00 +00001344LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001345
1346namespace llvm {
1347namespace yaml {
1348 template <>
1349 struct ScalarEnumerationTraits<AFlags> {
1350 static void enumeration(IO &io, AFlags &value) {
1351 io.enumCase(value, "a1", a1);
1352 io.enumCase(value, "a2", a2);
1353 io.enumCase(value, "a3", a3);
1354 }
1355 };
1356 template <>
1357 struct ScalarEnumerationTraits<BFlags> {
1358 static void enumeration(IO &io, BFlags &value) {
1359 io.enumCase(value, "b1", b1);
1360 io.enumCase(value, "b2", b2);
1361 io.enumCase(value, "b3", b3);
1362 }
1363 };
1364 template <>
1365 struct ScalarEnumerationTraits<Kind> {
1366 static void enumeration(IO &io, Kind &value) {
1367 io.enumCase(value, "A", kindA);
1368 io.enumCase(value, "B", kindB);
1369 }
1370 };
1371 template <>
1372 struct MappingTraits<KindAndFlags> {
1373 static void mapping(IO &io, KindAndFlags& kf) {
1374 io.mapRequired("kind", kf.kind);
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001375 // Type of "flags" field varies depending on "kind" field.
David Greene4162c2d2013-01-10 18:17:54 +00001376 // Use memcpy here to avoid breaking strict aliasing rules.
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001377 if (kf.kind == kindA) {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001378 AFlags aflags = static_cast<AFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001379 io.mapRequired("flags", aflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001380 kf.flags = aflags;
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001381 } else {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001382 BFlags bflags = static_cast<BFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001383 io.mapRequired("flags", bflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001384 kf.flags = bflags;
David Greene4162c2d2013-01-10 18:17:54 +00001385 }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001386 }
1387 };
1388}
1389}
1390
1391
1392//
1393// Test the reading of a yaml sequence dynamic types
1394//
1395TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1396 KindAndFlagsSequence seq;
1397 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1398 yin >> seq;
1399
1400 EXPECT_FALSE(yin.error());
1401 EXPECT_EQ(seq.size(), 2UL);
1402 EXPECT_EQ(seq[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001403 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001404 EXPECT_EQ(seq[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001405 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001406}
1407
1408//
1409// Test writing then reading back dynamic types
1410//
1411TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1412 std::string intermediate;
1413 {
1414 KindAndFlagsSequence seq;
1415 seq.push_back(KindAndFlags(kindA,a1));
1416 seq.push_back(KindAndFlags(kindB,b1));
1417 seq.push_back(KindAndFlags(kindA,a2));
1418 seq.push_back(KindAndFlags(kindB,b2));
1419 seq.push_back(KindAndFlags(kindA,a3));
1420
1421 llvm::raw_string_ostream ostr(intermediate);
1422 Output yout(ostr);
1423 yout << seq;
1424 }
1425 {
1426 Input yin(intermediate);
1427 KindAndFlagsSequence seq2;
1428 yin >> seq2;
1429
1430 EXPECT_FALSE(yin.error());
1431 EXPECT_EQ(seq2.size(), 5UL);
1432 EXPECT_EQ(seq2[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001433 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001434 EXPECT_EQ(seq2[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001435 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001436 EXPECT_EQ(seq2[2].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001437 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001438 EXPECT_EQ(seq2[3].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001439 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001440 EXPECT_EQ(seq2[4].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001441 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001442 }
1443}
1444
1445
1446//===----------------------------------------------------------------------===//
1447// Test document list
1448//===----------------------------------------------------------------------===//
1449
1450struct FooBarMap {
1451 int foo;
1452 int bar;
1453};
1454typedef std::vector<FooBarMap> FooBarMapDocumentList;
1455
1456LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1457
1458
1459namespace llvm {
1460namespace yaml {
1461 template <>
1462 struct MappingTraits<FooBarMap> {
1463 static void mapping(IO &io, FooBarMap& fb) {
1464 io.mapRequired("foo", fb.foo);
1465 io.mapRequired("bar", fb.bar);
1466 }
1467 };
1468}
1469}
1470
1471
1472//
1473// Test the reading of a yaml mapping
1474//
1475TEST(YAMLIO, TestDocRead) {
1476 FooBarMap doc;
1477 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1478 yin >> doc;
1479
1480 EXPECT_FALSE(yin.error());
1481 EXPECT_EQ(doc.foo, 3);
1482 EXPECT_EQ(doc.bar,5);
1483}
1484
1485
1486
1487//
1488// Test writing then reading back a sequence of mappings
1489//
1490TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1491 std::string intermediate;
1492 {
1493 FooBarMap doc1;
1494 doc1.foo = 10;
1495 doc1.bar = -3;
1496 FooBarMap doc2;
1497 doc2.foo = 257;
1498 doc2.bar = 0;
1499 std::vector<FooBarMap> docList;
1500 docList.push_back(doc1);
1501 docList.push_back(doc2);
1502
1503 llvm::raw_string_ostream ostr(intermediate);
1504 Output yout(ostr);
1505 yout << docList;
1506 }
1507
1508
1509 {
1510 Input yin(intermediate);
1511 std::vector<FooBarMap> docList2;
1512 yin >> docList2;
1513
1514 EXPECT_FALSE(yin.error());
1515 EXPECT_EQ(docList2.size(), 2UL);
1516 FooBarMap& map1 = docList2[0];
1517 FooBarMap& map2 = docList2[1];
1518 EXPECT_EQ(map1.foo, 10);
1519 EXPECT_EQ(map1.bar, -3);
1520 EXPECT_EQ(map2.foo, 257);
1521 EXPECT_EQ(map2.bar, 0);
1522 }
1523}
1524
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001525//===----------------------------------------------------------------------===//
1526// Test document tags
1527//===----------------------------------------------------------------------===//
1528
1529struct MyDouble {
1530 MyDouble() : value(0.0) { }
1531 MyDouble(double x) : value(x) { }
1532 double value;
1533};
1534
Nick Kledzik4a9f00d2013-11-14 03:03:05 +00001535LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001536
1537
1538namespace llvm {
1539namespace yaml {
1540 template <>
1541 struct MappingTraits<MyDouble> {
1542 static void mapping(IO &io, MyDouble &d) {
1543 if (io.mapTag("!decimal", true)) {
1544 mappingDecimal(io, d);
1545 } else if (io.mapTag("!fraction")) {
1546 mappingFraction(io, d);
1547 }
1548 }
1549 static void mappingDecimal(IO &io, MyDouble &d) {
1550 io.mapRequired("value", d.value);
1551 }
1552 static void mappingFraction(IO &io, MyDouble &d) {
1553 double num, denom;
1554 io.mapRequired("numerator", num);
1555 io.mapRequired("denominator", denom);
1556 // convert fraction to double
1557 d.value = num/denom;
1558 }
1559 };
1560 }
1561}
1562
1563
1564//
1565// Test the reading of two different tagged yaml documents.
1566//
1567TEST(YAMLIO, TestTaggedDocuments) {
1568 std::vector<MyDouble> docList;
1569 Input yin("--- !decimal\nvalue: 3.0\n"
1570 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1571 yin >> docList;
1572 EXPECT_FALSE(yin.error());
1573 EXPECT_EQ(docList.size(), 2UL);
1574 EXPECT_EQ(docList[0].value, 3.0);
1575 EXPECT_EQ(docList[1].value, 4.5);
1576}
1577
1578
1579
1580//
1581// Test writing then reading back tagged documents
1582//
1583TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1584 std::string intermediate;
1585 {
1586 MyDouble a(10.25);
1587 MyDouble b(-3.75);
1588 std::vector<MyDouble> docList;
1589 docList.push_back(a);
1590 docList.push_back(b);
1591
1592 llvm::raw_string_ostream ostr(intermediate);
1593 Output yout(ostr);
1594 yout << docList;
1595 }
1596
1597 {
1598 Input yin(intermediate);
1599 std::vector<MyDouble> docList2;
1600 yin >> docList2;
1601
1602 EXPECT_FALSE(yin.error());
1603 EXPECT_EQ(docList2.size(), 2UL);
1604 EXPECT_EQ(docList2[0].value, 10.25);
1605 EXPECT_EQ(docList2[1].value, -3.75);
1606 }
1607}
1608
1609
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001610//===----------------------------------------------------------------------===//
1611// Test mapping validation
1612//===----------------------------------------------------------------------===//
1613
1614struct MyValidation {
1615 double value;
1616};
1617
1618LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1619
1620namespace llvm {
1621namespace yaml {
1622 template <>
1623 struct MappingTraits<MyValidation> {
1624 static void mapping(IO &io, MyValidation &d) {
1625 io.mapRequired("value", d.value);
1626 }
1627 static StringRef validate(IO &io, MyValidation &d) {
1628 if (d.value < 0)
1629 return "negative value";
1630 return StringRef();
1631 }
1632 };
1633 }
1634}
1635
1636
1637//
1638// Test that validate() is called and complains about the negative value.
1639//
1640TEST(YAMLIO, TestValidatingInput) {
1641 std::vector<MyValidation> docList;
1642 Input yin("--- \nvalue: 3.0\n"
1643 "--- \nvalue: -1.0\n...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001644 nullptr, suppressErrorMessages);
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001645 yin >> docList;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001646 EXPECT_TRUE(!!yin.error());
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001647}
1648
Alex Lorenzb1225082015-05-04 20:11:40 +00001649//===----------------------------------------------------------------------===//
1650// Test flow mapping
1651//===----------------------------------------------------------------------===//
1652
1653struct FlowFooBar {
1654 int foo;
1655 int bar;
1656
1657 FlowFooBar() : foo(0), bar(0) {}
1658 FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1659};
1660
1661typedef std::vector<FlowFooBar> FlowFooBarSequence;
1662
1663LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1664
1665struct FlowFooBarDoc {
1666 FlowFooBar attribute;
1667 FlowFooBarSequence seq;
1668};
1669
1670namespace llvm {
1671namespace yaml {
1672 template <>
1673 struct MappingTraits<FlowFooBar> {
1674 static void mapping(IO &io, FlowFooBar &fb) {
1675 io.mapRequired("foo", fb.foo);
1676 io.mapRequired("bar", fb.bar);
1677 }
1678
1679 static const bool flow = true;
1680 };
1681
1682 template <>
1683 struct MappingTraits<FlowFooBarDoc> {
1684 static void mapping(IO &io, FlowFooBarDoc &fb) {
1685 io.mapRequired("attribute", fb.attribute);
1686 io.mapRequired("seq", fb.seq);
1687 }
1688 };
1689}
1690}
1691
1692//
1693// Test writing then reading back custom mappings
1694//
1695TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1696 std::string intermediate;
1697 {
1698 FlowFooBarDoc doc;
1699 doc.attribute = FlowFooBar(42, 907);
1700 doc.seq.push_back(FlowFooBar(1, 2));
1701 doc.seq.push_back(FlowFooBar(0, 0));
1702 doc.seq.push_back(FlowFooBar(-1, 1024));
1703
1704 llvm::raw_string_ostream ostr(intermediate);
1705 Output yout(ostr);
1706 yout << doc;
1707
1708 // Verify that mappings were written in flow style
1709 ostr.flush();
1710 llvm::StringRef flowOut(intermediate);
1711 EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1712 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1713 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1714 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1715 }
1716
1717 {
1718 Input yin(intermediate);
1719 FlowFooBarDoc doc2;
1720 yin >> doc2;
1721
1722 EXPECT_FALSE(yin.error());
1723 EXPECT_EQ(doc2.attribute.foo, 42);
1724 EXPECT_EQ(doc2.attribute.bar, 907);
1725 EXPECT_EQ(doc2.seq.size(), 3UL);
1726 EXPECT_EQ(doc2.seq[0].foo, 1);
1727 EXPECT_EQ(doc2.seq[0].bar, 2);
1728 EXPECT_EQ(doc2.seq[1].foo, 0);
1729 EXPECT_EQ(doc2.seq[1].bar, 0);
1730 EXPECT_EQ(doc2.seq[2].foo, -1);
1731 EXPECT_EQ(doc2.seq[2].bar, 1024);
1732 }
1733}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001734
1735//===----------------------------------------------------------------------===//
1736// Test error handling
1737//===----------------------------------------------------------------------===//
1738
Nick Kledzikf60a9272012-12-12 20:46:15 +00001739//
1740// Test error handling of unknown enumerated scalar
1741//
1742TEST(YAMLIO, TestColorsReadError) {
1743 ColorMap map;
1744 Input yin("---\n"
1745 "c1: blue\n"
1746 "c2: purple\n"
1747 "c3: green\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001748 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001749 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001750 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001751 yin >> map;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001752 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001753}
1754
1755
1756//
1757// Test error handling of flow sequence with unknown value
1758//
1759TEST(YAMLIO, TestFlagsReadError) {
1760 FlagsMap map;
1761 Input yin("---\n"
1762 "f1: [ big ]\n"
1763 "f2: [ round, hollow ]\n"
1764 "f3: []\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001765 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001766 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001767 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001768 yin >> map;
1769
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001770 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001771}
1772
1773
1774//
1775// Test error handling reading built-in uint8_t type
1776//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001777TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1778 std::vector<uint8_t> seq;
1779 Input yin("---\n"
1780 "- 255\n"
1781 "- 0\n"
1782 "- 257\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001783 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001784 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001785 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001786 yin >> seq;
1787
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001788 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001789}
1790
1791
1792//
1793// Test error handling reading built-in uint16_t type
1794//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001795TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1796 std::vector<uint16_t> seq;
1797 Input yin("---\n"
1798 "- 65535\n"
1799 "- 0\n"
1800 "- 66000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001801 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001802 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001803 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001804 yin >> seq;
1805
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001806 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001807}
1808
1809
1810//
1811// Test error handling reading built-in uint32_t type
1812//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001813TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1814 std::vector<uint32_t> seq;
1815 Input yin("---\n"
1816 "- 4000000000\n"
1817 "- 0\n"
1818 "- 5000000000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001819 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001820 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001821 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001822 yin >> seq;
1823
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001824 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001825}
1826
1827
1828//
1829// Test error handling reading built-in uint64_t type
1830//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001831TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1832 std::vector<uint64_t> seq;
1833 Input yin("---\n"
1834 "- 18446744073709551615\n"
1835 "- 0\n"
1836 "- 19446744073709551615\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001837 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001838 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001839 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001840 yin >> seq;
1841
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001842 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001843}
1844
1845
1846//
1847// Test error handling reading built-in int8_t type
1848//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001849TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1850 std::vector<int8_t> seq;
1851 Input yin("---\n"
1852 "- -128\n"
1853 "- 0\n"
1854 "- 127\n"
1855 "- 128\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001856 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001857 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001858 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001859 yin >> seq;
1860
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001861 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001862}
1863
1864//
1865// Test error handling reading built-in int8_t type
1866//
1867TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1868 std::vector<int8_t> seq;
1869 Input yin("---\n"
1870 "- -128\n"
1871 "- 0\n"
1872 "- 127\n"
1873 "- -129\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001874 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001875 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001876 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001877 yin >> seq;
1878
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001879 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001880}
1881
1882
1883//
1884// Test error handling reading built-in int16_t type
1885//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001886TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1887 std::vector<int16_t> seq;
1888 Input yin("---\n"
1889 "- 32767\n"
1890 "- 0\n"
1891 "- -32768\n"
1892 "- -32769\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001893 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001894 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001895 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001896 yin >> seq;
1897
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001898 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001899}
1900
1901
1902//
1903// Test error handling reading built-in int16_t type
1904//
1905TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1906 std::vector<int16_t> seq;
1907 Input yin("---\n"
1908 "- 32767\n"
1909 "- 0\n"
1910 "- -32768\n"
1911 "- 32768\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001912 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001913 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001914 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001915 yin >> seq;
1916
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001917 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001918}
1919
1920
1921//
1922// Test error handling reading built-in int32_t type
1923//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001924TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1925 std::vector<int32_t> seq;
1926 Input yin("---\n"
1927 "- 2147483647\n"
1928 "- 0\n"
1929 "- -2147483648\n"
1930 "- -2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001931 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001932 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001933 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001934 yin >> seq;
1935
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001936 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001937}
1938
1939//
1940// Test error handling reading built-in int32_t type
1941//
1942TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1943 std::vector<int32_t> seq;
1944 Input yin("---\n"
1945 "- 2147483647\n"
1946 "- 0\n"
1947 "- -2147483648\n"
1948 "- 2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001949 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001950 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001951 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001952 yin >> seq;
1953
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001954 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001955}
1956
1957
1958//
1959// Test error handling reading built-in int64_t type
1960//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001961TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1962 std::vector<int64_t> seq;
1963 Input yin("---\n"
1964 "- -9223372036854775808\n"
1965 "- 0\n"
1966 "- 9223372036854775807\n"
1967 "- -9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001968 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001969 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001970 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001971 yin >> seq;
1972
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001973 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001974}
1975
1976//
1977// Test error handling reading built-in int64_t type
1978//
1979TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1980 std::vector<int64_t> seq;
1981 Input yin("---\n"
1982 "- -9223372036854775808\n"
1983 "- 0\n"
1984 "- 9223372036854775807\n"
1985 "- 9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001986 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001987 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001988 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001989 yin >> seq;
1990
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001991 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001992}
1993
1994//
1995// Test error handling reading built-in float type
1996//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001997TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1998 std::vector<float> seq;
1999 Input yin("---\n"
2000 "- 0.0\n"
2001 "- 1000.1\n"
2002 "- -123.456\n"
2003 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002004 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002005 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002006 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002007 yin >> seq;
2008
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002009 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002010}
2011
2012//
2013// Test error handling reading built-in float type
2014//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002015TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
2016 std::vector<double> seq;
2017 Input yin("---\n"
2018 "- 0.0\n"
2019 "- 1000.1\n"
2020 "- -123.456\n"
2021 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002022 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002023 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002024 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002025 yin >> seq;
2026
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002027 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002028}
2029
2030//
2031// Test error handling reading built-in Hex8 type
2032//
2033LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
2034TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
2035 std::vector<Hex8> seq;
2036 Input yin("---\n"
2037 "- 0x12\n"
2038 "- 0xFE\n"
2039 "- 0x123\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002040 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002041 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002042 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002043 yin >> seq;
2044
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002045 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002046}
2047
2048
2049//
2050// Test error handling reading built-in Hex16 type
2051//
2052LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
2053TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
2054 std::vector<Hex16> seq;
2055 Input yin("---\n"
2056 "- 0x0012\n"
2057 "- 0xFEFF\n"
2058 "- 0x12345\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002059 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002060 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002061 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002062 yin >> seq;
2063
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002064 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002065}
2066
2067//
2068// Test error handling reading built-in Hex32 type
2069//
2070LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
2071TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
2072 std::vector<Hex32> seq;
2073 Input yin("---\n"
2074 "- 0x0012\n"
2075 "- 0xFEFF0000\n"
2076 "- 0x1234556789\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002077 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002078 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002079 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002080 yin >> seq;
2081
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002082 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002083}
2084
2085//
2086// Test error handling reading built-in Hex64 type
2087//
2088LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
2089TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
2090 std::vector<Hex64> seq;
2091 Input yin("---\n"
2092 "- 0x0012\n"
2093 "- 0xFFEEDDCCBBAA9988\n"
2094 "- 0x12345567890ABCDEF0\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002095 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002096 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002097 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002098 yin >> seq;
2099
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002100 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002101}
2102
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002103TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2104 FooBar doc;
2105 {
2106 // We pass the suppressErrorMessages handler to handle the error
2107 // message generated in the constructor of Input.
Craig Topper66f09ad2014-06-08 22:29:17 +00002108 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002109 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002110 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002111 }
2112
2113 {
Craig Topper66f09ad2014-06-08 22:29:17 +00002114 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002115 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002116 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002117 }
2118}
2119
Aaron Ballman0e63e532013-08-15 23:17:53 +00002120struct OptionalTest {
2121 std::vector<int> Numbers;
2122};
2123
2124struct OptionalTestSeq {
2125 std::vector<OptionalTest> Tests;
2126};
2127
Aaron Ballman381f59f2013-08-16 01:53:58 +00002128LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
Aaron Ballman0e63e532013-08-15 23:17:53 +00002129namespace llvm {
2130namespace yaml {
2131 template <>
2132 struct MappingTraits<OptionalTest> {
2133 static void mapping(IO& IO, OptionalTest &OT) {
2134 IO.mapOptional("Numbers", OT.Numbers);
2135 }
2136 };
2137
2138 template <>
2139 struct MappingTraits<OptionalTestSeq> {
2140 static void mapping(IO &IO, OptionalTestSeq &OTS) {
2141 IO.mapOptional("Tests", OTS.Tests);
2142 }
2143 };
2144}
2145}
2146
2147TEST(YAMLIO, SequenceElideTest) {
2148 // Test that writing out a purely optional structure with its fields set to
2149 // default followed by other data is properly read back in.
2150 OptionalTestSeq Seq;
2151 OptionalTest One, Two, Three, Four;
2152 int N[] = {1, 2, 3};
2153 Three.Numbers.assign(N, N + 3);
2154 Seq.Tests.push_back(One);
2155 Seq.Tests.push_back(Two);
2156 Seq.Tests.push_back(Three);
2157 Seq.Tests.push_back(Four);
2158
2159 std::string intermediate;
2160 {
2161 llvm::raw_string_ostream ostr(intermediate);
2162 Output yout(ostr);
2163 yout << Seq;
2164 }
2165
2166 Input yin(intermediate);
2167 OptionalTestSeq Seq2;
2168 yin >> Seq2;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00002169
Aaron Ballman0e63e532013-08-15 23:17:53 +00002170 EXPECT_FALSE(yin.error());
2171
2172 EXPECT_EQ(4UL, Seq2.Tests.size());
2173
2174 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2175 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2176
2177 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2178 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2179 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2180
2181 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2182}
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002183
2184TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2185 FooBar doc;
2186 Input yin("");
2187 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002188 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002189}
2190
2191TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2192 OptionalTest doc;
2193 Input yin("");
2194 yin >> doc;
2195 EXPECT_FALSE(yin.error());
2196}
2197
2198TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2199 std::vector<uint8_t> seq;
Craig Topper66f09ad2014-06-08 22:29:17 +00002200 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002201 yin >> seq;
2202
2203 EXPECT_FALSE(yin.error());
2204 EXPECT_TRUE(seq.empty());
2205}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002206
2207struct FlowMap {
2208 llvm::StringRef str1, str2, str3;
2209 FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2210 : str1(str1), str2(str2), str3(str3) {}
2211};
2212
Frederic Riss3733c032015-05-29 18:14:55 +00002213struct FlowSeq {
2214 llvm::StringRef str;
2215 FlowSeq(llvm::StringRef S) : str(S) {}
2216 FlowSeq() = default;
2217};
2218
Frederic Riss4939e6a2015-05-29 17:56:28 +00002219namespace llvm {
2220namespace yaml {
2221 template <>
2222 struct MappingTraits<FlowMap> {
2223 static void mapping(IO &io, FlowMap &fm) {
2224 io.mapRequired("str1", fm.str1);
2225 io.mapRequired("str2", fm.str2);
2226 io.mapRequired("str3", fm.str3);
2227 }
2228
2229 static const bool flow = true;
2230 };
Frederic Riss4939e6a2015-05-29 17:56:28 +00002231
2232template <>
2233struct ScalarTraits<FlowSeq> {
2234 static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2235 out << value.str;
2236 }
2237 static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2238 value.str = scalar;
2239 return "";
2240 }
2241
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002242 static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
Frederic Riss4939e6a2015-05-29 17:56:28 +00002243};
Frederic Riss3733c032015-05-29 18:14:55 +00002244}
2245}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002246
2247LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2248
2249TEST(YAMLIO, TestWrapFlow) {
2250 std::string out;
2251 llvm::raw_string_ostream ostr(out);
2252 FlowMap Map("This is str1", "This is str2", "This is str3");
2253 std::vector<FlowSeq> Seq;
2254 Seq.emplace_back("This is str1");
2255 Seq.emplace_back("This is str2");
2256 Seq.emplace_back("This is str3");
2257
2258 {
2259 // 20 is just bellow the total length of the first mapping field.
2260 // We should wreap at every element.
2261 Output yout(ostr, nullptr, 15);
2262
2263 yout << Map;
2264 ostr.flush();
2265 EXPECT_EQ(out,
2266 "---\n"
2267 "{ str1: This is str1, \n"
2268 " str2: This is str2, \n"
2269 " str3: This is str3 }\n"
2270 "...\n");
2271 out.clear();
2272
2273 yout << Seq;
2274 ostr.flush();
2275 EXPECT_EQ(out,
2276 "---\n"
2277 "[ This is str1, \n"
2278 " This is str2, \n"
2279 " This is str3 ]\n"
2280 "...\n");
2281 out.clear();
2282 }
2283 {
2284 // 25 will allow the second field to be output on the first line.
2285 Output yout(ostr, nullptr, 25);
2286
2287 yout << Map;
2288 ostr.flush();
2289 EXPECT_EQ(out,
2290 "---\n"
2291 "{ str1: This is str1, str2: This is str2, \n"
2292 " str3: This is str3 }\n"
2293 "...\n");
2294 out.clear();
2295
2296 yout << Seq;
2297 ostr.flush();
2298 EXPECT_EQ(out,
2299 "---\n"
2300 "[ This is str1, This is str2, \n"
2301 " This is str3 ]\n"
2302 "...\n");
2303 out.clear();
2304 }
2305 {
2306 // 0 means no wrapping.
2307 Output yout(ostr, nullptr, 0);
2308
2309 yout << Map;
2310 ostr.flush();
2311 EXPECT_EQ(out,
2312 "---\n"
2313 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2314 "...\n");
2315 out.clear();
2316
2317 yout << Seq;
2318 ostr.flush();
2319 EXPECT_EQ(out,
2320 "---\n"
2321 "[ This is str1, This is str2, This is str3 ]\n"
2322 "...\n");
2323 out.clear();
2324 }
2325}
Zachary Turner35377f82016-09-08 18:22:44 +00002326
2327struct MappingContext {
2328 int A = 0;
2329};
2330struct SimpleMap {
2331 int B = 0;
2332 int C = 0;
2333};
2334
2335struct NestedMap {
2336 NestedMap(MappingContext &Context) : Context(Context) {}
2337 SimpleMap Simple;
2338 MappingContext &Context;
2339};
2340
2341namespace llvm {
2342namespace yaml {
2343template <> struct MappingContextTraits<SimpleMap, MappingContext> {
2344 static void mapping(IO &io, SimpleMap &sm, MappingContext &Context) {
2345 io.mapRequired("B", sm.B);
2346 io.mapRequired("C", sm.C);
2347 ++Context.A;
2348 io.mapRequired("Context", Context.A);
2349 }
2350};
2351
2352template <> struct MappingTraits<NestedMap> {
2353 static void mapping(IO &io, NestedMap &nm) {
2354 io.mapRequired("Simple", nm.Simple, nm.Context);
2355 }
2356};
2357}
2358}
2359
2360TEST(YAMLIO, TestMapWithContext) {
2361 MappingContext Context;
2362 NestedMap Nested(Context);
2363 std::string out;
2364 llvm::raw_string_ostream ostr(out);
2365
2366 Output yout(ostr, nullptr, 15);
2367
2368 yout << Nested;
2369 ostr.flush();
2370 EXPECT_EQ(1, Context.A);
2371 EXPECT_EQ("---\n"
2372 "Simple: \n"
2373 " B: 0\n"
2374 " C: 0\n"
2375 " Context: 1\n"
2376 "...\n",
2377 out);
2378
2379 out.clear();
2380
2381 Nested.Simple.B = 2;
2382 Nested.Simple.C = 3;
2383 yout << Nested;
2384 ostr.flush();
2385 EXPECT_EQ(2, Context.A);
2386 EXPECT_EQ("---\n"
2387 "Simple: \n"
2388 " B: 2\n"
2389 " C: 3\n"
2390 " Context: 2\n"
2391 "...\n",
2392 out);
2393 out.clear();
2394}
Mehdi Amini3ab3fef2016-11-28 21:38:52 +00002395
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +00002396LLVM_YAML_IS_STRING_MAP(int)
2397
2398TEST(YAMLIO, TestCustomMapping) {
2399 std::map<std::string, int> x;
2400 x["foo"] = 1;
2401 x["bar"] = 2;
2402
2403 std::string out;
2404 llvm::raw_string_ostream ostr(out);
2405 Output xout(ostr, nullptr, 0);
2406
2407 xout << x;
2408 ostr.flush();
2409 EXPECT_EQ("---\n"
2410 "bar: 2\n"
2411 "foo: 1\n"
2412 "...\n",
2413 out);
2414
2415 Input yin(out);
2416 std::map<std::string, int> y;
2417 yin >> y;
2418 EXPECT_EQ(2ul, y.size());
2419 EXPECT_EQ(1, y["foo"]);
2420 EXPECT_EQ(2, y["bar"]);
2421}
2422
2423LLVM_YAML_IS_STRING_MAP(FooBar)
2424
2425TEST(YAMLIO, TestCustomMappingStruct) {
2426 std::map<std::string, FooBar> x;
2427 x["foo"].foo = 1;
2428 x["foo"].bar = 2;
2429 x["bar"].foo = 3;
2430 x["bar"].bar = 4;
2431
2432 std::string out;
2433 llvm::raw_string_ostream ostr(out);
2434 Output xout(ostr, nullptr, 0);
2435
2436 xout << x;
2437 ostr.flush();
2438 EXPECT_EQ("---\n"
2439 "bar: \n"
2440 " foo: 3\n"
2441 " bar: 4\n"
2442 "foo: \n"
2443 " foo: 1\n"
2444 " bar: 2\n"
2445 "...\n",
2446 out);
2447
2448 Input yin(out);
2449 std::map<std::string, FooBar> y;
2450 yin >> y;
2451 EXPECT_EQ(2ul, y.size());
2452 EXPECT_EQ(1, y["foo"].foo);
2453 EXPECT_EQ(2, y["foo"].bar);
2454 EXPECT_EQ(3, y["bar"].foo);
2455 EXPECT_EQ(4, y["bar"].bar);
2456}
2457
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002458static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002459 std::string out;
2460 llvm::raw_string_ostream ostr(out);
2461 Output xout(ostr, nullptr, 0);
2462
2463 llvm::yaml::EmptyContext Ctx;
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002464 yamlize(xout, Input, true, Ctx);
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002465
2466 ostr.flush();
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002467
2468 // Make a separate StringRef so we get nice byte-by-byte output.
2469 llvm::StringRef Got(out);
2470 EXPECT_EQ(Expected, Got);
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002471}
2472
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002473TEST(YAMLIO, TestEscaped) {
2474 // Single quote
2475 TestEscaped("@abc@", "'@abc@'");
2476 // No quote
2477 TestEscaped("abc/", "abc/");
2478 // Double quote non-printable
2479 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2480 // Double quote inside single quote
2481 TestEscaped("abc\"fdf", "'abc\"fdf'");
2482 // Double quote inside double quote
2483 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2484 // Single quote inside single quote
2485 TestEscaped("abc'fdf", "'abc''fdf'");
2486 // UTF8
2487 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2488 // UTF8 with single quote inside double quote
2489 TestEscaped("parameter 'параметр' is unused",
2490 "\"parameter 'параметр' is unused\"");
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002491
2492 // String with embedded non-printable multibyte UTF-8 sequence (U+200B
2493 // zero-width space). The thing to test here is that we emit a
2494 // unicode-scalar level escape like \uNNNN (at the YAML level), and don't
2495 // just pass the UTF-8 byte sequence through as with quoted printables.
2496 TestEscaped("foo\u200Bbar", "\"foo\\u200Bbar\"");
2497 {
2498 const unsigned char foobar[10] = {'f', 'o', 'o',
2499 0xE2, 0x80, 0x8B, // UTF-8 of U+200B
2500 'b', 'a', 'r',
2501 0x0};
2502 TestEscaped((char const *)foobar, "\"foo\\u200Bbar\"");
2503 }
Francis Visoiu Mistrihb2b961a2017-12-21 17:14:09 +00002504}