blob: 120773a0c8dd89600248e9a03de7cf10b7f87ca5 [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
Nick Kledzikf60a9272012-12-12 20:46:15 +000010#include "llvm/ADT/Twine.h"
11#include "llvm/Support/Casting.h"
Zachary Turner4fbf61d2016-06-07 19:32:09 +000012#include "llvm/Support/Endian.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000013#include "llvm/Support/Format.h"
14#include "llvm/Support/YAMLTraits.h"
15#include "gtest/gtest.h"
16
Nick Kledzikf60a9272012-12-12 20:46:15 +000017using llvm::yaml::Input;
18using llvm::yaml::Output;
19using llvm::yaml::IO;
20using llvm::yaml::MappingTraits;
21using llvm::yaml::MappingNormalization;
22using llvm::yaml::ScalarTraits;
23using llvm::yaml::Hex8;
24using llvm::yaml::Hex16;
25using llvm::yaml::Hex32;
26using llvm::yaml::Hex64;
27
28
Nick Kledzik7cd45f22013-11-21 00:28:07 +000029
30
31static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
32}
33
34
35
Nick Kledzikf60a9272012-12-12 20:46:15 +000036//===----------------------------------------------------------------------===//
37// Test MappingTraits
38//===----------------------------------------------------------------------===//
39
40struct FooBar {
41 int foo;
42 int bar;
43};
44typedef std::vector<FooBar> FooBarSequence;
45
46LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
47
Justin Bogner64d2cdf2015-03-02 17:26:43 +000048struct FooBarContainer {
49 FooBarSequence fbs;
50};
Nick Kledzikf60a9272012-12-12 20:46:15 +000051
52namespace llvm {
53namespace yaml {
54 template <>
55 struct MappingTraits<FooBar> {
56 static void mapping(IO &io, FooBar& fb) {
57 io.mapRequired("foo", fb.foo);
58 io.mapRequired("bar", fb.bar);
59 }
60 };
Justin Bogner64d2cdf2015-03-02 17:26:43 +000061
62 template <> struct MappingTraits<FooBarContainer> {
63 static void mapping(IO &io, FooBarContainer &fb) {
64 io.mapRequired("fbs", fb.fbs);
65 }
66 };
Nick Kledzikf60a9272012-12-12 20:46:15 +000067}
68}
69
70
71//
72// Test the reading of a yaml mapping
73//
74TEST(YAMLIO, TestMapRead) {
75 FooBar doc;
Alexander Kornienko681e37c2013-11-18 15:50:04 +000076 {
77 Input yin("---\nfoo: 3\nbar: 5\n...\n");
78 yin >> doc;
Nick Kledzikf60a9272012-12-12 20:46:15 +000079
Alexander Kornienko681e37c2013-11-18 15:50:04 +000080 EXPECT_FALSE(yin.error());
81 EXPECT_EQ(doc.foo, 3);
82 EXPECT_EQ(doc.bar, 5);
83 }
84
85 {
86 Input yin("{foo: 3, bar: 5}");
87 yin >> doc;
88
89 EXPECT_FALSE(yin.error());
90 EXPECT_EQ(doc.foo, 3);
91 EXPECT_EQ(doc.bar, 5);
92 }
Nick Kledzikf60a9272012-12-12 20:46:15 +000093}
94
Rafael Espindolaa97373f2014-08-08 13:58:00 +000095TEST(YAMLIO, TestMalformedMapRead) {
96 FooBar doc;
97 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
98 yin >> doc;
99 EXPECT_TRUE(!!yin.error());
100}
101
Nick Kledzikf60a9272012-12-12 20:46:15 +0000102//
103// Test the reading of a yaml sequence of mappings
104//
105TEST(YAMLIO, TestSequenceMapRead) {
106 FooBarSequence seq;
107 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
108 yin >> seq;
109
110 EXPECT_FALSE(yin.error());
111 EXPECT_EQ(seq.size(), 2UL);
112 FooBar& map1 = seq[0];
113 FooBar& map2 = seq[1];
114 EXPECT_EQ(map1.foo, 3);
115 EXPECT_EQ(map1.bar, 5);
116 EXPECT_EQ(map2.foo, 7);
117 EXPECT_EQ(map2.bar, 9);
118}
119
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000120//
121// Test the reading of a map containing a yaml sequence of mappings
122//
123TEST(YAMLIO, TestContainerSequenceMapRead) {
124 {
125 FooBarContainer cont;
126 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
127 yin2 >> cont;
128
129 EXPECT_FALSE(yin2.error());
130 EXPECT_EQ(cont.fbs.size(), 2UL);
131 EXPECT_EQ(cont.fbs[0].foo, 3);
132 EXPECT_EQ(cont.fbs[0].bar, 5);
133 EXPECT_EQ(cont.fbs[1].foo, 7);
134 EXPECT_EQ(cont.fbs[1].bar, 9);
135 }
136
137 {
138 FooBarContainer cont;
139 Input yin("---\nfbs:\n...\n");
140 yin >> cont;
141 // Okay: Empty node represents an empty array.
142 EXPECT_FALSE(yin.error());
143 EXPECT_EQ(cont.fbs.size(), 0UL);
144 }
145
146 {
147 FooBarContainer cont;
148 Input yin("---\nfbs: !!null null\n...\n");
149 yin >> cont;
150 // Okay: null represents an empty array.
151 EXPECT_FALSE(yin.error());
152 EXPECT_EQ(cont.fbs.size(), 0UL);
153 }
154
155 {
156 FooBarContainer cont;
157 Input yin("---\nfbs: ~\n...\n");
158 yin >> cont;
159 // Okay: null represents an empty array.
160 EXPECT_FALSE(yin.error());
161 EXPECT_EQ(cont.fbs.size(), 0UL);
162 }
163
164 {
165 FooBarContainer cont;
166 Input yin("---\nfbs: null\n...\n");
167 yin >> cont;
168 // Okay: null represents an empty array.
169 EXPECT_FALSE(yin.error());
170 EXPECT_EQ(cont.fbs.size(), 0UL);
171 }
172}
173
174//
175// Test the reading of a map containing a malformed yaml sequence
176//
177TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
178 {
179 FooBarContainer cont;
180 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
181 suppressErrorMessages);
182 yin >> cont;
183 // Error: fbs is not a sequence.
184 EXPECT_TRUE(!!yin.error());
185 EXPECT_EQ(cont.fbs.size(), 0UL);
186 }
187
188 {
189 FooBarContainer cont;
190 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
191 yin >> cont;
192 // This should be an error.
193 EXPECT_TRUE(!!yin.error());
194 EXPECT_EQ(cont.fbs.size(), 0UL);
195 }
196}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000197
198//
199// Test writing then reading back a sequence of mappings
200//
201TEST(YAMLIO, TestSequenceMapWriteAndRead) {
202 std::string intermediate;
203 {
204 FooBar entry1;
205 entry1.foo = 10;
206 entry1.bar = -3;
207 FooBar entry2;
208 entry2.foo = 257;
209 entry2.bar = 0;
210 FooBarSequence seq;
211 seq.push_back(entry1);
212 seq.push_back(entry2);
213
214 llvm::raw_string_ostream ostr(intermediate);
215 Output yout(ostr);
216 yout << seq;
217 }
218
219 {
220 Input yin(intermediate);
221 FooBarSequence seq2;
222 yin >> seq2;
223
224 EXPECT_FALSE(yin.error());
225 EXPECT_EQ(seq2.size(), 2UL);
226 FooBar& map1 = seq2[0];
227 FooBar& map2 = seq2[1];
228 EXPECT_EQ(map1.foo, 10);
229 EXPECT_EQ(map1.bar, -3);
230 EXPECT_EQ(map2.foo, 257);
231 EXPECT_EQ(map2.bar, 0);
232 }
233}
234
Alex Bradbury16843172017-07-17 11:41:30 +0000235//
236// Test YAML filename handling.
237//
238static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) {
239 EXPECT_EQ(Error.getFilename(), "foo.yaml");
240}
241
242TEST(YAMLIO, TestGivenFilename) {
243 auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
244 Input yin(*Buffer, nullptr, testErrorFilename);
245 FooBar Value;
246 yin >> Value;
247
248 EXPECT_TRUE(!!yin.error());
249}
250
Nick Kledzikf60a9272012-12-12 20:46:15 +0000251
252//===----------------------------------------------------------------------===//
253// Test built-in types
254//===----------------------------------------------------------------------===//
255
256struct BuiltInTypes {
257 llvm::StringRef str;
John Thompson48e018a2013-11-19 17:28:21 +0000258 std::string stdstr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000259 uint64_t u64;
260 uint32_t u32;
261 uint16_t u16;
262 uint8_t u8;
263 bool b;
264 int64_t s64;
265 int32_t s32;
266 int16_t s16;
267 int8_t s8;
268 float f;
269 double d;
270 Hex8 h8;
271 Hex16 h16;
272 Hex32 h32;
273 Hex64 h64;
274};
275
276namespace llvm {
277namespace yaml {
278 template <>
279 struct MappingTraits<BuiltInTypes> {
280 static void mapping(IO &io, BuiltInTypes& bt) {
281 io.mapRequired("str", bt.str);
John Thompson48e018a2013-11-19 17:28:21 +0000282 io.mapRequired("stdstr", bt.stdstr);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000283 io.mapRequired("u64", bt.u64);
284 io.mapRequired("u32", bt.u32);
285 io.mapRequired("u16", bt.u16);
286 io.mapRequired("u8", bt.u8);
287 io.mapRequired("b", bt.b);
288 io.mapRequired("s64", bt.s64);
289 io.mapRequired("s32", bt.s32);
290 io.mapRequired("s16", bt.s16);
291 io.mapRequired("s8", bt.s8);
292 io.mapRequired("f", bt.f);
293 io.mapRequired("d", bt.d);
294 io.mapRequired("h8", bt.h8);
295 io.mapRequired("h16", bt.h16);
296 io.mapRequired("h32", bt.h32);
297 io.mapRequired("h64", bt.h64);
298 }
299 };
300}
301}
302
303
304//
305// Test the reading of all built-in scalar conversions
306//
307TEST(YAMLIO, TestReadBuiltInTypes) {
308 BuiltInTypes map;
309 Input yin("---\n"
310 "str: hello there\n"
John Thompson48e018a2013-11-19 17:28:21 +0000311 "stdstr: hello where?\n"
Nick Kledzikf60a9272012-12-12 20:46:15 +0000312 "u64: 5000000000\n"
313 "u32: 4000000000\n"
314 "u16: 65000\n"
315 "u8: 255\n"
316 "b: false\n"
317 "s64: -5000000000\n"
318 "s32: -2000000000\n"
319 "s16: -32000\n"
320 "s8: -127\n"
321 "f: 137.125\n"
322 "d: -2.8625\n"
323 "h8: 0xFF\n"
324 "h16: 0x8765\n"
325 "h32: 0xFEDCBA98\n"
326 "h64: 0xFEDCBA9876543210\n"
327 "...\n");
328 yin >> map;
329
330 EXPECT_FALSE(yin.error());
331 EXPECT_TRUE(map.str.equals("hello there"));
John Thompson48e018a2013-11-19 17:28:21 +0000332 EXPECT_TRUE(map.stdstr == "hello where?");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000333 EXPECT_EQ(map.u64, 5000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000334 EXPECT_EQ(map.u32, 4000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000335 EXPECT_EQ(map.u16, 65000);
336 EXPECT_EQ(map.u8, 255);
337 EXPECT_EQ(map.b, false);
338 EXPECT_EQ(map.s64, -5000000000LL);
339 EXPECT_EQ(map.s32, -2000000000L);
340 EXPECT_EQ(map.s16, -32000);
341 EXPECT_EQ(map.s8, -127);
342 EXPECT_EQ(map.f, 137.125);
343 EXPECT_EQ(map.d, -2.8625);
344 EXPECT_EQ(map.h8, Hex8(255));
345 EXPECT_EQ(map.h16, Hex16(0x8765));
346 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
347 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
348}
349
350
351//
352// Test writing then reading back all built-in scalar types
353//
354TEST(YAMLIO, TestReadWriteBuiltInTypes) {
355 std::string intermediate;
356 {
357 BuiltInTypes map;
358 map.str = "one two";
John Thompson48e018a2013-11-19 17:28:21 +0000359 map.stdstr = "three four";
Nick Kledzikbed953d2012-12-17 22:11:17 +0000360 map.u64 = 6000000000ULL;
361 map.u32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000362 map.u16 = 50000;
363 map.u8 = 254;
364 map.b = true;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000365 map.s64 = -6000000000LL;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000366 map.s32 = -2000000000;
367 map.s16 = -32000;
368 map.s8 = -128;
369 map.f = 3.25;
370 map.d = -2.8625;
371 map.h8 = 254;
372 map.h16 = 50000;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000373 map.h32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000374 map.h64 = 6000000000LL;
375
376 llvm::raw_string_ostream ostr(intermediate);
377 Output yout(ostr);
378 yout << map;
379 }
380
381 {
382 Input yin(intermediate);
383 BuiltInTypes map;
384 yin >> map;
385
386 EXPECT_FALSE(yin.error());
387 EXPECT_TRUE(map.str.equals("one two"));
John Thompson48e018a2013-11-19 17:28:21 +0000388 EXPECT_TRUE(map.stdstr == "three four");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000389 EXPECT_EQ(map.u64, 6000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000390 EXPECT_EQ(map.u32, 3000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000391 EXPECT_EQ(map.u16, 50000);
392 EXPECT_EQ(map.u8, 254);
393 EXPECT_EQ(map.b, true);
394 EXPECT_EQ(map.s64, -6000000000LL);
395 EXPECT_EQ(map.s32, -2000000000L);
396 EXPECT_EQ(map.s16, -32000);
397 EXPECT_EQ(map.s8, -128);
398 EXPECT_EQ(map.f, 3.25);
399 EXPECT_EQ(map.d, -2.8625);
400 EXPECT_EQ(map.h8, Hex8(254));
401 EXPECT_EQ(map.h16, Hex16(50000));
Nick Kledzikbed953d2012-12-17 22:11:17 +0000402 EXPECT_EQ(map.h32, Hex32(3000000000U));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000403 EXPECT_EQ(map.h64, Hex64(6000000000LL));
404 }
405}
406
Zachary Turner4fbf61d2016-06-07 19:32:09 +0000407//===----------------------------------------------------------------------===//
408// Test endian-aware types
409//===----------------------------------------------------------------------===//
410
411struct EndianTypes {
412 typedef llvm::support::detail::packed_endian_specific_integral<
413 float, llvm::support::little, llvm::support::unaligned>
414 ulittle_float;
415 typedef llvm::support::detail::packed_endian_specific_integral<
416 double, llvm::support::little, llvm::support::unaligned>
417 ulittle_double;
418
419 llvm::support::ulittle64_t u64;
420 llvm::support::ulittle32_t u32;
421 llvm::support::ulittle16_t u16;
422 llvm::support::little64_t s64;
423 llvm::support::little32_t s32;
424 llvm::support::little16_t s16;
425 ulittle_float f;
426 ulittle_double d;
427};
428
429namespace llvm {
430namespace yaml {
431template <> struct MappingTraits<EndianTypes> {
432 static void mapping(IO &io, EndianTypes &et) {
433 io.mapRequired("u64", et.u64);
434 io.mapRequired("u32", et.u32);
435 io.mapRequired("u16", et.u16);
436 io.mapRequired("s64", et.s64);
437 io.mapRequired("s32", et.s32);
438 io.mapRequired("s16", et.s16);
439 io.mapRequired("f", et.f);
440 io.mapRequired("d", et.d);
441 }
442};
443}
444}
445
446//
447// Test the reading of all endian scalar conversions
448//
449TEST(YAMLIO, TestReadEndianTypes) {
450 EndianTypes map;
451 Input yin("---\n"
452 "u64: 5000000000\n"
453 "u32: 4000000000\n"
454 "u16: 65000\n"
455 "s64: -5000000000\n"
456 "s32: -2000000000\n"
457 "s16: -32000\n"
458 "f: 3.25\n"
459 "d: -2.8625\n"
460 "...\n");
461 yin >> map;
462
463 EXPECT_FALSE(yin.error());
464 EXPECT_EQ(map.u64, 5000000000ULL);
465 EXPECT_EQ(map.u32, 4000000000U);
466 EXPECT_EQ(map.u16, 65000);
467 EXPECT_EQ(map.s64, -5000000000LL);
468 EXPECT_EQ(map.s32, -2000000000L);
469 EXPECT_EQ(map.s16, -32000);
470 EXPECT_EQ(map.f, 3.25f);
471 EXPECT_EQ(map.d, -2.8625);
472}
473
474//
475// Test writing then reading back all endian-aware scalar types
476//
477TEST(YAMLIO, TestReadWriteEndianTypes) {
478 std::string intermediate;
479 {
480 EndianTypes map;
481 map.u64 = 6000000000ULL;
482 map.u32 = 3000000000U;
483 map.u16 = 50000;
484 map.s64 = -6000000000LL;
485 map.s32 = -2000000000;
486 map.s16 = -32000;
487 map.f = 3.25f;
488 map.d = -2.8625;
489
490 llvm::raw_string_ostream ostr(intermediate);
491 Output yout(ostr);
492 yout << map;
493 }
494
495 {
496 Input yin(intermediate);
497 EndianTypes map;
498 yin >> map;
499
500 EXPECT_FALSE(yin.error());
501 EXPECT_EQ(map.u64, 6000000000ULL);
502 EXPECT_EQ(map.u32, 3000000000U);
503 EXPECT_EQ(map.u16, 50000);
504 EXPECT_EQ(map.s64, -6000000000LL);
505 EXPECT_EQ(map.s32, -2000000000L);
506 EXPECT_EQ(map.s16, -32000);
507 EXPECT_EQ(map.f, 3.25f);
508 EXPECT_EQ(map.d, -2.8625);
509 }
510}
511
Rui Ueyama106eded2013-09-11 04:00:08 +0000512struct StringTypes {
513 llvm::StringRef str1;
514 llvm::StringRef str2;
515 llvm::StringRef str3;
516 llvm::StringRef str4;
517 llvm::StringRef str5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000518 llvm::StringRef str6;
David Majnemer77880332014-04-10 07:37:33 +0000519 llvm::StringRef str7;
520 llvm::StringRef str8;
521 llvm::StringRef str9;
522 llvm::StringRef str10;
523 llvm::StringRef str11;
John Thompson48e018a2013-11-19 17:28:21 +0000524 std::string stdstr1;
525 std::string stdstr2;
526 std::string stdstr3;
527 std::string stdstr4;
528 std::string stdstr5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000529 std::string stdstr6;
David Majnemer77880332014-04-10 07:37:33 +0000530 std::string stdstr7;
531 std::string stdstr8;
532 std::string stdstr9;
533 std::string stdstr10;
534 std::string stdstr11;
Rui Ueyama106eded2013-09-11 04:00:08 +0000535};
Nick Kledzikf60a9272012-12-12 20:46:15 +0000536
Rui Ueyama106eded2013-09-11 04:00:08 +0000537namespace llvm {
538namespace yaml {
539 template <>
540 struct MappingTraits<StringTypes> {
541 static void mapping(IO &io, StringTypes& st) {
542 io.mapRequired("str1", st.str1);
543 io.mapRequired("str2", st.str2);
544 io.mapRequired("str3", st.str3);
545 io.mapRequired("str4", st.str4);
546 io.mapRequired("str5", st.str5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000547 io.mapRequired("str6", st.str6);
David Majnemer77880332014-04-10 07:37:33 +0000548 io.mapRequired("str7", st.str7);
549 io.mapRequired("str8", st.str8);
550 io.mapRequired("str9", st.str9);
551 io.mapRequired("str10", st.str10);
552 io.mapRequired("str11", st.str11);
John Thompson48e018a2013-11-19 17:28:21 +0000553 io.mapRequired("stdstr1", st.stdstr1);
554 io.mapRequired("stdstr2", st.stdstr2);
555 io.mapRequired("stdstr3", st.stdstr3);
556 io.mapRequired("stdstr4", st.stdstr4);
557 io.mapRequired("stdstr5", st.stdstr5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000558 io.mapRequired("stdstr6", st.stdstr6);
David Majnemer77880332014-04-10 07:37:33 +0000559 io.mapRequired("stdstr7", st.stdstr7);
560 io.mapRequired("stdstr8", st.stdstr8);
561 io.mapRequired("stdstr9", st.stdstr9);
562 io.mapRequired("stdstr10", st.stdstr10);
563 io.mapRequired("stdstr11", st.stdstr11);
Rui Ueyama106eded2013-09-11 04:00:08 +0000564 }
565 };
566}
567}
568
569TEST(YAMLIO, TestReadWriteStringTypes) {
570 std::string intermediate;
571 {
572 StringTypes map;
573 map.str1 = "'aaa";
574 map.str2 = "\"bbb";
575 map.str3 = "`ccc";
576 map.str4 = "@ddd";
577 map.str5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000578 map.str6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000579 map.str7 = "true";
580 map.str8 = "FALSE";
581 map.str9 = "~";
582 map.str10 = "0.2e20";
583 map.str11 = "0x30";
John Thompson48e018a2013-11-19 17:28:21 +0000584 map.stdstr1 = "'eee";
585 map.stdstr2 = "\"fff";
586 map.stdstr3 = "`ggg";
587 map.stdstr4 = "@hhh";
588 map.stdstr5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000589 map.stdstr6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000590 map.stdstr7 = "true";
591 map.stdstr8 = "FALSE";
592 map.stdstr9 = "~";
593 map.stdstr10 = "0.2e20";
594 map.stdstr11 = "0x30";
Rui Ueyama106eded2013-09-11 04:00:08 +0000595
596 llvm::raw_string_ostream ostr(intermediate);
597 Output yout(ostr);
598 yout << map;
599 }
600
601 llvm::StringRef flowOut(intermediate);
602 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
603 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
604 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
605 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
606 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000607 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
David Majnemer77880332014-04-10 07:37:33 +0000608 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
609 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
610 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
611 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
612 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
John Thompson48e018a2013-11-19 17:28:21 +0000613 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
614 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
615 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
616 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
617 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000618 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
Rui Ueyama106eded2013-09-11 04:00:08 +0000619
620 {
621 Input yin(intermediate);
622 StringTypes map;
623 yin >> map;
624
625 EXPECT_FALSE(yin.error());
626 EXPECT_TRUE(map.str1.equals("'aaa"));
627 EXPECT_TRUE(map.str2.equals("\"bbb"));
628 EXPECT_TRUE(map.str3.equals("`ccc"));
629 EXPECT_TRUE(map.str4.equals("@ddd"));
630 EXPECT_TRUE(map.str5.equals(""));
David Majnemer97d8ee32014-04-09 17:04:27 +0000631 EXPECT_TRUE(map.str6.equals("0000000004000000"));
John Thompson48e018a2013-11-19 17:28:21 +0000632 EXPECT_TRUE(map.stdstr1 == "'eee");
633 EXPECT_TRUE(map.stdstr2 == "\"fff");
634 EXPECT_TRUE(map.stdstr3 == "`ggg");
635 EXPECT_TRUE(map.stdstr4 == "@hhh");
636 EXPECT_TRUE(map.stdstr5 == "");
David Majnemer97d8ee32014-04-09 17:04:27 +0000637 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
Rui Ueyama106eded2013-09-11 04:00:08 +0000638 }
639}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000640
641//===----------------------------------------------------------------------===//
642// Test ScalarEnumerationTraits
643//===----------------------------------------------------------------------===//
644
645enum Colors {
646 cRed,
647 cBlue,
648 cGreen,
649 cYellow
650};
651
652struct ColorMap {
653 Colors c1;
654 Colors c2;
655 Colors c3;
656 Colors c4;
657 Colors c5;
658 Colors c6;
659};
660
661namespace llvm {
662namespace yaml {
663 template <>
664 struct ScalarEnumerationTraits<Colors> {
665 static void enumeration(IO &io, Colors &value) {
666 io.enumCase(value, "red", cRed);
667 io.enumCase(value, "blue", cBlue);
668 io.enumCase(value, "green", cGreen);
669 io.enumCase(value, "yellow",cYellow);
670 }
671 };
672 template <>
673 struct MappingTraits<ColorMap> {
674 static void mapping(IO &io, ColorMap& c) {
675 io.mapRequired("c1", c.c1);
676 io.mapRequired("c2", c.c2);
677 io.mapRequired("c3", c.c3);
678 io.mapOptional("c4", c.c4, cBlue); // supplies default
679 io.mapOptional("c5", c.c5, cYellow); // supplies default
680 io.mapOptional("c6", c.c6, cRed); // supplies default
681 }
682 };
683}
684}
685
686
687//
688// Test reading enumerated scalars
689//
690TEST(YAMLIO, TestEnumRead) {
691 ColorMap map;
692 Input yin("---\n"
693 "c1: blue\n"
694 "c2: red\n"
695 "c3: green\n"
696 "c5: yellow\n"
697 "...\n");
698 yin >> map;
699
700 EXPECT_FALSE(yin.error());
701 EXPECT_EQ(cBlue, map.c1);
702 EXPECT_EQ(cRed, map.c2);
703 EXPECT_EQ(cGreen, map.c3);
704 EXPECT_EQ(cBlue, map.c4); // tests default
705 EXPECT_EQ(cYellow,map.c5); // tests overridden
706 EXPECT_EQ(cRed, map.c6); // tests default
707}
708
709
710
711//===----------------------------------------------------------------------===//
712// Test ScalarBitSetTraits
713//===----------------------------------------------------------------------===//
714
715enum MyFlags {
716 flagNone = 0,
717 flagBig = 1 << 0,
718 flagFlat = 1 << 1,
719 flagRound = 1 << 2,
720 flagPointy = 1 << 3
721};
722inline MyFlags operator|(MyFlags a, MyFlags b) {
723 return static_cast<MyFlags>(
724 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
725}
726
727struct FlagsMap {
728 MyFlags f1;
729 MyFlags f2;
730 MyFlags f3;
731 MyFlags f4;
732};
733
734
735namespace llvm {
736namespace yaml {
737 template <>
738 struct ScalarBitSetTraits<MyFlags> {
739 static void bitset(IO &io, MyFlags &value) {
740 io.bitSetCase(value, "big", flagBig);
741 io.bitSetCase(value, "flat", flagFlat);
742 io.bitSetCase(value, "round", flagRound);
743 io.bitSetCase(value, "pointy",flagPointy);
744 }
745 };
746 template <>
747 struct MappingTraits<FlagsMap> {
748 static void mapping(IO &io, FlagsMap& c) {
749 io.mapRequired("f1", c.f1);
750 io.mapRequired("f2", c.f2);
751 io.mapRequired("f3", c.f3);
752 io.mapOptional("f4", c.f4, MyFlags(flagRound));
753 }
754 };
755}
756}
757
758
759//
760// Test reading flow sequence representing bit-mask values
761//
762TEST(YAMLIO, TestFlagsRead) {
763 FlagsMap map;
764 Input yin("---\n"
765 "f1: [ big ]\n"
766 "f2: [ round, flat ]\n"
767 "f3: []\n"
768 "...\n");
769 yin >> map;
770
771 EXPECT_FALSE(yin.error());
772 EXPECT_EQ(flagBig, map.f1);
773 EXPECT_EQ(flagRound|flagFlat, map.f2);
774 EXPECT_EQ(flagNone, map.f3); // check empty set
775 EXPECT_EQ(flagRound, map.f4); // check optional key
776}
777
778
779//
780// Test writing then reading back bit-mask values
781//
782TEST(YAMLIO, TestReadWriteFlags) {
783 std::string intermediate;
784 {
785 FlagsMap map;
786 map.f1 = flagBig;
787 map.f2 = flagRound | flagFlat;
788 map.f3 = flagNone;
789 map.f4 = flagNone;
790
791 llvm::raw_string_ostream ostr(intermediate);
792 Output yout(ostr);
793 yout << map;
794 }
795
796 {
797 Input yin(intermediate);
798 FlagsMap map2;
799 yin >> map2;
800
801 EXPECT_FALSE(yin.error());
802 EXPECT_EQ(flagBig, map2.f1);
803 EXPECT_EQ(flagRound|flagFlat, map2.f2);
804 EXPECT_EQ(flagNone, map2.f3);
805 //EXPECT_EQ(flagRound, map2.f4); // check optional key
806 }
807}
808
809
810
811//===----------------------------------------------------------------------===//
812// Test ScalarTraits
813//===----------------------------------------------------------------------===//
814
815struct MyCustomType {
816 int length;
817 int width;
818};
819
820struct MyCustomTypeMap {
821 MyCustomType f1;
822 MyCustomType f2;
823 int f3;
824};
825
826
827namespace llvm {
828namespace yaml {
829 template <>
830 struct MappingTraits<MyCustomTypeMap> {
831 static void mapping(IO &io, MyCustomTypeMap& s) {
832 io.mapRequired("f1", s.f1);
833 io.mapRequired("f2", s.f2);
834 io.mapRequired("f3", s.f3);
835 }
836 };
837 // MyCustomType is formatted as a yaml scalar. A value of
838 // {length=3, width=4} would be represented in yaml as "3 by 4".
839 template<>
840 struct ScalarTraits<MyCustomType> {
841 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
842 out << llvm::format("%d by %d", value.length, value.width);
843 }
844 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
845 size_t byStart = scalar.find("by");
846 if ( byStart != StringRef::npos ) {
847 StringRef lenStr = scalar.slice(0, byStart);
848 lenStr = lenStr.rtrim();
849 if ( lenStr.getAsInteger(0, value.length) ) {
850 return "malformed length";
851 }
852 StringRef widthStr = scalar.drop_front(byStart+2);
853 widthStr = widthStr.ltrim();
854 if ( widthStr.getAsInteger(0, value.width) ) {
855 return "malformed width";
856 }
857 return StringRef();
858 }
859 else {
860 return "malformed by";
861 }
862 }
David Majnemer77880332014-04-10 07:37:33 +0000863 static bool mustQuote(StringRef) { return true; }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000864 };
865}
866}
867
868
869//
870// Test writing then reading back custom values
871//
872TEST(YAMLIO, TestReadWriteMyCustomType) {
873 std::string intermediate;
874 {
875 MyCustomTypeMap map;
876 map.f1.length = 1;
877 map.f1.width = 4;
878 map.f2.length = 100;
879 map.f2.width = 400;
880 map.f3 = 10;
881
882 llvm::raw_string_ostream ostr(intermediate);
883 Output yout(ostr);
884 yout << map;
885 }
886
887 {
888 Input yin(intermediate);
889 MyCustomTypeMap map2;
890 yin >> map2;
891
892 EXPECT_FALSE(yin.error());
893 EXPECT_EQ(1, map2.f1.length);
894 EXPECT_EQ(4, map2.f1.width);
895 EXPECT_EQ(100, map2.f2.length);
896 EXPECT_EQ(400, map2.f2.width);
897 EXPECT_EQ(10, map2.f3);
898 }
899}
900
901
902//===----------------------------------------------------------------------===//
Alex Lorenz68e787b2015-05-14 23:08:22 +0000903// Test BlockScalarTraits
904//===----------------------------------------------------------------------===//
905
906struct MultilineStringType {
907 std::string str;
908};
909
910struct MultilineStringTypeMap {
911 MultilineStringType name;
912 MultilineStringType description;
913 MultilineStringType ingredients;
914 MultilineStringType recipes;
915 MultilineStringType warningLabels;
916 MultilineStringType documentation;
917 int price;
918};
919
920namespace llvm {
921namespace yaml {
922 template <>
923 struct MappingTraits<MultilineStringTypeMap> {
924 static void mapping(IO &io, MultilineStringTypeMap& s) {
925 io.mapRequired("name", s.name);
926 io.mapRequired("description", s.description);
927 io.mapRequired("ingredients", s.ingredients);
928 io.mapRequired("recipes", s.recipes);
929 io.mapRequired("warningLabels", s.warningLabels);
930 io.mapRequired("documentation", s.documentation);
931 io.mapRequired("price", s.price);
932 }
933 };
934
935 // MultilineStringType is formatted as a yaml block literal scalar. A value of
936 // "Hello\nWorld" would be represented in yaml as
937 // |
938 // Hello
939 // World
940 template <>
941 struct BlockScalarTraits<MultilineStringType> {
942 static void output(const MultilineStringType &value, void *ctxt,
943 llvm::raw_ostream &out) {
944 out << value.str;
945 }
946 static StringRef input(StringRef scalar, void *ctxt,
947 MultilineStringType &value) {
948 value.str = scalar.str();
949 return StringRef();
950 }
951 };
952}
953}
954
955LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
956
957//
958// Test writing then reading back custom values
959//
960TEST(YAMLIO, TestReadWriteMultilineStringType) {
961 std::string intermediate;
962 {
963 MultilineStringTypeMap map;
964 map.name.str = "An Item";
965 map.description.str = "Hello\nWorld";
966 map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
967 map.recipes.str = "\n\nTest 1\n\n\n";
968 map.warningLabels.str = "";
969 map.documentation.str = "\n\n";
970 map.price = 350;
971
972 llvm::raw_string_ostream ostr(intermediate);
973 Output yout(ostr);
974 yout << map;
975 }
976 {
977 Input yin(intermediate);
978 MultilineStringTypeMap map2;
979 yin >> map2;
980
981 EXPECT_FALSE(yin.error());
982 EXPECT_EQ(map2.name.str, "An Item\n");
983 EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
984 EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
985 EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
986 EXPECT_TRUE(map2.warningLabels.str.empty());
987 EXPECT_TRUE(map2.documentation.str.empty());
988 EXPECT_EQ(map2.price, 350);
989 }
990}
991
992//
993// Test writing then reading back custom values
994//
995TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
996 std::string intermediate;
997 {
998 std::vector<MultilineStringType> documents;
999 MultilineStringType doc;
1000 doc.str = "Hello\nWorld";
1001 documents.push_back(doc);
1002
1003 llvm::raw_string_ostream ostr(intermediate);
1004 Output yout(ostr);
1005 yout << documents;
1006
1007 // Verify that the block scalar header was written out on the same line
1008 // as the document marker.
1009 EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
1010 }
1011 {
1012 Input yin(intermediate);
1013 std::vector<MultilineStringType> documents2;
1014 yin >> documents2;
1015
1016 EXPECT_FALSE(yin.error());
1017 EXPECT_EQ(documents2.size(), size_t(1));
1018 EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
1019 }
1020}
1021
1022TEST(YAMLIO, TestReadWriteBlockScalarValue) {
1023 std::string intermediate;
1024 {
1025 MultilineStringType doc;
1026 doc.str = "Just a block\nscalar doc";
1027
1028 llvm::raw_string_ostream ostr(intermediate);
1029 Output yout(ostr);
1030 yout << doc;
1031 }
1032 {
1033 Input yin(intermediate);
1034 MultilineStringType doc;
1035 yin >> doc;
1036
1037 EXPECT_FALSE(yin.error());
1038 EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
1039 }
1040}
1041
1042//===----------------------------------------------------------------------===//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001043// Test flow sequences
1044//===----------------------------------------------------------------------===//
1045
1046LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
1047LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
Richard Smithd0c0c132017-06-30 20:56:57 +00001048LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef, MyString)
1049LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001050
1051namespace llvm {
1052namespace yaml {
1053 template<>
1054 struct ScalarTraits<MyNumber> {
1055 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
1056 out << value;
1057 }
1058
1059 static StringRef input(StringRef scalar, void *, MyNumber &value) {
David Blaikieb088ff62012-12-12 22:14:32 +00001060 long long n;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001061 if ( getAsSignedInteger(scalar, 0, n) )
1062 return "invalid number";
1063 value = n;
1064 return StringRef();
1065 }
David Majnemer77880332014-04-10 07:37:33 +00001066
1067 static bool mustQuote(StringRef) { return false; }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001068 };
Richard Smithd0c0c132017-06-30 20:56:57 +00001069
1070 template <> struct ScalarTraits<MyString> {
1071 using Impl = ScalarTraits<StringRef>;
1072 static void output(const MyString &V, void *Ctx, raw_ostream &OS) {
1073 Impl::output(V, Ctx, OS);
1074 }
1075 static StringRef input(StringRef S, void *Ctx, MyString &V) {
1076 return Impl::input(S, Ctx, V.value);
1077 }
1078 static bool mustQuote(StringRef S) { return Impl::mustQuote(S); }
1079 };
Nick Kledzikf60a9272012-12-12 20:46:15 +00001080}
1081}
1082
1083struct NameAndNumbers {
1084 llvm::StringRef name;
Richard Smithd0c0c132017-06-30 20:56:57 +00001085 std::vector<MyString> strings;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001086 std::vector<MyNumber> single;
1087 std::vector<MyNumber> numbers;
1088};
1089
1090namespace llvm {
1091namespace yaml {
1092 template <>
1093 struct MappingTraits<NameAndNumbers> {
1094 static void mapping(IO &io, NameAndNumbers& nn) {
1095 io.mapRequired("name", nn.name);
1096 io.mapRequired("strings", nn.strings);
1097 io.mapRequired("single", nn.single);
1098 io.mapRequired("numbers", nn.numbers);
1099 }
1100 };
1101}
1102}
1103
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001104typedef std::vector<MyNumber> MyNumberFlowSequence;
1105
1106LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1107
1108struct NameAndNumbersFlow {
1109 llvm::StringRef name;
1110 std::vector<MyNumberFlowSequence> sequenceOfNumbers;
1111};
1112
1113namespace llvm {
1114namespace yaml {
1115 template <>
1116 struct MappingTraits<NameAndNumbersFlow> {
1117 static void mapping(IO &io, NameAndNumbersFlow& nn) {
1118 io.mapRequired("name", nn.name);
1119 io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
1120 }
1121 };
1122}
1123}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001124
1125//
1126// Test writing then reading back custom values
1127//
1128TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1129 std::string intermediate;
1130 {
1131 NameAndNumbers map;
1132 map.name = "hello";
1133 map.strings.push_back(llvm::StringRef("one"));
1134 map.strings.push_back(llvm::StringRef("two"));
1135 map.single.push_back(1);
1136 map.numbers.push_back(10);
1137 map.numbers.push_back(-30);
1138 map.numbers.push_back(1024);
1139
1140 llvm::raw_string_ostream ostr(intermediate);
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001141 Output yout(ostr);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001142 yout << map;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001143
Nick Kledzik11964f22013-01-04 19:32:00 +00001144 // Verify sequences were written in flow style
1145 ostr.flush();
1146 llvm::StringRef flowOut(intermediate);
1147 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1148 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001149 }
1150
1151 {
1152 Input yin(intermediate);
1153 NameAndNumbers map2;
1154 yin >> map2;
1155
1156 EXPECT_FALSE(yin.error());
1157 EXPECT_TRUE(map2.name.equals("hello"));
1158 EXPECT_EQ(map2.strings.size(), 2UL);
Richard Smithd0c0c132017-06-30 20:56:57 +00001159 EXPECT_TRUE(map2.strings[0].value.equals("one"));
1160 EXPECT_TRUE(map2.strings[1].value.equals("two"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001161 EXPECT_EQ(map2.single.size(), 1UL);
1162 EXPECT_EQ(1, map2.single[0]);
1163 EXPECT_EQ(map2.numbers.size(), 3UL);
1164 EXPECT_EQ(10, map2.numbers[0]);
1165 EXPECT_EQ(-30, map2.numbers[1]);
1166 EXPECT_EQ(1024, map2.numbers[2]);
1167 }
1168}
1169
1170
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001171//
1172// Test writing then reading back a sequence of flow sequences.
1173//
1174TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1175 std::string intermediate;
1176 {
1177 NameAndNumbersFlow map;
1178 map.name = "hello";
1179 MyNumberFlowSequence single = { 0 };
1180 MyNumberFlowSequence numbers = { 12, 1, -512 };
1181 map.sequenceOfNumbers.push_back(single);
1182 map.sequenceOfNumbers.push_back(numbers);
1183 map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1184
1185 llvm::raw_string_ostream ostr(intermediate);
1186 Output yout(ostr);
1187 yout << map;
1188
1189 // Verify sequences were written in flow style
1190 // and that the parent sequence used '-'.
1191 ostr.flush();
1192 llvm::StringRef flowOut(intermediate);
1193 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1194 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1195 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
1196 }
1197
1198 {
1199 Input yin(intermediate);
1200 NameAndNumbersFlow map2;
1201 yin >> map2;
1202
1203 EXPECT_FALSE(yin.error());
1204 EXPECT_TRUE(map2.name.equals("hello"));
1205 EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1206 EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1207 EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
1208 EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1209 EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
1210 EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
1211 EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1212 EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1213 }
1214}
1215
Nick Kledzikf60a9272012-12-12 20:46:15 +00001216//===----------------------------------------------------------------------===//
1217// Test normalizing/denormalizing
1218//===----------------------------------------------------------------------===//
1219
1220LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1221
1222typedef std::vector<TotalSeconds> SecondsSequence;
1223
Nick Kledzik11964f22013-01-04 19:32:00 +00001224LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001225
1226
1227namespace llvm {
1228namespace yaml {
1229 template <>
1230 struct MappingTraits<TotalSeconds> {
1231
1232 class NormalizedSeconds {
1233 public:
1234 NormalizedSeconds(IO &io)
1235 : hours(0), minutes(0), seconds(0) {
1236 }
1237 NormalizedSeconds(IO &, TotalSeconds &secs)
1238 : hours(secs/3600),
1239 minutes((secs - (hours*3600))/60),
1240 seconds(secs % 60) {
1241 }
1242 TotalSeconds denormalize(IO &) {
1243 return TotalSeconds(hours*3600 + minutes*60 + seconds);
1244 }
1245
1246 uint32_t hours;
1247 uint8_t minutes;
1248 uint8_t seconds;
1249 };
1250
1251 static void mapping(IO &io, TotalSeconds &secs) {
1252 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1253
1254 io.mapOptional("hours", keys->hours, (uint32_t)0);
1255 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
1256 io.mapRequired("seconds", keys->seconds);
1257 }
1258 };
1259}
1260}
1261
1262
1263//
1264// Test the reading of a yaml sequence of mappings
1265//
1266TEST(YAMLIO, TestReadMySecondsSequence) {
1267 SecondsSequence seq;
1268 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1269 yin >> seq;
1270
1271 EXPECT_FALSE(yin.error());
1272 EXPECT_EQ(seq.size(), 2UL);
1273 EXPECT_EQ(seq[0], 3605U);
1274 EXPECT_EQ(seq[1], 59U);
1275}
1276
1277
1278//
1279// Test writing then reading back custom values
1280//
1281TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1282 std::string intermediate;
1283 {
1284 SecondsSequence seq;
1285 seq.push_back(4000);
1286 seq.push_back(500);
1287 seq.push_back(59);
1288
1289 llvm::raw_string_ostream ostr(intermediate);
1290 Output yout(ostr);
1291 yout << seq;
1292 }
1293 {
1294 Input yin(intermediate);
1295 SecondsSequence seq2;
1296 yin >> seq2;
1297
1298 EXPECT_FALSE(yin.error());
1299 EXPECT_EQ(seq2.size(), 3UL);
1300 EXPECT_EQ(seq2[0], 4000U);
1301 EXPECT_EQ(seq2[1], 500U);
1302 EXPECT_EQ(seq2[2], 59U);
1303 }
1304}
1305
1306
1307//===----------------------------------------------------------------------===//
1308// Test dynamic typing
1309//===----------------------------------------------------------------------===//
1310
1311enum AFlags {
1312 a1,
1313 a2,
1314 a3
1315};
1316
1317enum BFlags {
1318 b1,
1319 b2,
1320 b3
1321};
1322
1323enum Kind {
1324 kindA,
1325 kindB
1326};
1327
1328struct KindAndFlags {
1329 KindAndFlags() : kind(kindA), flags(0) { }
1330 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1331 Kind kind;
1332 uint32_t flags;
1333};
1334
1335typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1336
Nick Kledzik11964f22013-01-04 19:32:00 +00001337LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001338
1339namespace llvm {
1340namespace yaml {
1341 template <>
1342 struct ScalarEnumerationTraits<AFlags> {
1343 static void enumeration(IO &io, AFlags &value) {
1344 io.enumCase(value, "a1", a1);
1345 io.enumCase(value, "a2", a2);
1346 io.enumCase(value, "a3", a3);
1347 }
1348 };
1349 template <>
1350 struct ScalarEnumerationTraits<BFlags> {
1351 static void enumeration(IO &io, BFlags &value) {
1352 io.enumCase(value, "b1", b1);
1353 io.enumCase(value, "b2", b2);
1354 io.enumCase(value, "b3", b3);
1355 }
1356 };
1357 template <>
1358 struct ScalarEnumerationTraits<Kind> {
1359 static void enumeration(IO &io, Kind &value) {
1360 io.enumCase(value, "A", kindA);
1361 io.enumCase(value, "B", kindB);
1362 }
1363 };
1364 template <>
1365 struct MappingTraits<KindAndFlags> {
1366 static void mapping(IO &io, KindAndFlags& kf) {
1367 io.mapRequired("kind", kf.kind);
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001368 // Type of "flags" field varies depending on "kind" field.
David Greene4162c2d2013-01-10 18:17:54 +00001369 // Use memcpy here to avoid breaking strict aliasing rules.
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001370 if (kf.kind == kindA) {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001371 AFlags aflags = static_cast<AFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001372 io.mapRequired("flags", aflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001373 kf.flags = aflags;
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001374 } else {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001375 BFlags bflags = static_cast<BFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001376 io.mapRequired("flags", bflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001377 kf.flags = bflags;
David Greene4162c2d2013-01-10 18:17:54 +00001378 }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001379 }
1380 };
1381}
1382}
1383
1384
1385//
1386// Test the reading of a yaml sequence dynamic types
1387//
1388TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1389 KindAndFlagsSequence seq;
1390 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1391 yin >> seq;
1392
1393 EXPECT_FALSE(yin.error());
1394 EXPECT_EQ(seq.size(), 2UL);
1395 EXPECT_EQ(seq[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001396 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001397 EXPECT_EQ(seq[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001398 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001399}
1400
1401//
1402// Test writing then reading back dynamic types
1403//
1404TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1405 std::string intermediate;
1406 {
1407 KindAndFlagsSequence seq;
1408 seq.push_back(KindAndFlags(kindA,a1));
1409 seq.push_back(KindAndFlags(kindB,b1));
1410 seq.push_back(KindAndFlags(kindA,a2));
1411 seq.push_back(KindAndFlags(kindB,b2));
1412 seq.push_back(KindAndFlags(kindA,a3));
1413
1414 llvm::raw_string_ostream ostr(intermediate);
1415 Output yout(ostr);
1416 yout << seq;
1417 }
1418 {
1419 Input yin(intermediate);
1420 KindAndFlagsSequence seq2;
1421 yin >> seq2;
1422
1423 EXPECT_FALSE(yin.error());
1424 EXPECT_EQ(seq2.size(), 5UL);
1425 EXPECT_EQ(seq2[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001426 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001427 EXPECT_EQ(seq2[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001428 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001429 EXPECT_EQ(seq2[2].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001430 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001431 EXPECT_EQ(seq2[3].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001432 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001433 EXPECT_EQ(seq2[4].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001434 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001435 }
1436}
1437
1438
1439//===----------------------------------------------------------------------===//
1440// Test document list
1441//===----------------------------------------------------------------------===//
1442
1443struct FooBarMap {
1444 int foo;
1445 int bar;
1446};
1447typedef std::vector<FooBarMap> FooBarMapDocumentList;
1448
1449LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1450
1451
1452namespace llvm {
1453namespace yaml {
1454 template <>
1455 struct MappingTraits<FooBarMap> {
1456 static void mapping(IO &io, FooBarMap& fb) {
1457 io.mapRequired("foo", fb.foo);
1458 io.mapRequired("bar", fb.bar);
1459 }
1460 };
1461}
1462}
1463
1464
1465//
1466// Test the reading of a yaml mapping
1467//
1468TEST(YAMLIO, TestDocRead) {
1469 FooBarMap doc;
1470 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1471 yin >> doc;
1472
1473 EXPECT_FALSE(yin.error());
1474 EXPECT_EQ(doc.foo, 3);
1475 EXPECT_EQ(doc.bar,5);
1476}
1477
1478
1479
1480//
1481// Test writing then reading back a sequence of mappings
1482//
1483TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1484 std::string intermediate;
1485 {
1486 FooBarMap doc1;
1487 doc1.foo = 10;
1488 doc1.bar = -3;
1489 FooBarMap doc2;
1490 doc2.foo = 257;
1491 doc2.bar = 0;
1492 std::vector<FooBarMap> docList;
1493 docList.push_back(doc1);
1494 docList.push_back(doc2);
1495
1496 llvm::raw_string_ostream ostr(intermediate);
1497 Output yout(ostr);
1498 yout << docList;
1499 }
1500
1501
1502 {
1503 Input yin(intermediate);
1504 std::vector<FooBarMap> docList2;
1505 yin >> docList2;
1506
1507 EXPECT_FALSE(yin.error());
1508 EXPECT_EQ(docList2.size(), 2UL);
1509 FooBarMap& map1 = docList2[0];
1510 FooBarMap& map2 = docList2[1];
1511 EXPECT_EQ(map1.foo, 10);
1512 EXPECT_EQ(map1.bar, -3);
1513 EXPECT_EQ(map2.foo, 257);
1514 EXPECT_EQ(map2.bar, 0);
1515 }
1516}
1517
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001518//===----------------------------------------------------------------------===//
1519// Test document tags
1520//===----------------------------------------------------------------------===//
1521
1522struct MyDouble {
1523 MyDouble() : value(0.0) { }
1524 MyDouble(double x) : value(x) { }
1525 double value;
1526};
1527
Nick Kledzik4a9f00d2013-11-14 03:03:05 +00001528LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001529
1530
1531namespace llvm {
1532namespace yaml {
1533 template <>
1534 struct MappingTraits<MyDouble> {
1535 static void mapping(IO &io, MyDouble &d) {
1536 if (io.mapTag("!decimal", true)) {
1537 mappingDecimal(io, d);
1538 } else if (io.mapTag("!fraction")) {
1539 mappingFraction(io, d);
1540 }
1541 }
1542 static void mappingDecimal(IO &io, MyDouble &d) {
1543 io.mapRequired("value", d.value);
1544 }
1545 static void mappingFraction(IO &io, MyDouble &d) {
1546 double num, denom;
1547 io.mapRequired("numerator", num);
1548 io.mapRequired("denominator", denom);
1549 // convert fraction to double
1550 d.value = num/denom;
1551 }
1552 };
1553 }
1554}
1555
1556
1557//
1558// Test the reading of two different tagged yaml documents.
1559//
1560TEST(YAMLIO, TestTaggedDocuments) {
1561 std::vector<MyDouble> docList;
1562 Input yin("--- !decimal\nvalue: 3.0\n"
1563 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1564 yin >> docList;
1565 EXPECT_FALSE(yin.error());
1566 EXPECT_EQ(docList.size(), 2UL);
1567 EXPECT_EQ(docList[0].value, 3.0);
1568 EXPECT_EQ(docList[1].value, 4.5);
1569}
1570
1571
1572
1573//
1574// Test writing then reading back tagged documents
1575//
1576TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1577 std::string intermediate;
1578 {
1579 MyDouble a(10.25);
1580 MyDouble b(-3.75);
1581 std::vector<MyDouble> docList;
1582 docList.push_back(a);
1583 docList.push_back(b);
1584
1585 llvm::raw_string_ostream ostr(intermediate);
1586 Output yout(ostr);
1587 yout << docList;
1588 }
1589
1590 {
1591 Input yin(intermediate);
1592 std::vector<MyDouble> docList2;
1593 yin >> docList2;
1594
1595 EXPECT_FALSE(yin.error());
1596 EXPECT_EQ(docList2.size(), 2UL);
1597 EXPECT_EQ(docList2[0].value, 10.25);
1598 EXPECT_EQ(docList2[1].value, -3.75);
1599 }
1600}
1601
1602
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001603//===----------------------------------------------------------------------===//
1604// Test mapping validation
1605//===----------------------------------------------------------------------===//
1606
1607struct MyValidation {
1608 double value;
1609};
1610
1611LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1612
1613namespace llvm {
1614namespace yaml {
1615 template <>
1616 struct MappingTraits<MyValidation> {
1617 static void mapping(IO &io, MyValidation &d) {
1618 io.mapRequired("value", d.value);
1619 }
1620 static StringRef validate(IO &io, MyValidation &d) {
1621 if (d.value < 0)
1622 return "negative value";
1623 return StringRef();
1624 }
1625 };
1626 }
1627}
1628
1629
1630//
1631// Test that validate() is called and complains about the negative value.
1632//
1633TEST(YAMLIO, TestValidatingInput) {
1634 std::vector<MyValidation> docList;
1635 Input yin("--- \nvalue: 3.0\n"
1636 "--- \nvalue: -1.0\n...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001637 nullptr, suppressErrorMessages);
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001638 yin >> docList;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001639 EXPECT_TRUE(!!yin.error());
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001640}
1641
Alex Lorenzb1225082015-05-04 20:11:40 +00001642//===----------------------------------------------------------------------===//
1643// Test flow mapping
1644//===----------------------------------------------------------------------===//
1645
1646struct FlowFooBar {
1647 int foo;
1648 int bar;
1649
1650 FlowFooBar() : foo(0), bar(0) {}
1651 FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1652};
1653
1654typedef std::vector<FlowFooBar> FlowFooBarSequence;
1655
1656LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1657
1658struct FlowFooBarDoc {
1659 FlowFooBar attribute;
1660 FlowFooBarSequence seq;
1661};
1662
1663namespace llvm {
1664namespace yaml {
1665 template <>
1666 struct MappingTraits<FlowFooBar> {
1667 static void mapping(IO &io, FlowFooBar &fb) {
1668 io.mapRequired("foo", fb.foo);
1669 io.mapRequired("bar", fb.bar);
1670 }
1671
1672 static const bool flow = true;
1673 };
1674
1675 template <>
1676 struct MappingTraits<FlowFooBarDoc> {
1677 static void mapping(IO &io, FlowFooBarDoc &fb) {
1678 io.mapRequired("attribute", fb.attribute);
1679 io.mapRequired("seq", fb.seq);
1680 }
1681 };
1682}
1683}
1684
1685//
1686// Test writing then reading back custom mappings
1687//
1688TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1689 std::string intermediate;
1690 {
1691 FlowFooBarDoc doc;
1692 doc.attribute = FlowFooBar(42, 907);
1693 doc.seq.push_back(FlowFooBar(1, 2));
1694 doc.seq.push_back(FlowFooBar(0, 0));
1695 doc.seq.push_back(FlowFooBar(-1, 1024));
1696
1697 llvm::raw_string_ostream ostr(intermediate);
1698 Output yout(ostr);
1699 yout << doc;
1700
1701 // Verify that mappings were written in flow style
1702 ostr.flush();
1703 llvm::StringRef flowOut(intermediate);
1704 EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1705 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1706 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1707 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1708 }
1709
1710 {
1711 Input yin(intermediate);
1712 FlowFooBarDoc doc2;
1713 yin >> doc2;
1714
1715 EXPECT_FALSE(yin.error());
1716 EXPECT_EQ(doc2.attribute.foo, 42);
1717 EXPECT_EQ(doc2.attribute.bar, 907);
1718 EXPECT_EQ(doc2.seq.size(), 3UL);
1719 EXPECT_EQ(doc2.seq[0].foo, 1);
1720 EXPECT_EQ(doc2.seq[0].bar, 2);
1721 EXPECT_EQ(doc2.seq[1].foo, 0);
1722 EXPECT_EQ(doc2.seq[1].bar, 0);
1723 EXPECT_EQ(doc2.seq[2].foo, -1);
1724 EXPECT_EQ(doc2.seq[2].bar, 1024);
1725 }
1726}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001727
1728//===----------------------------------------------------------------------===//
1729// Test error handling
1730//===----------------------------------------------------------------------===//
1731
Nick Kledzikf60a9272012-12-12 20:46:15 +00001732//
1733// Test error handling of unknown enumerated scalar
1734//
1735TEST(YAMLIO, TestColorsReadError) {
1736 ColorMap map;
1737 Input yin("---\n"
1738 "c1: blue\n"
1739 "c2: purple\n"
1740 "c3: green\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001741 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001742 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001743 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001744 yin >> map;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001745 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001746}
1747
1748
1749//
1750// Test error handling of flow sequence with unknown value
1751//
1752TEST(YAMLIO, TestFlagsReadError) {
1753 FlagsMap map;
1754 Input yin("---\n"
1755 "f1: [ big ]\n"
1756 "f2: [ round, hollow ]\n"
1757 "f3: []\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001758 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001759 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001760 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001761 yin >> map;
1762
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001763 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001764}
1765
1766
1767//
1768// Test error handling reading built-in uint8_t type
1769//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001770TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1771 std::vector<uint8_t> seq;
1772 Input yin("---\n"
1773 "- 255\n"
1774 "- 0\n"
1775 "- 257\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001776 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001777 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001778 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001779 yin >> seq;
1780
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001781 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001782}
1783
1784
1785//
1786// Test error handling reading built-in uint16_t type
1787//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001788TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1789 std::vector<uint16_t> seq;
1790 Input yin("---\n"
1791 "- 65535\n"
1792 "- 0\n"
1793 "- 66000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001794 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001795 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001796 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001797 yin >> seq;
1798
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001799 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001800}
1801
1802
1803//
1804// Test error handling reading built-in uint32_t type
1805//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001806TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1807 std::vector<uint32_t> seq;
1808 Input yin("---\n"
1809 "- 4000000000\n"
1810 "- 0\n"
1811 "- 5000000000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001812 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001813 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001814 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001815 yin >> seq;
1816
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001817 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001818}
1819
1820
1821//
1822// Test error handling reading built-in uint64_t type
1823//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001824TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1825 std::vector<uint64_t> seq;
1826 Input yin("---\n"
1827 "- 18446744073709551615\n"
1828 "- 0\n"
1829 "- 19446744073709551615\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001830 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001831 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001832 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001833 yin >> seq;
1834
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001835 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001836}
1837
1838
1839//
1840// Test error handling reading built-in int8_t type
1841//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001842TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1843 std::vector<int8_t> seq;
1844 Input yin("---\n"
1845 "- -128\n"
1846 "- 0\n"
1847 "- 127\n"
1848 "- 128\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001849 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001850 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001851 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001852 yin >> seq;
1853
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001854 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001855}
1856
1857//
1858// Test error handling reading built-in int8_t type
1859//
1860TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1861 std::vector<int8_t> seq;
1862 Input yin("---\n"
1863 "- -128\n"
1864 "- 0\n"
1865 "- 127\n"
1866 "- -129\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001867 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001868 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001869 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001870 yin >> seq;
1871
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001872 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001873}
1874
1875
1876//
1877// Test error handling reading built-in int16_t type
1878//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001879TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1880 std::vector<int16_t> seq;
1881 Input yin("---\n"
1882 "- 32767\n"
1883 "- 0\n"
1884 "- -32768\n"
1885 "- -32769\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001886 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001887 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001888 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001889 yin >> seq;
1890
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001891 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001892}
1893
1894
1895//
1896// Test error handling reading built-in int16_t type
1897//
1898TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1899 std::vector<int16_t> seq;
1900 Input yin("---\n"
1901 "- 32767\n"
1902 "- 0\n"
1903 "- -32768\n"
1904 "- 32768\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001905 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001906 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001907 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001908 yin >> seq;
1909
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001910 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001911}
1912
1913
1914//
1915// Test error handling reading built-in int32_t type
1916//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001917TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1918 std::vector<int32_t> seq;
1919 Input yin("---\n"
1920 "- 2147483647\n"
1921 "- 0\n"
1922 "- -2147483648\n"
1923 "- -2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001924 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001925 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001926 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001927 yin >> seq;
1928
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001929 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001930}
1931
1932//
1933// Test error handling reading built-in int32_t type
1934//
1935TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1936 std::vector<int32_t> seq;
1937 Input yin("---\n"
1938 "- 2147483647\n"
1939 "- 0\n"
1940 "- -2147483648\n"
1941 "- 2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001942 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001943 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001944 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001945 yin >> seq;
1946
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001947 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001948}
1949
1950
1951//
1952// Test error handling reading built-in int64_t type
1953//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001954TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1955 std::vector<int64_t> seq;
1956 Input yin("---\n"
1957 "- -9223372036854775808\n"
1958 "- 0\n"
1959 "- 9223372036854775807\n"
1960 "- -9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001961 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001962 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001963 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001964 yin >> seq;
1965
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001966 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001967}
1968
1969//
1970// Test error handling reading built-in int64_t type
1971//
1972TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1973 std::vector<int64_t> seq;
1974 Input yin("---\n"
1975 "- -9223372036854775808\n"
1976 "- 0\n"
1977 "- 9223372036854775807\n"
1978 "- 9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001979 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001980 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001981 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001982 yin >> seq;
1983
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001984 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001985}
1986
1987//
1988// Test error handling reading built-in float type
1989//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001990TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1991 std::vector<float> seq;
1992 Input yin("---\n"
1993 "- 0.0\n"
1994 "- 1000.1\n"
1995 "- -123.456\n"
1996 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001997 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001998 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001999 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002000 yin >> seq;
2001
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002002 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002003}
2004
2005//
2006// Test error handling reading built-in float type
2007//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002008TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
2009 std::vector<double> seq;
2010 Input yin("---\n"
2011 "- 0.0\n"
2012 "- 1000.1\n"
2013 "- -123.456\n"
2014 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002015 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002016 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002017 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002018 yin >> seq;
2019
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002020 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002021}
2022
2023//
2024// Test error handling reading built-in Hex8 type
2025//
2026LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
2027TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
2028 std::vector<Hex8> seq;
2029 Input yin("---\n"
2030 "- 0x12\n"
2031 "- 0xFE\n"
2032 "- 0x123\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002033 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002034 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002035 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002036 yin >> seq;
2037
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002038 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002039}
2040
2041
2042//
2043// Test error handling reading built-in Hex16 type
2044//
2045LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
2046TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
2047 std::vector<Hex16> seq;
2048 Input yin("---\n"
2049 "- 0x0012\n"
2050 "- 0xFEFF\n"
2051 "- 0x12345\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002052 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002053 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002054 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002055 yin >> seq;
2056
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002057 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002058}
2059
2060//
2061// Test error handling reading built-in Hex32 type
2062//
2063LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
2064TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
2065 std::vector<Hex32> seq;
2066 Input yin("---\n"
2067 "- 0x0012\n"
2068 "- 0xFEFF0000\n"
2069 "- 0x1234556789\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002070 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002071 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002072 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002073 yin >> seq;
2074
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002075 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002076}
2077
2078//
2079// Test error handling reading built-in Hex64 type
2080//
2081LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
2082TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
2083 std::vector<Hex64> seq;
2084 Input yin("---\n"
2085 "- 0x0012\n"
2086 "- 0xFFEEDDCCBBAA9988\n"
2087 "- 0x12345567890ABCDEF0\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002088 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002089 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002090 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002091 yin >> seq;
2092
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002093 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002094}
2095
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002096TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2097 FooBar doc;
2098 {
2099 // We pass the suppressErrorMessages handler to handle the error
2100 // message generated in the constructor of Input.
Craig Topper66f09ad2014-06-08 22:29:17 +00002101 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002102 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002103 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002104 }
2105
2106 {
Craig Topper66f09ad2014-06-08 22:29:17 +00002107 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002108 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002109 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002110 }
2111}
2112
Aaron Ballman0e63e532013-08-15 23:17:53 +00002113struct OptionalTest {
2114 std::vector<int> Numbers;
2115};
2116
2117struct OptionalTestSeq {
2118 std::vector<OptionalTest> Tests;
2119};
2120
Aaron Ballman381f59f2013-08-16 01:53:58 +00002121LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
Aaron Ballman0e63e532013-08-15 23:17:53 +00002122namespace llvm {
2123namespace yaml {
2124 template <>
2125 struct MappingTraits<OptionalTest> {
2126 static void mapping(IO& IO, OptionalTest &OT) {
2127 IO.mapOptional("Numbers", OT.Numbers);
2128 }
2129 };
2130
2131 template <>
2132 struct MappingTraits<OptionalTestSeq> {
2133 static void mapping(IO &IO, OptionalTestSeq &OTS) {
2134 IO.mapOptional("Tests", OTS.Tests);
2135 }
2136 };
2137}
2138}
2139
2140TEST(YAMLIO, SequenceElideTest) {
2141 // Test that writing out a purely optional structure with its fields set to
2142 // default followed by other data is properly read back in.
2143 OptionalTestSeq Seq;
2144 OptionalTest One, Two, Three, Four;
2145 int N[] = {1, 2, 3};
2146 Three.Numbers.assign(N, N + 3);
2147 Seq.Tests.push_back(One);
2148 Seq.Tests.push_back(Two);
2149 Seq.Tests.push_back(Three);
2150 Seq.Tests.push_back(Four);
2151
2152 std::string intermediate;
2153 {
2154 llvm::raw_string_ostream ostr(intermediate);
2155 Output yout(ostr);
2156 yout << Seq;
2157 }
2158
2159 Input yin(intermediate);
2160 OptionalTestSeq Seq2;
2161 yin >> Seq2;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00002162
Aaron Ballman0e63e532013-08-15 23:17:53 +00002163 EXPECT_FALSE(yin.error());
2164
2165 EXPECT_EQ(4UL, Seq2.Tests.size());
2166
2167 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2168 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2169
2170 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2171 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2172 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2173
2174 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2175}
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002176
2177TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2178 FooBar doc;
2179 Input yin("");
2180 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002181 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002182}
2183
2184TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2185 OptionalTest doc;
2186 Input yin("");
2187 yin >> doc;
2188 EXPECT_FALSE(yin.error());
2189}
2190
2191TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2192 std::vector<uint8_t> seq;
Craig Topper66f09ad2014-06-08 22:29:17 +00002193 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002194 yin >> seq;
2195
2196 EXPECT_FALSE(yin.error());
2197 EXPECT_TRUE(seq.empty());
2198}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002199
2200struct FlowMap {
2201 llvm::StringRef str1, str2, str3;
2202 FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2203 : str1(str1), str2(str2), str3(str3) {}
2204};
2205
Frederic Riss3733c032015-05-29 18:14:55 +00002206struct FlowSeq {
2207 llvm::StringRef str;
2208 FlowSeq(llvm::StringRef S) : str(S) {}
2209 FlowSeq() = default;
2210};
2211
Frederic Riss4939e6a2015-05-29 17:56:28 +00002212namespace llvm {
2213namespace yaml {
2214 template <>
2215 struct MappingTraits<FlowMap> {
2216 static void mapping(IO &io, FlowMap &fm) {
2217 io.mapRequired("str1", fm.str1);
2218 io.mapRequired("str2", fm.str2);
2219 io.mapRequired("str3", fm.str3);
2220 }
2221
2222 static const bool flow = true;
2223 };
Frederic Riss4939e6a2015-05-29 17:56:28 +00002224
2225template <>
2226struct ScalarTraits<FlowSeq> {
2227 static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2228 out << value.str;
2229 }
2230 static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2231 value.str = scalar;
2232 return "";
2233 }
2234
2235 static bool mustQuote(StringRef S) { return false; }
2236};
Frederic Riss3733c032015-05-29 18:14:55 +00002237}
2238}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002239
2240LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2241
2242TEST(YAMLIO, TestWrapFlow) {
2243 std::string out;
2244 llvm::raw_string_ostream ostr(out);
2245 FlowMap Map("This is str1", "This is str2", "This is str3");
2246 std::vector<FlowSeq> Seq;
2247 Seq.emplace_back("This is str1");
2248 Seq.emplace_back("This is str2");
2249 Seq.emplace_back("This is str3");
2250
2251 {
2252 // 20 is just bellow the total length of the first mapping field.
2253 // We should wreap at every element.
2254 Output yout(ostr, nullptr, 15);
2255
2256 yout << Map;
2257 ostr.flush();
2258 EXPECT_EQ(out,
2259 "---\n"
2260 "{ str1: This is str1, \n"
2261 " str2: This is str2, \n"
2262 " str3: This is str3 }\n"
2263 "...\n");
2264 out.clear();
2265
2266 yout << Seq;
2267 ostr.flush();
2268 EXPECT_EQ(out,
2269 "---\n"
2270 "[ This is str1, \n"
2271 " This is str2, \n"
2272 " This is str3 ]\n"
2273 "...\n");
2274 out.clear();
2275 }
2276 {
2277 // 25 will allow the second field to be output on the first line.
2278 Output yout(ostr, nullptr, 25);
2279
2280 yout << Map;
2281 ostr.flush();
2282 EXPECT_EQ(out,
2283 "---\n"
2284 "{ str1: This is str1, str2: This is str2, \n"
2285 " str3: This is str3 }\n"
2286 "...\n");
2287 out.clear();
2288
2289 yout << Seq;
2290 ostr.flush();
2291 EXPECT_EQ(out,
2292 "---\n"
2293 "[ This is str1, This is str2, \n"
2294 " This is str3 ]\n"
2295 "...\n");
2296 out.clear();
2297 }
2298 {
2299 // 0 means no wrapping.
2300 Output yout(ostr, nullptr, 0);
2301
2302 yout << Map;
2303 ostr.flush();
2304 EXPECT_EQ(out,
2305 "---\n"
2306 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2307 "...\n");
2308 out.clear();
2309
2310 yout << Seq;
2311 ostr.flush();
2312 EXPECT_EQ(out,
2313 "---\n"
2314 "[ This is str1, This is str2, This is str3 ]\n"
2315 "...\n");
2316 out.clear();
2317 }
2318}
Zachary Turner35377f82016-09-08 18:22:44 +00002319
2320struct MappingContext {
2321 int A = 0;
2322};
2323struct SimpleMap {
2324 int B = 0;
2325 int C = 0;
2326};
2327
2328struct NestedMap {
2329 NestedMap(MappingContext &Context) : Context(Context) {}
2330 SimpleMap Simple;
2331 MappingContext &Context;
2332};
2333
2334namespace llvm {
2335namespace yaml {
2336template <> struct MappingContextTraits<SimpleMap, MappingContext> {
2337 static void mapping(IO &io, SimpleMap &sm, MappingContext &Context) {
2338 io.mapRequired("B", sm.B);
2339 io.mapRequired("C", sm.C);
2340 ++Context.A;
2341 io.mapRequired("Context", Context.A);
2342 }
2343};
2344
2345template <> struct MappingTraits<NestedMap> {
2346 static void mapping(IO &io, NestedMap &nm) {
2347 io.mapRequired("Simple", nm.Simple, nm.Context);
2348 }
2349};
2350}
2351}
2352
2353TEST(YAMLIO, TestMapWithContext) {
2354 MappingContext Context;
2355 NestedMap Nested(Context);
2356 std::string out;
2357 llvm::raw_string_ostream ostr(out);
2358
2359 Output yout(ostr, nullptr, 15);
2360
2361 yout << Nested;
2362 ostr.flush();
2363 EXPECT_EQ(1, Context.A);
2364 EXPECT_EQ("---\n"
2365 "Simple: \n"
2366 " B: 0\n"
2367 " C: 0\n"
2368 " Context: 1\n"
2369 "...\n",
2370 out);
2371
2372 out.clear();
2373
2374 Nested.Simple.B = 2;
2375 Nested.Simple.C = 3;
2376 yout << Nested;
2377 ostr.flush();
2378 EXPECT_EQ(2, Context.A);
2379 EXPECT_EQ("---\n"
2380 "Simple: \n"
2381 " B: 2\n"
2382 " C: 3\n"
2383 " Context: 2\n"
2384 "...\n",
2385 out);
2386 out.clear();
2387}
Mehdi Amini3ab3fef2016-11-28 21:38:52 +00002388
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +00002389LLVM_YAML_IS_STRING_MAP(int)
2390
2391TEST(YAMLIO, TestCustomMapping) {
2392 std::map<std::string, int> x;
2393 x["foo"] = 1;
2394 x["bar"] = 2;
2395
2396 std::string out;
2397 llvm::raw_string_ostream ostr(out);
2398 Output xout(ostr, nullptr, 0);
2399
2400 xout << x;
2401 ostr.flush();
2402 EXPECT_EQ("---\n"
2403 "bar: 2\n"
2404 "foo: 1\n"
2405 "...\n",
2406 out);
2407
2408 Input yin(out);
2409 std::map<std::string, int> y;
2410 yin >> y;
2411 EXPECT_EQ(2ul, y.size());
2412 EXPECT_EQ(1, y["foo"]);
2413 EXPECT_EQ(2, y["bar"]);
2414}
2415
2416LLVM_YAML_IS_STRING_MAP(FooBar)
2417
2418TEST(YAMLIO, TestCustomMappingStruct) {
2419 std::map<std::string, FooBar> x;
2420 x["foo"].foo = 1;
2421 x["foo"].bar = 2;
2422 x["bar"].foo = 3;
2423 x["bar"].bar = 4;
2424
2425 std::string out;
2426 llvm::raw_string_ostream ostr(out);
2427 Output xout(ostr, nullptr, 0);
2428
2429 xout << x;
2430 ostr.flush();
2431 EXPECT_EQ("---\n"
2432 "bar: \n"
2433 " foo: 3\n"
2434 " bar: 4\n"
2435 "foo: \n"
2436 " foo: 1\n"
2437 " bar: 2\n"
2438 "...\n",
2439 out);
2440
2441 Input yin(out);
2442 std::map<std::string, FooBar> y;
2443 yin >> y;
2444 EXPECT_EQ(2ul, y.size());
2445 EXPECT_EQ(1, y["foo"].foo);
2446 EXPECT_EQ(2, y["foo"].bar);
2447 EXPECT_EQ(3, y["bar"].foo);
2448 EXPECT_EQ(4, y["bar"].bar);
2449}
2450
Mehdi Amini3ab3fef2016-11-28 21:38:52 +00002451TEST(YAMLIO, InvalidInput) {
2452 // polluting 1 value in the sequence
2453 Input yin("---\n- foo: 3\n bar: 5\n1\n- foo: 3\n bar: 5\n...\n");
2454 std::vector<FooBar> Data;
2455 yin >> Data;
2456 EXPECT_TRUE((bool)yin.error());
2457}