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