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/SmallString.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/YAMLTraits.h"
15 #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);
71 struct FooBarOptional {
78 template <> struct MappingTraits<FooBarOptional> {
79 static void mapping(IO &YamlIO, FooBarOptional &Obj) {
80 YamlIO.mapRequired("foo", Obj.Foo);
81 YamlIO.mapOptional("bar", Obj.Bar);
88 // Test the reading of a yaml mapping
90 TEST(YAMLIO, TestMapRead) {
93 Input yin("---\nfoo: 3\nbar: 5\n...\n");
96 EXPECT_FALSE(yin.error());
97 EXPECT_EQ(doc.foo, 3);
98 EXPECT_EQ(doc.bar, 5);
102 Input yin("{foo: 3, bar: 5}");
105 EXPECT_FALSE(yin.error());
106 EXPECT_EQ(doc.foo, 3);
107 EXPECT_EQ(doc.bar, 5);
111 TEST(YAMLIO, TestMapReadOptional) {
115 Input In("---\nfoo: 3\n...\n");
118 EXPECT_FALSE(In.error());
119 EXPECT_EQ(Doc.Foo, 3);
120 EXPECT_EQ(Doc.Bar, 0);
124 TEST(YAMLIO, TestMalformedMapRead) {
126 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
128 EXPECT_TRUE(!!yin.error());
132 // Test the reading of a yaml sequence of mappings
134 TEST(YAMLIO, TestSequenceMapRead) {
136 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
139 EXPECT_FALSE(yin.error());
140 EXPECT_EQ(seq.size(), 2UL);
141 FooBar& map1 = seq[0];
142 FooBar& map2 = seq[1];
143 EXPECT_EQ(map1.foo, 3);
144 EXPECT_EQ(map1.bar, 5);
145 EXPECT_EQ(map2.foo, 7);
146 EXPECT_EQ(map2.bar, 9);
150 // Test the reading of a map containing a yaml sequence of mappings
152 TEST(YAMLIO, TestContainerSequenceMapRead) {
154 FooBarContainer cont;
155 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
158 EXPECT_FALSE(yin2.error());
159 EXPECT_EQ(cont.fbs.size(), 2UL);
160 EXPECT_EQ(cont.fbs[0].foo, 3);
161 EXPECT_EQ(cont.fbs[0].bar, 5);
162 EXPECT_EQ(cont.fbs[1].foo, 7);
163 EXPECT_EQ(cont.fbs[1].bar, 9);
167 FooBarContainer cont;
168 Input yin("---\nfbs:\n...\n");
170 // Okay: Empty node represents an empty array.
171 EXPECT_FALSE(yin.error());
172 EXPECT_EQ(cont.fbs.size(), 0UL);
176 FooBarContainer cont;
177 Input yin("---\nfbs: !!null null\n...\n");
179 // Okay: null represents an empty array.
180 EXPECT_FALSE(yin.error());
181 EXPECT_EQ(cont.fbs.size(), 0UL);
185 FooBarContainer cont;
186 Input yin("---\nfbs: ~\n...\n");
188 // Okay: null represents an empty array.
189 EXPECT_FALSE(yin.error());
190 EXPECT_EQ(cont.fbs.size(), 0UL);
194 FooBarContainer cont;
195 Input yin("---\nfbs: null\n...\n");
197 // Okay: null represents an empty array.
198 EXPECT_FALSE(yin.error());
199 EXPECT_EQ(cont.fbs.size(), 0UL);
204 // Test the reading of a map containing a malformed yaml sequence
206 TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
208 FooBarContainer cont;
209 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
210 suppressErrorMessages);
212 // Error: fbs is not a sequence.
213 EXPECT_TRUE(!!yin.error());
214 EXPECT_EQ(cont.fbs.size(), 0UL);
218 FooBarContainer cont;
219 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
221 // This should be an error.
222 EXPECT_TRUE(!!yin.error());
223 EXPECT_EQ(cont.fbs.size(), 0UL);
228 // Test writing then reading back a sequence of mappings
230 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
231 std::string intermediate;
240 seq.push_back(entry1);
241 seq.push_back(entry2);
243 llvm::raw_string_ostream ostr(intermediate);
249 Input yin(intermediate);
253 EXPECT_FALSE(yin.error());
254 EXPECT_EQ(seq2.size(), 2UL);
255 FooBar& map1 = seq2[0];
256 FooBar& map2 = seq2[1];
257 EXPECT_EQ(map1.foo, 10);
258 EXPECT_EQ(map1.bar, -3);
259 EXPECT_EQ(map2.foo, 257);
260 EXPECT_EQ(map2.bar, 0);
265 //===----------------------------------------------------------------------===//
266 // Test built-in types
267 //===----------------------------------------------------------------------===//
269 struct BuiltInTypes {
292 struct MappingTraits<BuiltInTypes> {
293 static void mapping(IO &io, BuiltInTypes& bt) {
294 io.mapRequired("str", bt.str);
295 io.mapRequired("stdstr", bt.stdstr);
296 io.mapRequired("u64", bt.u64);
297 io.mapRequired("u32", bt.u32);
298 io.mapRequired("u16", bt.u16);
299 io.mapRequired("u8", bt.u8);
300 io.mapRequired("b", bt.b);
301 io.mapRequired("s64", bt.s64);
302 io.mapRequired("s32", bt.s32);
303 io.mapRequired("s16", bt.s16);
304 io.mapRequired("s8", bt.s8);
305 io.mapRequired("f", bt.f);
306 io.mapRequired("d", bt.d);
307 io.mapRequired("h8", bt.h8);
308 io.mapRequired("h16", bt.h16);
309 io.mapRequired("h32", bt.h32);
310 io.mapRequired("h64", bt.h64);
318 // Test the reading of all built-in scalar conversions
320 TEST(YAMLIO, TestReadBuiltInTypes) {
324 "stdstr: hello where?\n"
339 "h64: 0xFEDCBA9876543210\n"
343 EXPECT_FALSE(yin.error());
344 EXPECT_TRUE(map.str.equals("hello there"));
345 EXPECT_TRUE(map.stdstr == "hello where?");
346 EXPECT_EQ(map.u64, 5000000000ULL);
347 EXPECT_EQ(map.u32, 4000000000U);
348 EXPECT_EQ(map.u16, 65000);
349 EXPECT_EQ(map.u8, 255);
350 EXPECT_EQ(map.b, false);
351 EXPECT_EQ(map.s64, -5000000000LL);
352 EXPECT_EQ(map.s32, -2000000000L);
353 EXPECT_EQ(map.s16, -32000);
354 EXPECT_EQ(map.s8, -127);
355 EXPECT_EQ(map.f, 137.125);
356 EXPECT_EQ(map.d, -2.8625);
357 EXPECT_EQ(map.h8, Hex8(255));
358 EXPECT_EQ(map.h16, Hex16(0x8765));
359 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
360 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
365 // Test writing then reading back all built-in scalar types
367 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
368 std::string intermediate;
372 map.stdstr = "three four";
373 map.u64 = 6000000000ULL;
374 map.u32 = 3000000000U;
378 map.s64 = -6000000000LL;
379 map.s32 = -2000000000;
386 map.h32 = 3000000000U;
387 map.h64 = 6000000000LL;
389 llvm::raw_string_ostream ostr(intermediate);
395 Input yin(intermediate);
399 EXPECT_FALSE(yin.error());
400 EXPECT_TRUE(map.str.equals("one two"));
401 EXPECT_TRUE(map.stdstr == "three four");
402 EXPECT_EQ(map.u64, 6000000000ULL);
403 EXPECT_EQ(map.u32, 3000000000U);
404 EXPECT_EQ(map.u16, 50000);
405 EXPECT_EQ(map.u8, 254);
406 EXPECT_EQ(map.b, true);
407 EXPECT_EQ(map.s64, -6000000000LL);
408 EXPECT_EQ(map.s32, -2000000000L);
409 EXPECT_EQ(map.s16, -32000);
410 EXPECT_EQ(map.s8, -128);
411 EXPECT_EQ(map.f, 3.25);
412 EXPECT_EQ(map.d, -2.8625);
413 EXPECT_EQ(map.h8, Hex8(254));
414 EXPECT_EQ(map.h16, Hex16(50000));
415 EXPECT_EQ(map.h32, Hex32(3000000000U));
416 EXPECT_EQ(map.h64, Hex64(6000000000LL));
421 llvm::StringRef str1;
422 llvm::StringRef str2;
423 llvm::StringRef str3;
424 llvm::StringRef str4;
425 llvm::StringRef str5;
426 llvm::StringRef str6;
427 llvm::StringRef str7;
428 llvm::StringRef str8;
429 llvm::StringRef str9;
430 llvm::StringRef str10;
431 llvm::StringRef str11;
441 std::string stdstr10;
442 std::string stdstr11;
448 struct MappingTraits<StringTypes> {
449 static void mapping(IO &io, StringTypes& st) {
450 io.mapRequired("str1", st.str1);
451 io.mapRequired("str2", st.str2);
452 io.mapRequired("str3", st.str3);
453 io.mapRequired("str4", st.str4);
454 io.mapRequired("str5", st.str5);
455 io.mapRequired("str6", st.str6);
456 io.mapRequired("str7", st.str7);
457 io.mapRequired("str8", st.str8);
458 io.mapRequired("str9", st.str9);
459 io.mapRequired("str10", st.str10);
460 io.mapRequired("str11", st.str11);
461 io.mapRequired("stdstr1", st.stdstr1);
462 io.mapRequired("stdstr2", st.stdstr2);
463 io.mapRequired("stdstr3", st.stdstr3);
464 io.mapRequired("stdstr4", st.stdstr4);
465 io.mapRequired("stdstr5", st.stdstr5);
466 io.mapRequired("stdstr6", st.stdstr6);
467 io.mapRequired("stdstr7", st.stdstr7);
468 io.mapRequired("stdstr8", st.stdstr8);
469 io.mapRequired("stdstr9", st.stdstr9);
470 io.mapRequired("stdstr10", st.stdstr10);
471 io.mapRequired("stdstr11", st.stdstr11);
477 TEST(YAMLIO, TestReadWriteStringTypes) {
478 std::string intermediate;
486 map.str6 = "0000000004000000";
490 map.str10 = "0.2e20";
492 map.stdstr1 = "'eee";
493 map.stdstr2 = "\"fff";
494 map.stdstr3 = "`ggg";
495 map.stdstr4 = "@hhh";
497 map.stdstr6 = "0000000004000000";
498 map.stdstr7 = "true";
499 map.stdstr8 = "FALSE";
501 map.stdstr10 = "0.2e20";
502 map.stdstr11 = "0x30";
504 llvm::raw_string_ostream ostr(intermediate);
509 llvm::StringRef flowOut(intermediate);
510 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
511 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
512 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
513 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
514 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
515 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
516 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
517 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
518 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
519 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
520 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
521 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
522 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
523 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
524 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
525 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
526 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
529 Input yin(intermediate);
533 EXPECT_FALSE(yin.error());
534 EXPECT_TRUE(map.str1.equals("'aaa"));
535 EXPECT_TRUE(map.str2.equals("\"bbb"));
536 EXPECT_TRUE(map.str3.equals("`ccc"));
537 EXPECT_TRUE(map.str4.equals("@ddd"));
538 EXPECT_TRUE(map.str5.equals(""));
539 EXPECT_TRUE(map.str6.equals("0000000004000000"));
540 EXPECT_TRUE(map.stdstr1 == "'eee");
541 EXPECT_TRUE(map.stdstr2 == "\"fff");
542 EXPECT_TRUE(map.stdstr3 == "`ggg");
543 EXPECT_TRUE(map.stdstr4 == "@hhh");
544 EXPECT_TRUE(map.stdstr5 == "");
545 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
549 //===----------------------------------------------------------------------===//
550 // Test ScalarEnumerationTraits
551 //===----------------------------------------------------------------------===//
572 struct ScalarEnumerationTraits<Colors> {
573 static void enumeration(IO &io, Colors &value) {
574 io.enumCase(value, "red", cRed);
575 io.enumCase(value, "blue", cBlue);
576 io.enumCase(value, "green", cGreen);
577 io.enumCase(value, "yellow",cYellow);
581 struct MappingTraits<ColorMap> {
582 static void mapping(IO &io, ColorMap& c) {
583 io.mapRequired("c1", c.c1);
584 io.mapRequired("c2", c.c2);
585 io.mapRequired("c3", c.c3);
586 io.mapOptional("c4", c.c4, cBlue); // supplies default
587 io.mapOptional("c5", c.c5, cYellow); // supplies default
588 io.mapOptional("c6", c.c6, cRed); // supplies default
596 // Test reading enumerated scalars
598 TEST(YAMLIO, TestEnumRead) {
608 EXPECT_FALSE(yin.error());
609 EXPECT_EQ(cBlue, map.c1);
610 EXPECT_EQ(cRed, map.c2);
611 EXPECT_EQ(cGreen, map.c3);
612 EXPECT_EQ(cBlue, map.c4); // tests default
613 EXPECT_EQ(cYellow,map.c5); // tests overridden
614 EXPECT_EQ(cRed, map.c6); // tests default
619 //===----------------------------------------------------------------------===//
620 // Test ScalarBitSetTraits
621 //===----------------------------------------------------------------------===//
630 inline MyFlags operator|(MyFlags a, MyFlags b) {
631 return static_cast<MyFlags>(
632 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
646 struct ScalarBitSetTraits<MyFlags> {
647 static void bitset(IO &io, MyFlags &value) {
648 io.bitSetCase(value, "big", flagBig);
649 io.bitSetCase(value, "flat", flagFlat);
650 io.bitSetCase(value, "round", flagRound);
651 io.bitSetCase(value, "pointy",flagPointy);
655 struct MappingTraits<FlagsMap> {
656 static void mapping(IO &io, FlagsMap& c) {
657 io.mapRequired("f1", c.f1);
658 io.mapRequired("f2", c.f2);
659 io.mapRequired("f3", c.f3);
660 io.mapOptional("f4", c.f4, MyFlags(flagRound));
668 // Test reading flow sequence representing bit-mask values
670 TEST(YAMLIO, TestFlagsRead) {
674 "f2: [ round, flat ]\n"
679 EXPECT_FALSE(yin.error());
680 EXPECT_EQ(flagBig, map.f1);
681 EXPECT_EQ(flagRound|flagFlat, map.f2);
682 EXPECT_EQ(flagNone, map.f3); // check empty set
683 EXPECT_EQ(flagRound, map.f4); // check optional key
688 // Test writing then reading back bit-mask values
690 TEST(YAMLIO, TestReadWriteFlags) {
691 std::string intermediate;
695 map.f2 = flagRound | flagFlat;
699 llvm::raw_string_ostream ostr(intermediate);
705 Input yin(intermediate);
709 EXPECT_FALSE(yin.error());
710 EXPECT_EQ(flagBig, map2.f1);
711 EXPECT_EQ(flagRound|flagFlat, map2.f2);
712 EXPECT_EQ(flagNone, map2.f3);
713 //EXPECT_EQ(flagRound, map2.f4); // check optional key
719 //===----------------------------------------------------------------------===//
721 //===----------------------------------------------------------------------===//
723 struct MyCustomType {
728 struct MyCustomTypeMap {
738 struct MappingTraits<MyCustomTypeMap> {
739 static void mapping(IO &io, MyCustomTypeMap& s) {
740 io.mapRequired("f1", s.f1);
741 io.mapRequired("f2", s.f2);
742 io.mapRequired("f3", s.f3);
745 // MyCustomType is formatted as a yaml scalar. A value of
746 // {length=3, width=4} would be represented in yaml as "3 by 4".
748 struct ScalarTraits<MyCustomType> {
749 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
750 out << llvm::format("%d by %d", value.length, value.width);
752 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
753 size_t byStart = scalar.find("by");
754 if ( byStart != StringRef::npos ) {
755 StringRef lenStr = scalar.slice(0, byStart);
756 lenStr = lenStr.rtrim();
757 if ( lenStr.getAsInteger(0, value.length) ) {
758 return "malformed length";
760 StringRef widthStr = scalar.drop_front(byStart+2);
761 widthStr = widthStr.ltrim();
762 if ( widthStr.getAsInteger(0, value.width) ) {
763 return "malformed width";
768 return "malformed by";
771 static bool mustQuote(StringRef) { return true; }
778 // Test writing then reading back custom values
780 TEST(YAMLIO, TestReadWriteMyCustomType) {
781 std::string intermediate;
790 llvm::raw_string_ostream ostr(intermediate);
796 Input yin(intermediate);
797 MyCustomTypeMap map2;
800 EXPECT_FALSE(yin.error());
801 EXPECT_EQ(1, map2.f1.length);
802 EXPECT_EQ(4, map2.f1.width);
803 EXPECT_EQ(100, map2.f2.length);
804 EXPECT_EQ(400, map2.f2.width);
805 EXPECT_EQ(10, map2.f3);
810 //===----------------------------------------------------------------------===//
811 // Test BlockScalarTraits
812 //===----------------------------------------------------------------------===//
814 struct MultilineStringType {
818 struct MultilineStringTypeMap {
819 MultilineStringType name;
820 MultilineStringType description;
821 MultilineStringType ingredients;
822 MultilineStringType recipes;
823 MultilineStringType warningLabels;
824 MultilineStringType documentation;
831 struct MappingTraits<MultilineStringTypeMap> {
832 static void mapping(IO &io, MultilineStringTypeMap& s) {
833 io.mapRequired("name", s.name);
834 io.mapRequired("description", s.description);
835 io.mapRequired("ingredients", s.ingredients);
836 io.mapRequired("recipes", s.recipes);
837 io.mapRequired("warningLabels", s.warningLabels);
838 io.mapRequired("documentation", s.documentation);
839 io.mapRequired("price", s.price);
843 // MultilineStringType is formatted as a yaml block literal scalar. A value of
844 // "Hello\nWorld" would be represented in yaml as
849 struct BlockScalarTraits<MultilineStringType> {
850 static void output(const MultilineStringType &value, void *ctxt,
851 llvm::raw_ostream &out) {
854 static StringRef input(StringRef scalar, void *ctxt,
855 MultilineStringType &value) {
856 value.str = scalar.str();
863 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
866 // Test writing then reading back custom values
868 TEST(YAMLIO, TestReadWriteMultilineStringType) {
869 std::string intermediate;
871 MultilineStringTypeMap map;
872 map.name.str = "An Item";
873 map.description.str = "Hello\nWorld";
874 map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
875 map.recipes.str = "\n\nTest 1\n\n\n";
876 map.warningLabels.str = "";
877 map.documentation.str = "\n\n";
880 llvm::raw_string_ostream ostr(intermediate);
885 Input yin(intermediate);
886 MultilineStringTypeMap map2;
889 EXPECT_FALSE(yin.error());
890 EXPECT_EQ(map2.name.str, "An Item\n");
891 EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
892 EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
893 EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
894 EXPECT_TRUE(map2.warningLabels.str.empty());
895 EXPECT_TRUE(map2.documentation.str.empty());
896 EXPECT_EQ(map2.price, 350);
901 // Test writing then reading back custom values
903 TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
904 std::string intermediate;
906 std::vector<MultilineStringType> documents;
907 MultilineStringType doc;
908 doc.str = "Hello\nWorld";
909 documents.push_back(doc);
911 llvm::raw_string_ostream ostr(intermediate);
915 // Verify that the block scalar header was written out on the same line
916 // as the document marker.
917 EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
920 Input yin(intermediate);
921 std::vector<MultilineStringType> documents2;
924 EXPECT_FALSE(yin.error());
925 EXPECT_EQ(documents2.size(), size_t(1));
926 EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
930 TEST(YAMLIO, TestReadWriteBlockScalarValue) {
931 std::string intermediate;
933 MultilineStringType doc;
934 doc.str = "Just a block\nscalar doc";
936 llvm::raw_string_ostream ostr(intermediate);
941 Input yin(intermediate);
942 MultilineStringType doc;
945 EXPECT_FALSE(yin.error());
946 EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
950 //===----------------------------------------------------------------------===//
951 // Test flow sequences
952 //===----------------------------------------------------------------------===//
954 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
955 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
956 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
961 struct ScalarTraits<MyNumber> {
962 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
966 static StringRef input(StringRef scalar, void *, MyNumber &value) {
968 if ( getAsSignedInteger(scalar, 0, n) )
969 return "invalid number";
974 static bool mustQuote(StringRef) { return false; }
979 struct NameAndNumbers {
980 llvm::StringRef name;
981 std::vector<llvm::StringRef> strings;
982 std::vector<MyNumber> single;
983 std::vector<MyNumber> numbers;
989 struct MappingTraits<NameAndNumbers> {
990 static void mapping(IO &io, NameAndNumbers& nn) {
991 io.mapRequired("name", nn.name);
992 io.mapRequired("strings", nn.strings);
993 io.mapRequired("single", nn.single);
994 io.mapRequired("numbers", nn.numbers);
1000 typedef std::vector<MyNumber> MyNumberFlowSequence;
1002 LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1004 struct NameAndNumbersFlow {
1005 llvm::StringRef name;
1006 std::vector<MyNumberFlowSequence> sequenceOfNumbers;
1012 struct MappingTraits<NameAndNumbersFlow> {
1013 static void mapping(IO &io, NameAndNumbersFlow& nn) {
1014 io.mapRequired("name", nn.name);
1015 io.mapRequired("sequenceOfNumbers", nn.sequenceOfNumbers);
1022 // Test writing then reading back custom values
1024 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1025 std::string intermediate;
1029 map.strings.push_back(llvm::StringRef("one"));
1030 map.strings.push_back(llvm::StringRef("two"));
1031 map.single.push_back(1);
1032 map.numbers.push_back(10);
1033 map.numbers.push_back(-30);
1034 map.numbers.push_back(1024);
1036 llvm::raw_string_ostream ostr(intermediate);
1040 // Verify sequences were written in flow style
1042 llvm::StringRef flowOut(intermediate);
1043 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1044 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
1048 Input yin(intermediate);
1049 NameAndNumbers map2;
1052 EXPECT_FALSE(yin.error());
1053 EXPECT_TRUE(map2.name.equals("hello"));
1054 EXPECT_EQ(map2.strings.size(), 2UL);
1055 EXPECT_TRUE(map2.strings[0].equals("one"));
1056 EXPECT_TRUE(map2.strings[1].equals("two"));
1057 EXPECT_EQ(map2.single.size(), 1UL);
1058 EXPECT_EQ(1, map2.single[0]);
1059 EXPECT_EQ(map2.numbers.size(), 3UL);
1060 EXPECT_EQ(10, map2.numbers[0]);
1061 EXPECT_EQ(-30, map2.numbers[1]);
1062 EXPECT_EQ(1024, map2.numbers[2]);
1068 // Test writing then reading back a sequence of flow sequences.
1070 TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1071 std::string intermediate;
1073 NameAndNumbersFlow map;
1075 MyNumberFlowSequence single = { 0 };
1076 MyNumberFlowSequence numbers = { 12, 1, -512 };
1077 map.sequenceOfNumbers.push_back(single);
1078 map.sequenceOfNumbers.push_back(numbers);
1079 map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1081 llvm::raw_string_ostream ostr(intermediate);
1085 // Verify sequences were written in flow style
1086 // and that the parent sequence used '-'.
1088 llvm::StringRef flowOut(intermediate);
1089 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1090 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1091 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ ]"));
1095 Input yin(intermediate);
1096 NameAndNumbersFlow map2;
1099 EXPECT_FALSE(yin.error());
1100 EXPECT_TRUE(map2.name.equals("hello"));
1101 EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1102 EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1103 EXPECT_EQ(0, map2.sequenceOfNumbers[0][0]);
1104 EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1105 EXPECT_EQ(12, map2.sequenceOfNumbers[1][0]);
1106 EXPECT_EQ(1, map2.sequenceOfNumbers[1][1]);
1107 EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1108 EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1112 //===----------------------------------------------------------------------===//
1113 // Test normalizing/denormalizing
1114 //===----------------------------------------------------------------------===//
1116 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1118 typedef std::vector<TotalSeconds> SecondsSequence;
1120 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
1126 struct MappingTraits<TotalSeconds> {
1128 class NormalizedSeconds {
1130 NormalizedSeconds(IO &io)
1131 : hours(0), minutes(0), seconds(0) {
1133 NormalizedSeconds(IO &, TotalSeconds &secs)
1135 minutes((secs - (hours*3600))/60),
1136 seconds(secs % 60) {
1138 TotalSeconds denormalize(IO &) {
1139 return TotalSeconds(hours*3600 + minutes*60 + seconds);
1147 static void mapping(IO &io, TotalSeconds &secs) {
1148 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1150 io.mapOptional("hours", keys->hours, (uint32_t)0);
1151 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
1152 io.mapRequired("seconds", keys->seconds);
1160 // Test the reading of a yaml sequence of mappings
1162 TEST(YAMLIO, TestReadMySecondsSequence) {
1163 SecondsSequence seq;
1164 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1167 EXPECT_FALSE(yin.error());
1168 EXPECT_EQ(seq.size(), 2UL);
1169 EXPECT_EQ(seq[0], 3605U);
1170 EXPECT_EQ(seq[1], 59U);
1175 // Test writing then reading back custom values
1177 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1178 std::string intermediate;
1180 SecondsSequence seq;
1181 seq.push_back(4000);
1185 llvm::raw_string_ostream ostr(intermediate);
1190 Input yin(intermediate);
1191 SecondsSequence seq2;
1194 EXPECT_FALSE(yin.error());
1195 EXPECT_EQ(seq2.size(), 3UL);
1196 EXPECT_EQ(seq2[0], 4000U);
1197 EXPECT_EQ(seq2[1], 500U);
1198 EXPECT_EQ(seq2[2], 59U);
1203 //===----------------------------------------------------------------------===//
1204 // Test dynamic typing
1205 //===----------------------------------------------------------------------===//
1224 struct KindAndFlags {
1225 KindAndFlags() : kind(kindA), flags(0) { }
1226 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1231 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1233 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
1238 struct ScalarEnumerationTraits<AFlags> {
1239 static void enumeration(IO &io, AFlags &value) {
1240 io.enumCase(value, "a1", a1);
1241 io.enumCase(value, "a2", a2);
1242 io.enumCase(value, "a3", a3);
1246 struct ScalarEnumerationTraits<BFlags> {
1247 static void enumeration(IO &io, BFlags &value) {
1248 io.enumCase(value, "b1", b1);
1249 io.enumCase(value, "b2", b2);
1250 io.enumCase(value, "b3", b3);
1254 struct ScalarEnumerationTraits<Kind> {
1255 static void enumeration(IO &io, Kind &value) {
1256 io.enumCase(value, "A", kindA);
1257 io.enumCase(value, "B", kindB);
1261 struct MappingTraits<KindAndFlags> {
1262 static void mapping(IO &io, KindAndFlags& kf) {
1263 io.mapRequired("kind", kf.kind);
1264 // Type of "flags" field varies depending on "kind" field.
1265 // Use memcpy here to avoid breaking strict aliasing rules.
1266 if (kf.kind == kindA) {
1267 AFlags aflags = static_cast<AFlags>(kf.flags);
1268 io.mapRequired("flags", aflags);
1271 BFlags bflags = static_cast<BFlags>(kf.flags);
1272 io.mapRequired("flags", bflags);
1282 // Test the reading of a yaml sequence dynamic types
1284 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1285 KindAndFlagsSequence seq;
1286 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1289 EXPECT_FALSE(yin.error());
1290 EXPECT_EQ(seq.size(), 2UL);
1291 EXPECT_EQ(seq[0].kind, kindA);
1292 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
1293 EXPECT_EQ(seq[1].kind, kindB);
1294 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
1298 // Test writing then reading back dynamic types
1300 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1301 std::string intermediate;
1303 KindAndFlagsSequence seq;
1304 seq.push_back(KindAndFlags(kindA,a1));
1305 seq.push_back(KindAndFlags(kindB,b1));
1306 seq.push_back(KindAndFlags(kindA,a2));
1307 seq.push_back(KindAndFlags(kindB,b2));
1308 seq.push_back(KindAndFlags(kindA,a3));
1310 llvm::raw_string_ostream ostr(intermediate);
1315 Input yin(intermediate);
1316 KindAndFlagsSequence seq2;
1319 EXPECT_FALSE(yin.error());
1320 EXPECT_EQ(seq2.size(), 5UL);
1321 EXPECT_EQ(seq2[0].kind, kindA);
1322 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
1323 EXPECT_EQ(seq2[1].kind, kindB);
1324 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
1325 EXPECT_EQ(seq2[2].kind, kindA);
1326 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
1327 EXPECT_EQ(seq2[3].kind, kindB);
1328 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
1329 EXPECT_EQ(seq2[4].kind, kindA);
1330 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
1335 //===----------------------------------------------------------------------===//
1336 // Test document list
1337 //===----------------------------------------------------------------------===//
1343 typedef std::vector<FooBarMap> FooBarMapDocumentList;
1345 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1351 struct MappingTraits<FooBarMap> {
1352 static void mapping(IO &io, FooBarMap& fb) {
1353 io.mapRequired("foo", fb.foo);
1354 io.mapRequired("bar", fb.bar);
1362 // Test the reading of a yaml mapping
1364 TEST(YAMLIO, TestDocRead) {
1366 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1369 EXPECT_FALSE(yin.error());
1370 EXPECT_EQ(doc.foo, 3);
1371 EXPECT_EQ(doc.bar,5);
1377 // Test writing then reading back a sequence of mappings
1379 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1380 std::string intermediate;
1388 std::vector<FooBarMap> docList;
1389 docList.push_back(doc1);
1390 docList.push_back(doc2);
1392 llvm::raw_string_ostream ostr(intermediate);
1399 Input yin(intermediate);
1400 std::vector<FooBarMap> docList2;
1403 EXPECT_FALSE(yin.error());
1404 EXPECT_EQ(docList2.size(), 2UL);
1405 FooBarMap& map1 = docList2[0];
1406 FooBarMap& map2 = docList2[1];
1407 EXPECT_EQ(map1.foo, 10);
1408 EXPECT_EQ(map1.bar, -3);
1409 EXPECT_EQ(map2.foo, 257);
1410 EXPECT_EQ(map2.bar, 0);
1414 //===----------------------------------------------------------------------===//
1415 // Test document tags
1416 //===----------------------------------------------------------------------===//
1419 MyDouble() : value(0.0) { }
1420 MyDouble(double x) : value(x) { }
1424 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1430 struct MappingTraits<MyDouble> {
1431 static void mapping(IO &io, MyDouble &d) {
1432 if (io.mapTag("!decimal", true)) {
1433 mappingDecimal(io, d);
1434 } else if (io.mapTag("!fraction")) {
1435 mappingFraction(io, d);
1438 static void mappingDecimal(IO &io, MyDouble &d) {
1439 io.mapRequired("value", d.value);
1441 static void mappingFraction(IO &io, MyDouble &d) {
1443 io.mapRequired("numerator", num);
1444 io.mapRequired("denominator", denom);
1445 // convert fraction to double
1446 d.value = num/denom;
1454 // Test the reading of two different tagged yaml documents.
1456 TEST(YAMLIO, TestTaggedDocuments) {
1457 std::vector<MyDouble> docList;
1458 Input yin("--- !decimal\nvalue: 3.0\n"
1459 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1461 EXPECT_FALSE(yin.error());
1462 EXPECT_EQ(docList.size(), 2UL);
1463 EXPECT_EQ(docList[0].value, 3.0);
1464 EXPECT_EQ(docList[1].value, 4.5);
1470 // Test writing then reading back tagged documents
1472 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1473 std::string intermediate;
1477 std::vector<MyDouble> docList;
1478 docList.push_back(a);
1479 docList.push_back(b);
1481 llvm::raw_string_ostream ostr(intermediate);
1487 Input yin(intermediate);
1488 std::vector<MyDouble> docList2;
1491 EXPECT_FALSE(yin.error());
1492 EXPECT_EQ(docList2.size(), 2UL);
1493 EXPECT_EQ(docList2[0].value, 10.25);
1494 EXPECT_EQ(docList2[1].value, -3.75);
1499 //===----------------------------------------------------------------------===//
1500 // Test mapping validation
1501 //===----------------------------------------------------------------------===//
1503 struct MyValidation {
1507 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1512 struct MappingTraits<MyValidation> {
1513 static void mapping(IO &io, MyValidation &d) {
1514 io.mapRequired("value", d.value);
1516 static StringRef validate(IO &io, MyValidation &d) {
1518 return "negative value";
1527 // Test that validate() is called and complains about the negative value.
1529 TEST(YAMLIO, TestValidatingInput) {
1530 std::vector<MyValidation> docList;
1531 Input yin("--- \nvalue: 3.0\n"
1532 "--- \nvalue: -1.0\n...\n",
1533 nullptr, suppressErrorMessages);
1535 EXPECT_TRUE(!!yin.error());
1538 //===----------------------------------------------------------------------===//
1539 // Test flow mapping
1540 //===----------------------------------------------------------------------===//
1546 FlowFooBar() : foo(0), bar(0) {}
1547 FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1550 typedef std::vector<FlowFooBar> FlowFooBarSequence;
1552 LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1554 struct FlowFooBarDoc {
1555 FlowFooBar attribute;
1556 FlowFooBarSequence seq;
1562 struct MappingTraits<FlowFooBar> {
1563 static void mapping(IO &io, FlowFooBar &fb) {
1564 io.mapRequired("foo", fb.foo);
1565 io.mapRequired("bar", fb.bar);
1568 static const bool flow = true;
1572 struct MappingTraits<FlowFooBarDoc> {
1573 static void mapping(IO &io, FlowFooBarDoc &fb) {
1574 io.mapRequired("attribute", fb.attribute);
1575 io.mapRequired("seq", fb.seq);
1582 // Test writing then reading back custom mappings
1584 TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1585 std::string intermediate;
1588 doc.attribute = FlowFooBar(42, 907);
1589 doc.seq.push_back(FlowFooBar(1, 2));
1590 doc.seq.push_back(FlowFooBar(0, 0));
1591 doc.seq.push_back(FlowFooBar(-1, 1024));
1593 llvm::raw_string_ostream ostr(intermediate);
1597 // Verify that mappings were written in flow style
1599 llvm::StringRef flowOut(intermediate);
1600 EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1601 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1602 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1603 EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1607 Input yin(intermediate);
1611 EXPECT_FALSE(yin.error());
1612 EXPECT_EQ(doc2.attribute.foo, 42);
1613 EXPECT_EQ(doc2.attribute.bar, 907);
1614 EXPECT_EQ(doc2.seq.size(), 3UL);
1615 EXPECT_EQ(doc2.seq[0].foo, 1);
1616 EXPECT_EQ(doc2.seq[0].bar, 2);
1617 EXPECT_EQ(doc2.seq[1].foo, 0);
1618 EXPECT_EQ(doc2.seq[1].bar, 0);
1619 EXPECT_EQ(doc2.seq[2].foo, -1);
1620 EXPECT_EQ(doc2.seq[2].bar, 1024);
1624 //===----------------------------------------------------------------------===//
1625 // Test error handling
1626 //===----------------------------------------------------------------------===//
1629 // Test error handling of unknown enumerated scalar
1631 TEST(YAMLIO, TestColorsReadError) {
1639 suppressErrorMessages);
1641 EXPECT_TRUE(!!yin.error());
1646 // Test error handling of flow sequence with unknown value
1648 TEST(YAMLIO, TestFlagsReadError) {
1652 "f2: [ round, hollow ]\n"
1656 suppressErrorMessages);
1659 EXPECT_TRUE(!!yin.error());
1664 // Test error handling reading built-in uint8_t type
1666 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1667 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1668 std::vector<uint8_t> seq;
1675 suppressErrorMessages);
1678 EXPECT_TRUE(!!yin.error());
1683 // Test error handling reading built-in uint16_t type
1685 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1686 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1687 std::vector<uint16_t> seq;
1694 suppressErrorMessages);
1697 EXPECT_TRUE(!!yin.error());
1702 // Test error handling reading built-in uint32_t type
1704 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1705 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1706 std::vector<uint32_t> seq;
1713 suppressErrorMessages);
1716 EXPECT_TRUE(!!yin.error());
1721 // Test error handling reading built-in uint64_t type
1723 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1724 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1725 std::vector<uint64_t> seq;
1727 "- 18446744073709551615\n"
1729 "- 19446744073709551615\n"
1732 suppressErrorMessages);
1735 EXPECT_TRUE(!!yin.error());
1740 // Test error handling reading built-in int8_t type
1742 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1743 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1744 std::vector<int8_t> seq;
1752 suppressErrorMessages);
1755 EXPECT_TRUE(!!yin.error());
1759 // Test error handling reading built-in int8_t type
1761 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1762 std::vector<int8_t> seq;
1770 suppressErrorMessages);
1773 EXPECT_TRUE(!!yin.error());
1778 // Test error handling reading built-in int16_t type
1780 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1781 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1782 std::vector<int16_t> seq;
1790 suppressErrorMessages);
1793 EXPECT_TRUE(!!yin.error());
1798 // Test error handling reading built-in int16_t type
1800 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1801 std::vector<int16_t> seq;
1809 suppressErrorMessages);
1812 EXPECT_TRUE(!!yin.error());
1817 // Test error handling reading built-in int32_t type
1819 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1820 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1821 std::vector<int32_t> seq;
1829 suppressErrorMessages);
1832 EXPECT_TRUE(!!yin.error());
1836 // Test error handling reading built-in int32_t type
1838 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1839 std::vector<int32_t> seq;
1847 suppressErrorMessages);
1850 EXPECT_TRUE(!!yin.error());
1855 // Test error handling reading built-in int64_t type
1857 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1858 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1859 std::vector<int64_t> seq;
1861 "- -9223372036854775808\n"
1863 "- 9223372036854775807\n"
1864 "- -9223372036854775809\n"
1867 suppressErrorMessages);
1870 EXPECT_TRUE(!!yin.error());
1874 // Test error handling reading built-in int64_t type
1876 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1877 std::vector<int64_t> seq;
1879 "- -9223372036854775808\n"
1881 "- 9223372036854775807\n"
1882 "- 9223372036854775809\n"
1885 suppressErrorMessages);
1888 EXPECT_TRUE(!!yin.error());
1892 // Test error handling reading built-in float type
1894 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1895 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1896 std::vector<float> seq;
1904 suppressErrorMessages);
1907 EXPECT_TRUE(!!yin.error());
1911 // Test error handling reading built-in float type
1913 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1914 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1915 std::vector<double> seq;
1923 suppressErrorMessages);
1926 EXPECT_TRUE(!!yin.error());
1930 // Test error handling reading built-in Hex8 type
1932 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1933 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1934 std::vector<Hex8> seq;
1941 suppressErrorMessages);
1944 EXPECT_TRUE(!!yin.error());
1949 // Test error handling reading built-in Hex16 type
1951 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1952 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1953 std::vector<Hex16> seq;
1960 suppressErrorMessages);
1963 EXPECT_TRUE(!!yin.error());
1967 // Test error handling reading built-in Hex32 type
1969 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1970 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1971 std::vector<Hex32> seq;
1978 suppressErrorMessages);
1981 EXPECT_TRUE(!!yin.error());
1985 // Test error handling reading built-in Hex64 type
1987 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1988 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1989 std::vector<Hex64> seq;
1992 "- 0xFFEEDDCCBBAA9988\n"
1993 "- 0x12345567890ABCDEF0\n"
1996 suppressErrorMessages);
1999 EXPECT_TRUE(!!yin.error());
2002 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2005 // We pass the suppressErrorMessages handler to handle the error
2006 // message generated in the constructor of Input.
2007 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
2009 EXPECT_TRUE(!!yin.error());
2013 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
2015 EXPECT_TRUE(!!yin.error());
2019 struct OptionalTest {
2020 std::vector<int> Numbers;
2023 struct OptionalTestSeq {
2024 std::vector<OptionalTest> Tests;
2027 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
2031 struct MappingTraits<OptionalTest> {
2032 static void mapping(IO& IO, OptionalTest &OT) {
2033 IO.mapOptional("Numbers", OT.Numbers);
2038 struct MappingTraits<OptionalTestSeq> {
2039 static void mapping(IO &IO, OptionalTestSeq &OTS) {
2040 IO.mapOptional("Tests", OTS.Tests);
2046 TEST(YAMLIO, SequenceElideTest) {
2047 // Test that writing out a purely optional structure with its fields set to
2048 // default followed by other data is properly read back in.
2049 OptionalTestSeq Seq;
2050 OptionalTest One, Two, Three, Four;
2051 int N[] = {1, 2, 3};
2052 Three.Numbers.assign(N, N + 3);
2053 Seq.Tests.push_back(One);
2054 Seq.Tests.push_back(Two);
2055 Seq.Tests.push_back(Three);
2056 Seq.Tests.push_back(Four);
2058 std::string intermediate;
2060 llvm::raw_string_ostream ostr(intermediate);
2065 Input yin(intermediate);
2066 OptionalTestSeq Seq2;
2069 EXPECT_FALSE(yin.error());
2071 EXPECT_EQ(4UL, Seq2.Tests.size());
2073 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2074 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2076 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2077 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2078 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2080 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2083 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2087 EXPECT_TRUE(!!yin.error());
2090 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2094 EXPECT_FALSE(yin.error());
2097 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2098 std::vector<uint8_t> seq;
2099 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
2102 EXPECT_FALSE(yin.error());
2103 EXPECT_TRUE(seq.empty());
2107 llvm::StringRef str1, str2, str3;
2108 FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2109 : str1(str1), str2(str2), str3(str3) {}
2113 llvm::StringRef str;
2114 FlowSeq(llvm::StringRef S) : str(S) {}
2115 FlowSeq() = default;
2121 struct MappingTraits<FlowMap> {
2122 static void mapping(IO &io, FlowMap &fm) {
2123 io.mapRequired("str1", fm.str1);
2124 io.mapRequired("str2", fm.str2);
2125 io.mapRequired("str3", fm.str3);
2128 static const bool flow = true;
2132 struct ScalarTraits<FlowSeq> {
2133 static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2136 static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2141 static bool mustQuote(StringRef S) { return false; }
2146 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2148 TEST(YAMLIO, TestWrapFlow) {
2150 llvm::raw_string_ostream ostr(out);
2151 FlowMap Map("This is str1", "This is str2", "This is str3");
2152 std::vector<FlowSeq> Seq;
2153 Seq.emplace_back("This is str1");
2154 Seq.emplace_back("This is str2");
2155 Seq.emplace_back("This is str3");
2158 // 20 is just bellow the total length of the first mapping field.
2159 // We should wreap at every element.
2160 Output yout(ostr, nullptr, 15);
2166 "{ str1: This is str1, \n"
2167 " str2: This is str2, \n"
2168 " str3: This is str3 }\n"
2176 "[ This is str1, \n"
2183 // 25 will allow the second field to be output on the first line.
2184 Output yout(ostr, nullptr, 25);
2190 "{ str1: This is str1, str2: This is str2, \n"
2191 " str3: This is str3 }\n"
2199 "[ This is str1, This is str2, \n"
2205 // 0 means no wrapping.
2206 Output yout(ostr, nullptr, 0);
2212 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2220 "[ This is str1, This is str2, This is str3 ]\n"