1 //===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/Endian.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/YAMLTraits.h"
16 #include "gtest/gtest.h"
18 using llvm::yaml::Input;
19 using llvm::yaml::Output;
21 using llvm::yaml::MappingTraits;
22 using llvm::yaml::MappingNormalization;
23 using llvm::yaml::ScalarTraits;
24 using llvm::yaml::Hex8;
25 using llvm::yaml::Hex16;
26 using llvm::yaml::Hex32;
27 using llvm::yaml::Hex64;
32 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
45 typedef std::vector<FooBar> FooBarSequence;
47 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
49 struct FooBarContainer {
56 struct MappingTraits<FooBar> {
57 static void mapping(IO &io, FooBar& fb) {
58 io.mapRequired("foo", fb.foo);
59 io.mapRequired("bar", fb.bar);
63 template <> struct MappingTraits<FooBarContainer> {
64 static void mapping(IO &io, FooBarContainer &fb) {
65 io.mapRequired("fbs", fb.fbs);
73 // Test the reading of a yaml mapping
75 TEST(YAMLIO, TestMapRead) {
78 Input yin("---\nfoo: 3\nbar: 5\n...\n");
81 EXPECT_FALSE(yin.error());
82 EXPECT_EQ(doc.foo, 3);
83 EXPECT_EQ(doc.bar, 5);
87 Input yin("{foo: 3, bar: 5}");
90 EXPECT_FALSE(yin.error());
91 EXPECT_EQ(doc.foo, 3);
92 EXPECT_EQ(doc.bar, 5);
96 TEST(YAMLIO, TestMalformedMapRead) {
98 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
100 EXPECT_TRUE(!!yin.error());
104 // Test the reading of a yaml sequence of mappings
106 TEST(YAMLIO, TestSequenceMapRead) {
108 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
111 EXPECT_FALSE(yin.error());
112 EXPECT_EQ(seq.size(), 2UL);
113 FooBar& map1 = seq[0];
114 FooBar& map2 = seq[1];
115 EXPECT_EQ(map1.foo, 3);
116 EXPECT_EQ(map1.bar, 5);
117 EXPECT_EQ(map2.foo, 7);
118 EXPECT_EQ(map2.bar, 9);
122 // Test the reading of a map containing a yaml sequence of mappings
124 TEST(YAMLIO, TestContainerSequenceMapRead) {
126 FooBarContainer cont;
127 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
130 EXPECT_FALSE(yin2.error());
131 EXPECT_EQ(cont.fbs.size(), 2UL);
132 EXPECT_EQ(cont.fbs[0].foo, 3);
133 EXPECT_EQ(cont.fbs[0].bar, 5);
134 EXPECT_EQ(cont.fbs[1].foo, 7);
135 EXPECT_EQ(cont.fbs[1].bar, 9);
139 FooBarContainer cont;
140 Input yin("---\nfbs:\n...\n");
142 // Okay: Empty node represents an empty array.
143 EXPECT_FALSE(yin.error());
144 EXPECT_EQ(cont.fbs.size(), 0UL);
148 FooBarContainer cont;
149 Input yin("---\nfbs: !!null null\n...\n");
151 // Okay: null represents an empty array.
152 EXPECT_FALSE(yin.error());
153 EXPECT_EQ(cont.fbs.size(), 0UL);
157 FooBarContainer cont;
158 Input yin("---\nfbs: ~\n...\n");
160 // Okay: null represents an empty array.
161 EXPECT_FALSE(yin.error());
162 EXPECT_EQ(cont.fbs.size(), 0UL);
166 FooBarContainer cont;
167 Input yin("---\nfbs: null\n...\n");
169 // Okay: null represents an empty array.
170 EXPECT_FALSE(yin.error());
171 EXPECT_EQ(cont.fbs.size(), 0UL);
176 // Test the reading of a map containing a malformed yaml sequence
178 TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
180 FooBarContainer cont;
181 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
182 suppressErrorMessages);
184 // Error: fbs is not a sequence.
185 EXPECT_TRUE(!!yin.error());
186 EXPECT_EQ(cont.fbs.size(), 0UL);
190 FooBarContainer cont;
191 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
193 // This should be an error.
194 EXPECT_TRUE(!!yin.error());
195 EXPECT_EQ(cont.fbs.size(), 0UL);
200 // Test writing then reading back a sequence of mappings
202 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
203 std::string intermediate;
212 seq.push_back(entry1);
213 seq.push_back(entry2);
215 llvm::raw_string_ostream ostr(intermediate);
221 Input yin(intermediate);
225 EXPECT_FALSE(yin.error());
226 EXPECT_EQ(seq2.size(), 2UL);
227 FooBar& map1 = seq2[0];
228 FooBar& map2 = seq2[1];
229 EXPECT_EQ(map1.foo, 10);
230 EXPECT_EQ(map1.bar, -3);
231 EXPECT_EQ(map2.foo, 257);
232 EXPECT_EQ(map2.bar, 0);
237 // Test YAML filename handling.
239 static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) {
240 EXPECT_EQ(Error.getFilename(), "foo.yaml");
243 TEST(YAMLIO, TestGivenFilename) {
244 auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
245 Input yin(*Buffer, nullptr, testErrorFilename);
249 EXPECT_TRUE(!!yin.error());
253 //===----------------------------------------------------------------------===//
254 // Test built-in types
255 //===----------------------------------------------------------------------===//
257 struct BuiltInTypes {
280 struct MappingTraits<BuiltInTypes> {
281 static void mapping(IO &io, BuiltInTypes& bt) {
282 io.mapRequired("str", bt.str);
283 io.mapRequired("stdstr", bt.stdstr);
284 io.mapRequired("u64", bt.u64);
285 io.mapRequired("u32", bt.u32);
286 io.mapRequired("u16", bt.u16);
287 io.mapRequired("u8", bt.u8);
288 io.mapRequired("b", bt.b);
289 io.mapRequired("s64", bt.s64);
290 io.mapRequired("s32", bt.s32);
291 io.mapRequired("s16", bt.s16);
292 io.mapRequired("s8", bt.s8);
293 io.mapRequired("f", bt.f);
294 io.mapRequired("d", bt.d);
295 io.mapRequired("h8", bt.h8);
296 io.mapRequired("h16", bt.h16);
297 io.mapRequired("h32", bt.h32);
298 io.mapRequired("h64", bt.h64);
306 // Test the reading of all built-in scalar conversions
308 TEST(YAMLIO, TestReadBuiltInTypes) {
312 "stdstr: hello where?\n"
327 "h64: 0xFEDCBA9876543210\n"
331 EXPECT_FALSE(yin.error());
332 EXPECT_TRUE(map.str.equals("hello there"));
333 EXPECT_TRUE(map.stdstr == "hello where?");
334 EXPECT_EQ(map.u64, 5000000000ULL);
335 EXPECT_EQ(map.u32, 4000000000U);
336 EXPECT_EQ(map.u16, 65000);
337 EXPECT_EQ(map.u8, 255);
338 EXPECT_EQ(map.b, false);
339 EXPECT_EQ(map.s64, -5000000000LL);
340 EXPECT_EQ(map.s32, -2000000000L);
341 EXPECT_EQ(map.s16, -32000);
342 EXPECT_EQ(map.s8, -127);
343 EXPECT_EQ(map.f, 137.125);
344 EXPECT_EQ(map.d, -2.8625);
345 EXPECT_EQ(map.h8, Hex8(255));
346 EXPECT_EQ(map.h16, Hex16(0x8765));
347 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
348 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
353 // Test writing then reading back all built-in scalar types
355 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
356 std::string intermediate;
360 map.stdstr = "three four";
361 map.u64 = 6000000000ULL;
362 map.u32 = 3000000000U;
366 map.s64 = -6000000000LL;
367 map.s32 = -2000000000;
374 map.h32 = 3000000000U;
375 map.h64 = 6000000000LL;
377 llvm::raw_string_ostream ostr(intermediate);
383 Input yin(intermediate);
387 EXPECT_FALSE(yin.error());
388 EXPECT_TRUE(map.str.equals("one two"));
389 EXPECT_TRUE(map.stdstr == "three four");
390 EXPECT_EQ(map.u64, 6000000000ULL);
391 EXPECT_EQ(map.u32, 3000000000U);
392 EXPECT_EQ(map.u16, 50000);
393 EXPECT_EQ(map.u8, 254);
394 EXPECT_EQ(map.b, true);
395 EXPECT_EQ(map.s64, -6000000000LL);
396 EXPECT_EQ(map.s32, -2000000000L);
397 EXPECT_EQ(map.s16, -32000);
398 EXPECT_EQ(map.s8, -128);
399 EXPECT_EQ(map.f, 3.25);
400 EXPECT_EQ(map.d, -2.8625);
401 EXPECT_EQ(map.h8, Hex8(254));
402 EXPECT_EQ(map.h16, Hex16(50000));
403 EXPECT_EQ(map.h32, Hex32(3000000000U));
404 EXPECT_EQ(map.h64, Hex64(6000000000LL));
408 //===----------------------------------------------------------------------===//
409 // Test endian-aware types
410 //===----------------------------------------------------------------------===//
413 typedef llvm::support::detail::packed_endian_specific_integral<
414 float, llvm::support::little, llvm::support::unaligned>
416 typedef llvm::support::detail::packed_endian_specific_integral<
417 double, llvm::support::little, llvm::support::unaligned>
420 llvm::support::ulittle64_t u64;
421 llvm::support::ulittle32_t u32;
422 llvm::support::ulittle16_t u16;
423 llvm::support::little64_t s64;
424 llvm::support::little32_t s32;
425 llvm::support::little16_t s16;
432 template <> struct MappingTraits<EndianTypes> {
433 static void mapping(IO &io, EndianTypes &et) {
434 io.mapRequired("u64", et.u64);
435 io.mapRequired("u32", et.u32);
436 io.mapRequired("u16", et.u16);
437 io.mapRequired("s64", et.s64);
438 io.mapRequired("s32", et.s32);
439 io.mapRequired("s16", et.s16);
440 io.mapRequired("f", et.f);
441 io.mapRequired("d", et.d);
448 // Test the reading of all endian scalar conversions
450 TEST(YAMLIO, TestReadEndianTypes) {
464 EXPECT_FALSE(yin.error());
465 EXPECT_EQ(map.u64, 5000000000ULL);
466 EXPECT_EQ(map.u32, 4000000000U);
467 EXPECT_EQ(map.u16, 65000);
468 EXPECT_EQ(map.s64, -5000000000LL);
469 EXPECT_EQ(map.s32, -2000000000L);
470 EXPECT_EQ(map.s16, -32000);
471 EXPECT_EQ(map.f, 3.25f);
472 EXPECT_EQ(map.d, -2.8625);
476 // Test writing then reading back all endian-aware scalar types
478 TEST(YAMLIO, TestReadWriteEndianTypes) {
479 std::string intermediate;
482 map.u64 = 6000000000ULL;
483 map.u32 = 3000000000U;
485 map.s64 = -6000000000LL;
486 map.s32 = -2000000000;
491 llvm::raw_string_ostream ostr(intermediate);
497 Input yin(intermediate);
501 EXPECT_FALSE(yin.error());
502 EXPECT_EQ(map.u64, 6000000000ULL);
503 EXPECT_EQ(map.u32, 3000000000U);
504 EXPECT_EQ(map.u16, 50000);
505 EXPECT_EQ(map.s64, -6000000000LL);
506 EXPECT_EQ(map.s32, -2000000000L);
507 EXPECT_EQ(map.s16, -32000);
508 EXPECT_EQ(map.f, 3.25f);
509 EXPECT_EQ(map.d, -2.8625);
514 llvm::StringRef str1;
515 llvm::StringRef str2;
516 llvm::StringRef str3;
517 llvm::StringRef str4;
518 llvm::StringRef str5;
519 llvm::StringRef str6;
520 llvm::StringRef str7;
521 llvm::StringRef str8;
522 llvm::StringRef str9;
523 llvm::StringRef str10;
524 llvm::StringRef str11;
534 std::string stdstr10;
535 std::string stdstr11;
541 struct MappingTraits<StringTypes> {
542 static void mapping(IO &io, StringTypes& st) {
543 io.mapRequired("str1", st.str1);
544 io.mapRequired("str2", st.str2);
545 io.mapRequired("str3", st.str3);
546 io.mapRequired("str4", st.str4);
547 io.mapRequired("str5", st.str5);
548 io.mapRequired("str6", st.str6);
549 io.mapRequired("str7", st.str7);
550 io.mapRequired("str8", st.str8);
551 io.mapRequired("str9", st.str9);
552 io.mapRequired("str10", st.str10);
553 io.mapRequired("str11", st.str11);
554 io.mapRequired("stdstr1", st.stdstr1);
555 io.mapRequired("stdstr2", st.stdstr2);
556 io.mapRequired("stdstr3", st.stdstr3);
557 io.mapRequired("stdstr4", st.stdstr4);
558 io.mapRequired("stdstr5", st.stdstr5);
559 io.mapRequired("stdstr6", st.stdstr6);
560 io.mapRequired("stdstr7", st.stdstr7);
561 io.mapRequired("stdstr8", st.stdstr8);
562 io.mapRequired("stdstr9", st.stdstr9);
563 io.mapRequired("stdstr10", st.stdstr10);
564 io.mapRequired("stdstr11", st.stdstr11);
570 TEST(YAMLIO, TestReadWriteStringTypes) {
571 std::string intermediate;
579 map.str6 = "0000000004000000";
583 map.str10 = "0.2e20";
585 map.stdstr1 = "'eee";
586 map.stdstr2 = "\"fff";
587 map.stdstr3 = "`ggg";
588 map.stdstr4 = "@hhh";
590 map.stdstr6 = "0000000004000000";
591 map.stdstr7 = "true";
592 map.stdstr8 = "FALSE";
594 map.stdstr10 = "0.2e20";
595 map.stdstr11 = "0x30";
597 llvm::raw_string_ostream ostr(intermediate);
602 llvm::StringRef flowOut(intermediate);
603 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
604 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
605 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
606 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
607 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
608 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
609 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
610 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
611 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
612 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
613 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
614 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
615 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
616 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
617 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
618 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
619 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
622 Input yin(intermediate);
626 EXPECT_FALSE(yin.error());
627 EXPECT_TRUE(map.str1.equals("'aaa"));
628 EXPECT_TRUE(map.str2.equals("\"bbb"));
629 EXPECT_TRUE(map.str3.equals("`ccc"));
630 EXPECT_TRUE(map.str4.equals("@ddd"));
631 EXPECT_TRUE(map.str5.equals(""));
632 EXPECT_TRUE(map.str6.equals("0000000004000000"));
633 EXPECT_TRUE(map.stdstr1 == "'eee");
634 EXPECT_TRUE(map.stdstr2 == "\"fff");
635 EXPECT_TRUE(map.stdstr3 == "`ggg");
636 EXPECT_TRUE(map.stdstr4 == "@hhh");
637 EXPECT_TRUE(map.stdstr5 == "");
638 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
642 //===----------------------------------------------------------------------===//
643 // Test ScalarEnumerationTraits
644 //===----------------------------------------------------------------------===//
665 struct ScalarEnumerationTraits<Colors> {
666 static void enumeration(IO &io, Colors &value) {
667 io.enumCase(value, "red", cRed);
668 io.enumCase(value, "blue", cBlue);
669 io.enumCase(value, "green", cGreen);
670 io.enumCase(value, "yellow",cYellow);
674 struct MappingTraits<ColorMap> {
675 static void mapping(IO &io, ColorMap& c) {
676 io.mapRequired("c1", c.c1);
677 io.mapRequired("c2", c.c2);
678 io.mapRequired("c3", c.c3);
679 io.mapOptional("c4", c.c4, cBlue); // supplies default
680 io.mapOptional("c5", c.c5, cYellow); // supplies default
681 io.mapOptional("c6", c.c6, cRed); // supplies default
689 // Test reading enumerated scalars
691 TEST(YAMLIO, TestEnumRead) {
701 EXPECT_FALSE(yin.error());
702 EXPECT_EQ(cBlue, map.c1);
703 EXPECT_EQ(cRed, map.c2);
704 EXPECT_EQ(cGreen, map.c3);
705 EXPECT_EQ(cBlue, map.c4); // tests default
706 EXPECT_EQ(cYellow,map.c5); // tests overridden
707 EXPECT_EQ(cRed, map.c6); // tests default
712 //===----------------------------------------------------------------------===//
713 // Test ScalarBitSetTraits
714 //===----------------------------------------------------------------------===//
723 inline MyFlags operator|(MyFlags a, MyFlags b) {
724 return static_cast<MyFlags>(
725 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
739 struct ScalarBitSetTraits<MyFlags> {
740 static void bitset(IO &io, MyFlags &value) {
741 io.bitSetCase(value, "big", flagBig);
742 io.bitSetCase(value, "flat", flagFlat);
743 io.bitSetCase(value, "round", flagRound);
744 io.bitSetCase(value, "pointy",flagPointy);
748 struct MappingTraits<FlagsMap> {
749 static void mapping(IO &io, FlagsMap& c) {
750 io.mapRequired("f1", c.f1);
751 io.mapRequired("f2", c.f2);
752 io.mapRequired("f3", c.f3);
753 io.mapOptional("f4", c.f4, MyFlags(flagRound));
761 // Test reading flow sequence representing bit-mask values
763 TEST(YAMLIO, TestFlagsRead) {
767 "f2: [ round, flat ]\n"
772 EXPECT_FALSE(yin.error());
773 EXPECT_EQ(flagBig, map.f1);
774 EXPECT_EQ(flagRound|flagFlat, map.f2);
775 EXPECT_EQ(flagNone, map.f3); // check empty set
776 EXPECT_EQ(flagRound, map.f4); // check optional key
781 // Test writing then reading back bit-mask values
783 TEST(YAMLIO, TestReadWriteFlags) {
784 std::string intermediate;
788 map.f2 = flagRound | flagFlat;
792 llvm::raw_string_ostream ostr(intermediate);
798 Input yin(intermediate);
802 EXPECT_FALSE(yin.error());
803 EXPECT_EQ(flagBig, map2.f1);
804 EXPECT_EQ(flagRound|flagFlat, map2.f2);
805 EXPECT_EQ(flagNone, map2.f3);
806 //EXPECT_EQ(flagRound, map2.f4); // check optional key
812 //===----------------------------------------------------------------------===//
814 //===----------------------------------------------------------------------===//
816 struct MyCustomType {
821 struct MyCustomTypeMap {
831 struct MappingTraits<MyCustomTypeMap> {
832 static void mapping(IO &io, MyCustomTypeMap& s) {
833 io.mapRequired("f1", s.f1);
834 io.mapRequired("f2", s.f2);
835 io.mapRequired("f3", s.f3);
838 // MyCustomType is formatted as a yaml scalar. A value of
839 // {length=3, width=4} would be represented in yaml as "3 by 4".
841 struct ScalarTraits<MyCustomType> {
842 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
843 out << llvm::format("%d by %d", value.length, value.width);
845 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
846 size_t byStart = scalar.find("by");
847 if ( byStart != StringRef::npos ) {
848 StringRef lenStr = scalar.slice(0, byStart);
849 lenStr = lenStr.rtrim();
850 if ( lenStr.getAsInteger(0, value.length) ) {
851 return "malformed length";
853 StringRef widthStr = scalar.drop_front(byStart+2);
854 widthStr = widthStr.ltrim();
855 if ( widthStr.getAsInteger(0, value.width) ) {
856 return "malformed width";
861 return "malformed by";
864 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
871 // Test writing then reading back custom values
873 TEST(YAMLIO, TestReadWriteMyCustomType) {
874 std::string intermediate;
883 llvm::raw_string_ostream ostr(intermediate);
889 Input yin(intermediate);
890 MyCustomTypeMap map2;
893 EXPECT_FALSE(yin.error());
894 EXPECT_EQ(1, map2.f1.length);
895 EXPECT_EQ(4, map2.f1.width);
896 EXPECT_EQ(100, map2.f2.length);
897 EXPECT_EQ(400, map2.f2.width);
898 EXPECT_EQ(10, map2.f3);
903 //===----------------------------------------------------------------------===//
904 // Test BlockScalarTraits
905 //===----------------------------------------------------------------------===//
907 struct MultilineStringType {
911 struct MultilineStringTypeMap {
912 MultilineStringType name;
913 MultilineStringType description;
914 MultilineStringType ingredients;
915 MultilineStringType recipes;
916 MultilineStringType warningLabels;
917 MultilineStringType documentation;
924 struct MappingTraits<MultilineStringTypeMap> {
925 static void mapping(IO &io, MultilineStringTypeMap& s) {
926 io.mapRequired("name", s.name);
927 io.mapRequired("description", s.description);
928 io.mapRequired("ingredients", s.ingredients);
929 io.mapRequired("recipes", s.recipes);
930 io.mapRequired("warningLabels", s.warningLabels);
931 io.mapRequired("documentation", s.documentation);
932 io.mapRequired("price", s.price);
936 // MultilineStringType is formatted as a yaml block literal scalar. A value of
937 // "Hello\nWorld" would be represented in yaml as
942 struct BlockScalarTraits<MultilineStringType> {
943 static void output(const MultilineStringType &value, void *ctxt,
944 llvm::raw_ostream &out) {
947 static StringRef input(StringRef scalar, void *ctxt,
948 MultilineStringType &value) {
949 value.str = scalar.str();
956 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
959 // Test writing then reading back custom values
961 TEST(YAMLIO, TestReadWriteMultilineStringType) {
962 std::string intermediate;
964 MultilineStringTypeMap map;
965 map.name.str = "An Item";
966 map.description.str = "Hello\nWorld";
967 map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
968 map.recipes.str = "\n\nTest 1\n\n\n";
969 map.warningLabels.str = "";
970 map.documentation.str = "\n\n";
973 llvm::raw_string_ostream ostr(intermediate);
978 Input yin(intermediate);
979 MultilineStringTypeMap map2;
982 EXPECT_FALSE(yin.error());
983 EXPECT_EQ(map2.name.str, "An Item\n");
984 EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
985 EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
986 EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
987 EXPECT_TRUE(map2.warningLabels.str.empty());
988 EXPECT_TRUE(map2.documentation.str.empty());
989 EXPECT_EQ(map2.price, 350);
994 // Test writing then reading back custom values
996 TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
997 std::string intermediate;
999 std::vector<MultilineStringType> documents;
1000 MultilineStringType doc;
1001 doc.str = "Hello\nWorld";
1002 documents.push_back(doc);
1004 llvm::raw_string_ostream ostr(intermediate);
1008 // Verify that the block scalar header was written out on the same line
1009 // as the document marker.
1010 EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
1013 Input yin(intermediate);
1014 std::vector<MultilineStringType> documents2;
1017 EXPECT_FALSE(yin.error());
1018 EXPECT_EQ(documents2.size(), size_t(1));
1019 EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
1023 TEST(YAMLIO, TestReadWriteBlockScalarValue) {
1024 std::string intermediate;
1026 MultilineStringType doc;
1027 doc.str = "Just a block\nscalar doc";
1029 llvm::raw_string_ostream ostr(intermediate);
1034 Input yin(intermediate);
1035 MultilineStringType doc;
1038 EXPECT_FALSE(yin.error());
1039 EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
1043 //===----------------------------------------------------------------------===//
1044 // Test flow sequences
1045 //===----------------------------------------------------------------------===//
1047 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
1048 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
1049 LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef, MyString)
1050 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString)
1055 struct ScalarTraits<MyNumber> {
1056 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
1060 static StringRef input(StringRef scalar, void *, MyNumber &value) {
1062 if ( getAsSignedInteger(scalar, 0, n) )
1063 return "invalid number";
1068 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1071 template <> struct ScalarTraits<MyString> {
1072 using Impl = ScalarTraits<StringRef>;
1073 static void output(const MyString &V, void *Ctx, raw_ostream &OS) {
1074 Impl::output(V, Ctx, OS);
1076 static StringRef input(StringRef S, void *Ctx, MyString &V) {
1077 return Impl::input(S, Ctx, V.value);
1079 static QuotingType mustQuote(StringRef S) {
1080 return Impl::mustQuote(S);
1086 struct NameAndNumbers {
1087 llvm::StringRef name;
1088 std::vector<MyString> strings;
1089 std::vector<MyNumber> single;
1090 std::vector<MyNumber> numbers;
1096 struct MappingTraits<NameAndNumbers> {
1097 static void mapping(IO &io, NameAndNumbers& nn) {
1098 io.mapRequired("name", nn.name);
1099 io.mapRequired("strings", nn.strings);
1100 io.mapRequired("single", nn.single);
1101 io.mapRequired("numbers", nn.numbers);
1107 typedef std::vector<MyNumber> MyNumberFlowSequence;
1109 LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1111 struct NameAndNumbersFlow {
1112 llvm::StringRef name;
1113 std::vector<MyNumberFlowSequence> sequenceOfNumbers;
1119 struct MappingTraits<NameAndNumbersFlow> {
1120 static void mapping(IO &io, NameAndNumbersFlow& nn) {
1121 io.mapRequired("name", nn.name);
1122 io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
1129 // Test writing then reading back custom values
1131 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1132 std::string intermediate;
1136 map.strings.push_back(llvm::StringRef("one"));
1137 map.strings.push_back(llvm::StringRef("two"));
1138 map.single.push_back(1);
1139 map.numbers.push_back(10);
1140 map.numbers.push_back(-30);
1141 map.numbers.push_back(1024);
1143 llvm::raw_string_ostream ostr(intermediate);
1147 // Verify sequences were written in flow style
1149 llvm::StringRef flowOut(intermediate);
1150 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1151 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
1155 Input yin(intermediate);
1156 NameAndNumbers map2;
1159 EXPECT_FALSE(yin.error());
1160 EXPECT_TRUE(map2.name.equals("hello"));
1161 EXPECT_EQ(map2.strings.size(), 2UL);
1162 EXPECT_TRUE(map2.strings[0].value.equals("one"));
1163 EXPECT_TRUE(map2.strings[1].value.equals("two"));
1164 EXPECT_EQ(map2.single.size(), 1UL);
1165 EXPECT_EQ(1, map2.single[0]);
1166 EXPECT_EQ(map2.numbers.size(), 3UL);
1167 EXPECT_EQ(10, map2.numbers[0]);
1168 EXPECT_EQ(-30, map2.numbers[1]);
1169 EXPECT_EQ(1024, map2.numbers[2]);
1175 // Test writing then reading back a sequence of flow sequences.
1177 TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1178 std::string intermediate;
1180 NameAndNumbersFlow map;
1182 MyNumberFlowSequence single = { 0 };
1183 MyNumberFlowSequence numbers = { 12, 1, -512 };
1184 map.sequenceOfNumbers.push_back(single);
1185 map.sequenceOfNumbers.push_back(numbers);
1186 map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1188 llvm::raw_string_ostream ostr(intermediate);
1192 // Verify sequences were written in flow style
1193 // and that the parent sequence used '-'.
1195 llvm::StringRef flowOut(intermediate);
1196 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1197 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1198 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
1202 Input yin(intermediate);
1203 NameAndNumbersFlow map2;
1206 EXPECT_FALSE(yin.error());
1207 EXPECT_TRUE(map2.name.equals("hello"));
1208 EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1209 EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1210 EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
1211 EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1212 EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
1213 EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
1214 EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1215 EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1219 //===----------------------------------------------------------------------===//
1220 // Test normalizing/denormalizing
1221 //===----------------------------------------------------------------------===//
1223 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1225 typedef std::vector<TotalSeconds> SecondsSequence;
1227 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
1233 struct MappingTraits<TotalSeconds> {
1235 class NormalizedSeconds {
1237 NormalizedSeconds(IO &io)
1238 : hours(0), minutes(0), seconds(0) {
1240 NormalizedSeconds(IO &, TotalSeconds &secs)
1242 minutes((secs - (hours*3600))/60),
1243 seconds(secs % 60) {
1245 TotalSeconds denormalize(IO &) {
1246 return TotalSeconds(hours*3600 + minutes*60 + seconds);
1254 static void mapping(IO &io, TotalSeconds &secs) {
1255 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1257 io.mapOptional("hours", keys->hours, (uint32_t)0);
1258 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
1259 io.mapRequired("seconds", keys->seconds);
1267 // Test the reading of a yaml sequence of mappings
1269 TEST(YAMLIO, TestReadMySecondsSequence) {
1270 SecondsSequence seq;
1271 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1274 EXPECT_FALSE(yin.error());
1275 EXPECT_EQ(seq.size(), 2UL);
1276 EXPECT_EQ(seq[0], 3605U);
1277 EXPECT_EQ(seq[1], 59U);
1282 // Test writing then reading back custom values
1284 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1285 std::string intermediate;
1287 SecondsSequence seq;
1288 seq.push_back(4000);
1292 llvm::raw_string_ostream ostr(intermediate);
1297 Input yin(intermediate);
1298 SecondsSequence seq2;
1301 EXPECT_FALSE(yin.error());
1302 EXPECT_EQ(seq2.size(), 3UL);
1303 EXPECT_EQ(seq2[0], 4000U);
1304 EXPECT_EQ(seq2[1], 500U);
1305 EXPECT_EQ(seq2[2], 59U);
1310 //===----------------------------------------------------------------------===//
1311 // Test dynamic typing
1312 //===----------------------------------------------------------------------===//
1331 struct KindAndFlags {
1332 KindAndFlags() : kind(kindA), flags(0) { }
1333 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1338 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1340 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
1345 struct ScalarEnumerationTraits<AFlags> {
1346 static void enumeration(IO &io, AFlags &value) {
1347 io.enumCase(value, "a1", a1);
1348 io.enumCase(value, "a2", a2);
1349 io.enumCase(value, "a3", a3);
1353 struct ScalarEnumerationTraits<BFlags> {
1354 static void enumeration(IO &io, BFlags &value) {
1355 io.enumCase(value, "b1", b1);
1356 io.enumCase(value, "b2", b2);
1357 io.enumCase(value, "b3", b3);
1361 struct ScalarEnumerationTraits<Kind> {
1362 static void enumeration(IO &io, Kind &value) {
1363 io.enumCase(value, "A", kindA);
1364 io.enumCase(value, "B", kindB);
1368 struct MappingTraits<KindAndFlags> {
1369 static void mapping(IO &io, KindAndFlags& kf) {
1370 io.mapRequired("kind", kf.kind);
1371 // Type of "flags" field varies depending on "kind" field.
1372 // Use memcpy here to avoid breaking strict aliasing rules.
1373 if (kf.kind == kindA) {
1374 AFlags aflags = static_cast<AFlags>(kf.flags);
1375 io.mapRequired("flags", aflags);
1378 BFlags bflags = static_cast<BFlags>(kf.flags);
1379 io.mapRequired("flags", bflags);
1389 // Test the reading of a yaml sequence dynamic types
1391 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1392 KindAndFlagsSequence seq;
1393 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1396 EXPECT_FALSE(yin.error());
1397 EXPECT_EQ(seq.size(), 2UL);
1398 EXPECT_EQ(seq[0].kind, kindA);
1399 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
1400 EXPECT_EQ(seq[1].kind, kindB);
1401 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
1405 // Test writing then reading back dynamic types
1407 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1408 std::string intermediate;
1410 KindAndFlagsSequence seq;
1411 seq.push_back(KindAndFlags(kindA,a1));
1412 seq.push_back(KindAndFlags(kindB,b1));
1413 seq.push_back(KindAndFlags(kindA,a2));
1414 seq.push_back(KindAndFlags(kindB,b2));
1415 seq.push_back(KindAndFlags(kindA,a3));
1417 llvm::raw_string_ostream ostr(intermediate);
1422 Input yin(intermediate);
1423 KindAndFlagsSequence seq2;
1426 EXPECT_FALSE(yin.error());
1427 EXPECT_EQ(seq2.size(), 5UL);
1428 EXPECT_EQ(seq2[0].kind, kindA);
1429 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
1430 EXPECT_EQ(seq2[1].kind, kindB);
1431 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
1432 EXPECT_EQ(seq2[2].kind, kindA);
1433 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
1434 EXPECT_EQ(seq2[3].kind, kindB);
1435 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
1436 EXPECT_EQ(seq2[4].kind, kindA);
1437 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
1442 //===----------------------------------------------------------------------===//
1443 // Test document list
1444 //===----------------------------------------------------------------------===//
1450 typedef std::vector<FooBarMap> FooBarMapDocumentList;
1452 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1458 struct MappingTraits<FooBarMap> {
1459 static void mapping(IO &io, FooBarMap& fb) {
1460 io.mapRequired("foo", fb.foo);
1461 io.mapRequired("bar", fb.bar);
1469 // Test the reading of a yaml mapping
1471 TEST(YAMLIO, TestDocRead) {
1473 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1476 EXPECT_FALSE(yin.error());
1477 EXPECT_EQ(doc.foo, 3);
1478 EXPECT_EQ(doc.bar,5);
1484 // Test writing then reading back a sequence of mappings
1486 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1487 std::string intermediate;
1495 std::vector<FooBarMap> docList;
1496 docList.push_back(doc1);
1497 docList.push_back(doc2);
1499 llvm::raw_string_ostream ostr(intermediate);
1506 Input yin(intermediate);
1507 std::vector<FooBarMap> docList2;
1510 EXPECT_FALSE(yin.error());
1511 EXPECT_EQ(docList2.size(), 2UL);
1512 FooBarMap& map1 = docList2[0];
1513 FooBarMap& map2 = docList2[1];
1514 EXPECT_EQ(map1.foo, 10);
1515 EXPECT_EQ(map1.bar, -3);
1516 EXPECT_EQ(map2.foo, 257);
1517 EXPECT_EQ(map2.bar, 0);
1521 //===----------------------------------------------------------------------===//
1522 // Test document tags
1523 //===----------------------------------------------------------------------===//
1526 MyDouble() : value(0.0) { }
1527 MyDouble(double x) : value(x) { }
1531 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1537 struct MappingTraits<MyDouble> {
1538 static void mapping(IO &io, MyDouble &d) {
1539 if (io.mapTag("!decimal", true)) {
1540 mappingDecimal(io, d);
1541 } else if (io.mapTag("!fraction")) {
1542 mappingFraction(io, d);
1545 static void mappingDecimal(IO &io, MyDouble &d) {
1546 io.mapRequired("value", d.value);
1548 static void mappingFraction(IO &io, MyDouble &d) {
1550 io.mapRequired("numerator", num);
1551 io.mapRequired("denominator", denom);
1552 // convert fraction to double
1553 d.value = num/denom;
1561 // Test the reading of two different tagged yaml documents.
1563 TEST(YAMLIO, TestTaggedDocuments) {
1564 std::vector<MyDouble> docList;
1565 Input yin("--- !decimal\nvalue: 3.0\n"
1566 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1568 EXPECT_FALSE(yin.error());
1569 EXPECT_EQ(docList.size(), 2UL);
1570 EXPECT_EQ(docList[0].value, 3.0);
1571 EXPECT_EQ(docList[1].value, 4.5);
1577 // Test writing then reading back tagged documents
1579 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1580 std::string intermediate;
1584 std::vector<MyDouble> docList;
1585 docList.push_back(a);
1586 docList.push_back(b);
1588 llvm::raw_string_ostream ostr(intermediate);
1594 Input yin(intermediate);
1595 std::vector<MyDouble> docList2;
1598 EXPECT_FALSE(yin.error());
1599 EXPECT_EQ(docList2.size(), 2UL);
1600 EXPECT_EQ(docList2[0].value, 10.25);
1601 EXPECT_EQ(docList2[1].value, -3.75);
1606 //===----------------------------------------------------------------------===//
1607 // Test mapping validation
1608 //===----------------------------------------------------------------------===//
1610 struct MyValidation {
1614 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1619 struct MappingTraits<MyValidation> {
1620 static void mapping(IO &io, MyValidation &d) {
1621 io.mapRequired("value", d.value);
1623 static StringRef validate(IO &io, MyValidation &d) {
1625 return "negative value";
1634 // Test that validate() is called and complains about the negative value.
1636 TEST(YAMLIO, TestValidatingInput) {
1637 std::vector<MyValidation> docList;
1638 Input yin("--- \nvalue: 3.0\n"
1639 "--- \nvalue: -1.0\n...\n",
1640 nullptr, suppressErrorMessages);
1642 EXPECT_TRUE(!!yin.error());
1645 //===----------------------------------------------------------------------===//
1646 // Test flow mapping
1647 //===----------------------------------------------------------------------===//
1653 FlowFooBar() : foo(0), bar(0) {}
1654 FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1657 typedef std::vector<FlowFooBar> FlowFooBarSequence;
1659 LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1661 struct FlowFooBarDoc {
1662 FlowFooBar attribute;
1663 FlowFooBarSequence seq;
1669 struct MappingTraits<FlowFooBar> {
1670 static void mapping(IO &io, FlowFooBar &fb) {
1671 io.mapRequired("foo", fb.foo);
1672 io.mapRequired("bar", fb.bar);
1675 static const bool flow = true;
1679 struct MappingTraits<FlowFooBarDoc> {
1680 static void mapping(IO &io, FlowFooBarDoc &fb) {
1681 io.mapRequired("attribute", fb.attribute);
1682 io.mapRequired("seq", fb.seq);
1689 // Test writing then reading back custom mappings
1691 TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1692 std::string intermediate;
1695 doc.attribute = FlowFooBar(42, 907);
1696 doc.seq.push_back(FlowFooBar(1, 2));
1697 doc.seq.push_back(FlowFooBar(0, 0));
1698 doc.seq.push_back(FlowFooBar(-1, 1024));
1700 llvm::raw_string_ostream ostr(intermediate);
1704 // Verify that mappings were written in flow style
1706 llvm::StringRef flowOut(intermediate);
1707 EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1708 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1709 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1710 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1714 Input yin(intermediate);
1718 EXPECT_FALSE(yin.error());
1719 EXPECT_EQ(doc2.attribute.foo, 42);
1720 EXPECT_EQ(doc2.attribute.bar, 907);
1721 EXPECT_EQ(doc2.seq.size(), 3UL);
1722 EXPECT_EQ(doc2.seq[0].foo, 1);
1723 EXPECT_EQ(doc2.seq[0].bar, 2);
1724 EXPECT_EQ(doc2.seq[1].foo, 0);
1725 EXPECT_EQ(doc2.seq[1].bar, 0);
1726 EXPECT_EQ(doc2.seq[2].foo, -1);
1727 EXPECT_EQ(doc2.seq[2].bar, 1024);
1731 //===----------------------------------------------------------------------===//
1732 // Test error handling
1733 //===----------------------------------------------------------------------===//
1736 // Test error handling of unknown enumerated scalar
1738 TEST(YAMLIO, TestColorsReadError) {
1746 suppressErrorMessages);
1748 EXPECT_TRUE(!!yin.error());
1753 // Test error handling of flow sequence with unknown value
1755 TEST(YAMLIO, TestFlagsReadError) {
1759 "f2: [ round, hollow ]\n"
1763 suppressErrorMessages);
1766 EXPECT_TRUE(!!yin.error());
1771 // Test error handling reading built-in uint8_t type
1773 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1774 std::vector<uint8_t> seq;
1781 suppressErrorMessages);
1784 EXPECT_TRUE(!!yin.error());
1789 // Test error handling reading built-in uint16_t type
1791 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1792 std::vector<uint16_t> seq;
1799 suppressErrorMessages);
1802 EXPECT_TRUE(!!yin.error());
1807 // Test error handling reading built-in uint32_t type
1809 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1810 std::vector<uint32_t> seq;
1817 suppressErrorMessages);
1820 EXPECT_TRUE(!!yin.error());
1825 // Test error handling reading built-in uint64_t type
1827 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1828 std::vector<uint64_t> seq;
1830 "- 18446744073709551615\n"
1832 "- 19446744073709551615\n"
1835 suppressErrorMessages);
1838 EXPECT_TRUE(!!yin.error());
1843 // Test error handling reading built-in int8_t type
1845 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1846 std::vector<int8_t> seq;
1854 suppressErrorMessages);
1857 EXPECT_TRUE(!!yin.error());
1861 // Test error handling reading built-in int8_t type
1863 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1864 std::vector<int8_t> seq;
1872 suppressErrorMessages);
1875 EXPECT_TRUE(!!yin.error());
1880 // Test error handling reading built-in int16_t type
1882 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1883 std::vector<int16_t> seq;
1891 suppressErrorMessages);
1894 EXPECT_TRUE(!!yin.error());
1899 // Test error handling reading built-in int16_t type
1901 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1902 std::vector<int16_t> seq;
1910 suppressErrorMessages);
1913 EXPECT_TRUE(!!yin.error());
1918 // Test error handling reading built-in int32_t type
1920 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1921 std::vector<int32_t> seq;
1929 suppressErrorMessages);
1932 EXPECT_TRUE(!!yin.error());
1936 // Test error handling reading built-in int32_t type
1938 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1939 std::vector<int32_t> seq;
1947 suppressErrorMessages);
1950 EXPECT_TRUE(!!yin.error());
1955 // Test error handling reading built-in int64_t type
1957 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1958 std::vector<int64_t> seq;
1960 "- -9223372036854775808\n"
1962 "- 9223372036854775807\n"
1963 "- -9223372036854775809\n"
1966 suppressErrorMessages);
1969 EXPECT_TRUE(!!yin.error());
1973 // Test error handling reading built-in int64_t type
1975 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1976 std::vector<int64_t> seq;
1978 "- -9223372036854775808\n"
1980 "- 9223372036854775807\n"
1981 "- 9223372036854775809\n"
1984 suppressErrorMessages);
1987 EXPECT_TRUE(!!yin.error());
1991 // Test error handling reading built-in float type
1993 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1994 std::vector<float> seq;
2002 suppressErrorMessages);
2005 EXPECT_TRUE(!!yin.error());
2009 // Test error handling reading built-in float type
2011 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
2012 std::vector<double> seq;
2020 suppressErrorMessages);
2023 EXPECT_TRUE(!!yin.error());
2027 // Test error handling reading built-in Hex8 type
2029 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
2030 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
2031 std::vector<Hex8> seq;
2038 suppressErrorMessages);
2041 EXPECT_TRUE(!!yin.error());
2046 // Test error handling reading built-in Hex16 type
2048 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
2049 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
2050 std::vector<Hex16> seq;
2057 suppressErrorMessages);
2060 EXPECT_TRUE(!!yin.error());
2064 // Test error handling reading built-in Hex32 type
2066 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
2067 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
2068 std::vector<Hex32> seq;
2075 suppressErrorMessages);
2078 EXPECT_TRUE(!!yin.error());
2082 // Test error handling reading built-in Hex64 type
2084 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
2085 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
2086 std::vector<Hex64> seq;
2089 "- 0xFFEEDDCCBBAA9988\n"
2090 "- 0x12345567890ABCDEF0\n"
2093 suppressErrorMessages);
2096 EXPECT_TRUE(!!yin.error());
2099 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2102 // We pass the suppressErrorMessages handler to handle the error
2103 // message generated in the constructor of Input.
2104 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
2106 EXPECT_TRUE(!!yin.error());
2110 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
2112 EXPECT_TRUE(!!yin.error());
2116 struct OptionalTest {
2117 std::vector<int> Numbers;
2120 struct OptionalTestSeq {
2121 std::vector<OptionalTest> Tests;
2124 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
2128 struct MappingTraits<OptionalTest> {
2129 static void mapping(IO& IO, OptionalTest &OT) {
2130 IO.mapOptional("Numbers", OT.Numbers);
2135 struct MappingTraits<OptionalTestSeq> {
2136 static void mapping(IO &IO, OptionalTestSeq &OTS) {
2137 IO.mapOptional("Tests", OTS.Tests);
2143 TEST(YAMLIO, SequenceElideTest) {
2144 // Test that writing out a purely optional structure with its fields set to
2145 // default followed by other data is properly read back in.
2146 OptionalTestSeq Seq;
2147 OptionalTest One, Two, Three, Four;
2148 int N[] = {1, 2, 3};
2149 Three.Numbers.assign(N, N + 3);
2150 Seq.Tests.push_back(One);
2151 Seq.Tests.push_back(Two);
2152 Seq.Tests.push_back(Three);
2153 Seq.Tests.push_back(Four);
2155 std::string intermediate;
2157 llvm::raw_string_ostream ostr(intermediate);
2162 Input yin(intermediate);
2163 OptionalTestSeq Seq2;
2166 EXPECT_FALSE(yin.error());
2168 EXPECT_EQ(4UL, Seq2.Tests.size());
2170 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2171 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2173 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2174 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2175 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2177 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2180 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2184 EXPECT_TRUE(!!yin.error());
2187 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2191 EXPECT_FALSE(yin.error());
2194 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2195 std::vector<uint8_t> seq;
2196 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
2199 EXPECT_FALSE(yin.error());
2200 EXPECT_TRUE(seq.empty());
2204 llvm::StringRef str1, str2, str3;
2205 FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2206 : str1(str1), str2(str2), str3(str3) {}
2210 llvm::StringRef str;
2211 FlowSeq(llvm::StringRef S) : str(S) {}
2212 FlowSeq() = default;
2218 struct MappingTraits<FlowMap> {
2219 static void mapping(IO &io, FlowMap &fm) {
2220 io.mapRequired("str1", fm.str1);
2221 io.mapRequired("str2", fm.str2);
2222 io.mapRequired("str3", fm.str3);
2225 static const bool flow = true;
2229 struct ScalarTraits<FlowSeq> {
2230 static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2233 static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2238 static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
2243 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2245 TEST(YAMLIO, TestWrapFlow) {
2247 llvm::raw_string_ostream ostr(out);
2248 FlowMap Map("This is str1", "This is str2", "This is str3");
2249 std::vector<FlowSeq> Seq;
2250 Seq.emplace_back("This is str1");
2251 Seq.emplace_back("This is str2");
2252 Seq.emplace_back("This is str3");
2255 // 20 is just bellow the total length of the first mapping field.
2256 // We should wreap at every element.
2257 Output yout(ostr, nullptr, 15);
2263 "{ str1: This is str1, \n"
2264 " str2: This is str2, \n"
2265 " str3: This is str3 }\n"
2273 "[ This is str1, \n"
2280 // 25 will allow the second field to be output on the first line.
2281 Output yout(ostr, nullptr, 25);
2287 "{ str1: This is str1, str2: This is str2, \n"
2288 " str3: This is str3 }\n"
2296 "[ This is str1, This is str2, \n"
2302 // 0 means no wrapping.
2303 Output yout(ostr, nullptr, 0);
2309 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2317 "[ This is str1, This is str2, This is str3 ]\n"
2323 struct MappingContext {
2332 NestedMap(MappingContext &Context) : Context(Context) {}
2334 MappingContext &Context;
2339 template <> struct MappingContextTraits<SimpleMap, MappingContext> {
2340 static void mapping(IO &io, SimpleMap &sm, MappingContext &Context) {
2341 io.mapRequired("B", sm.B);
2342 io.mapRequired("C", sm.C);
2344 io.mapRequired("Context", Context.A);
2348 template <> struct MappingTraits<NestedMap> {
2349 static void mapping(IO &io, NestedMap &nm) {
2350 io.mapRequired("Simple", nm.Simple, nm.Context);
2356 TEST(YAMLIO, TestMapWithContext) {
2357 MappingContext Context;
2358 NestedMap Nested(Context);
2360 llvm::raw_string_ostream ostr(out);
2362 Output yout(ostr, nullptr, 15);
2366 EXPECT_EQ(1, Context.A);
2377 Nested.Simple.B = 2;
2378 Nested.Simple.C = 3;
2381 EXPECT_EQ(2, Context.A);
2392 LLVM_YAML_IS_STRING_MAP(int)
2394 TEST(YAMLIO, TestCustomMapping) {
2395 std::map<std::string, int> x;
2400 llvm::raw_string_ostream ostr(out);
2401 Output xout(ostr, nullptr, 0);
2412 std::map<std::string, int> y;
2414 EXPECT_EQ(2ul, y.size());
2415 EXPECT_EQ(1, y["foo"]);
2416 EXPECT_EQ(2, y["bar"]);
2419 LLVM_YAML_IS_STRING_MAP(FooBar)
2421 TEST(YAMLIO, TestCustomMappingStruct) {
2422 std::map<std::string, FooBar> x;
2429 llvm::raw_string_ostream ostr(out);
2430 Output xout(ostr, nullptr, 0);
2445 std::map<std::string, FooBar> y;
2447 EXPECT_EQ(2ul, y.size());
2448 EXPECT_EQ(1, y["foo"].foo);
2449 EXPECT_EQ(2, y["foo"].bar);
2450 EXPECT_EQ(3, y["bar"].foo);
2451 EXPECT_EQ(4, y["bar"].bar);
2454 static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
2456 llvm::raw_string_ostream ostr(out);
2457 Output xout(ostr, nullptr, 0);
2459 llvm::yaml::EmptyContext Ctx;
2460 yamlize(xout, Input, true, Ctx);
2463 EXPECT_EQ(Expected, out);
2466 TEST(YAMLIO, TestEscaped) {
2468 TestEscaped("@abc@", "'@abc@'");
2470 TestEscaped("abc/", "abc/");
2471 // Double quote non-printable
2472 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2473 // Double quote inside single quote
2474 TestEscaped("abc\"fdf", "'abc\"fdf'");
2475 // Double quote inside double quote
2476 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2477 // Single quote inside single quote
2478 TestEscaped("abc'fdf", "'abc''fdf'");
2480 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2481 // UTF8 with single quote inside double quote
2482 TestEscaped("parameter 'параметр' is unused",
2483 "\"parameter 'параметр' is unused\"");