blob: 17f38f9b86cf7e43b5f10e2df001bbd4c8bc933a [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
Scott Linderc0830f52018-11-14 19:39:59 +000010#include "llvm/ADT/StringMap.h"
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +000011#include "llvm/ADT/StringRef.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000012#include "llvm/ADT/Twine.h"
13#include "llvm/Support/Casting.h"
Zachary Turner4fbf61d2016-06-07 19:32:09 +000014#include "llvm/Support/Endian.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000015#include "llvm/Support/Format.h"
16#include "llvm/Support/YAMLTraits.h"
Ilya Biryukov54135102018-05-30 10:40:11 +000017#include "gmock/gmock.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000018#include "gtest/gtest.h"
19
Nick Kledzikf60a9272012-12-12 20:46:15 +000020using llvm::yaml::Hex16;
21using llvm::yaml::Hex32;
22using llvm::yaml::Hex64;
Kirill Bobyrev5f26a642018-08-20 07:00:36 +000023using llvm::yaml::Hex8;
24using llvm::yaml::Input;
25using llvm::yaml::IO;
26using llvm::yaml::isNumeric;
27using llvm::yaml::MappingNormalization;
28using llvm::yaml::MappingTraits;
29using llvm::yaml::Output;
30using llvm::yaml::ScalarTraits;
Ilya Biryukov54135102018-05-30 10:40:11 +000031using ::testing::StartsWith;
Nick Kledzikf60a9272012-12-12 20:46:15 +000032
33
Nick Kledzik7cd45f22013-11-21 00:28:07 +000034
35
36static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
37}
38
39
40
Nick Kledzikf60a9272012-12-12 20:46:15 +000041//===----------------------------------------------------------------------===//
42// Test MappingTraits
43//===----------------------------------------------------------------------===//
44
45struct FooBar {
46 int foo;
47 int bar;
48};
49typedef std::vector<FooBar> FooBarSequence;
50
51LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
52
Justin Bogner64d2cdf2015-03-02 17:26:43 +000053struct FooBarContainer {
54 FooBarSequence fbs;
55};
Nick Kledzikf60a9272012-12-12 20:46:15 +000056
57namespace llvm {
58namespace yaml {
59 template <>
60 struct MappingTraits<FooBar> {
61 static void mapping(IO &io, FooBar& fb) {
62 io.mapRequired("foo", fb.foo);
63 io.mapRequired("bar", fb.bar);
64 }
65 };
Justin Bogner64d2cdf2015-03-02 17:26:43 +000066
67 template <> struct MappingTraits<FooBarContainer> {
68 static void mapping(IO &io, FooBarContainer &fb) {
69 io.mapRequired("fbs", fb.fbs);
70 }
71 };
Nick Kledzikf60a9272012-12-12 20:46:15 +000072}
73}
74
75
76//
77// Test the reading of a yaml mapping
78//
79TEST(YAMLIO, TestMapRead) {
80 FooBar doc;
Alexander Kornienko681e37c2013-11-18 15:50:04 +000081 {
82 Input yin("---\nfoo: 3\nbar: 5\n...\n");
83 yin >> doc;
Nick Kledzikf60a9272012-12-12 20:46:15 +000084
Alexander Kornienko681e37c2013-11-18 15:50:04 +000085 EXPECT_FALSE(yin.error());
86 EXPECT_EQ(doc.foo, 3);
87 EXPECT_EQ(doc.bar, 5);
88 }
89
90 {
91 Input yin("{foo: 3, bar: 5}");
92 yin >> doc;
93
94 EXPECT_FALSE(yin.error());
95 EXPECT_EQ(doc.foo, 3);
96 EXPECT_EQ(doc.bar, 5);
97 }
Nick Kledzikf60a9272012-12-12 20:46:15 +000098}
99
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000100TEST(YAMLIO, TestMalformedMapRead) {
101 FooBar doc;
102 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
103 yin >> doc;
104 EXPECT_TRUE(!!yin.error());
105}
106
Nick Kledzikf60a9272012-12-12 20:46:15 +0000107//
108// Test the reading of a yaml sequence of mappings
109//
110TEST(YAMLIO, TestSequenceMapRead) {
111 FooBarSequence seq;
112 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
113 yin >> seq;
114
115 EXPECT_FALSE(yin.error());
116 EXPECT_EQ(seq.size(), 2UL);
117 FooBar& map1 = seq[0];
118 FooBar& map2 = seq[1];
119 EXPECT_EQ(map1.foo, 3);
120 EXPECT_EQ(map1.bar, 5);
121 EXPECT_EQ(map2.foo, 7);
122 EXPECT_EQ(map2.bar, 9);
123}
124
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000125//
126// Test the reading of a map containing a yaml sequence of mappings
127//
128TEST(YAMLIO, TestContainerSequenceMapRead) {
129 {
130 FooBarContainer cont;
131 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
132 yin2 >> cont;
133
134 EXPECT_FALSE(yin2.error());
135 EXPECT_EQ(cont.fbs.size(), 2UL);
136 EXPECT_EQ(cont.fbs[0].foo, 3);
137 EXPECT_EQ(cont.fbs[0].bar, 5);
138 EXPECT_EQ(cont.fbs[1].foo, 7);
139 EXPECT_EQ(cont.fbs[1].bar, 9);
140 }
141
142 {
143 FooBarContainer cont;
144 Input yin("---\nfbs:\n...\n");
145 yin >> cont;
146 // Okay: Empty node represents an empty array.
147 EXPECT_FALSE(yin.error());
148 EXPECT_EQ(cont.fbs.size(), 0UL);
149 }
150
151 {
152 FooBarContainer cont;
153 Input yin("---\nfbs: !!null null\n...\n");
154 yin >> cont;
155 // Okay: null represents an empty array.
156 EXPECT_FALSE(yin.error());
157 EXPECT_EQ(cont.fbs.size(), 0UL);
158 }
159
160 {
161 FooBarContainer cont;
162 Input yin("---\nfbs: ~\n...\n");
163 yin >> cont;
164 // Okay: null represents an empty array.
165 EXPECT_FALSE(yin.error());
166 EXPECT_EQ(cont.fbs.size(), 0UL);
167 }
168
169 {
170 FooBarContainer cont;
171 Input yin("---\nfbs: null\n...\n");
172 yin >> cont;
173 // Okay: null represents an empty array.
174 EXPECT_FALSE(yin.error());
175 EXPECT_EQ(cont.fbs.size(), 0UL);
176 }
177}
178
179//
180// Test the reading of a map containing a malformed yaml sequence
181//
182TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
183 {
184 FooBarContainer cont;
185 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
186 suppressErrorMessages);
187 yin >> cont;
188 // Error: fbs is not a sequence.
189 EXPECT_TRUE(!!yin.error());
190 EXPECT_EQ(cont.fbs.size(), 0UL);
191 }
192
193 {
194 FooBarContainer cont;
195 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
196 yin >> cont;
197 // This should be an error.
198 EXPECT_TRUE(!!yin.error());
199 EXPECT_EQ(cont.fbs.size(), 0UL);
200 }
201}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000202
203//
204// Test writing then reading back a sequence of mappings
205//
206TEST(YAMLIO, TestSequenceMapWriteAndRead) {
207 std::string intermediate;
208 {
209 FooBar entry1;
210 entry1.foo = 10;
211 entry1.bar = -3;
212 FooBar entry2;
213 entry2.foo = 257;
214 entry2.bar = 0;
215 FooBarSequence seq;
216 seq.push_back(entry1);
217 seq.push_back(entry2);
218
219 llvm::raw_string_ostream ostr(intermediate);
220 Output yout(ostr);
221 yout << seq;
222 }
223
224 {
225 Input yin(intermediate);
226 FooBarSequence seq2;
227 yin >> seq2;
228
229 EXPECT_FALSE(yin.error());
230 EXPECT_EQ(seq2.size(), 2UL);
231 FooBar& map1 = seq2[0];
232 FooBar& map2 = seq2[1];
233 EXPECT_EQ(map1.foo, 10);
234 EXPECT_EQ(map1.bar, -3);
235 EXPECT_EQ(map2.foo, 257);
236 EXPECT_EQ(map2.bar, 0);
237 }
238}
239
Alex Bradbury16843172017-07-17 11:41:30 +0000240//
241// Test YAML filename handling.
242//
243static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) {
244 EXPECT_EQ(Error.getFilename(), "foo.yaml");
245}
246
247TEST(YAMLIO, TestGivenFilename) {
248 auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
249 Input yin(*Buffer, nullptr, testErrorFilename);
250 FooBar Value;
251 yin >> Value;
252
253 EXPECT_TRUE(!!yin.error());
254}
255
Ilya Biryukov54135102018-05-30 10:40:11 +0000256struct WithStringField {
257 std::string str1;
258 std::string str2;
259 std::string str3;
260};
261
262namespace llvm {
263namespace yaml {
264template <> struct MappingTraits<WithStringField> {
265 static void mapping(IO &io, WithStringField &fb) {
266 io.mapRequired("str1", fb.str1);
267 io.mapRequired("str2", fb.str2);
268 io.mapRequired("str3", fb.str3);
269 }
270};
271} // namespace yaml
272} // namespace llvm
273
274TEST(YAMLIO, MultilineStrings) {
275 WithStringField Original;
276 Original.str1 = "a multiline string\nfoobarbaz";
277 Original.str2 = "another one\rfoobarbaz";
278 Original.str3 = "a one-line string";
279
280 std::string Serialized;
281 {
282 llvm::raw_string_ostream OS(Serialized);
283 Output YOut(OS);
284 YOut << Original;
285 }
286 auto Expected = "---\n"
287 "str1: 'a multiline string\n"
288 "foobarbaz'\n"
289 "str2: 'another one\r"
290 "foobarbaz'\n"
291 "str3: a one-line string\n"
292 "...\n";
293 ASSERT_EQ(Serialized, Expected);
294
295 // Also check it parses back without the errors.
296 WithStringField Deserialized;
297 {
298 Input YIn(Serialized);
299 YIn >> Deserialized;
300 ASSERT_FALSE(YIn.error())
301 << "Parsing error occurred during deserialization. Serialized string:\n"
302 << Serialized;
303 }
304 EXPECT_EQ(Original.str1, Deserialized.str1);
305 EXPECT_EQ(Original.str2, Deserialized.str2);
306 EXPECT_EQ(Original.str3, Deserialized.str3);
307}
308
309TEST(YAMLIO, NoQuotesForTab) {
310 WithStringField WithTab;
311 WithTab.str1 = "aba\tcaba";
312 std::string Serialized;
313 {
314 llvm::raw_string_ostream OS(Serialized);
315 Output YOut(OS);
316 YOut << WithTab;
317 }
318 auto ExpectedPrefix = "---\n"
319 "str1: aba\tcaba\n";
320 EXPECT_THAT(Serialized, StartsWith(ExpectedPrefix));
321}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000322
323//===----------------------------------------------------------------------===//
324// Test built-in types
325//===----------------------------------------------------------------------===//
326
327struct BuiltInTypes {
328 llvm::StringRef str;
John Thompson48e018a2013-11-19 17:28:21 +0000329 std::string stdstr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000330 uint64_t u64;
331 uint32_t u32;
332 uint16_t u16;
333 uint8_t u8;
334 bool b;
335 int64_t s64;
336 int32_t s32;
337 int16_t s16;
338 int8_t s8;
339 float f;
340 double d;
341 Hex8 h8;
342 Hex16 h16;
343 Hex32 h32;
344 Hex64 h64;
345};
346
347namespace llvm {
348namespace yaml {
349 template <>
350 struct MappingTraits<BuiltInTypes> {
351 static void mapping(IO &io, BuiltInTypes& bt) {
352 io.mapRequired("str", bt.str);
John Thompson48e018a2013-11-19 17:28:21 +0000353 io.mapRequired("stdstr", bt.stdstr);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000354 io.mapRequired("u64", bt.u64);
355 io.mapRequired("u32", bt.u32);
356 io.mapRequired("u16", bt.u16);
357 io.mapRequired("u8", bt.u8);
358 io.mapRequired("b", bt.b);
359 io.mapRequired("s64", bt.s64);
360 io.mapRequired("s32", bt.s32);
361 io.mapRequired("s16", bt.s16);
362 io.mapRequired("s8", bt.s8);
363 io.mapRequired("f", bt.f);
364 io.mapRequired("d", bt.d);
365 io.mapRequired("h8", bt.h8);
366 io.mapRequired("h16", bt.h16);
367 io.mapRequired("h32", bt.h32);
368 io.mapRequired("h64", bt.h64);
369 }
370 };
371}
372}
373
374
375//
376// Test the reading of all built-in scalar conversions
377//
378TEST(YAMLIO, TestReadBuiltInTypes) {
379 BuiltInTypes map;
380 Input yin("---\n"
381 "str: hello there\n"
John Thompson48e018a2013-11-19 17:28:21 +0000382 "stdstr: hello where?\n"
Nick Kledzikf60a9272012-12-12 20:46:15 +0000383 "u64: 5000000000\n"
384 "u32: 4000000000\n"
385 "u16: 65000\n"
386 "u8: 255\n"
387 "b: false\n"
388 "s64: -5000000000\n"
389 "s32: -2000000000\n"
390 "s16: -32000\n"
391 "s8: -127\n"
392 "f: 137.125\n"
393 "d: -2.8625\n"
394 "h8: 0xFF\n"
395 "h16: 0x8765\n"
396 "h32: 0xFEDCBA98\n"
397 "h64: 0xFEDCBA9876543210\n"
398 "...\n");
399 yin >> map;
400
401 EXPECT_FALSE(yin.error());
402 EXPECT_TRUE(map.str.equals("hello there"));
John Thompson48e018a2013-11-19 17:28:21 +0000403 EXPECT_TRUE(map.stdstr == "hello where?");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000404 EXPECT_EQ(map.u64, 5000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000405 EXPECT_EQ(map.u32, 4000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000406 EXPECT_EQ(map.u16, 65000);
407 EXPECT_EQ(map.u8, 255);
408 EXPECT_EQ(map.b, false);
409 EXPECT_EQ(map.s64, -5000000000LL);
410 EXPECT_EQ(map.s32, -2000000000L);
411 EXPECT_EQ(map.s16, -32000);
412 EXPECT_EQ(map.s8, -127);
413 EXPECT_EQ(map.f, 137.125);
414 EXPECT_EQ(map.d, -2.8625);
415 EXPECT_EQ(map.h8, Hex8(255));
416 EXPECT_EQ(map.h16, Hex16(0x8765));
417 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
418 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
419}
420
421
422//
423// Test writing then reading back all built-in scalar types
424//
425TEST(YAMLIO, TestReadWriteBuiltInTypes) {
426 std::string intermediate;
427 {
428 BuiltInTypes map;
429 map.str = "one two";
John Thompson48e018a2013-11-19 17:28:21 +0000430 map.stdstr = "three four";
Nick Kledzikbed953d2012-12-17 22:11:17 +0000431 map.u64 = 6000000000ULL;
432 map.u32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000433 map.u16 = 50000;
434 map.u8 = 254;
435 map.b = true;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000436 map.s64 = -6000000000LL;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000437 map.s32 = -2000000000;
438 map.s16 = -32000;
439 map.s8 = -128;
440 map.f = 3.25;
441 map.d = -2.8625;
442 map.h8 = 254;
443 map.h16 = 50000;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000444 map.h32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000445 map.h64 = 6000000000LL;
446
447 llvm::raw_string_ostream ostr(intermediate);
448 Output yout(ostr);
449 yout << map;
450 }
451
452 {
453 Input yin(intermediate);
454 BuiltInTypes map;
455 yin >> map;
456
457 EXPECT_FALSE(yin.error());
458 EXPECT_TRUE(map.str.equals("one two"));
John Thompson48e018a2013-11-19 17:28:21 +0000459 EXPECT_TRUE(map.stdstr == "three four");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000460 EXPECT_EQ(map.u64, 6000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000461 EXPECT_EQ(map.u32, 3000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000462 EXPECT_EQ(map.u16, 50000);
463 EXPECT_EQ(map.u8, 254);
464 EXPECT_EQ(map.b, true);
465 EXPECT_EQ(map.s64, -6000000000LL);
466 EXPECT_EQ(map.s32, -2000000000L);
467 EXPECT_EQ(map.s16, -32000);
468 EXPECT_EQ(map.s8, -128);
469 EXPECT_EQ(map.f, 3.25);
470 EXPECT_EQ(map.d, -2.8625);
471 EXPECT_EQ(map.h8, Hex8(254));
472 EXPECT_EQ(map.h16, Hex16(50000));
Nick Kledzikbed953d2012-12-17 22:11:17 +0000473 EXPECT_EQ(map.h32, Hex32(3000000000U));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000474 EXPECT_EQ(map.h64, Hex64(6000000000LL));
475 }
476}
477
Zachary Turner4fbf61d2016-06-07 19:32:09 +0000478//===----------------------------------------------------------------------===//
479// Test endian-aware types
480//===----------------------------------------------------------------------===//
481
482struct EndianTypes {
483 typedef llvm::support::detail::packed_endian_specific_integral<
484 float, llvm::support::little, llvm::support::unaligned>
485 ulittle_float;
486 typedef llvm::support::detail::packed_endian_specific_integral<
487 double, llvm::support::little, llvm::support::unaligned>
488 ulittle_double;
489
490 llvm::support::ulittle64_t u64;
491 llvm::support::ulittle32_t u32;
492 llvm::support::ulittle16_t u16;
493 llvm::support::little64_t s64;
494 llvm::support::little32_t s32;
495 llvm::support::little16_t s16;
496 ulittle_float f;
497 ulittle_double d;
498};
499
500namespace llvm {
501namespace yaml {
502template <> struct MappingTraits<EndianTypes> {
503 static void mapping(IO &io, EndianTypes &et) {
504 io.mapRequired("u64", et.u64);
505 io.mapRequired("u32", et.u32);
506 io.mapRequired("u16", et.u16);
507 io.mapRequired("s64", et.s64);
508 io.mapRequired("s32", et.s32);
509 io.mapRequired("s16", et.s16);
510 io.mapRequired("f", et.f);
511 io.mapRequired("d", et.d);
512 }
513};
514}
515}
516
517//
518// Test the reading of all endian scalar conversions
519//
520TEST(YAMLIO, TestReadEndianTypes) {
521 EndianTypes map;
522 Input yin("---\n"
523 "u64: 5000000000\n"
524 "u32: 4000000000\n"
525 "u16: 65000\n"
526 "s64: -5000000000\n"
527 "s32: -2000000000\n"
528 "s16: -32000\n"
529 "f: 3.25\n"
530 "d: -2.8625\n"
531 "...\n");
532 yin >> map;
533
534 EXPECT_FALSE(yin.error());
535 EXPECT_EQ(map.u64, 5000000000ULL);
536 EXPECT_EQ(map.u32, 4000000000U);
537 EXPECT_EQ(map.u16, 65000);
538 EXPECT_EQ(map.s64, -5000000000LL);
539 EXPECT_EQ(map.s32, -2000000000L);
540 EXPECT_EQ(map.s16, -32000);
541 EXPECT_EQ(map.f, 3.25f);
542 EXPECT_EQ(map.d, -2.8625);
543}
544
545//
546// Test writing then reading back all endian-aware scalar types
547//
548TEST(YAMLIO, TestReadWriteEndianTypes) {
549 std::string intermediate;
550 {
551 EndianTypes map;
552 map.u64 = 6000000000ULL;
553 map.u32 = 3000000000U;
554 map.u16 = 50000;
555 map.s64 = -6000000000LL;
556 map.s32 = -2000000000;
557 map.s16 = -32000;
558 map.f = 3.25f;
559 map.d = -2.8625;
560
561 llvm::raw_string_ostream ostr(intermediate);
562 Output yout(ostr);
563 yout << map;
564 }
565
566 {
567 Input yin(intermediate);
568 EndianTypes map;
569 yin >> map;
570
571 EXPECT_FALSE(yin.error());
572 EXPECT_EQ(map.u64, 6000000000ULL);
573 EXPECT_EQ(map.u32, 3000000000U);
574 EXPECT_EQ(map.u16, 50000);
575 EXPECT_EQ(map.s64, -6000000000LL);
576 EXPECT_EQ(map.s32, -2000000000L);
577 EXPECT_EQ(map.s16, -32000);
578 EXPECT_EQ(map.f, 3.25f);
579 EXPECT_EQ(map.d, -2.8625);
580 }
581}
582
Rui Ueyama106eded2013-09-11 04:00:08 +0000583struct StringTypes {
584 llvm::StringRef str1;
585 llvm::StringRef str2;
586 llvm::StringRef str3;
587 llvm::StringRef str4;
588 llvm::StringRef str5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000589 llvm::StringRef str6;
David Majnemer77880332014-04-10 07:37:33 +0000590 llvm::StringRef str7;
591 llvm::StringRef str8;
592 llvm::StringRef str9;
593 llvm::StringRef str10;
594 llvm::StringRef str11;
John Thompson48e018a2013-11-19 17:28:21 +0000595 std::string stdstr1;
596 std::string stdstr2;
597 std::string stdstr3;
598 std::string stdstr4;
599 std::string stdstr5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000600 std::string stdstr6;
David Majnemer77880332014-04-10 07:37:33 +0000601 std::string stdstr7;
602 std::string stdstr8;
603 std::string stdstr9;
604 std::string stdstr10;
605 std::string stdstr11;
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000606 std::string stdstr12;
Rui Ueyama106eded2013-09-11 04:00:08 +0000607};
Nick Kledzikf60a9272012-12-12 20:46:15 +0000608
Rui Ueyama106eded2013-09-11 04:00:08 +0000609namespace llvm {
610namespace yaml {
611 template <>
612 struct MappingTraits<StringTypes> {
613 static void mapping(IO &io, StringTypes& st) {
614 io.mapRequired("str1", st.str1);
615 io.mapRequired("str2", st.str2);
616 io.mapRequired("str3", st.str3);
617 io.mapRequired("str4", st.str4);
618 io.mapRequired("str5", st.str5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000619 io.mapRequired("str6", st.str6);
David Majnemer77880332014-04-10 07:37:33 +0000620 io.mapRequired("str7", st.str7);
621 io.mapRequired("str8", st.str8);
622 io.mapRequired("str9", st.str9);
623 io.mapRequired("str10", st.str10);
624 io.mapRequired("str11", st.str11);
John Thompson48e018a2013-11-19 17:28:21 +0000625 io.mapRequired("stdstr1", st.stdstr1);
626 io.mapRequired("stdstr2", st.stdstr2);
627 io.mapRequired("stdstr3", st.stdstr3);
628 io.mapRequired("stdstr4", st.stdstr4);
629 io.mapRequired("stdstr5", st.stdstr5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000630 io.mapRequired("stdstr6", st.stdstr6);
David Majnemer77880332014-04-10 07:37:33 +0000631 io.mapRequired("stdstr7", st.stdstr7);
632 io.mapRequired("stdstr8", st.stdstr8);
633 io.mapRequired("stdstr9", st.stdstr9);
634 io.mapRequired("stdstr10", st.stdstr10);
635 io.mapRequired("stdstr11", st.stdstr11);
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000636 io.mapRequired("stdstr12", st.stdstr12);
Rui Ueyama106eded2013-09-11 04:00:08 +0000637 }
638 };
639}
640}
641
642TEST(YAMLIO, TestReadWriteStringTypes) {
643 std::string intermediate;
644 {
645 StringTypes map;
646 map.str1 = "'aaa";
647 map.str2 = "\"bbb";
648 map.str3 = "`ccc";
649 map.str4 = "@ddd";
650 map.str5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000651 map.str6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000652 map.str7 = "true";
653 map.str8 = "FALSE";
654 map.str9 = "~";
655 map.str10 = "0.2e20";
656 map.str11 = "0x30";
John Thompson48e018a2013-11-19 17:28:21 +0000657 map.stdstr1 = "'eee";
658 map.stdstr2 = "\"fff";
659 map.stdstr3 = "`ggg";
660 map.stdstr4 = "@hhh";
661 map.stdstr5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000662 map.stdstr6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000663 map.stdstr7 = "true";
664 map.stdstr8 = "FALSE";
665 map.stdstr9 = "~";
666 map.stdstr10 = "0.2e20";
667 map.stdstr11 = "0x30";
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000668 map.stdstr12 = "- match";
Rui Ueyama106eded2013-09-11 04:00:08 +0000669
670 llvm::raw_string_ostream ostr(intermediate);
671 Output yout(ostr);
672 yout << map;
673 }
674
675 llvm::StringRef flowOut(intermediate);
676 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
677 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
678 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
679 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
680 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000681 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
David Majnemer77880332014-04-10 07:37:33 +0000682 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
683 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
684 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
685 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
686 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000687 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'- match'\n"));
John Thompson48e018a2013-11-19 17:28:21 +0000688 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
689 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
690 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
691 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
692 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000693 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
Rui Ueyama106eded2013-09-11 04:00:08 +0000694
695 {
696 Input yin(intermediate);
697 StringTypes map;
698 yin >> map;
699
700 EXPECT_FALSE(yin.error());
701 EXPECT_TRUE(map.str1.equals("'aaa"));
702 EXPECT_TRUE(map.str2.equals("\"bbb"));
703 EXPECT_TRUE(map.str3.equals("`ccc"));
704 EXPECT_TRUE(map.str4.equals("@ddd"));
705 EXPECT_TRUE(map.str5.equals(""));
David Majnemer97d8ee32014-04-09 17:04:27 +0000706 EXPECT_TRUE(map.str6.equals("0000000004000000"));
John Thompson48e018a2013-11-19 17:28:21 +0000707 EXPECT_TRUE(map.stdstr1 == "'eee");
708 EXPECT_TRUE(map.stdstr2 == "\"fff");
709 EXPECT_TRUE(map.stdstr3 == "`ggg");
710 EXPECT_TRUE(map.stdstr4 == "@hhh");
711 EXPECT_TRUE(map.stdstr5 == "");
David Majnemer97d8ee32014-04-09 17:04:27 +0000712 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
Rui Ueyama106eded2013-09-11 04:00:08 +0000713 }
714}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000715
716//===----------------------------------------------------------------------===//
717// Test ScalarEnumerationTraits
718//===----------------------------------------------------------------------===//
719
720enum Colors {
721 cRed,
722 cBlue,
723 cGreen,
724 cYellow
725};
726
727struct ColorMap {
728 Colors c1;
729 Colors c2;
730 Colors c3;
731 Colors c4;
732 Colors c5;
733 Colors c6;
734};
735
736namespace llvm {
737namespace yaml {
738 template <>
739 struct ScalarEnumerationTraits<Colors> {
740 static void enumeration(IO &io, Colors &value) {
741 io.enumCase(value, "red", cRed);
742 io.enumCase(value, "blue", cBlue);
743 io.enumCase(value, "green", cGreen);
744 io.enumCase(value, "yellow",cYellow);
745 }
746 };
747 template <>
748 struct MappingTraits<ColorMap> {
749 static void mapping(IO &io, ColorMap& c) {
750 io.mapRequired("c1", c.c1);
751 io.mapRequired("c2", c.c2);
752 io.mapRequired("c3", c.c3);
753 io.mapOptional("c4", c.c4, cBlue); // supplies default
754 io.mapOptional("c5", c.c5, cYellow); // supplies default
755 io.mapOptional("c6", c.c6, cRed); // supplies default
756 }
757 };
758}
759}
760
761
762//
763// Test reading enumerated scalars
764//
765TEST(YAMLIO, TestEnumRead) {
766 ColorMap map;
767 Input yin("---\n"
768 "c1: blue\n"
769 "c2: red\n"
770 "c3: green\n"
771 "c5: yellow\n"
772 "...\n");
773 yin >> map;
774
775 EXPECT_FALSE(yin.error());
776 EXPECT_EQ(cBlue, map.c1);
777 EXPECT_EQ(cRed, map.c2);
778 EXPECT_EQ(cGreen, map.c3);
779 EXPECT_EQ(cBlue, map.c4); // tests default
780 EXPECT_EQ(cYellow,map.c5); // tests overridden
781 EXPECT_EQ(cRed, map.c6); // tests default
782}
783
784
785
786//===----------------------------------------------------------------------===//
787// Test ScalarBitSetTraits
788//===----------------------------------------------------------------------===//
789
790enum MyFlags {
791 flagNone = 0,
792 flagBig = 1 << 0,
793 flagFlat = 1 << 1,
794 flagRound = 1 << 2,
795 flagPointy = 1 << 3
796};
797inline MyFlags operator|(MyFlags a, MyFlags b) {
798 return static_cast<MyFlags>(
799 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
800}
801
802struct FlagsMap {
803 MyFlags f1;
804 MyFlags f2;
805 MyFlags f3;
806 MyFlags f4;
807};
808
809
810namespace llvm {
811namespace yaml {
812 template <>
813 struct ScalarBitSetTraits<MyFlags> {
814 static void bitset(IO &io, MyFlags &value) {
815 io.bitSetCase(value, "big", flagBig);
816 io.bitSetCase(value, "flat", flagFlat);
817 io.bitSetCase(value, "round", flagRound);
818 io.bitSetCase(value, "pointy",flagPointy);
819 }
820 };
821 template <>
822 struct MappingTraits<FlagsMap> {
823 static void mapping(IO &io, FlagsMap& c) {
824 io.mapRequired("f1", c.f1);
825 io.mapRequired("f2", c.f2);
826 io.mapRequired("f3", c.f3);
827 io.mapOptional("f4", c.f4, MyFlags(flagRound));
828 }
829 };
830}
831}
832
833
834//
835// Test reading flow sequence representing bit-mask values
836//
837TEST(YAMLIO, TestFlagsRead) {
838 FlagsMap map;
839 Input yin("---\n"
840 "f1: [ big ]\n"
841 "f2: [ round, flat ]\n"
842 "f3: []\n"
843 "...\n");
844 yin >> map;
845
846 EXPECT_FALSE(yin.error());
847 EXPECT_EQ(flagBig, map.f1);
848 EXPECT_EQ(flagRound|flagFlat, map.f2);
849 EXPECT_EQ(flagNone, map.f3); // check empty set
850 EXPECT_EQ(flagRound, map.f4); // check optional key
851}
852
853
854//
855// Test writing then reading back bit-mask values
856//
857TEST(YAMLIO, TestReadWriteFlags) {
858 std::string intermediate;
859 {
860 FlagsMap map;
861 map.f1 = flagBig;
862 map.f2 = flagRound | flagFlat;
863 map.f3 = flagNone;
864 map.f4 = flagNone;
865
866 llvm::raw_string_ostream ostr(intermediate);
867 Output yout(ostr);
868 yout << map;
869 }
870
871 {
872 Input yin(intermediate);
873 FlagsMap map2;
874 yin >> map2;
875
876 EXPECT_FALSE(yin.error());
877 EXPECT_EQ(flagBig, map2.f1);
878 EXPECT_EQ(flagRound|flagFlat, map2.f2);
879 EXPECT_EQ(flagNone, map2.f3);
880 //EXPECT_EQ(flagRound, map2.f4); // check optional key
881 }
882}
883
884
885
886//===----------------------------------------------------------------------===//
887// Test ScalarTraits
888//===----------------------------------------------------------------------===//
889
890struct MyCustomType {
891 int length;
892 int width;
893};
894
895struct MyCustomTypeMap {
896 MyCustomType f1;
897 MyCustomType f2;
898 int f3;
899};
900
901
902namespace llvm {
903namespace yaml {
904 template <>
905 struct MappingTraits<MyCustomTypeMap> {
906 static void mapping(IO &io, MyCustomTypeMap& s) {
907 io.mapRequired("f1", s.f1);
908 io.mapRequired("f2", s.f2);
909 io.mapRequired("f3", s.f3);
910 }
911 };
912 // MyCustomType is formatted as a yaml scalar. A value of
913 // {length=3, width=4} would be represented in yaml as "3 by 4".
914 template<>
915 struct ScalarTraits<MyCustomType> {
916 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
917 out << llvm::format("%d by %d", value.length, value.width);
918 }
919 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
920 size_t byStart = scalar.find("by");
921 if ( byStart != StringRef::npos ) {
922 StringRef lenStr = scalar.slice(0, byStart);
923 lenStr = lenStr.rtrim();
924 if ( lenStr.getAsInteger(0, value.length) ) {
925 return "malformed length";
926 }
927 StringRef widthStr = scalar.drop_front(byStart+2);
928 widthStr = widthStr.ltrim();
929 if ( widthStr.getAsInteger(0, value.width) ) {
930 return "malformed width";
931 }
932 return StringRef();
933 }
934 else {
935 return "malformed by";
936 }
937 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000938 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000939 };
940}
941}
942
943
944//
945// Test writing then reading back custom values
946//
947TEST(YAMLIO, TestReadWriteMyCustomType) {
948 std::string intermediate;
949 {
950 MyCustomTypeMap map;
951 map.f1.length = 1;
952 map.f1.width = 4;
953 map.f2.length = 100;
954 map.f2.width = 400;
955 map.f3 = 10;
956
957 llvm::raw_string_ostream ostr(intermediate);
958 Output yout(ostr);
959 yout << map;
960 }
961
962 {
963 Input yin(intermediate);
964 MyCustomTypeMap map2;
965 yin >> map2;
966
967 EXPECT_FALSE(yin.error());
968 EXPECT_EQ(1, map2.f1.length);
969 EXPECT_EQ(4, map2.f1.width);
970 EXPECT_EQ(100, map2.f2.length);
971 EXPECT_EQ(400, map2.f2.width);
972 EXPECT_EQ(10, map2.f3);
973 }
974}
975
976
977//===----------------------------------------------------------------------===//
Alex Lorenz68e787b2015-05-14 23:08:22 +0000978// Test BlockScalarTraits
979//===----------------------------------------------------------------------===//
980
981struct MultilineStringType {
982 std::string str;
983};
984
985struct MultilineStringTypeMap {
986 MultilineStringType name;
987 MultilineStringType description;
988 MultilineStringType ingredients;
989 MultilineStringType recipes;
990 MultilineStringType warningLabels;
991 MultilineStringType documentation;
992 int price;
993};
994
995namespace llvm {
996namespace yaml {
997 template <>
998 struct MappingTraits<MultilineStringTypeMap> {
999 static void mapping(IO &io, MultilineStringTypeMap& s) {
1000 io.mapRequired("name", s.name);
1001 io.mapRequired("description", s.description);
1002 io.mapRequired("ingredients", s.ingredients);
1003 io.mapRequired("recipes", s.recipes);
1004 io.mapRequired("warningLabels", s.warningLabels);
1005 io.mapRequired("documentation", s.documentation);
1006 io.mapRequired("price", s.price);
1007 }
1008 };
1009
1010 // MultilineStringType is formatted as a yaml block literal scalar. A value of
1011 // "Hello\nWorld" would be represented in yaml as
1012 // |
1013 // Hello
1014 // World
1015 template <>
1016 struct BlockScalarTraits<MultilineStringType> {
1017 static void output(const MultilineStringType &value, void *ctxt,
1018 llvm::raw_ostream &out) {
1019 out << value.str;
1020 }
1021 static StringRef input(StringRef scalar, void *ctxt,
1022 MultilineStringType &value) {
1023 value.str = scalar.str();
1024 return StringRef();
1025 }
1026 };
1027}
1028}
1029
1030LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
1031
1032//
1033// Test writing then reading back custom values
1034//
1035TEST(YAMLIO, TestReadWriteMultilineStringType) {
1036 std::string intermediate;
1037 {
1038 MultilineStringTypeMap map;
1039 map.name.str = "An Item";
1040 map.description.str = "Hello\nWorld";
1041 map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
1042 map.recipes.str = "\n\nTest 1\n\n\n";
1043 map.warningLabels.str = "";
1044 map.documentation.str = "\n\n";
1045 map.price = 350;
1046
1047 llvm::raw_string_ostream ostr(intermediate);
1048 Output yout(ostr);
1049 yout << map;
1050 }
1051 {
1052 Input yin(intermediate);
1053 MultilineStringTypeMap map2;
1054 yin >> map2;
1055
1056 EXPECT_FALSE(yin.error());
1057 EXPECT_EQ(map2.name.str, "An Item\n");
1058 EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
1059 EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
1060 EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
1061 EXPECT_TRUE(map2.warningLabels.str.empty());
1062 EXPECT_TRUE(map2.documentation.str.empty());
1063 EXPECT_EQ(map2.price, 350);
1064 }
1065}
1066
1067//
1068// Test writing then reading back custom values
1069//
1070TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
1071 std::string intermediate;
1072 {
1073 std::vector<MultilineStringType> documents;
1074 MultilineStringType doc;
1075 doc.str = "Hello\nWorld";
1076 documents.push_back(doc);
1077
1078 llvm::raw_string_ostream ostr(intermediate);
1079 Output yout(ostr);
1080 yout << documents;
1081
1082 // Verify that the block scalar header was written out on the same line
1083 // as the document marker.
1084 EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
1085 }
1086 {
1087 Input yin(intermediate);
1088 std::vector<MultilineStringType> documents2;
1089 yin >> documents2;
1090
1091 EXPECT_FALSE(yin.error());
1092 EXPECT_EQ(documents2.size(), size_t(1));
1093 EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
1094 }
1095}
1096
1097TEST(YAMLIO, TestReadWriteBlockScalarValue) {
1098 std::string intermediate;
1099 {
1100 MultilineStringType doc;
1101 doc.str = "Just a block\nscalar doc";
1102
1103 llvm::raw_string_ostream ostr(intermediate);
1104 Output yout(ostr);
1105 yout << doc;
1106 }
1107 {
1108 Input yin(intermediate);
1109 MultilineStringType doc;
1110 yin >> doc;
1111
1112 EXPECT_FALSE(yin.error());
1113 EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
1114 }
1115}
1116
1117//===----------------------------------------------------------------------===//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001118// Test flow sequences
1119//===----------------------------------------------------------------------===//
1120
1121LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
1122LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
Richard Smithd0c0c132017-06-30 20:56:57 +00001123LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef, MyString)
1124LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001125
1126namespace llvm {
1127namespace yaml {
1128 template<>
1129 struct ScalarTraits<MyNumber> {
1130 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
1131 out << value;
1132 }
1133
1134 static StringRef input(StringRef scalar, void *, MyNumber &value) {
David Blaikieb088ff62012-12-12 22:14:32 +00001135 long long n;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001136 if ( getAsSignedInteger(scalar, 0, n) )
1137 return "invalid number";
1138 value = n;
1139 return StringRef();
1140 }
David Majnemer77880332014-04-10 07:37:33 +00001141
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00001142 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001143 };
Richard Smithd0c0c132017-06-30 20:56:57 +00001144
1145 template <> struct ScalarTraits<MyString> {
1146 using Impl = ScalarTraits<StringRef>;
1147 static void output(const MyString &V, void *Ctx, raw_ostream &OS) {
1148 Impl::output(V, Ctx, OS);
1149 }
1150 static StringRef input(StringRef S, void *Ctx, MyString &V) {
1151 return Impl::input(S, Ctx, V.value);
1152 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00001153 static QuotingType mustQuote(StringRef S) {
1154 return Impl::mustQuote(S);
1155 }
Richard Smithd0c0c132017-06-30 20:56:57 +00001156 };
Nick Kledzikf60a9272012-12-12 20:46:15 +00001157}
1158}
1159
1160struct NameAndNumbers {
1161 llvm::StringRef name;
Richard Smithd0c0c132017-06-30 20:56:57 +00001162 std::vector<MyString> strings;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001163 std::vector<MyNumber> single;
1164 std::vector<MyNumber> numbers;
1165};
1166
1167namespace llvm {
1168namespace yaml {
1169 template <>
1170 struct MappingTraits<NameAndNumbers> {
1171 static void mapping(IO &io, NameAndNumbers& nn) {
1172 io.mapRequired("name", nn.name);
1173 io.mapRequired("strings", nn.strings);
1174 io.mapRequired("single", nn.single);
1175 io.mapRequired("numbers", nn.numbers);
1176 }
1177 };
1178}
1179}
1180
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001181typedef std::vector<MyNumber> MyNumberFlowSequence;
1182
1183LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1184
1185struct NameAndNumbersFlow {
1186 llvm::StringRef name;
1187 std::vector<MyNumberFlowSequence> sequenceOfNumbers;
1188};
1189
1190namespace llvm {
1191namespace yaml {
1192 template <>
1193 struct MappingTraits<NameAndNumbersFlow> {
1194 static void mapping(IO &io, NameAndNumbersFlow& nn) {
1195 io.mapRequired("name", nn.name);
1196 io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
1197 }
1198 };
1199}
1200}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001201
1202//
1203// Test writing then reading back custom values
1204//
1205TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1206 std::string intermediate;
1207 {
1208 NameAndNumbers map;
1209 map.name = "hello";
1210 map.strings.push_back(llvm::StringRef("one"));
1211 map.strings.push_back(llvm::StringRef("two"));
1212 map.single.push_back(1);
1213 map.numbers.push_back(10);
1214 map.numbers.push_back(-30);
1215 map.numbers.push_back(1024);
1216
1217 llvm::raw_string_ostream ostr(intermediate);
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001218 Output yout(ostr);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001219 yout << map;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001220
Nick Kledzik11964f22013-01-04 19:32:00 +00001221 // Verify sequences were written in flow style
1222 ostr.flush();
1223 llvm::StringRef flowOut(intermediate);
1224 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1225 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001226 }
1227
1228 {
1229 Input yin(intermediate);
1230 NameAndNumbers map2;
1231 yin >> map2;
1232
1233 EXPECT_FALSE(yin.error());
1234 EXPECT_TRUE(map2.name.equals("hello"));
1235 EXPECT_EQ(map2.strings.size(), 2UL);
Richard Smithd0c0c132017-06-30 20:56:57 +00001236 EXPECT_TRUE(map2.strings[0].value.equals("one"));
1237 EXPECT_TRUE(map2.strings[1].value.equals("two"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001238 EXPECT_EQ(map2.single.size(), 1UL);
1239 EXPECT_EQ(1, map2.single[0]);
1240 EXPECT_EQ(map2.numbers.size(), 3UL);
1241 EXPECT_EQ(10, map2.numbers[0]);
1242 EXPECT_EQ(-30, map2.numbers[1]);
1243 EXPECT_EQ(1024, map2.numbers[2]);
1244 }
1245}
1246
1247
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001248//
1249// Test writing then reading back a sequence of flow sequences.
1250//
1251TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1252 std::string intermediate;
1253 {
1254 NameAndNumbersFlow map;
1255 map.name = "hello";
1256 MyNumberFlowSequence single = { 0 };
1257 MyNumberFlowSequence numbers = { 12, 1, -512 };
1258 map.sequenceOfNumbers.push_back(single);
1259 map.sequenceOfNumbers.push_back(numbers);
1260 map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1261
1262 llvm::raw_string_ostream ostr(intermediate);
1263 Output yout(ostr);
1264 yout << map;
1265
1266 // Verify sequences were written in flow style
1267 // and that the parent sequence used '-'.
1268 ostr.flush();
1269 llvm::StringRef flowOut(intermediate);
1270 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1271 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1272 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
1273 }
1274
1275 {
1276 Input yin(intermediate);
1277 NameAndNumbersFlow map2;
1278 yin >> map2;
1279
1280 EXPECT_FALSE(yin.error());
1281 EXPECT_TRUE(map2.name.equals("hello"));
1282 EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1283 EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1284 EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
1285 EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1286 EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
1287 EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
1288 EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1289 EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1290 }
1291}
1292
Nick Kledzikf60a9272012-12-12 20:46:15 +00001293//===----------------------------------------------------------------------===//
1294// Test normalizing/denormalizing
1295//===----------------------------------------------------------------------===//
1296
1297LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1298
1299typedef std::vector<TotalSeconds> SecondsSequence;
1300
Nick Kledzik11964f22013-01-04 19:32:00 +00001301LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001302
1303
1304namespace llvm {
1305namespace yaml {
1306 template <>
1307 struct MappingTraits<TotalSeconds> {
1308
1309 class NormalizedSeconds {
1310 public:
1311 NormalizedSeconds(IO &io)
1312 : hours(0), minutes(0), seconds(0) {
1313 }
1314 NormalizedSeconds(IO &, TotalSeconds &secs)
1315 : hours(secs/3600),
1316 minutes((secs - (hours*3600))/60),
1317 seconds(secs % 60) {
1318 }
1319 TotalSeconds denormalize(IO &) {
1320 return TotalSeconds(hours*3600 + minutes*60 + seconds);
1321 }
1322
1323 uint32_t hours;
1324 uint8_t minutes;
1325 uint8_t seconds;
1326 };
1327
1328 static void mapping(IO &io, TotalSeconds &secs) {
1329 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1330
1331 io.mapOptional("hours", keys->hours, (uint32_t)0);
1332 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
1333 io.mapRequired("seconds", keys->seconds);
1334 }
1335 };
1336}
1337}
1338
1339
1340//
1341// Test the reading of a yaml sequence of mappings
1342//
1343TEST(YAMLIO, TestReadMySecondsSequence) {
1344 SecondsSequence seq;
1345 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1346 yin >> seq;
1347
1348 EXPECT_FALSE(yin.error());
1349 EXPECT_EQ(seq.size(), 2UL);
1350 EXPECT_EQ(seq[0], 3605U);
1351 EXPECT_EQ(seq[1], 59U);
1352}
1353
1354
1355//
1356// Test writing then reading back custom values
1357//
1358TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1359 std::string intermediate;
1360 {
1361 SecondsSequence seq;
1362 seq.push_back(4000);
1363 seq.push_back(500);
1364 seq.push_back(59);
1365
1366 llvm::raw_string_ostream ostr(intermediate);
1367 Output yout(ostr);
1368 yout << seq;
1369 }
1370 {
1371 Input yin(intermediate);
1372 SecondsSequence seq2;
1373 yin >> seq2;
1374
1375 EXPECT_FALSE(yin.error());
1376 EXPECT_EQ(seq2.size(), 3UL);
1377 EXPECT_EQ(seq2[0], 4000U);
1378 EXPECT_EQ(seq2[1], 500U);
1379 EXPECT_EQ(seq2[2], 59U);
1380 }
1381}
1382
1383
1384//===----------------------------------------------------------------------===//
1385// Test dynamic typing
1386//===----------------------------------------------------------------------===//
1387
1388enum AFlags {
1389 a1,
1390 a2,
1391 a3
1392};
1393
1394enum BFlags {
1395 b1,
1396 b2,
1397 b3
1398};
1399
1400enum Kind {
1401 kindA,
1402 kindB
1403};
1404
1405struct KindAndFlags {
1406 KindAndFlags() : kind(kindA), flags(0) { }
1407 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1408 Kind kind;
1409 uint32_t flags;
1410};
1411
1412typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1413
Nick Kledzik11964f22013-01-04 19:32:00 +00001414LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001415
1416namespace llvm {
1417namespace yaml {
1418 template <>
1419 struct ScalarEnumerationTraits<AFlags> {
1420 static void enumeration(IO &io, AFlags &value) {
1421 io.enumCase(value, "a1", a1);
1422 io.enumCase(value, "a2", a2);
1423 io.enumCase(value, "a3", a3);
1424 }
1425 };
1426 template <>
1427 struct ScalarEnumerationTraits<BFlags> {
1428 static void enumeration(IO &io, BFlags &value) {
1429 io.enumCase(value, "b1", b1);
1430 io.enumCase(value, "b2", b2);
1431 io.enumCase(value, "b3", b3);
1432 }
1433 };
1434 template <>
1435 struct ScalarEnumerationTraits<Kind> {
1436 static void enumeration(IO &io, Kind &value) {
1437 io.enumCase(value, "A", kindA);
1438 io.enumCase(value, "B", kindB);
1439 }
1440 };
1441 template <>
1442 struct MappingTraits<KindAndFlags> {
1443 static void mapping(IO &io, KindAndFlags& kf) {
1444 io.mapRequired("kind", kf.kind);
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001445 // Type of "flags" field varies depending on "kind" field.
David Greene4162c2d2013-01-10 18:17:54 +00001446 // Use memcpy here to avoid breaking strict aliasing rules.
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001447 if (kf.kind == kindA) {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001448 AFlags aflags = static_cast<AFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001449 io.mapRequired("flags", aflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001450 kf.flags = aflags;
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001451 } else {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001452 BFlags bflags = static_cast<BFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001453 io.mapRequired("flags", bflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001454 kf.flags = bflags;
David Greene4162c2d2013-01-10 18:17:54 +00001455 }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001456 }
1457 };
1458}
1459}
1460
1461
1462//
1463// Test the reading of a yaml sequence dynamic types
1464//
1465TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1466 KindAndFlagsSequence seq;
1467 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1468 yin >> seq;
1469
1470 EXPECT_FALSE(yin.error());
1471 EXPECT_EQ(seq.size(), 2UL);
1472 EXPECT_EQ(seq[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001473 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001474 EXPECT_EQ(seq[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001475 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001476}
1477
1478//
1479// Test writing then reading back dynamic types
1480//
1481TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1482 std::string intermediate;
1483 {
1484 KindAndFlagsSequence seq;
1485 seq.push_back(KindAndFlags(kindA,a1));
1486 seq.push_back(KindAndFlags(kindB,b1));
1487 seq.push_back(KindAndFlags(kindA,a2));
1488 seq.push_back(KindAndFlags(kindB,b2));
1489 seq.push_back(KindAndFlags(kindA,a3));
1490
1491 llvm::raw_string_ostream ostr(intermediate);
1492 Output yout(ostr);
1493 yout << seq;
1494 }
1495 {
1496 Input yin(intermediate);
1497 KindAndFlagsSequence seq2;
1498 yin >> seq2;
1499
1500 EXPECT_FALSE(yin.error());
1501 EXPECT_EQ(seq2.size(), 5UL);
1502 EXPECT_EQ(seq2[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001503 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001504 EXPECT_EQ(seq2[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001505 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001506 EXPECT_EQ(seq2[2].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001507 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001508 EXPECT_EQ(seq2[3].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001509 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001510 EXPECT_EQ(seq2[4].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001511 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001512 }
1513}
1514
1515
1516//===----------------------------------------------------------------------===//
1517// Test document list
1518//===----------------------------------------------------------------------===//
1519
1520struct FooBarMap {
1521 int foo;
1522 int bar;
1523};
1524typedef std::vector<FooBarMap> FooBarMapDocumentList;
1525
1526LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1527
1528
1529namespace llvm {
1530namespace yaml {
1531 template <>
1532 struct MappingTraits<FooBarMap> {
1533 static void mapping(IO &io, FooBarMap& fb) {
1534 io.mapRequired("foo", fb.foo);
1535 io.mapRequired("bar", fb.bar);
1536 }
1537 };
1538}
1539}
1540
1541
1542//
1543// Test the reading of a yaml mapping
1544//
1545TEST(YAMLIO, TestDocRead) {
1546 FooBarMap doc;
1547 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1548 yin >> doc;
1549
1550 EXPECT_FALSE(yin.error());
1551 EXPECT_EQ(doc.foo, 3);
1552 EXPECT_EQ(doc.bar,5);
1553}
1554
1555
1556
1557//
1558// Test writing then reading back a sequence of mappings
1559//
1560TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1561 std::string intermediate;
1562 {
1563 FooBarMap doc1;
1564 doc1.foo = 10;
1565 doc1.bar = -3;
1566 FooBarMap doc2;
1567 doc2.foo = 257;
1568 doc2.bar = 0;
1569 std::vector<FooBarMap> docList;
1570 docList.push_back(doc1);
1571 docList.push_back(doc2);
1572
1573 llvm::raw_string_ostream ostr(intermediate);
1574 Output yout(ostr);
1575 yout << docList;
1576 }
1577
1578
1579 {
1580 Input yin(intermediate);
1581 std::vector<FooBarMap> docList2;
1582 yin >> docList2;
1583
1584 EXPECT_FALSE(yin.error());
1585 EXPECT_EQ(docList2.size(), 2UL);
1586 FooBarMap& map1 = docList2[0];
1587 FooBarMap& map2 = docList2[1];
1588 EXPECT_EQ(map1.foo, 10);
1589 EXPECT_EQ(map1.bar, -3);
1590 EXPECT_EQ(map2.foo, 257);
1591 EXPECT_EQ(map2.bar, 0);
1592 }
1593}
1594
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001595//===----------------------------------------------------------------------===//
1596// Test document tags
1597//===----------------------------------------------------------------------===//
1598
1599struct MyDouble {
1600 MyDouble() : value(0.0) { }
1601 MyDouble(double x) : value(x) { }
1602 double value;
1603};
1604
Nick Kledzik4a9f00d2013-11-14 03:03:05 +00001605LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001606
1607
1608namespace llvm {
1609namespace yaml {
1610 template <>
1611 struct MappingTraits<MyDouble> {
1612 static void mapping(IO &io, MyDouble &d) {
1613 if (io.mapTag("!decimal", true)) {
1614 mappingDecimal(io, d);
1615 } else if (io.mapTag("!fraction")) {
1616 mappingFraction(io, d);
1617 }
1618 }
1619 static void mappingDecimal(IO &io, MyDouble &d) {
1620 io.mapRequired("value", d.value);
1621 }
1622 static void mappingFraction(IO &io, MyDouble &d) {
1623 double num, denom;
1624 io.mapRequired("numerator", num);
1625 io.mapRequired("denominator", denom);
1626 // convert fraction to double
1627 d.value = num/denom;
1628 }
1629 };
1630 }
1631}
1632
1633
1634//
1635// Test the reading of two different tagged yaml documents.
1636//
1637TEST(YAMLIO, TestTaggedDocuments) {
1638 std::vector<MyDouble> docList;
1639 Input yin("--- !decimal\nvalue: 3.0\n"
1640 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1641 yin >> docList;
1642 EXPECT_FALSE(yin.error());
1643 EXPECT_EQ(docList.size(), 2UL);
1644 EXPECT_EQ(docList[0].value, 3.0);
1645 EXPECT_EQ(docList[1].value, 4.5);
1646}
1647
1648
1649
1650//
1651// Test writing then reading back tagged documents
1652//
1653TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1654 std::string intermediate;
1655 {
1656 MyDouble a(10.25);
1657 MyDouble b(-3.75);
1658 std::vector<MyDouble> docList;
1659 docList.push_back(a);
1660 docList.push_back(b);
1661
1662 llvm::raw_string_ostream ostr(intermediate);
1663 Output yout(ostr);
1664 yout << docList;
1665 }
1666
1667 {
1668 Input yin(intermediate);
1669 std::vector<MyDouble> docList2;
1670 yin >> docList2;
1671
1672 EXPECT_FALSE(yin.error());
1673 EXPECT_EQ(docList2.size(), 2UL);
1674 EXPECT_EQ(docList2[0].value, 10.25);
1675 EXPECT_EQ(docList2[1].value, -3.75);
1676 }
1677}
1678
1679
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001680//===----------------------------------------------------------------------===//
1681// Test mapping validation
1682//===----------------------------------------------------------------------===//
1683
1684struct MyValidation {
1685 double value;
1686};
1687
1688LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1689
1690namespace llvm {
1691namespace yaml {
1692 template <>
1693 struct MappingTraits<MyValidation> {
1694 static void mapping(IO &io, MyValidation &d) {
1695 io.mapRequired("value", d.value);
1696 }
1697 static StringRef validate(IO &io, MyValidation &d) {
1698 if (d.value < 0)
1699 return "negative value";
1700 return StringRef();
1701 }
1702 };
1703 }
1704}
1705
1706
1707//
1708// Test that validate() is called and complains about the negative value.
1709//
1710TEST(YAMLIO, TestValidatingInput) {
1711 std::vector<MyValidation> docList;
1712 Input yin("--- \nvalue: 3.0\n"
1713 "--- \nvalue: -1.0\n...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001714 nullptr, suppressErrorMessages);
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001715 yin >> docList;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001716 EXPECT_TRUE(!!yin.error());
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001717}
1718
Alex Lorenzb1225082015-05-04 20:11:40 +00001719//===----------------------------------------------------------------------===//
1720// Test flow mapping
1721//===----------------------------------------------------------------------===//
1722
1723struct FlowFooBar {
1724 int foo;
1725 int bar;
1726
1727 FlowFooBar() : foo(0), bar(0) {}
1728 FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1729};
1730
1731typedef std::vector<FlowFooBar> FlowFooBarSequence;
1732
1733LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1734
1735struct FlowFooBarDoc {
1736 FlowFooBar attribute;
1737 FlowFooBarSequence seq;
1738};
1739
1740namespace llvm {
1741namespace yaml {
1742 template <>
1743 struct MappingTraits<FlowFooBar> {
1744 static void mapping(IO &io, FlowFooBar &fb) {
1745 io.mapRequired("foo", fb.foo);
1746 io.mapRequired("bar", fb.bar);
1747 }
1748
1749 static const bool flow = true;
1750 };
1751
1752 template <>
1753 struct MappingTraits<FlowFooBarDoc> {
1754 static void mapping(IO &io, FlowFooBarDoc &fb) {
1755 io.mapRequired("attribute", fb.attribute);
1756 io.mapRequired("seq", fb.seq);
1757 }
1758 };
1759}
1760}
1761
1762//
1763// Test writing then reading back custom mappings
1764//
1765TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1766 std::string intermediate;
1767 {
1768 FlowFooBarDoc doc;
1769 doc.attribute = FlowFooBar(42, 907);
1770 doc.seq.push_back(FlowFooBar(1, 2));
1771 doc.seq.push_back(FlowFooBar(0, 0));
1772 doc.seq.push_back(FlowFooBar(-1, 1024));
1773
1774 llvm::raw_string_ostream ostr(intermediate);
1775 Output yout(ostr);
1776 yout << doc;
1777
1778 // Verify that mappings were written in flow style
1779 ostr.flush();
1780 llvm::StringRef flowOut(intermediate);
1781 EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1782 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1783 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1784 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1785 }
1786
1787 {
1788 Input yin(intermediate);
1789 FlowFooBarDoc doc2;
1790 yin >> doc2;
1791
1792 EXPECT_FALSE(yin.error());
1793 EXPECT_EQ(doc2.attribute.foo, 42);
1794 EXPECT_EQ(doc2.attribute.bar, 907);
1795 EXPECT_EQ(doc2.seq.size(), 3UL);
1796 EXPECT_EQ(doc2.seq[0].foo, 1);
1797 EXPECT_EQ(doc2.seq[0].bar, 2);
1798 EXPECT_EQ(doc2.seq[1].foo, 0);
1799 EXPECT_EQ(doc2.seq[1].bar, 0);
1800 EXPECT_EQ(doc2.seq[2].foo, -1);
1801 EXPECT_EQ(doc2.seq[2].bar, 1024);
1802 }
1803}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001804
1805//===----------------------------------------------------------------------===//
1806// Test error handling
1807//===----------------------------------------------------------------------===//
1808
Nick Kledzikf60a9272012-12-12 20:46:15 +00001809//
1810// Test error handling of unknown enumerated scalar
1811//
1812TEST(YAMLIO, TestColorsReadError) {
1813 ColorMap map;
1814 Input yin("---\n"
1815 "c1: blue\n"
1816 "c2: purple\n"
1817 "c3: green\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001818 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001819 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001820 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001821 yin >> map;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001822 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001823}
1824
1825
1826//
1827// Test error handling of flow sequence with unknown value
1828//
1829TEST(YAMLIO, TestFlagsReadError) {
1830 FlagsMap map;
1831 Input yin("---\n"
1832 "f1: [ big ]\n"
1833 "f2: [ round, hollow ]\n"
1834 "f3: []\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001835 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001836 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001837 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001838 yin >> map;
1839
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001840 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001841}
1842
1843
1844//
1845// Test error handling reading built-in uint8_t type
1846//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001847TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1848 std::vector<uint8_t> seq;
1849 Input yin("---\n"
1850 "- 255\n"
1851 "- 0\n"
1852 "- 257\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001853 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001854 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001855 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001856 yin >> seq;
1857
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001858 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001859}
1860
1861
1862//
1863// Test error handling reading built-in uint16_t type
1864//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001865TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1866 std::vector<uint16_t> seq;
1867 Input yin("---\n"
1868 "- 65535\n"
1869 "- 0\n"
1870 "- 66000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001871 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001872 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001873 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001874 yin >> seq;
1875
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001876 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001877}
1878
1879
1880//
1881// Test error handling reading built-in uint32_t type
1882//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001883TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1884 std::vector<uint32_t> seq;
1885 Input yin("---\n"
1886 "- 4000000000\n"
1887 "- 0\n"
1888 "- 5000000000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001889 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001890 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001891 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001892 yin >> seq;
1893
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001894 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001895}
1896
1897
1898//
1899// Test error handling reading built-in uint64_t type
1900//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001901TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1902 std::vector<uint64_t> seq;
1903 Input yin("---\n"
1904 "- 18446744073709551615\n"
1905 "- 0\n"
1906 "- 19446744073709551615\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001907 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001908 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001909 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001910 yin >> seq;
1911
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001912 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001913}
1914
1915
1916//
1917// Test error handling reading built-in int8_t type
1918//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001919TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1920 std::vector<int8_t> seq;
1921 Input yin("---\n"
1922 "- -128\n"
1923 "- 0\n"
1924 "- 127\n"
1925 "- 128\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001926 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001927 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001928 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001929 yin >> seq;
1930
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001931 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001932}
1933
1934//
1935// Test error handling reading built-in int8_t type
1936//
1937TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1938 std::vector<int8_t> seq;
1939 Input yin("---\n"
1940 "- -128\n"
1941 "- 0\n"
1942 "- 127\n"
1943 "- -129\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001944 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001945 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001946 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001947 yin >> seq;
1948
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001949 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001950}
1951
1952
1953//
1954// Test error handling reading built-in int16_t type
1955//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001956TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1957 std::vector<int16_t> seq;
1958 Input yin("---\n"
1959 "- 32767\n"
1960 "- 0\n"
1961 "- -32768\n"
1962 "- -32769\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001963 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001964 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001965 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001966 yin >> seq;
1967
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001968 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001969}
1970
1971
1972//
1973// Test error handling reading built-in int16_t type
1974//
1975TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1976 std::vector<int16_t> seq;
1977 Input yin("---\n"
1978 "- 32767\n"
1979 "- 0\n"
1980 "- -32768\n"
1981 "- 32768\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001982 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001983 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001984 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001985 yin >> seq;
1986
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001987 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001988}
1989
1990
1991//
1992// Test error handling reading built-in int32_t type
1993//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001994TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1995 std::vector<int32_t> seq;
1996 Input yin("---\n"
1997 "- 2147483647\n"
1998 "- 0\n"
1999 "- -2147483648\n"
2000 "- -2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002001 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002002 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002003 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002004 yin >> seq;
2005
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002006 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002007}
2008
2009//
2010// Test error handling reading built-in int32_t type
2011//
2012TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
2013 std::vector<int32_t> seq;
2014 Input yin("---\n"
2015 "- 2147483647\n"
2016 "- 0\n"
2017 "- -2147483648\n"
2018 "- 2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002019 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002020 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002021 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002022 yin >> seq;
2023
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002024 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002025}
2026
2027
2028//
2029// Test error handling reading built-in int64_t type
2030//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002031TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
2032 std::vector<int64_t> seq;
2033 Input yin("---\n"
2034 "- -9223372036854775808\n"
2035 "- 0\n"
2036 "- 9223372036854775807\n"
2037 "- -9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002038 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002039 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002040 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002041 yin >> seq;
2042
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002043 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002044}
2045
2046//
2047// Test error handling reading built-in int64_t type
2048//
2049TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
2050 std::vector<int64_t> seq;
2051 Input yin("---\n"
2052 "- -9223372036854775808\n"
2053 "- 0\n"
2054 "- 9223372036854775807\n"
2055 "- 9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002056 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002057 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002058 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002059 yin >> seq;
2060
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002061 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002062}
2063
2064//
2065// Test error handling reading built-in float type
2066//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002067TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
2068 std::vector<float> seq;
2069 Input yin("---\n"
2070 "- 0.0\n"
2071 "- 1000.1\n"
2072 "- -123.456\n"
2073 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002074 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002075 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002076 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002077 yin >> seq;
2078
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002079 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002080}
2081
2082//
2083// Test error handling reading built-in float type
2084//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002085TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
2086 std::vector<double> seq;
2087 Input yin("---\n"
2088 "- 0.0\n"
2089 "- 1000.1\n"
2090 "- -123.456\n"
2091 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002092 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002093 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002094 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002095 yin >> seq;
2096
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002097 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002098}
2099
2100//
2101// Test error handling reading built-in Hex8 type
2102//
2103LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
2104TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
2105 std::vector<Hex8> seq;
2106 Input yin("---\n"
2107 "- 0x12\n"
2108 "- 0xFE\n"
2109 "- 0x123\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002110 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002111 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002112 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002113 yin >> seq;
2114
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002115 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002116}
2117
2118
2119//
2120// Test error handling reading built-in Hex16 type
2121//
2122LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
2123TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
2124 std::vector<Hex16> seq;
2125 Input yin("---\n"
2126 "- 0x0012\n"
2127 "- 0xFEFF\n"
2128 "- 0x12345\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002129 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002130 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002131 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002132 yin >> seq;
2133
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002134 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002135}
2136
2137//
2138// Test error handling reading built-in Hex32 type
2139//
2140LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
2141TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
2142 std::vector<Hex32> seq;
2143 Input yin("---\n"
2144 "- 0x0012\n"
2145 "- 0xFEFF0000\n"
2146 "- 0x1234556789\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002147 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002148 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002149 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002150 yin >> seq;
2151
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002152 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002153}
2154
2155//
2156// Test error handling reading built-in Hex64 type
2157//
2158LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
2159TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
2160 std::vector<Hex64> seq;
2161 Input yin("---\n"
2162 "- 0x0012\n"
2163 "- 0xFFEEDDCCBBAA9988\n"
2164 "- 0x12345567890ABCDEF0\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002165 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002166 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002167 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002168 yin >> seq;
2169
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002170 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002171}
2172
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002173TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2174 FooBar doc;
2175 {
2176 // We pass the suppressErrorMessages handler to handle the error
2177 // message generated in the constructor of Input.
Craig Topper66f09ad2014-06-08 22:29:17 +00002178 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002179 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002180 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002181 }
2182
2183 {
Craig Topper66f09ad2014-06-08 22:29:17 +00002184 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002185 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002186 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002187 }
2188}
2189
Aaron Ballman0e63e532013-08-15 23:17:53 +00002190struct OptionalTest {
2191 std::vector<int> Numbers;
2192};
2193
2194struct OptionalTestSeq {
2195 std::vector<OptionalTest> Tests;
2196};
2197
Aaron Ballman381f59f2013-08-16 01:53:58 +00002198LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
Aaron Ballman0e63e532013-08-15 23:17:53 +00002199namespace llvm {
2200namespace yaml {
2201 template <>
2202 struct MappingTraits<OptionalTest> {
2203 static void mapping(IO& IO, OptionalTest &OT) {
2204 IO.mapOptional("Numbers", OT.Numbers);
2205 }
2206 };
2207
2208 template <>
2209 struct MappingTraits<OptionalTestSeq> {
2210 static void mapping(IO &IO, OptionalTestSeq &OTS) {
2211 IO.mapOptional("Tests", OTS.Tests);
2212 }
2213 };
2214}
2215}
2216
2217TEST(YAMLIO, SequenceElideTest) {
2218 // Test that writing out a purely optional structure with its fields set to
2219 // default followed by other data is properly read back in.
2220 OptionalTestSeq Seq;
2221 OptionalTest One, Two, Three, Four;
2222 int N[] = {1, 2, 3};
2223 Three.Numbers.assign(N, N + 3);
2224 Seq.Tests.push_back(One);
2225 Seq.Tests.push_back(Two);
2226 Seq.Tests.push_back(Three);
2227 Seq.Tests.push_back(Four);
2228
2229 std::string intermediate;
2230 {
2231 llvm::raw_string_ostream ostr(intermediate);
2232 Output yout(ostr);
2233 yout << Seq;
2234 }
2235
2236 Input yin(intermediate);
2237 OptionalTestSeq Seq2;
2238 yin >> Seq2;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00002239
Aaron Ballman0e63e532013-08-15 23:17:53 +00002240 EXPECT_FALSE(yin.error());
2241
2242 EXPECT_EQ(4UL, Seq2.Tests.size());
2243
2244 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2245 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2246
2247 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2248 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2249 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2250
2251 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2252}
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002253
2254TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2255 FooBar doc;
2256 Input yin("");
2257 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002258 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002259}
2260
2261TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2262 OptionalTest doc;
2263 Input yin("");
2264 yin >> doc;
2265 EXPECT_FALSE(yin.error());
2266}
2267
2268TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2269 std::vector<uint8_t> seq;
Craig Topper66f09ad2014-06-08 22:29:17 +00002270 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002271 yin >> seq;
2272
2273 EXPECT_FALSE(yin.error());
2274 EXPECT_TRUE(seq.empty());
2275}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002276
2277struct FlowMap {
2278 llvm::StringRef str1, str2, str3;
2279 FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2280 : str1(str1), str2(str2), str3(str3) {}
2281};
2282
Frederic Riss3733c032015-05-29 18:14:55 +00002283struct FlowSeq {
2284 llvm::StringRef str;
2285 FlowSeq(llvm::StringRef S) : str(S) {}
2286 FlowSeq() = default;
2287};
2288
Frederic Riss4939e6a2015-05-29 17:56:28 +00002289namespace llvm {
2290namespace yaml {
2291 template <>
2292 struct MappingTraits<FlowMap> {
2293 static void mapping(IO &io, FlowMap &fm) {
2294 io.mapRequired("str1", fm.str1);
2295 io.mapRequired("str2", fm.str2);
2296 io.mapRequired("str3", fm.str3);
2297 }
2298
2299 static const bool flow = true;
2300 };
Frederic Riss4939e6a2015-05-29 17:56:28 +00002301
2302template <>
2303struct ScalarTraits<FlowSeq> {
2304 static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2305 out << value.str;
2306 }
2307 static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2308 value.str = scalar;
2309 return "";
2310 }
2311
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002312 static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
Frederic Riss4939e6a2015-05-29 17:56:28 +00002313};
Frederic Riss3733c032015-05-29 18:14:55 +00002314}
2315}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002316
2317LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2318
2319TEST(YAMLIO, TestWrapFlow) {
2320 std::string out;
2321 llvm::raw_string_ostream ostr(out);
2322 FlowMap Map("This is str1", "This is str2", "This is str3");
2323 std::vector<FlowSeq> Seq;
2324 Seq.emplace_back("This is str1");
2325 Seq.emplace_back("This is str2");
2326 Seq.emplace_back("This is str3");
2327
2328 {
2329 // 20 is just bellow the total length of the first mapping field.
2330 // We should wreap at every element.
2331 Output yout(ostr, nullptr, 15);
2332
2333 yout << Map;
2334 ostr.flush();
2335 EXPECT_EQ(out,
2336 "---\n"
2337 "{ str1: This is str1, \n"
2338 " str2: This is str2, \n"
2339 " str3: This is str3 }\n"
2340 "...\n");
2341 out.clear();
2342
2343 yout << Seq;
2344 ostr.flush();
2345 EXPECT_EQ(out,
2346 "---\n"
2347 "[ This is str1, \n"
2348 " This is str2, \n"
2349 " This is str3 ]\n"
2350 "...\n");
2351 out.clear();
2352 }
2353 {
2354 // 25 will allow the second field to be output on the first line.
2355 Output yout(ostr, nullptr, 25);
2356
2357 yout << Map;
2358 ostr.flush();
2359 EXPECT_EQ(out,
2360 "---\n"
2361 "{ str1: This is str1, str2: This is str2, \n"
2362 " str3: This is str3 }\n"
2363 "...\n");
2364 out.clear();
2365
2366 yout << Seq;
2367 ostr.flush();
2368 EXPECT_EQ(out,
2369 "---\n"
2370 "[ This is str1, This is str2, \n"
2371 " This is str3 ]\n"
2372 "...\n");
2373 out.clear();
2374 }
2375 {
2376 // 0 means no wrapping.
2377 Output yout(ostr, nullptr, 0);
2378
2379 yout << Map;
2380 ostr.flush();
2381 EXPECT_EQ(out,
2382 "---\n"
2383 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2384 "...\n");
2385 out.clear();
2386
2387 yout << Seq;
2388 ostr.flush();
2389 EXPECT_EQ(out,
2390 "---\n"
2391 "[ This is str1, This is str2, This is str3 ]\n"
2392 "...\n");
2393 out.clear();
2394 }
2395}
Zachary Turner35377f82016-09-08 18:22:44 +00002396
2397struct MappingContext {
2398 int A = 0;
2399};
2400struct SimpleMap {
2401 int B = 0;
2402 int C = 0;
2403};
2404
2405struct NestedMap {
2406 NestedMap(MappingContext &Context) : Context(Context) {}
2407 SimpleMap Simple;
2408 MappingContext &Context;
2409};
2410
2411namespace llvm {
2412namespace yaml {
2413template <> struct MappingContextTraits<SimpleMap, MappingContext> {
2414 static void mapping(IO &io, SimpleMap &sm, MappingContext &Context) {
2415 io.mapRequired("B", sm.B);
2416 io.mapRequired("C", sm.C);
2417 ++Context.A;
2418 io.mapRequired("Context", Context.A);
2419 }
2420};
2421
2422template <> struct MappingTraits<NestedMap> {
2423 static void mapping(IO &io, NestedMap &nm) {
2424 io.mapRequired("Simple", nm.Simple, nm.Context);
2425 }
2426};
2427}
2428}
2429
2430TEST(YAMLIO, TestMapWithContext) {
2431 MappingContext Context;
2432 NestedMap Nested(Context);
2433 std::string out;
2434 llvm::raw_string_ostream ostr(out);
2435
2436 Output yout(ostr, nullptr, 15);
2437
2438 yout << Nested;
2439 ostr.flush();
2440 EXPECT_EQ(1, Context.A);
2441 EXPECT_EQ("---\n"
2442 "Simple: \n"
2443 " B: 0\n"
2444 " C: 0\n"
2445 " Context: 1\n"
2446 "...\n",
2447 out);
2448
2449 out.clear();
2450
2451 Nested.Simple.B = 2;
2452 Nested.Simple.C = 3;
2453 yout << Nested;
2454 ostr.flush();
2455 EXPECT_EQ(2, Context.A);
2456 EXPECT_EQ("---\n"
2457 "Simple: \n"
2458 " B: 2\n"
2459 " C: 3\n"
2460 " Context: 2\n"
2461 "...\n",
2462 out);
2463 out.clear();
2464}
Mehdi Amini3ab3fef2016-11-28 21:38:52 +00002465
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +00002466LLVM_YAML_IS_STRING_MAP(int)
2467
2468TEST(YAMLIO, TestCustomMapping) {
2469 std::map<std::string, int> x;
2470 x["foo"] = 1;
2471 x["bar"] = 2;
2472
2473 std::string out;
2474 llvm::raw_string_ostream ostr(out);
2475 Output xout(ostr, nullptr, 0);
2476
2477 xout << x;
2478 ostr.flush();
2479 EXPECT_EQ("---\n"
2480 "bar: 2\n"
2481 "foo: 1\n"
2482 "...\n",
2483 out);
2484
2485 Input yin(out);
2486 std::map<std::string, int> y;
2487 yin >> y;
2488 EXPECT_EQ(2ul, y.size());
2489 EXPECT_EQ(1, y["foo"]);
2490 EXPECT_EQ(2, y["bar"]);
2491}
2492
2493LLVM_YAML_IS_STRING_MAP(FooBar)
2494
2495TEST(YAMLIO, TestCustomMappingStruct) {
2496 std::map<std::string, FooBar> x;
2497 x["foo"].foo = 1;
2498 x["foo"].bar = 2;
2499 x["bar"].foo = 3;
2500 x["bar"].bar = 4;
2501
2502 std::string out;
2503 llvm::raw_string_ostream ostr(out);
2504 Output xout(ostr, nullptr, 0);
2505
2506 xout << x;
2507 ostr.flush();
2508 EXPECT_EQ("---\n"
2509 "bar: \n"
2510 " foo: 3\n"
2511 " bar: 4\n"
2512 "foo: \n"
2513 " foo: 1\n"
2514 " bar: 2\n"
2515 "...\n",
2516 out);
2517
2518 Input yin(out);
2519 std::map<std::string, FooBar> y;
2520 yin >> y;
2521 EXPECT_EQ(2ul, y.size());
2522 EXPECT_EQ(1, y["foo"].foo);
2523 EXPECT_EQ(2, y["foo"].bar);
2524 EXPECT_EQ(3, y["bar"].foo);
2525 EXPECT_EQ(4, y["bar"].bar);
2526}
2527
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002528static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002529 std::string out;
2530 llvm::raw_string_ostream ostr(out);
2531 Output xout(ostr, nullptr, 0);
2532
2533 llvm::yaml::EmptyContext Ctx;
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002534 yamlize(xout, Input, true, Ctx);
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002535
2536 ostr.flush();
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002537
2538 // Make a separate StringRef so we get nice byte-by-byte output.
2539 llvm::StringRef Got(out);
2540 EXPECT_EQ(Expected, Got);
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002541}
2542
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002543TEST(YAMLIO, TestEscaped) {
2544 // Single quote
2545 TestEscaped("@abc@", "'@abc@'");
2546 // No quote
Zachary Turner9f169af2018-10-12 16:31:20 +00002547 TestEscaped("abc", "abc");
2548 // Forward slash quoted
2549 TestEscaped("abc/", "'abc/'");
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002550 // Double quote non-printable
2551 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2552 // Double quote inside single quote
2553 TestEscaped("abc\"fdf", "'abc\"fdf'");
2554 // Double quote inside double quote
2555 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2556 // Single quote inside single quote
2557 TestEscaped("abc'fdf", "'abc''fdf'");
2558 // UTF8
2559 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2560 // UTF8 with single quote inside double quote
2561 TestEscaped("parameter 'параметр' is unused",
2562 "\"parameter 'параметр' is unused\"");
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002563
2564 // String with embedded non-printable multibyte UTF-8 sequence (U+200B
2565 // zero-width space). The thing to test here is that we emit a
2566 // unicode-scalar level escape like \uNNNN (at the YAML level), and don't
2567 // just pass the UTF-8 byte sequence through as with quoted printables.
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002568 {
2569 const unsigned char foobar[10] = {'f', 'o', 'o',
2570 0xE2, 0x80, 0x8B, // UTF-8 of U+200B
2571 'b', 'a', 'r',
2572 0x0};
2573 TestEscaped((char const *)foobar, "\"foo\\u200Bbar\"");
2574 }
Francis Visoiu Mistrihb2b961a2017-12-21 17:14:09 +00002575}
Kirill Bobyrev5f26a642018-08-20 07:00:36 +00002576
2577TEST(YAMLIO, Numeric) {
2578 EXPECT_TRUE(isNumeric(".inf"));
2579 EXPECT_TRUE(isNumeric(".INF"));
2580 EXPECT_TRUE(isNumeric(".Inf"));
2581 EXPECT_TRUE(isNumeric("-.inf"));
2582 EXPECT_TRUE(isNumeric("+.inf"));
2583
2584 EXPECT_TRUE(isNumeric(".nan"));
2585 EXPECT_TRUE(isNumeric(".NaN"));
2586 EXPECT_TRUE(isNumeric(".NAN"));
2587
2588 EXPECT_TRUE(isNumeric("0"));
2589 EXPECT_TRUE(isNumeric("0."));
2590 EXPECT_TRUE(isNumeric("0.0"));
2591 EXPECT_TRUE(isNumeric("-0.0"));
2592 EXPECT_TRUE(isNumeric("+0.0"));
2593
2594 EXPECT_TRUE(isNumeric("12345"));
2595 EXPECT_TRUE(isNumeric("012345"));
2596 EXPECT_TRUE(isNumeric("+12.0"));
2597 EXPECT_TRUE(isNumeric(".5"));
2598 EXPECT_TRUE(isNumeric("+.5"));
2599 EXPECT_TRUE(isNumeric("-1.0"));
2600
2601 EXPECT_TRUE(isNumeric("2.3e4"));
2602 EXPECT_TRUE(isNumeric("-2E+05"));
2603 EXPECT_TRUE(isNumeric("+12e03"));
2604 EXPECT_TRUE(isNumeric("6.8523015e+5"));
2605
2606 EXPECT_TRUE(isNumeric("1.e+1"));
2607 EXPECT_TRUE(isNumeric(".0e+1"));
2608
2609 EXPECT_TRUE(isNumeric("0x2aF3"));
2610 EXPECT_TRUE(isNumeric("0o01234567"));
2611
2612 EXPECT_FALSE(isNumeric("not a number"));
2613 EXPECT_FALSE(isNumeric("."));
2614 EXPECT_FALSE(isNumeric(".e+1"));
2615 EXPECT_FALSE(isNumeric(".1e"));
2616 EXPECT_FALSE(isNumeric(".1e+"));
2617 EXPECT_FALSE(isNumeric(".1e++1"));
2618
2619 EXPECT_FALSE(isNumeric("ABCD"));
2620 EXPECT_FALSE(isNumeric("+0x2AF3"));
2621 EXPECT_FALSE(isNumeric("-0x2AF3"));
2622 EXPECT_FALSE(isNumeric("0x2AF3Z"));
2623 EXPECT_FALSE(isNumeric("0o012345678"));
2624 EXPECT_FALSE(isNumeric("0xZ"));
2625 EXPECT_FALSE(isNumeric("-0o012345678"));
2626 EXPECT_FALSE(isNumeric("000003A8229434B839616A25C16B0291F77A438B"));
2627
2628 EXPECT_FALSE(isNumeric(""));
2629 EXPECT_FALSE(isNumeric("."));
2630 EXPECT_FALSE(isNumeric(".e+1"));
2631 EXPECT_FALSE(isNumeric(".e+"));
2632 EXPECT_FALSE(isNumeric(".e"));
2633 EXPECT_FALSE(isNumeric("e1"));
2634
2635 // Deprecated formats: as for YAML 1.2 specification, the following are not
2636 // valid numbers anymore:
2637 //
2638 // * Sexagecimal numbers
2639 // * Decimal numbers with comma s the delimiter
2640 // * "inf", "nan" without '.' prefix
2641 EXPECT_FALSE(isNumeric("3:25:45"));
2642 EXPECT_FALSE(isNumeric("+12,345"));
2643 EXPECT_FALSE(isNumeric("-inf"));
2644 EXPECT_FALSE(isNumeric("1,230.15"));
2645}
Scott Linderc0830f52018-11-14 19:39:59 +00002646
2647//===----------------------------------------------------------------------===//
2648// Test PolymorphicTraits and TaggedScalarTraits
2649//===----------------------------------------------------------------------===//
2650
2651struct Poly {
2652 enum NodeKind {
2653 NK_Scalar,
2654 NK_Seq,
2655 NK_Map,
2656 } Kind;
2657
2658 Poly(NodeKind Kind) : Kind(Kind) {}
2659
2660 virtual ~Poly() = default;
2661
2662 NodeKind getKind() const { return Kind; }
2663};
2664
2665struct Scalar : Poly {
2666 enum ScalarKind {
2667 SK_Unknown,
2668 SK_Double,
2669 SK_Bool,
2670 } SKind;
2671
2672 union {
2673 double DoubleValue;
2674 bool BoolValue;
2675 };
2676
2677 Scalar() : Poly(NK_Scalar), SKind(SK_Unknown) {}
2678 Scalar(double DoubleValue)
2679 : Poly(NK_Scalar), SKind(SK_Double), DoubleValue(DoubleValue) {}
2680 Scalar(bool BoolValue)
2681 : Poly(NK_Scalar), SKind(SK_Bool), BoolValue(BoolValue) {}
2682
2683 static bool classof(const Poly *N) { return N->getKind() == NK_Scalar; }
2684};
2685
2686struct Seq : Poly, std::vector<std::unique_ptr<Poly>> {
2687 Seq() : Poly(NK_Seq) {}
2688
2689 static bool classof(const Poly *N) { return N->getKind() == NK_Seq; }
2690};
2691
2692struct Map : Poly, llvm::StringMap<std::unique_ptr<Poly>> {
2693 Map() : Poly(NK_Map) {}
2694
2695 static bool classof(const Poly *N) { return N->getKind() == NK_Map; }
2696};
2697
2698namespace llvm {
2699namespace yaml {
2700
2701template <> struct PolymorphicTraits<std::unique_ptr<Poly>> {
2702 static NodeKind getKind(const std::unique_ptr<Poly> &N) {
2703 if (isa<Scalar>(*N))
2704 return NodeKind::Scalar;
2705 if (isa<Seq>(*N))
2706 return NodeKind::Sequence;
2707 if (isa<Map>(*N))
2708 return NodeKind::Map;
2709 llvm_unreachable("unsupported node type");
2710 }
2711
2712 static Scalar &getAsScalar(std::unique_ptr<Poly> &N) {
2713 if (!N || !isa<Scalar>(*N))
2714 N = llvm::make_unique<Scalar>();
2715 return *cast<Scalar>(N.get());
2716 }
2717
2718 static Seq &getAsSequence(std::unique_ptr<Poly> &N) {
2719 if (!N || !isa<Seq>(*N))
2720 N = llvm::make_unique<Seq>();
2721 return *cast<Seq>(N.get());
2722 }
2723
2724 static Map &getAsMap(std::unique_ptr<Poly> &N) {
2725 if (!N || !isa<Map>(*N))
2726 N = llvm::make_unique<Map>();
2727 return *cast<Map>(N.get());
2728 }
2729};
2730
2731template <> struct TaggedScalarTraits<Scalar> {
2732 static void output(const Scalar &S, void *Ctxt, raw_ostream &ScalarOS,
2733 raw_ostream &TagOS) {
2734 switch (S.SKind) {
2735 case Scalar::SK_Unknown:
2736 report_fatal_error("output unknown scalar");
2737 break;
2738 case Scalar::SK_Double:
2739 TagOS << "!double";
2740 ScalarTraits<double>::output(S.DoubleValue, Ctxt, ScalarOS);
2741 break;
2742 case Scalar::SK_Bool:
2743 TagOS << "!bool";
2744 ScalarTraits<bool>::output(S.BoolValue, Ctxt, ScalarOS);
2745 break;
2746 }
2747 }
2748
2749 static StringRef input(StringRef ScalarStr, StringRef Tag, void *Ctxt,
2750 Scalar &S) {
2751 S.SKind = StringSwitch<Scalar::ScalarKind>(Tag)
2752 .Case("!double", Scalar::SK_Double)
2753 .Case("!bool", Scalar::SK_Bool)
2754 .Default(Scalar::SK_Unknown);
2755 switch (S.SKind) {
2756 case Scalar::SK_Unknown:
2757 return StringRef("unknown scalar tag");
2758 case Scalar::SK_Double:
2759 return ScalarTraits<double>::input(ScalarStr, Ctxt, S.DoubleValue);
2760 case Scalar::SK_Bool:
2761 return ScalarTraits<bool>::input(ScalarStr, Ctxt, S.BoolValue);
2762 }
2763 llvm_unreachable("unknown scalar kind");
2764 }
2765
2766 static QuotingType mustQuote(const Scalar &S, StringRef Str) {
2767 switch (S.SKind) {
2768 case Scalar::SK_Unknown:
2769 report_fatal_error("quote unknown scalar");
2770 case Scalar::SK_Double:
2771 return ScalarTraits<double>::mustQuote(Str);
2772 case Scalar::SK_Bool:
2773 return ScalarTraits<bool>::mustQuote(Str);
2774 }
2775 llvm_unreachable("unknown scalar kind");
2776 }
2777};
2778
2779template <> struct CustomMappingTraits<Map> {
2780 static void inputOne(IO &IO, StringRef Key, Map &M) {
2781 IO.mapRequired(Key.str().c_str(), M[Key]);
2782 }
2783
2784 static void output(IO &IO, Map &M) {
2785 for (auto &N : M)
2786 IO.mapRequired(N.getKey().str().c_str(), N.getValue());
2787 }
2788};
2789
2790template <> struct SequenceTraits<Seq> {
2791 static size_t size(IO &IO, Seq &A) { return A.size(); }
2792
2793 static std::unique_ptr<Poly> &element(IO &IO, Seq &A, size_t Index) {
2794 if (Index >= A.size())
2795 A.resize(Index + 1);
2796 return A[Index];
2797 }
2798};
2799
2800} // namespace yaml
2801} // namespace llvm
2802
2803TEST(YAMLIO, TestReadWritePolymorphicScalar) {
2804 std::string intermediate;
2805 std::unique_ptr<Poly> node = llvm::make_unique<Scalar>(true);
2806
2807 llvm::raw_string_ostream ostr(intermediate);
2808 Output yout(ostr);
2809#ifdef GTEST_HAS_DEATH_TEST
2810#ifndef NDEBUG
2811 EXPECT_DEATH(yout << node, "plain scalar documents are not supported");
2812#endif
2813#endif
2814}
2815
2816TEST(YAMLIO, TestReadWritePolymorphicSeq) {
2817 std::string intermediate;
2818 {
2819 auto seq = llvm::make_unique<Seq>();
2820 seq->push_back(llvm::make_unique<Scalar>(true));
2821 seq->push_back(llvm::make_unique<Scalar>(1.0));
2822 auto node = llvm::unique_dyn_cast<Poly>(seq);
2823
2824 llvm::raw_string_ostream ostr(intermediate);
2825 Output yout(ostr);
2826 yout << node;
2827 }
2828 {
2829 Input yin(intermediate);
2830 std::unique_ptr<Poly> node;
2831 yin >> node;
2832
2833 EXPECT_FALSE(yin.error());
2834 auto seq = llvm::dyn_cast<Seq>(node.get());
2835 ASSERT_TRUE(seq);
2836 ASSERT_EQ(seq->size(), 2u);
2837 auto first = llvm::dyn_cast<Scalar>((*seq)[0].get());
2838 ASSERT_TRUE(first);
2839 EXPECT_EQ(first->SKind, Scalar::SK_Bool);
2840 EXPECT_TRUE(first->BoolValue);
2841 auto second = llvm::dyn_cast<Scalar>((*seq)[1].get());
2842 ASSERT_TRUE(second);
2843 EXPECT_EQ(second->SKind, Scalar::SK_Double);
2844 EXPECT_EQ(second->DoubleValue, 1.0);
2845 }
2846}
2847
2848TEST(YAMLIO, TestReadWritePolymorphicMap) {
2849 std::string intermediate;
2850 {
2851 auto map = llvm::make_unique<Map>();
2852 (*map)["foo"] = llvm::make_unique<Scalar>(false);
2853 (*map)["bar"] = llvm::make_unique<Scalar>(2.0);
2854 std::unique_ptr<Poly> node = llvm::unique_dyn_cast<Poly>(map);
2855
2856 llvm::raw_string_ostream ostr(intermediate);
2857 Output yout(ostr);
2858 yout << node;
2859 }
2860 {
2861 Input yin(intermediate);
2862 std::unique_ptr<Poly> node;
2863 yin >> node;
2864
2865 EXPECT_FALSE(yin.error());
2866 auto map = llvm::dyn_cast<Map>(node.get());
2867 ASSERT_TRUE(map);
2868 auto foo = llvm::dyn_cast<Scalar>((*map)["foo"].get());
2869 ASSERT_TRUE(foo);
2870 EXPECT_EQ(foo->SKind, Scalar::SK_Bool);
2871 EXPECT_FALSE(foo->BoolValue);
2872 auto bar = llvm::dyn_cast<Scalar>((*map)["bar"].get());
2873 ASSERT_TRUE(bar);
2874 EXPECT_EQ(bar->SKind, Scalar::SK_Double);
2875 EXPECT_EQ(bar->DoubleValue, 2.0);
2876 }
2877}