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