blob: 4530482ec8090a83cd80014b02f03f02fea62bc8 [file] [log] [blame]
Nick Kledzikf60a9272012-12-12 20:46:15 +00001//===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +000010#include "llvm/ADT/StringRef.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000011#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
Zachary Turner4fbf61d2016-06-07 19:32:09 +000013#include "llvm/Support/Endian.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000014#include "llvm/Support/Format.h"
15#include "llvm/Support/YAMLTraits.h"
Ilya Biryukov54135102018-05-30 10:40:11 +000016#include "gmock/gmock.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000017#include "gtest/gtest.h"
18
Nick Kledzikf60a9272012-12-12 20:46:15 +000019using llvm::yaml::Hex16;
20using llvm::yaml::Hex32;
21using llvm::yaml::Hex64;
Kirill Bobyrev5f26a642018-08-20 07:00:36 +000022using llvm::yaml::Hex8;
23using llvm::yaml::Input;
24using llvm::yaml::IO;
25using llvm::yaml::isNumeric;
26using llvm::yaml::MappingNormalization;
27using llvm::yaml::MappingTraits;
28using llvm::yaml::Output;
29using llvm::yaml::ScalarTraits;
Ilya Biryukov54135102018-05-30 10:40:11 +000030using ::testing::StartsWith;
Nick Kledzikf60a9272012-12-12 20:46:15 +000031
32
Nick Kledzik7cd45f22013-11-21 00:28:07 +000033
34
35static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
36}
37
38
39
Nick Kledzikf60a9272012-12-12 20:46:15 +000040//===----------------------------------------------------------------------===//
41// Test MappingTraits
42//===----------------------------------------------------------------------===//
43
44struct FooBar {
45 int foo;
46 int bar;
47};
48typedef std::vector<FooBar> FooBarSequence;
49
50LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
51
Justin Bogner64d2cdf2015-03-02 17:26:43 +000052struct FooBarContainer {
53 FooBarSequence fbs;
54};
Nick Kledzikf60a9272012-12-12 20:46:15 +000055
56namespace llvm {
57namespace yaml {
58 template <>
59 struct MappingTraits<FooBar> {
60 static void mapping(IO &io, FooBar& fb) {
61 io.mapRequired("foo", fb.foo);
62 io.mapRequired("bar", fb.bar);
63 }
64 };
Justin Bogner64d2cdf2015-03-02 17:26:43 +000065
66 template <> struct MappingTraits<FooBarContainer> {
67 static void mapping(IO &io, FooBarContainer &fb) {
68 io.mapRequired("fbs", fb.fbs);
69 }
70 };
Nick Kledzikf60a9272012-12-12 20:46:15 +000071}
72}
73
74
75//
76// Test the reading of a yaml mapping
77//
78TEST(YAMLIO, TestMapRead) {
79 FooBar doc;
Alexander Kornienko681e37c2013-11-18 15:50:04 +000080 {
81 Input yin("---\nfoo: 3\nbar: 5\n...\n");
82 yin >> doc;
Nick Kledzikf60a9272012-12-12 20:46:15 +000083
Alexander Kornienko681e37c2013-11-18 15:50:04 +000084 EXPECT_FALSE(yin.error());
85 EXPECT_EQ(doc.foo, 3);
86 EXPECT_EQ(doc.bar, 5);
87 }
88
89 {
90 Input yin("{foo: 3, bar: 5}");
91 yin >> doc;
92
93 EXPECT_FALSE(yin.error());
94 EXPECT_EQ(doc.foo, 3);
95 EXPECT_EQ(doc.bar, 5);
96 }
Nick Kledzikf60a9272012-12-12 20:46:15 +000097}
98
Rafael Espindolaa97373f2014-08-08 13:58:00 +000099TEST(YAMLIO, TestMalformedMapRead) {
100 FooBar doc;
101 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
102 yin >> doc;
103 EXPECT_TRUE(!!yin.error());
104}
105
Nick Kledzikf60a9272012-12-12 20:46:15 +0000106//
107// Test the reading of a yaml sequence of mappings
108//
109TEST(YAMLIO, TestSequenceMapRead) {
110 FooBarSequence seq;
111 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
112 yin >> seq;
113
114 EXPECT_FALSE(yin.error());
115 EXPECT_EQ(seq.size(), 2UL);
116 FooBar& map1 = seq[0];
117 FooBar& map2 = seq[1];
118 EXPECT_EQ(map1.foo, 3);
119 EXPECT_EQ(map1.bar, 5);
120 EXPECT_EQ(map2.foo, 7);
121 EXPECT_EQ(map2.bar, 9);
122}
123
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000124//
125// Test the reading of a map containing a yaml sequence of mappings
126//
127TEST(YAMLIO, TestContainerSequenceMapRead) {
128 {
129 FooBarContainer cont;
130 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
131 yin2 >> cont;
132
133 EXPECT_FALSE(yin2.error());
134 EXPECT_EQ(cont.fbs.size(), 2UL);
135 EXPECT_EQ(cont.fbs[0].foo, 3);
136 EXPECT_EQ(cont.fbs[0].bar, 5);
137 EXPECT_EQ(cont.fbs[1].foo, 7);
138 EXPECT_EQ(cont.fbs[1].bar, 9);
139 }
140
141 {
142 FooBarContainer cont;
143 Input yin("---\nfbs:\n...\n");
144 yin >> cont;
145 // Okay: Empty node represents an empty array.
146 EXPECT_FALSE(yin.error());
147 EXPECT_EQ(cont.fbs.size(), 0UL);
148 }
149
150 {
151 FooBarContainer cont;
152 Input yin("---\nfbs: !!null null\n...\n");
153 yin >> cont;
154 // Okay: null represents an empty array.
155 EXPECT_FALSE(yin.error());
156 EXPECT_EQ(cont.fbs.size(), 0UL);
157 }
158
159 {
160 FooBarContainer cont;
161 Input yin("---\nfbs: ~\n...\n");
162 yin >> cont;
163 // Okay: null represents an empty array.
164 EXPECT_FALSE(yin.error());
165 EXPECT_EQ(cont.fbs.size(), 0UL);
166 }
167
168 {
169 FooBarContainer cont;
170 Input yin("---\nfbs: null\n...\n");
171 yin >> cont;
172 // Okay: null represents an empty array.
173 EXPECT_FALSE(yin.error());
174 EXPECT_EQ(cont.fbs.size(), 0UL);
175 }
176}
177
178//
179// Test the reading of a map containing a malformed yaml sequence
180//
181TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
182 {
183 FooBarContainer cont;
184 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
185 suppressErrorMessages);
186 yin >> cont;
187 // Error: fbs is not a sequence.
188 EXPECT_TRUE(!!yin.error());
189 EXPECT_EQ(cont.fbs.size(), 0UL);
190 }
191
192 {
193 FooBarContainer cont;
194 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
195 yin >> cont;
196 // This should be an error.
197 EXPECT_TRUE(!!yin.error());
198 EXPECT_EQ(cont.fbs.size(), 0UL);
199 }
200}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000201
202//
203// Test writing then reading back a sequence of mappings
204//
205TEST(YAMLIO, TestSequenceMapWriteAndRead) {
206 std::string intermediate;
207 {
208 FooBar entry1;
209 entry1.foo = 10;
210 entry1.bar = -3;
211 FooBar entry2;
212 entry2.foo = 257;
213 entry2.bar = 0;
214 FooBarSequence seq;
215 seq.push_back(entry1);
216 seq.push_back(entry2);
217
218 llvm::raw_string_ostream ostr(intermediate);
219 Output yout(ostr);
220 yout << seq;
221 }
222
223 {
224 Input yin(intermediate);
225 FooBarSequence seq2;
226 yin >> seq2;
227
228 EXPECT_FALSE(yin.error());
229 EXPECT_EQ(seq2.size(), 2UL);
230 FooBar& map1 = seq2[0];
231 FooBar& map2 = seq2[1];
232 EXPECT_EQ(map1.foo, 10);
233 EXPECT_EQ(map1.bar, -3);
234 EXPECT_EQ(map2.foo, 257);
235 EXPECT_EQ(map2.bar, 0);
236 }
237}
238
Alex Bradbury16843172017-07-17 11:41:30 +0000239//
240// Test YAML filename handling.
241//
242static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) {
243 EXPECT_EQ(Error.getFilename(), "foo.yaml");
244}
245
246TEST(YAMLIO, TestGivenFilename) {
247 auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
248 Input yin(*Buffer, nullptr, testErrorFilename);
249 FooBar Value;
250 yin >> Value;
251
252 EXPECT_TRUE(!!yin.error());
253}
254
Ilya Biryukov54135102018-05-30 10:40:11 +0000255struct WithStringField {
256 std::string str1;
257 std::string str2;
258 std::string str3;
259};
260
261namespace llvm {
262namespace yaml {
263template <> struct MappingTraits<WithStringField> {
264 static void mapping(IO &io, WithStringField &fb) {
265 io.mapRequired("str1", fb.str1);
266 io.mapRequired("str2", fb.str2);
267 io.mapRequired("str3", fb.str3);
268 }
269};
270} // namespace yaml
271} // namespace llvm
272
273TEST(YAMLIO, MultilineStrings) {
274 WithStringField Original;
275 Original.str1 = "a multiline string\nfoobarbaz";
276 Original.str2 = "another one\rfoobarbaz";
277 Original.str3 = "a one-line string";
278
279 std::string Serialized;
280 {
281 llvm::raw_string_ostream OS(Serialized);
282 Output YOut(OS);
283 YOut << Original;
284 }
285 auto Expected = "---\n"
286 "str1: 'a multiline string\n"
287 "foobarbaz'\n"
288 "str2: 'another one\r"
289 "foobarbaz'\n"
290 "str3: a one-line string\n"
291 "...\n";
292 ASSERT_EQ(Serialized, Expected);
293
294 // Also check it parses back without the errors.
295 WithStringField Deserialized;
296 {
297 Input YIn(Serialized);
298 YIn >> Deserialized;
299 ASSERT_FALSE(YIn.error())
300 << "Parsing error occurred during deserialization. Serialized string:\n"
301 << Serialized;
302 }
303 EXPECT_EQ(Original.str1, Deserialized.str1);
304 EXPECT_EQ(Original.str2, Deserialized.str2);
305 EXPECT_EQ(Original.str3, Deserialized.str3);
306}
307
308TEST(YAMLIO, NoQuotesForTab) {
309 WithStringField WithTab;
310 WithTab.str1 = "aba\tcaba";
311 std::string Serialized;
312 {
313 llvm::raw_string_ostream OS(Serialized);
314 Output YOut(OS);
315 YOut << WithTab;
316 }
317 auto ExpectedPrefix = "---\n"
318 "str1: aba\tcaba\n";
319 EXPECT_THAT(Serialized, StartsWith(ExpectedPrefix));
320}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000321
322//===----------------------------------------------------------------------===//
323// Test built-in types
324//===----------------------------------------------------------------------===//
325
326struct BuiltInTypes {
327 llvm::StringRef str;
John Thompson48e018a2013-11-19 17:28:21 +0000328 std::string stdstr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000329 uint64_t u64;
330 uint32_t u32;
331 uint16_t u16;
332 uint8_t u8;
333 bool b;
334 int64_t s64;
335 int32_t s32;
336 int16_t s16;
337 int8_t s8;
338 float f;
339 double d;
340 Hex8 h8;
341 Hex16 h16;
342 Hex32 h32;
343 Hex64 h64;
344};
345
346namespace llvm {
347namespace yaml {
348 template <>
349 struct MappingTraits<BuiltInTypes> {
350 static void mapping(IO &io, BuiltInTypes& bt) {
351 io.mapRequired("str", bt.str);
John Thompson48e018a2013-11-19 17:28:21 +0000352 io.mapRequired("stdstr", bt.stdstr);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000353 io.mapRequired("u64", bt.u64);
354 io.mapRequired("u32", bt.u32);
355 io.mapRequired("u16", bt.u16);
356 io.mapRequired("u8", bt.u8);
357 io.mapRequired("b", bt.b);
358 io.mapRequired("s64", bt.s64);
359 io.mapRequired("s32", bt.s32);
360 io.mapRequired("s16", bt.s16);
361 io.mapRequired("s8", bt.s8);
362 io.mapRequired("f", bt.f);
363 io.mapRequired("d", bt.d);
364 io.mapRequired("h8", bt.h8);
365 io.mapRequired("h16", bt.h16);
366 io.mapRequired("h32", bt.h32);
367 io.mapRequired("h64", bt.h64);
368 }
369 };
370}
371}
372
373
374//
375// Test the reading of all built-in scalar conversions
376//
377TEST(YAMLIO, TestReadBuiltInTypes) {
378 BuiltInTypes map;
379 Input yin("---\n"
380 "str: hello there\n"
John Thompson48e018a2013-11-19 17:28:21 +0000381 "stdstr: hello where?\n"
Nick Kledzikf60a9272012-12-12 20:46:15 +0000382 "u64: 5000000000\n"
383 "u32: 4000000000\n"
384 "u16: 65000\n"
385 "u8: 255\n"
386 "b: false\n"
387 "s64: -5000000000\n"
388 "s32: -2000000000\n"
389 "s16: -32000\n"
390 "s8: -127\n"
391 "f: 137.125\n"
392 "d: -2.8625\n"
393 "h8: 0xFF\n"
394 "h16: 0x8765\n"
395 "h32: 0xFEDCBA98\n"
396 "h64: 0xFEDCBA9876543210\n"
397 "...\n");
398 yin >> map;
399
400 EXPECT_FALSE(yin.error());
401 EXPECT_TRUE(map.str.equals("hello there"));
John Thompson48e018a2013-11-19 17:28:21 +0000402 EXPECT_TRUE(map.stdstr == "hello where?");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000403 EXPECT_EQ(map.u64, 5000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000404 EXPECT_EQ(map.u32, 4000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000405 EXPECT_EQ(map.u16, 65000);
406 EXPECT_EQ(map.u8, 255);
407 EXPECT_EQ(map.b, false);
408 EXPECT_EQ(map.s64, -5000000000LL);
409 EXPECT_EQ(map.s32, -2000000000L);
410 EXPECT_EQ(map.s16, -32000);
411 EXPECT_EQ(map.s8, -127);
412 EXPECT_EQ(map.f, 137.125);
413 EXPECT_EQ(map.d, -2.8625);
414 EXPECT_EQ(map.h8, Hex8(255));
415 EXPECT_EQ(map.h16, Hex16(0x8765));
416 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
417 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
418}
419
420
421//
422// Test writing then reading back all built-in scalar types
423//
424TEST(YAMLIO, TestReadWriteBuiltInTypes) {
425 std::string intermediate;
426 {
427 BuiltInTypes map;
428 map.str = "one two";
John Thompson48e018a2013-11-19 17:28:21 +0000429 map.stdstr = "three four";
Nick Kledzikbed953d2012-12-17 22:11:17 +0000430 map.u64 = 6000000000ULL;
431 map.u32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000432 map.u16 = 50000;
433 map.u8 = 254;
434 map.b = true;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000435 map.s64 = -6000000000LL;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000436 map.s32 = -2000000000;
437 map.s16 = -32000;
438 map.s8 = -128;
439 map.f = 3.25;
440 map.d = -2.8625;
441 map.h8 = 254;
442 map.h16 = 50000;
Nick Kledzikbed953d2012-12-17 22:11:17 +0000443 map.h32 = 3000000000U;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000444 map.h64 = 6000000000LL;
445
446 llvm::raw_string_ostream ostr(intermediate);
447 Output yout(ostr);
448 yout << map;
449 }
450
451 {
452 Input yin(intermediate);
453 BuiltInTypes map;
454 yin >> map;
455
456 EXPECT_FALSE(yin.error());
457 EXPECT_TRUE(map.str.equals("one two"));
John Thompson48e018a2013-11-19 17:28:21 +0000458 EXPECT_TRUE(map.stdstr == "three four");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000459 EXPECT_EQ(map.u64, 6000000000ULL);
Nick Kledzikbed953d2012-12-17 22:11:17 +0000460 EXPECT_EQ(map.u32, 3000000000U);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000461 EXPECT_EQ(map.u16, 50000);
462 EXPECT_EQ(map.u8, 254);
463 EXPECT_EQ(map.b, true);
464 EXPECT_EQ(map.s64, -6000000000LL);
465 EXPECT_EQ(map.s32, -2000000000L);
466 EXPECT_EQ(map.s16, -32000);
467 EXPECT_EQ(map.s8, -128);
468 EXPECT_EQ(map.f, 3.25);
469 EXPECT_EQ(map.d, -2.8625);
470 EXPECT_EQ(map.h8, Hex8(254));
471 EXPECT_EQ(map.h16, Hex16(50000));
Nick Kledzikbed953d2012-12-17 22:11:17 +0000472 EXPECT_EQ(map.h32, Hex32(3000000000U));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000473 EXPECT_EQ(map.h64, Hex64(6000000000LL));
474 }
475}
476
Zachary Turner4fbf61d2016-06-07 19:32:09 +0000477//===----------------------------------------------------------------------===//
478// Test endian-aware types
479//===----------------------------------------------------------------------===//
480
481struct EndianTypes {
482 typedef llvm::support::detail::packed_endian_specific_integral<
483 float, llvm::support::little, llvm::support::unaligned>
484 ulittle_float;
485 typedef llvm::support::detail::packed_endian_specific_integral<
486 double, llvm::support::little, llvm::support::unaligned>
487 ulittle_double;
488
489 llvm::support::ulittle64_t u64;
490 llvm::support::ulittle32_t u32;
491 llvm::support::ulittle16_t u16;
492 llvm::support::little64_t s64;
493 llvm::support::little32_t s32;
494 llvm::support::little16_t s16;
495 ulittle_float f;
496 ulittle_double d;
497};
498
499namespace llvm {
500namespace yaml {
501template <> struct MappingTraits<EndianTypes> {
502 static void mapping(IO &io, EndianTypes &et) {
503 io.mapRequired("u64", et.u64);
504 io.mapRequired("u32", et.u32);
505 io.mapRequired("u16", et.u16);
506 io.mapRequired("s64", et.s64);
507 io.mapRequired("s32", et.s32);
508 io.mapRequired("s16", et.s16);
509 io.mapRequired("f", et.f);
510 io.mapRequired("d", et.d);
511 }
512};
513}
514}
515
516//
517// Test the reading of all endian scalar conversions
518//
519TEST(YAMLIO, TestReadEndianTypes) {
520 EndianTypes map;
521 Input yin("---\n"
522 "u64: 5000000000\n"
523 "u32: 4000000000\n"
524 "u16: 65000\n"
525 "s64: -5000000000\n"
526 "s32: -2000000000\n"
527 "s16: -32000\n"
528 "f: 3.25\n"
529 "d: -2.8625\n"
530 "...\n");
531 yin >> map;
532
533 EXPECT_FALSE(yin.error());
534 EXPECT_EQ(map.u64, 5000000000ULL);
535 EXPECT_EQ(map.u32, 4000000000U);
536 EXPECT_EQ(map.u16, 65000);
537 EXPECT_EQ(map.s64, -5000000000LL);
538 EXPECT_EQ(map.s32, -2000000000L);
539 EXPECT_EQ(map.s16, -32000);
540 EXPECT_EQ(map.f, 3.25f);
541 EXPECT_EQ(map.d, -2.8625);
542}
543
544//
545// Test writing then reading back all endian-aware scalar types
546//
547TEST(YAMLIO, TestReadWriteEndianTypes) {
548 std::string intermediate;
549 {
550 EndianTypes map;
551 map.u64 = 6000000000ULL;
552 map.u32 = 3000000000U;
553 map.u16 = 50000;
554 map.s64 = -6000000000LL;
555 map.s32 = -2000000000;
556 map.s16 = -32000;
557 map.f = 3.25f;
558 map.d = -2.8625;
559
560 llvm::raw_string_ostream ostr(intermediate);
561 Output yout(ostr);
562 yout << map;
563 }
564
565 {
566 Input yin(intermediate);
567 EndianTypes map;
568 yin >> map;
569
570 EXPECT_FALSE(yin.error());
571 EXPECT_EQ(map.u64, 6000000000ULL);
572 EXPECT_EQ(map.u32, 3000000000U);
573 EXPECT_EQ(map.u16, 50000);
574 EXPECT_EQ(map.s64, -6000000000LL);
575 EXPECT_EQ(map.s32, -2000000000L);
576 EXPECT_EQ(map.s16, -32000);
577 EXPECT_EQ(map.f, 3.25f);
578 EXPECT_EQ(map.d, -2.8625);
579 }
580}
581
Rui Ueyama106eded2013-09-11 04:00:08 +0000582struct StringTypes {
583 llvm::StringRef str1;
584 llvm::StringRef str2;
585 llvm::StringRef str3;
586 llvm::StringRef str4;
587 llvm::StringRef str5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000588 llvm::StringRef str6;
David Majnemer77880332014-04-10 07:37:33 +0000589 llvm::StringRef str7;
590 llvm::StringRef str8;
591 llvm::StringRef str9;
592 llvm::StringRef str10;
593 llvm::StringRef str11;
John Thompson48e018a2013-11-19 17:28:21 +0000594 std::string stdstr1;
595 std::string stdstr2;
596 std::string stdstr3;
597 std::string stdstr4;
598 std::string stdstr5;
David Majnemer97d8ee32014-04-09 17:04:27 +0000599 std::string stdstr6;
David Majnemer77880332014-04-10 07:37:33 +0000600 std::string stdstr7;
601 std::string stdstr8;
602 std::string stdstr9;
603 std::string stdstr10;
604 std::string stdstr11;
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000605 std::string stdstr12;
Rui Ueyama106eded2013-09-11 04:00:08 +0000606};
Nick Kledzikf60a9272012-12-12 20:46:15 +0000607
Rui Ueyama106eded2013-09-11 04:00:08 +0000608namespace llvm {
609namespace yaml {
610 template <>
611 struct MappingTraits<StringTypes> {
612 static void mapping(IO &io, StringTypes& st) {
613 io.mapRequired("str1", st.str1);
614 io.mapRequired("str2", st.str2);
615 io.mapRequired("str3", st.str3);
616 io.mapRequired("str4", st.str4);
617 io.mapRequired("str5", st.str5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000618 io.mapRequired("str6", st.str6);
David Majnemer77880332014-04-10 07:37:33 +0000619 io.mapRequired("str7", st.str7);
620 io.mapRequired("str8", st.str8);
621 io.mapRequired("str9", st.str9);
622 io.mapRequired("str10", st.str10);
623 io.mapRequired("str11", st.str11);
John Thompson48e018a2013-11-19 17:28:21 +0000624 io.mapRequired("stdstr1", st.stdstr1);
625 io.mapRequired("stdstr2", st.stdstr2);
626 io.mapRequired("stdstr3", st.stdstr3);
627 io.mapRequired("stdstr4", st.stdstr4);
628 io.mapRequired("stdstr5", st.stdstr5);
David Majnemer97d8ee32014-04-09 17:04:27 +0000629 io.mapRequired("stdstr6", st.stdstr6);
David Majnemer77880332014-04-10 07:37:33 +0000630 io.mapRequired("stdstr7", st.stdstr7);
631 io.mapRequired("stdstr8", st.stdstr8);
632 io.mapRequired("stdstr9", st.stdstr9);
633 io.mapRequired("stdstr10", st.stdstr10);
634 io.mapRequired("stdstr11", st.stdstr11);
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000635 io.mapRequired("stdstr12", st.stdstr12);
Rui Ueyama106eded2013-09-11 04:00:08 +0000636 }
637 };
638}
639}
640
641TEST(YAMLIO, TestReadWriteStringTypes) {
642 std::string intermediate;
643 {
644 StringTypes map;
645 map.str1 = "'aaa";
646 map.str2 = "\"bbb";
647 map.str3 = "`ccc";
648 map.str4 = "@ddd";
649 map.str5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000650 map.str6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000651 map.str7 = "true";
652 map.str8 = "FALSE";
653 map.str9 = "~";
654 map.str10 = "0.2e20";
655 map.str11 = "0x30";
John Thompson48e018a2013-11-19 17:28:21 +0000656 map.stdstr1 = "'eee";
657 map.stdstr2 = "\"fff";
658 map.stdstr3 = "`ggg";
659 map.stdstr4 = "@hhh";
660 map.stdstr5 = "";
David Majnemer97d8ee32014-04-09 17:04:27 +0000661 map.stdstr6 = "0000000004000000";
David Majnemer77880332014-04-10 07:37:33 +0000662 map.stdstr7 = "true";
663 map.stdstr8 = "FALSE";
664 map.stdstr9 = "~";
665 map.stdstr10 = "0.2e20";
666 map.stdstr11 = "0x30";
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000667 map.stdstr12 = "- match";
Rui Ueyama106eded2013-09-11 04:00:08 +0000668
669 llvm::raw_string_ostream ostr(intermediate);
670 Output yout(ostr);
671 yout << map;
672 }
673
674 llvm::StringRef flowOut(intermediate);
675 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
676 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
677 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
678 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
679 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000680 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
David Majnemer77880332014-04-10 07:37:33 +0000681 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
682 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
683 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
684 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
685 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
Haojian Wu0bbe66e2018-01-22 10:20:48 +0000686 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'- match'\n"));
John Thompson48e018a2013-11-19 17:28:21 +0000687 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
688 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
689 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
690 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
691 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
David Majnemer97d8ee32014-04-09 17:04:27 +0000692 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
Rui Ueyama106eded2013-09-11 04:00:08 +0000693
694 {
695 Input yin(intermediate);
696 StringTypes map;
697 yin >> map;
698
699 EXPECT_FALSE(yin.error());
700 EXPECT_TRUE(map.str1.equals("'aaa"));
701 EXPECT_TRUE(map.str2.equals("\"bbb"));
702 EXPECT_TRUE(map.str3.equals("`ccc"));
703 EXPECT_TRUE(map.str4.equals("@ddd"));
704 EXPECT_TRUE(map.str5.equals(""));
David Majnemer97d8ee32014-04-09 17:04:27 +0000705 EXPECT_TRUE(map.str6.equals("0000000004000000"));
John Thompson48e018a2013-11-19 17:28:21 +0000706 EXPECT_TRUE(map.stdstr1 == "'eee");
707 EXPECT_TRUE(map.stdstr2 == "\"fff");
708 EXPECT_TRUE(map.stdstr3 == "`ggg");
709 EXPECT_TRUE(map.stdstr4 == "@hhh");
710 EXPECT_TRUE(map.stdstr5 == "");
David Majnemer97d8ee32014-04-09 17:04:27 +0000711 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
Rui Ueyama106eded2013-09-11 04:00:08 +0000712 }
713}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000714
715//===----------------------------------------------------------------------===//
716// Test ScalarEnumerationTraits
717//===----------------------------------------------------------------------===//
718
719enum Colors {
720 cRed,
721 cBlue,
722 cGreen,
723 cYellow
724};
725
726struct ColorMap {
727 Colors c1;
728 Colors c2;
729 Colors c3;
730 Colors c4;
731 Colors c5;
732 Colors c6;
733};
734
735namespace llvm {
736namespace yaml {
737 template <>
738 struct ScalarEnumerationTraits<Colors> {
739 static void enumeration(IO &io, Colors &value) {
740 io.enumCase(value, "red", cRed);
741 io.enumCase(value, "blue", cBlue);
742 io.enumCase(value, "green", cGreen);
743 io.enumCase(value, "yellow",cYellow);
744 }
745 };
746 template <>
747 struct MappingTraits<ColorMap> {
748 static void mapping(IO &io, ColorMap& c) {
749 io.mapRequired("c1", c.c1);
750 io.mapRequired("c2", c.c2);
751 io.mapRequired("c3", c.c3);
752 io.mapOptional("c4", c.c4, cBlue); // supplies default
753 io.mapOptional("c5", c.c5, cYellow); // supplies default
754 io.mapOptional("c6", c.c6, cRed); // supplies default
755 }
756 };
757}
758}
759
760
761//
762// Test reading enumerated scalars
763//
764TEST(YAMLIO, TestEnumRead) {
765 ColorMap map;
766 Input yin("---\n"
767 "c1: blue\n"
768 "c2: red\n"
769 "c3: green\n"
770 "c5: yellow\n"
771 "...\n");
772 yin >> map;
773
774 EXPECT_FALSE(yin.error());
775 EXPECT_EQ(cBlue, map.c1);
776 EXPECT_EQ(cRed, map.c2);
777 EXPECT_EQ(cGreen, map.c3);
778 EXPECT_EQ(cBlue, map.c4); // tests default
779 EXPECT_EQ(cYellow,map.c5); // tests overridden
780 EXPECT_EQ(cRed, map.c6); // tests default
781}
782
783
784
785//===----------------------------------------------------------------------===//
786// Test ScalarBitSetTraits
787//===----------------------------------------------------------------------===//
788
789enum MyFlags {
790 flagNone = 0,
791 flagBig = 1 << 0,
792 flagFlat = 1 << 1,
793 flagRound = 1 << 2,
794 flagPointy = 1 << 3
795};
796inline MyFlags operator|(MyFlags a, MyFlags b) {
797 return static_cast<MyFlags>(
798 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
799}
800
801struct FlagsMap {
802 MyFlags f1;
803 MyFlags f2;
804 MyFlags f3;
805 MyFlags f4;
806};
807
808
809namespace llvm {
810namespace yaml {
811 template <>
812 struct ScalarBitSetTraits<MyFlags> {
813 static void bitset(IO &io, MyFlags &value) {
814 io.bitSetCase(value, "big", flagBig);
815 io.bitSetCase(value, "flat", flagFlat);
816 io.bitSetCase(value, "round", flagRound);
817 io.bitSetCase(value, "pointy",flagPointy);
818 }
819 };
820 template <>
821 struct MappingTraits<FlagsMap> {
822 static void mapping(IO &io, FlagsMap& c) {
823 io.mapRequired("f1", c.f1);
824 io.mapRequired("f2", c.f2);
825 io.mapRequired("f3", c.f3);
826 io.mapOptional("f4", c.f4, MyFlags(flagRound));
827 }
828 };
829}
830}
831
832
833//
834// Test reading flow sequence representing bit-mask values
835//
836TEST(YAMLIO, TestFlagsRead) {
837 FlagsMap map;
838 Input yin("---\n"
839 "f1: [ big ]\n"
840 "f2: [ round, flat ]\n"
841 "f3: []\n"
842 "...\n");
843 yin >> map;
844
845 EXPECT_FALSE(yin.error());
846 EXPECT_EQ(flagBig, map.f1);
847 EXPECT_EQ(flagRound|flagFlat, map.f2);
848 EXPECT_EQ(flagNone, map.f3); // check empty set
849 EXPECT_EQ(flagRound, map.f4); // check optional key
850}
851
852
853//
854// Test writing then reading back bit-mask values
855//
856TEST(YAMLIO, TestReadWriteFlags) {
857 std::string intermediate;
858 {
859 FlagsMap map;
860 map.f1 = flagBig;
861 map.f2 = flagRound | flagFlat;
862 map.f3 = flagNone;
863 map.f4 = flagNone;
864
865 llvm::raw_string_ostream ostr(intermediate);
866 Output yout(ostr);
867 yout << map;
868 }
869
870 {
871 Input yin(intermediate);
872 FlagsMap map2;
873 yin >> map2;
874
875 EXPECT_FALSE(yin.error());
876 EXPECT_EQ(flagBig, map2.f1);
877 EXPECT_EQ(flagRound|flagFlat, map2.f2);
878 EXPECT_EQ(flagNone, map2.f3);
879 //EXPECT_EQ(flagRound, map2.f4); // check optional key
880 }
881}
882
883
884
885//===----------------------------------------------------------------------===//
886// Test ScalarTraits
887//===----------------------------------------------------------------------===//
888
889struct MyCustomType {
890 int length;
891 int width;
892};
893
894struct MyCustomTypeMap {
895 MyCustomType f1;
896 MyCustomType f2;
897 int f3;
898};
899
900
901namespace llvm {
902namespace yaml {
903 template <>
904 struct MappingTraits<MyCustomTypeMap> {
905 static void mapping(IO &io, MyCustomTypeMap& s) {
906 io.mapRequired("f1", s.f1);
907 io.mapRequired("f2", s.f2);
908 io.mapRequired("f3", s.f3);
909 }
910 };
911 // MyCustomType is formatted as a yaml scalar. A value of
912 // {length=3, width=4} would be represented in yaml as "3 by 4".
913 template<>
914 struct ScalarTraits<MyCustomType> {
915 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
916 out << llvm::format("%d by %d", value.length, value.width);
917 }
918 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
919 size_t byStart = scalar.find("by");
920 if ( byStart != StringRef::npos ) {
921 StringRef lenStr = scalar.slice(0, byStart);
922 lenStr = lenStr.rtrim();
923 if ( lenStr.getAsInteger(0, value.length) ) {
924 return "malformed length";
925 }
926 StringRef widthStr = scalar.drop_front(byStart+2);
927 widthStr = widthStr.ltrim();
928 if ( widthStr.getAsInteger(0, value.width) ) {
929 return "malformed width";
930 }
931 return StringRef();
932 }
933 else {
934 return "malformed by";
935 }
936 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000937 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000938 };
939}
940}
941
942
943//
944// Test writing then reading back custom values
945//
946TEST(YAMLIO, TestReadWriteMyCustomType) {
947 std::string intermediate;
948 {
949 MyCustomTypeMap map;
950 map.f1.length = 1;
951 map.f1.width = 4;
952 map.f2.length = 100;
953 map.f2.width = 400;
954 map.f3 = 10;
955
956 llvm::raw_string_ostream ostr(intermediate);
957 Output yout(ostr);
958 yout << map;
959 }
960
961 {
962 Input yin(intermediate);
963 MyCustomTypeMap map2;
964 yin >> map2;
965
966 EXPECT_FALSE(yin.error());
967 EXPECT_EQ(1, map2.f1.length);
968 EXPECT_EQ(4, map2.f1.width);
969 EXPECT_EQ(100, map2.f2.length);
970 EXPECT_EQ(400, map2.f2.width);
971 EXPECT_EQ(10, map2.f3);
972 }
973}
974
975
976//===----------------------------------------------------------------------===//
Alex Lorenz68e787b2015-05-14 23:08:22 +0000977// Test BlockScalarTraits
978//===----------------------------------------------------------------------===//
979
980struct MultilineStringType {
981 std::string str;
982};
983
984struct MultilineStringTypeMap {
985 MultilineStringType name;
986 MultilineStringType description;
987 MultilineStringType ingredients;
988 MultilineStringType recipes;
989 MultilineStringType warningLabels;
990 MultilineStringType documentation;
991 int price;
992};
993
994namespace llvm {
995namespace yaml {
996 template <>
997 struct MappingTraits<MultilineStringTypeMap> {
998 static void mapping(IO &io, MultilineStringTypeMap& s) {
999 io.mapRequired("name", s.name);
1000 io.mapRequired("description", s.description);
1001 io.mapRequired("ingredients", s.ingredients);
1002 io.mapRequired("recipes", s.recipes);
1003 io.mapRequired("warningLabels", s.warningLabels);
1004 io.mapRequired("documentation", s.documentation);
1005 io.mapRequired("price", s.price);
1006 }
1007 };
1008
1009 // MultilineStringType is formatted as a yaml block literal scalar. A value of
1010 // "Hello\nWorld" would be represented in yaml as
1011 // |
1012 // Hello
1013 // World
1014 template <>
1015 struct BlockScalarTraits<MultilineStringType> {
1016 static void output(const MultilineStringType &value, void *ctxt,
1017 llvm::raw_ostream &out) {
1018 out << value.str;
1019 }
1020 static StringRef input(StringRef scalar, void *ctxt,
1021 MultilineStringType &value) {
1022 value.str = scalar.str();
1023 return StringRef();
1024 }
1025 };
1026}
1027}
1028
1029LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
1030
1031//
1032// Test writing then reading back custom values
1033//
1034TEST(YAMLIO, TestReadWriteMultilineStringType) {
1035 std::string intermediate;
1036 {
1037 MultilineStringTypeMap map;
1038 map.name.str = "An Item";
1039 map.description.str = "Hello\nWorld";
1040 map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
1041 map.recipes.str = "\n\nTest 1\n\n\n";
1042 map.warningLabels.str = "";
1043 map.documentation.str = "\n\n";
1044 map.price = 350;
1045
1046 llvm::raw_string_ostream ostr(intermediate);
1047 Output yout(ostr);
1048 yout << map;
1049 }
1050 {
1051 Input yin(intermediate);
1052 MultilineStringTypeMap map2;
1053 yin >> map2;
1054
1055 EXPECT_FALSE(yin.error());
1056 EXPECT_EQ(map2.name.str, "An Item\n");
1057 EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
1058 EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
1059 EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
1060 EXPECT_TRUE(map2.warningLabels.str.empty());
1061 EXPECT_TRUE(map2.documentation.str.empty());
1062 EXPECT_EQ(map2.price, 350);
1063 }
1064}
1065
1066//
1067// Test writing then reading back custom values
1068//
1069TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
1070 std::string intermediate;
1071 {
1072 std::vector<MultilineStringType> documents;
1073 MultilineStringType doc;
1074 doc.str = "Hello\nWorld";
1075 documents.push_back(doc);
1076
1077 llvm::raw_string_ostream ostr(intermediate);
1078 Output yout(ostr);
1079 yout << documents;
1080
1081 // Verify that the block scalar header was written out on the same line
1082 // as the document marker.
1083 EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
1084 }
1085 {
1086 Input yin(intermediate);
1087 std::vector<MultilineStringType> documents2;
1088 yin >> documents2;
1089
1090 EXPECT_FALSE(yin.error());
1091 EXPECT_EQ(documents2.size(), size_t(1));
1092 EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
1093 }
1094}
1095
1096TEST(YAMLIO, TestReadWriteBlockScalarValue) {
1097 std::string intermediate;
1098 {
1099 MultilineStringType doc;
1100 doc.str = "Just a block\nscalar doc";
1101
1102 llvm::raw_string_ostream ostr(intermediate);
1103 Output yout(ostr);
1104 yout << doc;
1105 }
1106 {
1107 Input yin(intermediate);
1108 MultilineStringType doc;
1109 yin >> doc;
1110
1111 EXPECT_FALSE(yin.error());
1112 EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
1113 }
1114}
1115
1116//===----------------------------------------------------------------------===//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001117// Test flow sequences
1118//===----------------------------------------------------------------------===//
1119
1120LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
1121LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
Richard Smithd0c0c132017-06-30 20:56:57 +00001122LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef, MyString)
1123LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001124
1125namespace llvm {
1126namespace yaml {
1127 template<>
1128 struct ScalarTraits<MyNumber> {
1129 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
1130 out << value;
1131 }
1132
1133 static StringRef input(StringRef scalar, void *, MyNumber &value) {
David Blaikieb088ff62012-12-12 22:14:32 +00001134 long long n;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001135 if ( getAsSignedInteger(scalar, 0, n) )
1136 return "invalid number";
1137 value = n;
1138 return StringRef();
1139 }
David Majnemer77880332014-04-10 07:37:33 +00001140
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00001141 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001142 };
Richard Smithd0c0c132017-06-30 20:56:57 +00001143
1144 template <> struct ScalarTraits<MyString> {
1145 using Impl = ScalarTraits<StringRef>;
1146 static void output(const MyString &V, void *Ctx, raw_ostream &OS) {
1147 Impl::output(V, Ctx, OS);
1148 }
1149 static StringRef input(StringRef S, void *Ctx, MyString &V) {
1150 return Impl::input(S, Ctx, V.value);
1151 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00001152 static QuotingType mustQuote(StringRef S) {
1153 return Impl::mustQuote(S);
1154 }
Richard Smithd0c0c132017-06-30 20:56:57 +00001155 };
Nick Kledzikf60a9272012-12-12 20:46:15 +00001156}
1157}
1158
1159struct NameAndNumbers {
1160 llvm::StringRef name;
Richard Smithd0c0c132017-06-30 20:56:57 +00001161 std::vector<MyString> strings;
Nick Kledzikf60a9272012-12-12 20:46:15 +00001162 std::vector<MyNumber> single;
1163 std::vector<MyNumber> numbers;
1164};
1165
1166namespace llvm {
1167namespace yaml {
1168 template <>
1169 struct MappingTraits<NameAndNumbers> {
1170 static void mapping(IO &io, NameAndNumbers& nn) {
1171 io.mapRequired("name", nn.name);
1172 io.mapRequired("strings", nn.strings);
1173 io.mapRequired("single", nn.single);
1174 io.mapRequired("numbers", nn.numbers);
1175 }
1176 };
1177}
1178}
1179
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001180typedef std::vector<MyNumber> MyNumberFlowSequence;
1181
1182LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1183
1184struct NameAndNumbersFlow {
1185 llvm::StringRef name;
1186 std::vector<MyNumberFlowSequence> sequenceOfNumbers;
1187};
1188
1189namespace llvm {
1190namespace yaml {
1191 template <>
1192 struct MappingTraits<NameAndNumbersFlow> {
1193 static void mapping(IO &io, NameAndNumbersFlow& nn) {
1194 io.mapRequired("name", nn.name);
1195 io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
1196 }
1197 };
1198}
1199}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001200
1201//
1202// Test writing then reading back custom values
1203//
1204TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1205 std::string intermediate;
1206 {
1207 NameAndNumbers map;
1208 map.name = "hello";
1209 map.strings.push_back(llvm::StringRef("one"));
1210 map.strings.push_back(llvm::StringRef("two"));
1211 map.single.push_back(1);
1212 map.numbers.push_back(10);
1213 map.numbers.push_back(-30);
1214 map.numbers.push_back(1024);
1215
1216 llvm::raw_string_ostream ostr(intermediate);
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001217 Output yout(ostr);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001218 yout << map;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00001219
Nick Kledzik11964f22013-01-04 19:32:00 +00001220 // Verify sequences were written in flow style
1221 ostr.flush();
1222 llvm::StringRef flowOut(intermediate);
1223 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1224 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001225 }
1226
1227 {
1228 Input yin(intermediate);
1229 NameAndNumbers map2;
1230 yin >> map2;
1231
1232 EXPECT_FALSE(yin.error());
1233 EXPECT_TRUE(map2.name.equals("hello"));
1234 EXPECT_EQ(map2.strings.size(), 2UL);
Richard Smithd0c0c132017-06-30 20:56:57 +00001235 EXPECT_TRUE(map2.strings[0].value.equals("one"));
1236 EXPECT_TRUE(map2.strings[1].value.equals("two"));
Nick Kledzikf60a9272012-12-12 20:46:15 +00001237 EXPECT_EQ(map2.single.size(), 1UL);
1238 EXPECT_EQ(1, map2.single[0]);
1239 EXPECT_EQ(map2.numbers.size(), 3UL);
1240 EXPECT_EQ(10, map2.numbers[0]);
1241 EXPECT_EQ(-30, map2.numbers[1]);
1242 EXPECT_EQ(1024, map2.numbers[2]);
1243 }
1244}
1245
1246
Alex Lorenz42e91fa2015-05-01 18:34:25 +00001247//
1248// Test writing then reading back a sequence of flow sequences.
1249//
1250TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1251 std::string intermediate;
1252 {
1253 NameAndNumbersFlow map;
1254 map.name = "hello";
1255 MyNumberFlowSequence single = { 0 };
1256 MyNumberFlowSequence numbers = { 12, 1, -512 };
1257 map.sequenceOfNumbers.push_back(single);
1258 map.sequenceOfNumbers.push_back(numbers);
1259 map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1260
1261 llvm::raw_string_ostream ostr(intermediate);
1262 Output yout(ostr);
1263 yout << map;
1264
1265 // Verify sequences were written in flow style
1266 // and that the parent sequence used '-'.
1267 ostr.flush();
1268 llvm::StringRef flowOut(intermediate);
1269 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1270 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1271 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
1272 }
1273
1274 {
1275 Input yin(intermediate);
1276 NameAndNumbersFlow map2;
1277 yin >> map2;
1278
1279 EXPECT_FALSE(yin.error());
1280 EXPECT_TRUE(map2.name.equals("hello"));
1281 EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1282 EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1283 EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
1284 EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1285 EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
1286 EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
1287 EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1288 EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1289 }
1290}
1291
Nick Kledzikf60a9272012-12-12 20:46:15 +00001292//===----------------------------------------------------------------------===//
1293// Test normalizing/denormalizing
1294//===----------------------------------------------------------------------===//
1295
1296LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1297
1298typedef std::vector<TotalSeconds> SecondsSequence;
1299
Nick Kledzik11964f22013-01-04 19:32:00 +00001300LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001301
1302
1303namespace llvm {
1304namespace yaml {
1305 template <>
1306 struct MappingTraits<TotalSeconds> {
1307
1308 class NormalizedSeconds {
1309 public:
1310 NormalizedSeconds(IO &io)
1311 : hours(0), minutes(0), seconds(0) {
1312 }
1313 NormalizedSeconds(IO &, TotalSeconds &secs)
1314 : hours(secs/3600),
1315 minutes((secs - (hours*3600))/60),
1316 seconds(secs % 60) {
1317 }
1318 TotalSeconds denormalize(IO &) {
1319 return TotalSeconds(hours*3600 + minutes*60 + seconds);
1320 }
1321
1322 uint32_t hours;
1323 uint8_t minutes;
1324 uint8_t seconds;
1325 };
1326
1327 static void mapping(IO &io, TotalSeconds &secs) {
1328 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1329
1330 io.mapOptional("hours", keys->hours, (uint32_t)0);
1331 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
1332 io.mapRequired("seconds", keys->seconds);
1333 }
1334 };
1335}
1336}
1337
1338
1339//
1340// Test the reading of a yaml sequence of mappings
1341//
1342TEST(YAMLIO, TestReadMySecondsSequence) {
1343 SecondsSequence seq;
1344 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1345 yin >> seq;
1346
1347 EXPECT_FALSE(yin.error());
1348 EXPECT_EQ(seq.size(), 2UL);
1349 EXPECT_EQ(seq[0], 3605U);
1350 EXPECT_EQ(seq[1], 59U);
1351}
1352
1353
1354//
1355// Test writing then reading back custom values
1356//
1357TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1358 std::string intermediate;
1359 {
1360 SecondsSequence seq;
1361 seq.push_back(4000);
1362 seq.push_back(500);
1363 seq.push_back(59);
1364
1365 llvm::raw_string_ostream ostr(intermediate);
1366 Output yout(ostr);
1367 yout << seq;
1368 }
1369 {
1370 Input yin(intermediate);
1371 SecondsSequence seq2;
1372 yin >> seq2;
1373
1374 EXPECT_FALSE(yin.error());
1375 EXPECT_EQ(seq2.size(), 3UL);
1376 EXPECT_EQ(seq2[0], 4000U);
1377 EXPECT_EQ(seq2[1], 500U);
1378 EXPECT_EQ(seq2[2], 59U);
1379 }
1380}
1381
1382
1383//===----------------------------------------------------------------------===//
1384// Test dynamic typing
1385//===----------------------------------------------------------------------===//
1386
1387enum AFlags {
1388 a1,
1389 a2,
1390 a3
1391};
1392
1393enum BFlags {
1394 b1,
1395 b2,
1396 b3
1397};
1398
1399enum Kind {
1400 kindA,
1401 kindB
1402};
1403
1404struct KindAndFlags {
1405 KindAndFlags() : kind(kindA), flags(0) { }
1406 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1407 Kind kind;
1408 uint32_t flags;
1409};
1410
1411typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1412
Nick Kledzik11964f22013-01-04 19:32:00 +00001413LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
Nick Kledzikf60a9272012-12-12 20:46:15 +00001414
1415namespace llvm {
1416namespace yaml {
1417 template <>
1418 struct ScalarEnumerationTraits<AFlags> {
1419 static void enumeration(IO &io, AFlags &value) {
1420 io.enumCase(value, "a1", a1);
1421 io.enumCase(value, "a2", a2);
1422 io.enumCase(value, "a3", a3);
1423 }
1424 };
1425 template <>
1426 struct ScalarEnumerationTraits<BFlags> {
1427 static void enumeration(IO &io, BFlags &value) {
1428 io.enumCase(value, "b1", b1);
1429 io.enumCase(value, "b2", b2);
1430 io.enumCase(value, "b3", b3);
1431 }
1432 };
1433 template <>
1434 struct ScalarEnumerationTraits<Kind> {
1435 static void enumeration(IO &io, Kind &value) {
1436 io.enumCase(value, "A", kindA);
1437 io.enumCase(value, "B", kindB);
1438 }
1439 };
1440 template <>
1441 struct MappingTraits<KindAndFlags> {
1442 static void mapping(IO &io, KindAndFlags& kf) {
1443 io.mapRequired("kind", kf.kind);
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001444 // Type of "flags" field varies depending on "kind" field.
David Greene4162c2d2013-01-10 18:17:54 +00001445 // Use memcpy here to avoid breaking strict aliasing rules.
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001446 if (kf.kind == kindA) {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001447 AFlags aflags = static_cast<AFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001448 io.mapRequired("flags", aflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001449 kf.flags = aflags;
Dmitri Gribenkoba9d1b52013-01-10 21:10:44 +00001450 } else {
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001451 BFlags bflags = static_cast<BFlags>(kf.flags);
David Greene4162c2d2013-01-10 18:17:54 +00001452 io.mapRequired("flags", bflags);
Dmitri Gribenko7b4fb9a2013-01-10 21:21:32 +00001453 kf.flags = bflags;
David Greene4162c2d2013-01-10 18:17:54 +00001454 }
Nick Kledzikf60a9272012-12-12 20:46:15 +00001455 }
1456 };
1457}
1458}
1459
1460
1461//
1462// Test the reading of a yaml sequence dynamic types
1463//
1464TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1465 KindAndFlagsSequence seq;
1466 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1467 yin >> seq;
1468
1469 EXPECT_FALSE(yin.error());
1470 EXPECT_EQ(seq.size(), 2UL);
1471 EXPECT_EQ(seq[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001472 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001473 EXPECT_EQ(seq[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001474 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001475}
1476
1477//
1478// Test writing then reading back dynamic types
1479//
1480TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1481 std::string intermediate;
1482 {
1483 KindAndFlagsSequence seq;
1484 seq.push_back(KindAndFlags(kindA,a1));
1485 seq.push_back(KindAndFlags(kindB,b1));
1486 seq.push_back(KindAndFlags(kindA,a2));
1487 seq.push_back(KindAndFlags(kindB,b2));
1488 seq.push_back(KindAndFlags(kindA,a3));
1489
1490 llvm::raw_string_ostream ostr(intermediate);
1491 Output yout(ostr);
1492 yout << seq;
1493 }
1494 {
1495 Input yin(intermediate);
1496 KindAndFlagsSequence seq2;
1497 yin >> seq2;
1498
1499 EXPECT_FALSE(yin.error());
1500 EXPECT_EQ(seq2.size(), 5UL);
1501 EXPECT_EQ(seq2[0].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001502 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001503 EXPECT_EQ(seq2[1].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001504 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001505 EXPECT_EQ(seq2[2].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001506 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001507 EXPECT_EQ(seq2[3].kind, kindB);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001508 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001509 EXPECT_EQ(seq2[4].kind, kindA);
Nick Kledzik52bfd382012-12-17 20:43:53 +00001510 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001511 }
1512}
1513
1514
1515//===----------------------------------------------------------------------===//
1516// Test document list
1517//===----------------------------------------------------------------------===//
1518
1519struct FooBarMap {
1520 int foo;
1521 int bar;
1522};
1523typedef std::vector<FooBarMap> FooBarMapDocumentList;
1524
1525LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1526
1527
1528namespace llvm {
1529namespace yaml {
1530 template <>
1531 struct MappingTraits<FooBarMap> {
1532 static void mapping(IO &io, FooBarMap& fb) {
1533 io.mapRequired("foo", fb.foo);
1534 io.mapRequired("bar", fb.bar);
1535 }
1536 };
1537}
1538}
1539
1540
1541//
1542// Test the reading of a yaml mapping
1543//
1544TEST(YAMLIO, TestDocRead) {
1545 FooBarMap doc;
1546 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1547 yin >> doc;
1548
1549 EXPECT_FALSE(yin.error());
1550 EXPECT_EQ(doc.foo, 3);
1551 EXPECT_EQ(doc.bar,5);
1552}
1553
1554
1555
1556//
1557// Test writing then reading back a sequence of mappings
1558//
1559TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1560 std::string intermediate;
1561 {
1562 FooBarMap doc1;
1563 doc1.foo = 10;
1564 doc1.bar = -3;
1565 FooBarMap doc2;
1566 doc2.foo = 257;
1567 doc2.bar = 0;
1568 std::vector<FooBarMap> docList;
1569 docList.push_back(doc1);
1570 docList.push_back(doc2);
1571
1572 llvm::raw_string_ostream ostr(intermediate);
1573 Output yout(ostr);
1574 yout << docList;
1575 }
1576
1577
1578 {
1579 Input yin(intermediate);
1580 std::vector<FooBarMap> docList2;
1581 yin >> docList2;
1582
1583 EXPECT_FALSE(yin.error());
1584 EXPECT_EQ(docList2.size(), 2UL);
1585 FooBarMap& map1 = docList2[0];
1586 FooBarMap& map2 = docList2[1];
1587 EXPECT_EQ(map1.foo, 10);
1588 EXPECT_EQ(map1.bar, -3);
1589 EXPECT_EQ(map2.foo, 257);
1590 EXPECT_EQ(map2.bar, 0);
1591 }
1592}
1593
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001594//===----------------------------------------------------------------------===//
1595// Test document tags
1596//===----------------------------------------------------------------------===//
1597
1598struct MyDouble {
1599 MyDouble() : value(0.0) { }
1600 MyDouble(double x) : value(x) { }
1601 double value;
1602};
1603
Nick Kledzik4a9f00d2013-11-14 03:03:05 +00001604LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
Nick Kledzik1e6033c2013-11-14 00:59:59 +00001605
1606
1607namespace llvm {
1608namespace yaml {
1609 template <>
1610 struct MappingTraits<MyDouble> {
1611 static void mapping(IO &io, MyDouble &d) {
1612 if (io.mapTag("!decimal", true)) {
1613 mappingDecimal(io, d);
1614 } else if (io.mapTag("!fraction")) {
1615 mappingFraction(io, d);
1616 }
1617 }
1618 static void mappingDecimal(IO &io, MyDouble &d) {
1619 io.mapRequired("value", d.value);
1620 }
1621 static void mappingFraction(IO &io, MyDouble &d) {
1622 double num, denom;
1623 io.mapRequired("numerator", num);
1624 io.mapRequired("denominator", denom);
1625 // convert fraction to double
1626 d.value = num/denom;
1627 }
1628 };
1629 }
1630}
1631
1632
1633//
1634// Test the reading of two different tagged yaml documents.
1635//
1636TEST(YAMLIO, TestTaggedDocuments) {
1637 std::vector<MyDouble> docList;
1638 Input yin("--- !decimal\nvalue: 3.0\n"
1639 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1640 yin >> docList;
1641 EXPECT_FALSE(yin.error());
1642 EXPECT_EQ(docList.size(), 2UL);
1643 EXPECT_EQ(docList[0].value, 3.0);
1644 EXPECT_EQ(docList[1].value, 4.5);
1645}
1646
1647
1648
1649//
1650// Test writing then reading back tagged documents
1651//
1652TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1653 std::string intermediate;
1654 {
1655 MyDouble a(10.25);
1656 MyDouble b(-3.75);
1657 std::vector<MyDouble> docList;
1658 docList.push_back(a);
1659 docList.push_back(b);
1660
1661 llvm::raw_string_ostream ostr(intermediate);
1662 Output yout(ostr);
1663 yout << docList;
1664 }
1665
1666 {
1667 Input yin(intermediate);
1668 std::vector<MyDouble> docList2;
1669 yin >> docList2;
1670
1671 EXPECT_FALSE(yin.error());
1672 EXPECT_EQ(docList2.size(), 2UL);
1673 EXPECT_EQ(docList2[0].value, 10.25);
1674 EXPECT_EQ(docList2[1].value, -3.75);
1675 }
1676}
1677
1678
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001679//===----------------------------------------------------------------------===//
1680// Test mapping validation
1681//===----------------------------------------------------------------------===//
1682
1683struct MyValidation {
1684 double value;
1685};
1686
1687LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1688
1689namespace llvm {
1690namespace yaml {
1691 template <>
1692 struct MappingTraits<MyValidation> {
1693 static void mapping(IO &io, MyValidation &d) {
1694 io.mapRequired("value", d.value);
1695 }
1696 static StringRef validate(IO &io, MyValidation &d) {
1697 if (d.value < 0)
1698 return "negative value";
1699 return StringRef();
1700 }
1701 };
1702 }
1703}
1704
1705
1706//
1707// Test that validate() is called and complains about the negative value.
1708//
1709TEST(YAMLIO, TestValidatingInput) {
1710 std::vector<MyValidation> docList;
1711 Input yin("--- \nvalue: 3.0\n"
1712 "--- \nvalue: -1.0\n...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001713 nullptr, suppressErrorMessages);
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001714 yin >> docList;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001715 EXPECT_TRUE(!!yin.error());
Nick Kledzik7cd45f22013-11-21 00:28:07 +00001716}
1717
Alex Lorenzb1225082015-05-04 20:11:40 +00001718//===----------------------------------------------------------------------===//
1719// Test flow mapping
1720//===----------------------------------------------------------------------===//
1721
1722struct FlowFooBar {
1723 int foo;
1724 int bar;
1725
1726 FlowFooBar() : foo(0), bar(0) {}
1727 FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1728};
1729
1730typedef std::vector<FlowFooBar> FlowFooBarSequence;
1731
1732LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1733
1734struct FlowFooBarDoc {
1735 FlowFooBar attribute;
1736 FlowFooBarSequence seq;
1737};
1738
1739namespace llvm {
1740namespace yaml {
1741 template <>
1742 struct MappingTraits<FlowFooBar> {
1743 static void mapping(IO &io, FlowFooBar &fb) {
1744 io.mapRequired("foo", fb.foo);
1745 io.mapRequired("bar", fb.bar);
1746 }
1747
1748 static const bool flow = true;
1749 };
1750
1751 template <>
1752 struct MappingTraits<FlowFooBarDoc> {
1753 static void mapping(IO &io, FlowFooBarDoc &fb) {
1754 io.mapRequired("attribute", fb.attribute);
1755 io.mapRequired("seq", fb.seq);
1756 }
1757 };
1758}
1759}
1760
1761//
1762// Test writing then reading back custom mappings
1763//
1764TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1765 std::string intermediate;
1766 {
1767 FlowFooBarDoc doc;
1768 doc.attribute = FlowFooBar(42, 907);
1769 doc.seq.push_back(FlowFooBar(1, 2));
1770 doc.seq.push_back(FlowFooBar(0, 0));
1771 doc.seq.push_back(FlowFooBar(-1, 1024));
1772
1773 llvm::raw_string_ostream ostr(intermediate);
1774 Output yout(ostr);
1775 yout << doc;
1776
1777 // Verify that mappings were written in flow style
1778 ostr.flush();
1779 llvm::StringRef flowOut(intermediate);
1780 EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1781 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1782 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1783 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1784 }
1785
1786 {
1787 Input yin(intermediate);
1788 FlowFooBarDoc doc2;
1789 yin >> doc2;
1790
1791 EXPECT_FALSE(yin.error());
1792 EXPECT_EQ(doc2.attribute.foo, 42);
1793 EXPECT_EQ(doc2.attribute.bar, 907);
1794 EXPECT_EQ(doc2.seq.size(), 3UL);
1795 EXPECT_EQ(doc2.seq[0].foo, 1);
1796 EXPECT_EQ(doc2.seq[0].bar, 2);
1797 EXPECT_EQ(doc2.seq[1].foo, 0);
1798 EXPECT_EQ(doc2.seq[1].bar, 0);
1799 EXPECT_EQ(doc2.seq[2].foo, -1);
1800 EXPECT_EQ(doc2.seq[2].bar, 1024);
1801 }
1802}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001803
1804//===----------------------------------------------------------------------===//
1805// Test error handling
1806//===----------------------------------------------------------------------===//
1807
Nick Kledzikf60a9272012-12-12 20:46:15 +00001808//
1809// Test error handling of unknown enumerated scalar
1810//
1811TEST(YAMLIO, TestColorsReadError) {
1812 ColorMap map;
1813 Input yin("---\n"
1814 "c1: blue\n"
1815 "c2: purple\n"
1816 "c3: green\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001817 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001818 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001819 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001820 yin >> map;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001821 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001822}
1823
1824
1825//
1826// Test error handling of flow sequence with unknown value
1827//
1828TEST(YAMLIO, TestFlagsReadError) {
1829 FlagsMap map;
1830 Input yin("---\n"
1831 "f1: [ big ]\n"
1832 "f2: [ round, hollow ]\n"
1833 "f3: []\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001834 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001835 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001836 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001837 yin >> map;
1838
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001839 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001840}
1841
1842
1843//
1844// Test error handling reading built-in uint8_t type
1845//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001846TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1847 std::vector<uint8_t> seq;
1848 Input yin("---\n"
1849 "- 255\n"
1850 "- 0\n"
1851 "- 257\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001852 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001853 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001854 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001855 yin >> seq;
1856
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001857 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001858}
1859
1860
1861//
1862// Test error handling reading built-in uint16_t type
1863//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001864TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1865 std::vector<uint16_t> seq;
1866 Input yin("---\n"
1867 "- 65535\n"
1868 "- 0\n"
1869 "- 66000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001870 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001871 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001872 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001873 yin >> seq;
1874
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001875 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001876}
1877
1878
1879//
1880// Test error handling reading built-in uint32_t type
1881//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001882TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1883 std::vector<uint32_t> seq;
1884 Input yin("---\n"
1885 "- 4000000000\n"
1886 "- 0\n"
1887 "- 5000000000\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001888 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001889 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001890 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001891 yin >> seq;
1892
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001893 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001894}
1895
1896
1897//
1898// Test error handling reading built-in uint64_t type
1899//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001900TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1901 std::vector<uint64_t> seq;
1902 Input yin("---\n"
1903 "- 18446744073709551615\n"
1904 "- 0\n"
1905 "- 19446744073709551615\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001906 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001907 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001908 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001909 yin >> seq;
1910
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001911 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001912}
1913
1914
1915//
1916// Test error handling reading built-in int8_t type
1917//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001918TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1919 std::vector<int8_t> seq;
1920 Input yin("---\n"
1921 "- -128\n"
1922 "- 0\n"
1923 "- 127\n"
1924 "- 128\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001925 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001926 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001927 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001928 yin >> seq;
1929
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001930 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001931}
1932
1933//
1934// Test error handling reading built-in int8_t type
1935//
1936TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1937 std::vector<int8_t> seq;
1938 Input yin("---\n"
1939 "- -128\n"
1940 "- 0\n"
1941 "- 127\n"
1942 "- -129\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001943 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001944 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001945 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001946 yin >> seq;
1947
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001948 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001949}
1950
1951
1952//
1953// Test error handling reading built-in int16_t type
1954//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001955TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1956 std::vector<int16_t> seq;
1957 Input yin("---\n"
1958 "- 32767\n"
1959 "- 0\n"
1960 "- -32768\n"
1961 "- -32769\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001962 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001963 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001964 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001965 yin >> seq;
1966
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001967 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001968}
1969
1970
1971//
1972// Test error handling reading built-in int16_t type
1973//
1974TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1975 std::vector<int16_t> seq;
1976 Input yin("---\n"
1977 "- 32767\n"
1978 "- 0\n"
1979 "- -32768\n"
1980 "- 32768\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001981 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00001982 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00001983 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00001984 yin >> seq;
1985
Rafael Espindolad9a25d82014-06-03 04:42:24 +00001986 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00001987}
1988
1989
1990//
1991// Test error handling reading built-in int32_t type
1992//
Nick Kledzikf60a9272012-12-12 20:46:15 +00001993TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1994 std::vector<int32_t> seq;
1995 Input yin("---\n"
1996 "- 2147483647\n"
1997 "- 0\n"
1998 "- -2147483648\n"
1999 "- -2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002000 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002001 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002002 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002003 yin >> seq;
2004
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002005 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002006}
2007
2008//
2009// Test error handling reading built-in int32_t type
2010//
2011TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
2012 std::vector<int32_t> seq;
2013 Input yin("---\n"
2014 "- 2147483647\n"
2015 "- 0\n"
2016 "- -2147483648\n"
2017 "- 2147483649\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002018 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002019 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002020 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002021 yin >> seq;
2022
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002023 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002024}
2025
2026
2027//
2028// Test error handling reading built-in int64_t type
2029//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002030TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
2031 std::vector<int64_t> seq;
2032 Input yin("---\n"
2033 "- -9223372036854775808\n"
2034 "- 0\n"
2035 "- 9223372036854775807\n"
2036 "- -9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002037 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002038 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002039 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002040 yin >> seq;
2041
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002042 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002043}
2044
2045//
2046// Test error handling reading built-in int64_t type
2047//
2048TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
2049 std::vector<int64_t> seq;
2050 Input yin("---\n"
2051 "- -9223372036854775808\n"
2052 "- 0\n"
2053 "- 9223372036854775807\n"
2054 "- 9223372036854775809\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002055 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002056 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002057 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002058 yin >> seq;
2059
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002060 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002061}
2062
2063//
2064// Test error handling reading built-in float type
2065//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002066TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
2067 std::vector<float> seq;
2068 Input yin("---\n"
2069 "- 0.0\n"
2070 "- 1000.1\n"
2071 "- -123.456\n"
2072 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002073 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002074 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002075 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002076 yin >> seq;
2077
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002078 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002079}
2080
2081//
2082// Test error handling reading built-in float type
2083//
Nick Kledzikf60a9272012-12-12 20:46:15 +00002084TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
2085 std::vector<double> seq;
2086 Input yin("---\n"
2087 "- 0.0\n"
2088 "- 1000.1\n"
2089 "- -123.456\n"
2090 "- 1.2.3\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002091 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002092 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002093 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002094 yin >> seq;
2095
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002096 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002097}
2098
2099//
2100// Test error handling reading built-in Hex8 type
2101//
2102LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
2103TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
2104 std::vector<Hex8> seq;
2105 Input yin("---\n"
2106 "- 0x12\n"
2107 "- 0xFE\n"
2108 "- 0x123\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002109 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002110 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002111 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002112 yin >> seq;
2113
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002114 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002115}
2116
2117
2118//
2119// Test error handling reading built-in Hex16 type
2120//
2121LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
2122TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
2123 std::vector<Hex16> seq;
2124 Input yin("---\n"
2125 "- 0x0012\n"
2126 "- 0xFEFF\n"
2127 "- 0x12345\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002128 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002129 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002130 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002131 yin >> seq;
2132
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002133 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002134}
2135
2136//
2137// Test error handling reading built-in Hex32 type
2138//
2139LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
2140TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
2141 std::vector<Hex32> seq;
2142 Input yin("---\n"
2143 "- 0x0012\n"
2144 "- 0xFEFF0000\n"
2145 "- 0x1234556789\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002146 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002147 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002148 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002149 yin >> seq;
2150
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002151 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002152}
2153
2154//
2155// Test error handling reading built-in Hex64 type
2156//
2157LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
2158TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
2159 std::vector<Hex64> seq;
2160 Input yin("---\n"
2161 "- 0x0012\n"
2162 "- 0xFFEEDDCCBBAA9988\n"
2163 "- 0x12345567890ABCDEF0\n"
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002164 "...\n",
Craig Topper66f09ad2014-06-08 22:29:17 +00002165 /*Ctxt=*/nullptr,
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002166 suppressErrorMessages);
Nick Kledzikf60a9272012-12-12 20:46:15 +00002167 yin >> seq;
2168
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002169 EXPECT_TRUE(!!yin.error());
Nick Kledzikf60a9272012-12-12 20:46:15 +00002170}
2171
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002172TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2173 FooBar doc;
2174 {
2175 // We pass the suppressErrorMessages handler to handle the error
2176 // message generated in the constructor of Input.
Craig Topper66f09ad2014-06-08 22:29:17 +00002177 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002178 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002179 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002180 }
2181
2182 {
Craig Topper66f09ad2014-06-08 22:29:17 +00002183 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002184 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002185 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002186 }
2187}
2188
Aaron Ballman0e63e532013-08-15 23:17:53 +00002189struct OptionalTest {
2190 std::vector<int> Numbers;
2191};
2192
2193struct OptionalTestSeq {
2194 std::vector<OptionalTest> Tests;
2195};
2196
Aaron Ballman381f59f2013-08-16 01:53:58 +00002197LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
Aaron Ballman0e63e532013-08-15 23:17:53 +00002198namespace llvm {
2199namespace yaml {
2200 template <>
2201 struct MappingTraits<OptionalTest> {
2202 static void mapping(IO& IO, OptionalTest &OT) {
2203 IO.mapOptional("Numbers", OT.Numbers);
2204 }
2205 };
2206
2207 template <>
2208 struct MappingTraits<OptionalTestSeq> {
2209 static void mapping(IO &IO, OptionalTestSeq &OTS) {
2210 IO.mapOptional("Tests", OTS.Tests);
2211 }
2212 };
2213}
2214}
2215
2216TEST(YAMLIO, SequenceElideTest) {
2217 // Test that writing out a purely optional structure with its fields set to
2218 // default followed by other data is properly read back in.
2219 OptionalTestSeq Seq;
2220 OptionalTest One, Two, Three, Four;
2221 int N[] = {1, 2, 3};
2222 Three.Numbers.assign(N, N + 3);
2223 Seq.Tests.push_back(One);
2224 Seq.Tests.push_back(Two);
2225 Seq.Tests.push_back(Three);
2226 Seq.Tests.push_back(Four);
2227
2228 std::string intermediate;
2229 {
2230 llvm::raw_string_ostream ostr(intermediate);
2231 Output yout(ostr);
2232 yout << Seq;
2233 }
2234
2235 Input yin(intermediate);
2236 OptionalTestSeq Seq2;
2237 yin >> Seq2;
Rui Ueyama38dfffa2013-09-11 00:53:07 +00002238
Aaron Ballman0e63e532013-08-15 23:17:53 +00002239 EXPECT_FALSE(yin.error());
2240
2241 EXPECT_EQ(4UL, Seq2.Tests.size());
2242
2243 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2244 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2245
2246 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2247 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2248 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2249
2250 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2251}
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002252
2253TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2254 FooBar doc;
2255 Input yin("");
2256 yin >> doc;
Rafael Espindolad9a25d82014-06-03 04:42:24 +00002257 EXPECT_TRUE(!!yin.error());
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002258}
2259
2260TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2261 OptionalTest doc;
2262 Input yin("");
2263 yin >> doc;
2264 EXPECT_FALSE(yin.error());
2265}
2266
2267TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2268 std::vector<uint8_t> seq;
Craig Topper66f09ad2014-06-08 22:29:17 +00002269 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
Alexander Kornienko681e37c2013-11-18 15:50:04 +00002270 yin >> seq;
2271
2272 EXPECT_FALSE(yin.error());
2273 EXPECT_TRUE(seq.empty());
2274}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002275
2276struct FlowMap {
2277 llvm::StringRef str1, str2, str3;
2278 FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2279 : str1(str1), str2(str2), str3(str3) {}
2280};
2281
Frederic Riss3733c032015-05-29 18:14:55 +00002282struct FlowSeq {
2283 llvm::StringRef str;
2284 FlowSeq(llvm::StringRef S) : str(S) {}
2285 FlowSeq() = default;
2286};
2287
Frederic Riss4939e6a2015-05-29 17:56:28 +00002288namespace llvm {
2289namespace yaml {
2290 template <>
2291 struct MappingTraits<FlowMap> {
2292 static void mapping(IO &io, FlowMap &fm) {
2293 io.mapRequired("str1", fm.str1);
2294 io.mapRequired("str2", fm.str2);
2295 io.mapRequired("str3", fm.str3);
2296 }
2297
2298 static const bool flow = true;
2299 };
Frederic Riss4939e6a2015-05-29 17:56:28 +00002300
2301template <>
2302struct ScalarTraits<FlowSeq> {
2303 static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2304 out << value.str;
2305 }
2306 static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2307 value.str = scalar;
2308 return "";
2309 }
2310
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002311 static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
Frederic Riss4939e6a2015-05-29 17:56:28 +00002312};
Frederic Riss3733c032015-05-29 18:14:55 +00002313}
2314}
Frederic Riss4939e6a2015-05-29 17:56:28 +00002315
2316LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2317
2318TEST(YAMLIO, TestWrapFlow) {
2319 std::string out;
2320 llvm::raw_string_ostream ostr(out);
2321 FlowMap Map("This is str1", "This is str2", "This is str3");
2322 std::vector<FlowSeq> Seq;
2323 Seq.emplace_back("This is str1");
2324 Seq.emplace_back("This is str2");
2325 Seq.emplace_back("This is str3");
2326
2327 {
2328 // 20 is just bellow the total length of the first mapping field.
2329 // We should wreap at every element.
2330 Output yout(ostr, nullptr, 15);
2331
2332 yout << Map;
2333 ostr.flush();
2334 EXPECT_EQ(out,
2335 "---\n"
2336 "{ str1: This is str1, \n"
2337 " str2: This is str2, \n"
2338 " str3: This is str3 }\n"
2339 "...\n");
2340 out.clear();
2341
2342 yout << Seq;
2343 ostr.flush();
2344 EXPECT_EQ(out,
2345 "---\n"
2346 "[ This is str1, \n"
2347 " This is str2, \n"
2348 " This is str3 ]\n"
2349 "...\n");
2350 out.clear();
2351 }
2352 {
2353 // 25 will allow the second field to be output on the first line.
2354 Output yout(ostr, nullptr, 25);
2355
2356 yout << Map;
2357 ostr.flush();
2358 EXPECT_EQ(out,
2359 "---\n"
2360 "{ str1: This is str1, str2: This is str2, \n"
2361 " str3: This is str3 }\n"
2362 "...\n");
2363 out.clear();
2364
2365 yout << Seq;
2366 ostr.flush();
2367 EXPECT_EQ(out,
2368 "---\n"
2369 "[ This is str1, This is str2, \n"
2370 " This is str3 ]\n"
2371 "...\n");
2372 out.clear();
2373 }
2374 {
2375 // 0 means no wrapping.
2376 Output yout(ostr, nullptr, 0);
2377
2378 yout << Map;
2379 ostr.flush();
2380 EXPECT_EQ(out,
2381 "---\n"
2382 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2383 "...\n");
2384 out.clear();
2385
2386 yout << Seq;
2387 ostr.flush();
2388 EXPECT_EQ(out,
2389 "---\n"
2390 "[ This is str1, This is str2, This is str3 ]\n"
2391 "...\n");
2392 out.clear();
2393 }
2394}
Zachary Turner35377f82016-09-08 18:22:44 +00002395
2396struct MappingContext {
2397 int A = 0;
2398};
2399struct SimpleMap {
2400 int B = 0;
2401 int C = 0;
2402};
2403
2404struct NestedMap {
2405 NestedMap(MappingContext &Context) : Context(Context) {}
2406 SimpleMap Simple;
2407 MappingContext &Context;
2408};
2409
2410namespace llvm {
2411namespace yaml {
2412template <> struct MappingContextTraits<SimpleMap, MappingContext> {
2413 static void mapping(IO &io, SimpleMap &sm, MappingContext &Context) {
2414 io.mapRequired("B", sm.B);
2415 io.mapRequired("C", sm.C);
2416 ++Context.A;
2417 io.mapRequired("Context", Context.A);
2418 }
2419};
2420
2421template <> struct MappingTraits<NestedMap> {
2422 static void mapping(IO &io, NestedMap &nm) {
2423 io.mapRequired("Simple", nm.Simple, nm.Context);
2424 }
2425};
2426}
2427}
2428
2429TEST(YAMLIO, TestMapWithContext) {
2430 MappingContext Context;
2431 NestedMap Nested(Context);
2432 std::string out;
2433 llvm::raw_string_ostream ostr(out);
2434
2435 Output yout(ostr, nullptr, 15);
2436
2437 yout << Nested;
2438 ostr.flush();
2439 EXPECT_EQ(1, Context.A);
2440 EXPECT_EQ("---\n"
2441 "Simple: \n"
2442 " B: 0\n"
2443 " C: 0\n"
2444 " Context: 1\n"
2445 "...\n",
2446 out);
2447
2448 out.clear();
2449
2450 Nested.Simple.B = 2;
2451 Nested.Simple.C = 3;
2452 yout << Nested;
2453 ostr.flush();
2454 EXPECT_EQ(2, Context.A);
2455 EXPECT_EQ("---\n"
2456 "Simple: \n"
2457 " B: 2\n"
2458 " C: 3\n"
2459 " Context: 2\n"
2460 "...\n",
2461 out);
2462 out.clear();
2463}
Mehdi Amini3ab3fef2016-11-28 21:38:52 +00002464
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +00002465LLVM_YAML_IS_STRING_MAP(int)
2466
2467TEST(YAMLIO, TestCustomMapping) {
2468 std::map<std::string, int> x;
2469 x["foo"] = 1;
2470 x["bar"] = 2;
2471
2472 std::string out;
2473 llvm::raw_string_ostream ostr(out);
2474 Output xout(ostr, nullptr, 0);
2475
2476 xout << x;
2477 ostr.flush();
2478 EXPECT_EQ("---\n"
2479 "bar: 2\n"
2480 "foo: 1\n"
2481 "...\n",
2482 out);
2483
2484 Input yin(out);
2485 std::map<std::string, int> y;
2486 yin >> y;
2487 EXPECT_EQ(2ul, y.size());
2488 EXPECT_EQ(1, y["foo"]);
2489 EXPECT_EQ(2, y["bar"]);
2490}
2491
2492LLVM_YAML_IS_STRING_MAP(FooBar)
2493
2494TEST(YAMLIO, TestCustomMappingStruct) {
2495 std::map<std::string, FooBar> x;
2496 x["foo"].foo = 1;
2497 x["foo"].bar = 2;
2498 x["bar"].foo = 3;
2499 x["bar"].bar = 4;
2500
2501 std::string out;
2502 llvm::raw_string_ostream ostr(out);
2503 Output xout(ostr, nullptr, 0);
2504
2505 xout << x;
2506 ostr.flush();
2507 EXPECT_EQ("---\n"
2508 "bar: \n"
2509 " foo: 3\n"
2510 " bar: 4\n"
2511 "foo: \n"
2512 " foo: 1\n"
2513 " bar: 2\n"
2514 "...\n",
2515 out);
2516
2517 Input yin(out);
2518 std::map<std::string, FooBar> y;
2519 yin >> y;
2520 EXPECT_EQ(2ul, y.size());
2521 EXPECT_EQ(1, y["foo"].foo);
2522 EXPECT_EQ(2, y["foo"].bar);
2523 EXPECT_EQ(3, y["bar"].foo);
2524 EXPECT_EQ(4, y["bar"].bar);
2525}
2526
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002527static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002528 std::string out;
2529 llvm::raw_string_ostream ostr(out);
2530 Output xout(ostr, nullptr, 0);
2531
2532 llvm::yaml::EmptyContext Ctx;
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002533 yamlize(xout, Input, true, Ctx);
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002534
2535 ostr.flush();
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002536
2537 // Make a separate StringRef so we get nice byte-by-byte output.
2538 llvm::StringRef Got(out);
2539 EXPECT_EQ(Expected, Got);
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +00002540}
2541
Francis Visoiu Mistrihc9a84512017-12-21 17:14:13 +00002542TEST(YAMLIO, TestEscaped) {
2543 // Single quote
2544 TestEscaped("@abc@", "'@abc@'");
2545 // No quote
2546 TestEscaped("abc/", "abc/");
2547 // Double quote non-printable
2548 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2549 // Double quote inside single quote
2550 TestEscaped("abc\"fdf", "'abc\"fdf'");
2551 // Double quote inside double quote
2552 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2553 // Single quote inside single quote
2554 TestEscaped("abc'fdf", "'abc''fdf'");
2555 // UTF8
2556 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2557 // UTF8 with single quote inside double quote
2558 TestEscaped("parameter 'параметр' is unused",
2559 "\"parameter 'параметр' is unused\"");
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002560
2561 // String with embedded non-printable multibyte UTF-8 sequence (U+200B
2562 // zero-width space). The thing to test here is that we emit a
2563 // unicode-scalar level escape like \uNNNN (at the YAML level), and don't
2564 // just pass the UTF-8 byte sequence through as with quoted printables.
Graydon Hoare926cd9b2018-03-27 19:52:45 +00002565 {
2566 const unsigned char foobar[10] = {'f', 'o', 'o',
2567 0xE2, 0x80, 0x8B, // UTF-8 of U+200B
2568 'b', 'a', 'r',
2569 0x0};
2570 TestEscaped((char const *)foobar, "\"foo\\u200Bbar\"");
2571 }
Francis Visoiu Mistrihb2b961a2017-12-21 17:14:09 +00002572}
Kirill Bobyrev5f26a642018-08-20 07:00:36 +00002573
2574TEST(YAMLIO, Numeric) {
2575 EXPECT_TRUE(isNumeric(".inf"));
2576 EXPECT_TRUE(isNumeric(".INF"));
2577 EXPECT_TRUE(isNumeric(".Inf"));
2578 EXPECT_TRUE(isNumeric("-.inf"));
2579 EXPECT_TRUE(isNumeric("+.inf"));
2580
2581 EXPECT_TRUE(isNumeric(".nan"));
2582 EXPECT_TRUE(isNumeric(".NaN"));
2583 EXPECT_TRUE(isNumeric(".NAN"));
2584
2585 EXPECT_TRUE(isNumeric("0"));
2586 EXPECT_TRUE(isNumeric("0."));
2587 EXPECT_TRUE(isNumeric("0.0"));
2588 EXPECT_TRUE(isNumeric("-0.0"));
2589 EXPECT_TRUE(isNumeric("+0.0"));
2590
2591 EXPECT_TRUE(isNumeric("12345"));
2592 EXPECT_TRUE(isNumeric("012345"));
2593 EXPECT_TRUE(isNumeric("+12.0"));
2594 EXPECT_TRUE(isNumeric(".5"));
2595 EXPECT_TRUE(isNumeric("+.5"));
2596 EXPECT_TRUE(isNumeric("-1.0"));
2597
2598 EXPECT_TRUE(isNumeric("2.3e4"));
2599 EXPECT_TRUE(isNumeric("-2E+05"));
2600 EXPECT_TRUE(isNumeric("+12e03"));
2601 EXPECT_TRUE(isNumeric("6.8523015e+5"));
2602
2603 EXPECT_TRUE(isNumeric("1.e+1"));
2604 EXPECT_TRUE(isNumeric(".0e+1"));
2605
2606 EXPECT_TRUE(isNumeric("0x2aF3"));
2607 EXPECT_TRUE(isNumeric("0o01234567"));
2608
2609 EXPECT_FALSE(isNumeric("not a number"));
2610 EXPECT_FALSE(isNumeric("."));
2611 EXPECT_FALSE(isNumeric(".e+1"));
2612 EXPECT_FALSE(isNumeric(".1e"));
2613 EXPECT_FALSE(isNumeric(".1e+"));
2614 EXPECT_FALSE(isNumeric(".1e++1"));
2615
2616 EXPECT_FALSE(isNumeric("ABCD"));
2617 EXPECT_FALSE(isNumeric("+0x2AF3"));
2618 EXPECT_FALSE(isNumeric("-0x2AF3"));
2619 EXPECT_FALSE(isNumeric("0x2AF3Z"));
2620 EXPECT_FALSE(isNumeric("0o012345678"));
2621 EXPECT_FALSE(isNumeric("0xZ"));
2622 EXPECT_FALSE(isNumeric("-0o012345678"));
2623 EXPECT_FALSE(isNumeric("000003A8229434B839616A25C16B0291F77A438B"));
2624
2625 EXPECT_FALSE(isNumeric(""));
2626 EXPECT_FALSE(isNumeric("."));
2627 EXPECT_FALSE(isNumeric(".e+1"));
2628 EXPECT_FALSE(isNumeric(".e+"));
2629 EXPECT_FALSE(isNumeric(".e"));
2630 EXPECT_FALSE(isNumeric("e1"));
2631
2632 // Deprecated formats: as for YAML 1.2 specification, the following are not
2633 // valid numbers anymore:
2634 //
2635 // * Sexagecimal numbers
2636 // * Decimal numbers with comma s the delimiter
2637 // * "inf", "nan" without '.' prefix
2638 EXPECT_FALSE(isNumeric("3:25:45"));
2639 EXPECT_FALSE(isNumeric("+12,345"));
2640 EXPECT_FALSE(isNumeric("-inf"));
2641 EXPECT_FALSE(isNumeric("1,230.15"));
2642}