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)
53 struct MappingTraits<FooBar> {
54 static void mapping(IO &io, FooBar& fb) {
55 io.mapRequired("foo", fb.foo);
56 io.mapRequired("bar", fb.bar);
64 // Test the reading of a yaml mapping
66 TEST(YAMLIO, TestMapRead) {
69 Input yin("---\nfoo: 3\nbar: 5\n...\n");
72 EXPECT_FALSE(yin.error());
73 EXPECT_EQ(doc.foo, 3);
74 EXPECT_EQ(doc.bar, 5);
78 Input yin("{foo: 3, bar: 5}");
81 EXPECT_FALSE(yin.error());
82 EXPECT_EQ(doc.foo, 3);
83 EXPECT_EQ(doc.bar, 5);
88 // Test the reading of a yaml sequence of mappings
90 TEST(YAMLIO, TestSequenceMapRead) {
92 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
95 EXPECT_FALSE(yin.error());
96 EXPECT_EQ(seq.size(), 2UL);
97 FooBar& map1 = seq[0];
98 FooBar& map2 = seq[1];
99 EXPECT_EQ(map1.foo, 3);
100 EXPECT_EQ(map1.bar, 5);
101 EXPECT_EQ(map2.foo, 7);
102 EXPECT_EQ(map2.bar, 9);
107 // Test writing then reading back a sequence of mappings
109 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
110 std::string intermediate;
119 seq.push_back(entry1);
120 seq.push_back(entry2);
122 llvm::raw_string_ostream ostr(intermediate);
128 Input yin(intermediate);
132 EXPECT_FALSE(yin.error());
133 EXPECT_EQ(seq2.size(), 2UL);
134 FooBar& map1 = seq2[0];
135 FooBar& map2 = seq2[1];
136 EXPECT_EQ(map1.foo, 10);
137 EXPECT_EQ(map1.bar, -3);
138 EXPECT_EQ(map2.foo, 257);
139 EXPECT_EQ(map2.bar, 0);
144 //===----------------------------------------------------------------------===//
145 // Test built-in types
146 //===----------------------------------------------------------------------===//
148 struct BuiltInTypes {
171 struct MappingTraits<BuiltInTypes> {
172 static void mapping(IO &io, BuiltInTypes& bt) {
173 io.mapRequired("str", bt.str);
174 io.mapRequired("stdstr", bt.stdstr);
175 io.mapRequired("u64", bt.u64);
176 io.mapRequired("u32", bt.u32);
177 io.mapRequired("u16", bt.u16);
178 io.mapRequired("u8", bt.u8);
179 io.mapRequired("b", bt.b);
180 io.mapRequired("s64", bt.s64);
181 io.mapRequired("s32", bt.s32);
182 io.mapRequired("s16", bt.s16);
183 io.mapRequired("s8", bt.s8);
184 io.mapRequired("f", bt.f);
185 io.mapRequired("d", bt.d);
186 io.mapRequired("h8", bt.h8);
187 io.mapRequired("h16", bt.h16);
188 io.mapRequired("h32", bt.h32);
189 io.mapRequired("h64", bt.h64);
197 // Test the reading of all built-in scalar conversions
199 TEST(YAMLIO, TestReadBuiltInTypes) {
203 "stdstr: hello where?\n"
218 "h64: 0xFEDCBA9876543210\n"
222 EXPECT_FALSE(yin.error());
223 EXPECT_TRUE(map.str.equals("hello there"));
224 EXPECT_TRUE(map.stdstr == "hello where?");
225 EXPECT_EQ(map.u64, 5000000000ULL);
226 EXPECT_EQ(map.u32, 4000000000U);
227 EXPECT_EQ(map.u16, 65000);
228 EXPECT_EQ(map.u8, 255);
229 EXPECT_EQ(map.b, false);
230 EXPECT_EQ(map.s64, -5000000000LL);
231 EXPECT_EQ(map.s32, -2000000000L);
232 EXPECT_EQ(map.s16, -32000);
233 EXPECT_EQ(map.s8, -127);
234 EXPECT_EQ(map.f, 137.125);
235 EXPECT_EQ(map.d, -2.8625);
236 EXPECT_EQ(map.h8, Hex8(255));
237 EXPECT_EQ(map.h16, Hex16(0x8765));
238 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
239 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
244 // Test writing then reading back all built-in scalar types
246 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
247 std::string intermediate;
251 map.stdstr = "three four";
252 map.u64 = 6000000000ULL;
253 map.u32 = 3000000000U;
257 map.s64 = -6000000000LL;
258 map.s32 = -2000000000;
265 map.h32 = 3000000000U;
266 map.h64 = 6000000000LL;
268 llvm::raw_string_ostream ostr(intermediate);
274 Input yin(intermediate);
278 EXPECT_FALSE(yin.error());
279 EXPECT_TRUE(map.str.equals("one two"));
280 EXPECT_TRUE(map.stdstr == "three four");
281 EXPECT_EQ(map.u64, 6000000000ULL);
282 EXPECT_EQ(map.u32, 3000000000U);
283 EXPECT_EQ(map.u16, 50000);
284 EXPECT_EQ(map.u8, 254);
285 EXPECT_EQ(map.b, true);
286 EXPECT_EQ(map.s64, -6000000000LL);
287 EXPECT_EQ(map.s32, -2000000000L);
288 EXPECT_EQ(map.s16, -32000);
289 EXPECT_EQ(map.s8, -128);
290 EXPECT_EQ(map.f, 3.25);
291 EXPECT_EQ(map.d, -2.8625);
292 EXPECT_EQ(map.h8, Hex8(254));
293 EXPECT_EQ(map.h16, Hex16(50000));
294 EXPECT_EQ(map.h32, Hex32(3000000000U));
295 EXPECT_EQ(map.h64, Hex64(6000000000LL));
300 llvm::StringRef str1;
301 llvm::StringRef str2;
302 llvm::StringRef str3;
303 llvm::StringRef str4;
304 llvm::StringRef str5;
305 llvm::StringRef str6;
317 struct MappingTraits<StringTypes> {
318 static void mapping(IO &io, StringTypes& st) {
319 io.mapRequired("str1", st.str1);
320 io.mapRequired("str2", st.str2);
321 io.mapRequired("str3", st.str3);
322 io.mapRequired("str4", st.str4);
323 io.mapRequired("str5", st.str5);
324 io.mapRequired("str6", st.str6);
325 io.mapRequired("stdstr1", st.stdstr1);
326 io.mapRequired("stdstr2", st.stdstr2);
327 io.mapRequired("stdstr3", st.stdstr3);
328 io.mapRequired("stdstr4", st.stdstr4);
329 io.mapRequired("stdstr5", st.stdstr5);
330 io.mapRequired("stdstr6", st.stdstr6);
336 TEST(YAMLIO, TestReadWriteStringTypes) {
337 std::string intermediate;
345 map.str6 = "0000000004000000";
346 map.stdstr1 = "'eee";
347 map.stdstr2 = "\"fff";
348 map.stdstr3 = "`ggg";
349 map.stdstr4 = "@hhh";
351 map.stdstr6 = "0000000004000000";
353 llvm::raw_string_ostream ostr(intermediate);
358 llvm::StringRef flowOut(intermediate);
359 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
360 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
361 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
362 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
363 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
364 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
365 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
366 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
367 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
368 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
369 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
370 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
373 Input yin(intermediate);
377 EXPECT_FALSE(yin.error());
378 EXPECT_TRUE(map.str1.equals("'aaa"));
379 EXPECT_TRUE(map.str2.equals("\"bbb"));
380 EXPECT_TRUE(map.str3.equals("`ccc"));
381 EXPECT_TRUE(map.str4.equals("@ddd"));
382 EXPECT_TRUE(map.str5.equals(""));
383 EXPECT_TRUE(map.str6.equals("0000000004000000"));
384 EXPECT_TRUE(map.stdstr1 == "'eee");
385 EXPECT_TRUE(map.stdstr2 == "\"fff");
386 EXPECT_TRUE(map.stdstr3 == "`ggg");
387 EXPECT_TRUE(map.stdstr4 == "@hhh");
388 EXPECT_TRUE(map.stdstr5 == "");
389 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
393 //===----------------------------------------------------------------------===//
394 // Test ScalarEnumerationTraits
395 //===----------------------------------------------------------------------===//
416 struct ScalarEnumerationTraits<Colors> {
417 static void enumeration(IO &io, Colors &value) {
418 io.enumCase(value, "red", cRed);
419 io.enumCase(value, "blue", cBlue);
420 io.enumCase(value, "green", cGreen);
421 io.enumCase(value, "yellow",cYellow);
425 struct MappingTraits<ColorMap> {
426 static void mapping(IO &io, ColorMap& c) {
427 io.mapRequired("c1", c.c1);
428 io.mapRequired("c2", c.c2);
429 io.mapRequired("c3", c.c3);
430 io.mapOptional("c4", c.c4, cBlue); // supplies default
431 io.mapOptional("c5", c.c5, cYellow); // supplies default
432 io.mapOptional("c6", c.c6, cRed); // supplies default
440 // Test reading enumerated scalars
442 TEST(YAMLIO, TestEnumRead) {
452 EXPECT_FALSE(yin.error());
453 EXPECT_EQ(cBlue, map.c1);
454 EXPECT_EQ(cRed, map.c2);
455 EXPECT_EQ(cGreen, map.c3);
456 EXPECT_EQ(cBlue, map.c4); // tests default
457 EXPECT_EQ(cYellow,map.c5); // tests overridden
458 EXPECT_EQ(cRed, map.c6); // tests default
463 //===----------------------------------------------------------------------===//
464 // Test ScalarBitSetTraits
465 //===----------------------------------------------------------------------===//
474 inline MyFlags operator|(MyFlags a, MyFlags b) {
475 return static_cast<MyFlags>(
476 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
490 struct ScalarBitSetTraits<MyFlags> {
491 static void bitset(IO &io, MyFlags &value) {
492 io.bitSetCase(value, "big", flagBig);
493 io.bitSetCase(value, "flat", flagFlat);
494 io.bitSetCase(value, "round", flagRound);
495 io.bitSetCase(value, "pointy",flagPointy);
499 struct MappingTraits<FlagsMap> {
500 static void mapping(IO &io, FlagsMap& c) {
501 io.mapRequired("f1", c.f1);
502 io.mapRequired("f2", c.f2);
503 io.mapRequired("f3", c.f3);
504 io.mapOptional("f4", c.f4, MyFlags(flagRound));
512 // Test reading flow sequence representing bit-mask values
514 TEST(YAMLIO, TestFlagsRead) {
518 "f2: [ round, flat ]\n"
523 EXPECT_FALSE(yin.error());
524 EXPECT_EQ(flagBig, map.f1);
525 EXPECT_EQ(flagRound|flagFlat, map.f2);
526 EXPECT_EQ(flagNone, map.f3); // check empty set
527 EXPECT_EQ(flagRound, map.f4); // check optional key
532 // Test writing then reading back bit-mask values
534 TEST(YAMLIO, TestReadWriteFlags) {
535 std::string intermediate;
539 map.f2 = flagRound | flagFlat;
543 llvm::raw_string_ostream ostr(intermediate);
549 Input yin(intermediate);
553 EXPECT_FALSE(yin.error());
554 EXPECT_EQ(flagBig, map2.f1);
555 EXPECT_EQ(flagRound|flagFlat, map2.f2);
556 EXPECT_EQ(flagNone, map2.f3);
557 //EXPECT_EQ(flagRound, map2.f4); // check optional key
563 //===----------------------------------------------------------------------===//
565 //===----------------------------------------------------------------------===//
567 struct MyCustomType {
572 struct MyCustomTypeMap {
582 struct MappingTraits<MyCustomTypeMap> {
583 static void mapping(IO &io, MyCustomTypeMap& s) {
584 io.mapRequired("f1", s.f1);
585 io.mapRequired("f2", s.f2);
586 io.mapRequired("f3", s.f3);
589 // MyCustomType is formatted as a yaml scalar. A value of
590 // {length=3, width=4} would be represented in yaml as "3 by 4".
592 struct ScalarTraits<MyCustomType> {
593 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
594 out << llvm::format("%d by %d", value.length, value.width);
596 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
597 size_t byStart = scalar.find("by");
598 if ( byStart != StringRef::npos ) {
599 StringRef lenStr = scalar.slice(0, byStart);
600 lenStr = lenStr.rtrim();
601 if ( lenStr.getAsInteger(0, value.length) ) {
602 return "malformed length";
604 StringRef widthStr = scalar.drop_front(byStart+2);
605 widthStr = widthStr.ltrim();
606 if ( widthStr.getAsInteger(0, value.width) ) {
607 return "malformed width";
612 return "malformed by";
621 // Test writing then reading back custom values
623 TEST(YAMLIO, TestReadWriteMyCustomType) {
624 std::string intermediate;
633 llvm::raw_string_ostream ostr(intermediate);
639 Input yin(intermediate);
640 MyCustomTypeMap map2;
643 EXPECT_FALSE(yin.error());
644 EXPECT_EQ(1, map2.f1.length);
645 EXPECT_EQ(4, map2.f1.width);
646 EXPECT_EQ(100, map2.f2.length);
647 EXPECT_EQ(400, map2.f2.width);
648 EXPECT_EQ(10, map2.f3);
653 //===----------------------------------------------------------------------===//
654 // Test flow sequences
655 //===----------------------------------------------------------------------===//
657 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
658 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
659 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
664 struct ScalarTraits<MyNumber> {
665 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
669 static StringRef input(StringRef scalar, void *, MyNumber &value) {
671 if ( getAsSignedInteger(scalar, 0, n) )
672 return "invalid number";
680 struct NameAndNumbers {
681 llvm::StringRef name;
682 std::vector<llvm::StringRef> strings;
683 std::vector<MyNumber> single;
684 std::vector<MyNumber> numbers;
690 struct MappingTraits<NameAndNumbers> {
691 static void mapping(IO &io, NameAndNumbers& nn) {
692 io.mapRequired("name", nn.name);
693 io.mapRequired("strings", nn.strings);
694 io.mapRequired("single", nn.single);
695 io.mapRequired("numbers", nn.numbers);
703 // Test writing then reading back custom values
705 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
706 std::string intermediate;
710 map.strings.push_back(llvm::StringRef("one"));
711 map.strings.push_back(llvm::StringRef("two"));
712 map.single.push_back(1);
713 map.numbers.push_back(10);
714 map.numbers.push_back(-30);
715 map.numbers.push_back(1024);
717 llvm::raw_string_ostream ostr(intermediate);
721 // Verify sequences were written in flow style
723 llvm::StringRef flowOut(intermediate);
724 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
725 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
729 Input yin(intermediate);
733 EXPECT_FALSE(yin.error());
734 EXPECT_TRUE(map2.name.equals("hello"));
735 EXPECT_EQ(map2.strings.size(), 2UL);
736 EXPECT_TRUE(map2.strings[0].equals("one"));
737 EXPECT_TRUE(map2.strings[1].equals("two"));
738 EXPECT_EQ(map2.single.size(), 1UL);
739 EXPECT_EQ(1, map2.single[0]);
740 EXPECT_EQ(map2.numbers.size(), 3UL);
741 EXPECT_EQ(10, map2.numbers[0]);
742 EXPECT_EQ(-30, map2.numbers[1]);
743 EXPECT_EQ(1024, map2.numbers[2]);
748 //===----------------------------------------------------------------------===//
749 // Test normalizing/denormalizing
750 //===----------------------------------------------------------------------===//
752 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
754 typedef std::vector<TotalSeconds> SecondsSequence;
756 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
762 struct MappingTraits<TotalSeconds> {
764 class NormalizedSeconds {
766 NormalizedSeconds(IO &io)
767 : hours(0), minutes(0), seconds(0) {
769 NormalizedSeconds(IO &, TotalSeconds &secs)
771 minutes((secs - (hours*3600))/60),
774 TotalSeconds denormalize(IO &) {
775 return TotalSeconds(hours*3600 + minutes*60 + seconds);
783 static void mapping(IO &io, TotalSeconds &secs) {
784 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
786 io.mapOptional("hours", keys->hours, (uint32_t)0);
787 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
788 io.mapRequired("seconds", keys->seconds);
796 // Test the reading of a yaml sequence of mappings
798 TEST(YAMLIO, TestReadMySecondsSequence) {
800 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
803 EXPECT_FALSE(yin.error());
804 EXPECT_EQ(seq.size(), 2UL);
805 EXPECT_EQ(seq[0], 3605U);
806 EXPECT_EQ(seq[1], 59U);
811 // Test writing then reading back custom values
813 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
814 std::string intermediate;
821 llvm::raw_string_ostream ostr(intermediate);
826 Input yin(intermediate);
827 SecondsSequence seq2;
830 EXPECT_FALSE(yin.error());
831 EXPECT_EQ(seq2.size(), 3UL);
832 EXPECT_EQ(seq2[0], 4000U);
833 EXPECT_EQ(seq2[1], 500U);
834 EXPECT_EQ(seq2[2], 59U);
839 //===----------------------------------------------------------------------===//
840 // Test dynamic typing
841 //===----------------------------------------------------------------------===//
860 struct KindAndFlags {
861 KindAndFlags() : kind(kindA), flags(0) { }
862 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
867 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
869 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
874 struct ScalarEnumerationTraits<AFlags> {
875 static void enumeration(IO &io, AFlags &value) {
876 io.enumCase(value, "a1", a1);
877 io.enumCase(value, "a2", a2);
878 io.enumCase(value, "a3", a3);
882 struct ScalarEnumerationTraits<BFlags> {
883 static void enumeration(IO &io, BFlags &value) {
884 io.enumCase(value, "b1", b1);
885 io.enumCase(value, "b2", b2);
886 io.enumCase(value, "b3", b3);
890 struct ScalarEnumerationTraits<Kind> {
891 static void enumeration(IO &io, Kind &value) {
892 io.enumCase(value, "A", kindA);
893 io.enumCase(value, "B", kindB);
897 struct MappingTraits<KindAndFlags> {
898 static void mapping(IO &io, KindAndFlags& kf) {
899 io.mapRequired("kind", kf.kind);
900 // Type of "flags" field varies depending on "kind" field.
901 // Use memcpy here to avoid breaking strict aliasing rules.
902 if (kf.kind == kindA) {
903 AFlags aflags = static_cast<AFlags>(kf.flags);
904 io.mapRequired("flags", aflags);
907 BFlags bflags = static_cast<BFlags>(kf.flags);
908 io.mapRequired("flags", bflags);
918 // Test the reading of a yaml sequence dynamic types
920 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
921 KindAndFlagsSequence seq;
922 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
925 EXPECT_FALSE(yin.error());
926 EXPECT_EQ(seq.size(), 2UL);
927 EXPECT_EQ(seq[0].kind, kindA);
928 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
929 EXPECT_EQ(seq[1].kind, kindB);
930 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
934 // Test writing then reading back dynamic types
936 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
937 std::string intermediate;
939 KindAndFlagsSequence seq;
940 seq.push_back(KindAndFlags(kindA,a1));
941 seq.push_back(KindAndFlags(kindB,b1));
942 seq.push_back(KindAndFlags(kindA,a2));
943 seq.push_back(KindAndFlags(kindB,b2));
944 seq.push_back(KindAndFlags(kindA,a3));
946 llvm::raw_string_ostream ostr(intermediate);
951 Input yin(intermediate);
952 KindAndFlagsSequence seq2;
955 EXPECT_FALSE(yin.error());
956 EXPECT_EQ(seq2.size(), 5UL);
957 EXPECT_EQ(seq2[0].kind, kindA);
958 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
959 EXPECT_EQ(seq2[1].kind, kindB);
960 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
961 EXPECT_EQ(seq2[2].kind, kindA);
962 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
963 EXPECT_EQ(seq2[3].kind, kindB);
964 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
965 EXPECT_EQ(seq2[4].kind, kindA);
966 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
971 //===----------------------------------------------------------------------===//
972 // Test document list
973 //===----------------------------------------------------------------------===//
979 typedef std::vector<FooBarMap> FooBarMapDocumentList;
981 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
987 struct MappingTraits<FooBarMap> {
988 static void mapping(IO &io, FooBarMap& fb) {
989 io.mapRequired("foo", fb.foo);
990 io.mapRequired("bar", fb.bar);
998 // Test the reading of a yaml mapping
1000 TEST(YAMLIO, TestDocRead) {
1002 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1005 EXPECT_FALSE(yin.error());
1006 EXPECT_EQ(doc.foo, 3);
1007 EXPECT_EQ(doc.bar,5);
1013 // Test writing then reading back a sequence of mappings
1015 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1016 std::string intermediate;
1024 std::vector<FooBarMap> docList;
1025 docList.push_back(doc1);
1026 docList.push_back(doc2);
1028 llvm::raw_string_ostream ostr(intermediate);
1035 Input yin(intermediate);
1036 std::vector<FooBarMap> docList2;
1039 EXPECT_FALSE(yin.error());
1040 EXPECT_EQ(docList2.size(), 2UL);
1041 FooBarMap& map1 = docList2[0];
1042 FooBarMap& map2 = docList2[1];
1043 EXPECT_EQ(map1.foo, 10);
1044 EXPECT_EQ(map1.bar, -3);
1045 EXPECT_EQ(map2.foo, 257);
1046 EXPECT_EQ(map2.bar, 0);
1050 //===----------------------------------------------------------------------===//
1051 // Test document tags
1052 //===----------------------------------------------------------------------===//
1055 MyDouble() : value(0.0) { }
1056 MyDouble(double x) : value(x) { }
1060 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1066 struct MappingTraits<MyDouble> {
1067 static void mapping(IO &io, MyDouble &d) {
1068 if (io.mapTag("!decimal", true)) {
1069 mappingDecimal(io, d);
1070 } else if (io.mapTag("!fraction")) {
1071 mappingFraction(io, d);
1074 static void mappingDecimal(IO &io, MyDouble &d) {
1075 io.mapRequired("value", d.value);
1077 static void mappingFraction(IO &io, MyDouble &d) {
1079 io.mapRequired("numerator", num);
1080 io.mapRequired("denominator", denom);
1081 // convert fraction to double
1082 d.value = num/denom;
1090 // Test the reading of two different tagged yaml documents.
1092 TEST(YAMLIO, TestTaggedDocuments) {
1093 std::vector<MyDouble> docList;
1094 Input yin("--- !decimal\nvalue: 3.0\n"
1095 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1097 EXPECT_FALSE(yin.error());
1098 EXPECT_EQ(docList.size(), 2UL);
1099 EXPECT_EQ(docList[0].value, 3.0);
1100 EXPECT_EQ(docList[1].value, 4.5);
1106 // Test writing then reading back tagged documents
1108 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1109 std::string intermediate;
1113 std::vector<MyDouble> docList;
1114 docList.push_back(a);
1115 docList.push_back(b);
1117 llvm::raw_string_ostream ostr(intermediate);
1123 Input yin(intermediate);
1124 std::vector<MyDouble> docList2;
1127 EXPECT_FALSE(yin.error());
1128 EXPECT_EQ(docList2.size(), 2UL);
1129 EXPECT_EQ(docList2[0].value, 10.25);
1130 EXPECT_EQ(docList2[1].value, -3.75);
1135 //===----------------------------------------------------------------------===//
1136 // Test mapping validation
1137 //===----------------------------------------------------------------------===//
1139 struct MyValidation {
1143 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1148 struct MappingTraits<MyValidation> {
1149 static void mapping(IO &io, MyValidation &d) {
1150 io.mapRequired("value", d.value);
1152 static StringRef validate(IO &io, MyValidation &d) {
1154 return "negative value";
1163 // Test that validate() is called and complains about the negative value.
1165 TEST(YAMLIO, TestValidatingInput) {
1166 std::vector<MyValidation> docList;
1167 Input yin("--- \nvalue: 3.0\n"
1168 "--- \nvalue: -1.0\n...\n",
1169 NULL, suppressErrorMessages);
1171 EXPECT_TRUE(yin.error());
1175 //===----------------------------------------------------------------------===//
1176 // Test error handling
1177 //===----------------------------------------------------------------------===//
1180 // Test error handling of unknown enumerated scalar
1182 TEST(YAMLIO, TestColorsReadError) {
1190 suppressErrorMessages);
1192 EXPECT_TRUE(yin.error());
1197 // Test error handling of flow sequence with unknown value
1199 TEST(YAMLIO, TestFlagsReadError) {
1203 "f2: [ round, hollow ]\n"
1207 suppressErrorMessages);
1210 EXPECT_TRUE(yin.error());
1215 // Test error handling reading built-in uint8_t type
1217 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1218 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1219 std::vector<uint8_t> seq;
1226 suppressErrorMessages);
1229 EXPECT_TRUE(yin.error());
1234 // Test error handling reading built-in uint16_t type
1236 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1237 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1238 std::vector<uint16_t> seq;
1245 suppressErrorMessages);
1248 EXPECT_TRUE(yin.error());
1253 // Test error handling reading built-in uint32_t type
1255 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1256 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1257 std::vector<uint32_t> seq;
1264 suppressErrorMessages);
1267 EXPECT_TRUE(yin.error());
1272 // Test error handling reading built-in uint64_t type
1274 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1275 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1276 std::vector<uint64_t> seq;
1278 "- 18446744073709551615\n"
1280 "- 19446744073709551615\n"
1283 suppressErrorMessages);
1286 EXPECT_TRUE(yin.error());
1291 // Test error handling reading built-in int8_t type
1293 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1294 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1295 std::vector<int8_t> seq;
1303 suppressErrorMessages);
1306 EXPECT_TRUE(yin.error());
1310 // Test error handling reading built-in int8_t type
1312 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1313 std::vector<int8_t> seq;
1321 suppressErrorMessages);
1324 EXPECT_TRUE(yin.error());
1329 // Test error handling reading built-in int16_t type
1331 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1332 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1333 std::vector<int16_t> seq;
1341 suppressErrorMessages);
1344 EXPECT_TRUE(yin.error());
1349 // Test error handling reading built-in int16_t type
1351 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1352 std::vector<int16_t> seq;
1360 suppressErrorMessages);
1363 EXPECT_TRUE(yin.error());
1368 // Test error handling reading built-in int32_t type
1370 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1371 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1372 std::vector<int32_t> seq;
1380 suppressErrorMessages);
1383 EXPECT_TRUE(yin.error());
1387 // Test error handling reading built-in int32_t type
1389 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1390 std::vector<int32_t> seq;
1398 suppressErrorMessages);
1401 EXPECT_TRUE(yin.error());
1406 // Test error handling reading built-in int64_t type
1408 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1409 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1410 std::vector<int64_t> seq;
1412 "- -9223372036854775808\n"
1414 "- 9223372036854775807\n"
1415 "- -9223372036854775809\n"
1418 suppressErrorMessages);
1421 EXPECT_TRUE(yin.error());
1425 // Test error handling reading built-in int64_t type
1427 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1428 std::vector<int64_t> seq;
1430 "- -9223372036854775808\n"
1432 "- 9223372036854775807\n"
1433 "- 9223372036854775809\n"
1436 suppressErrorMessages);
1439 EXPECT_TRUE(yin.error());
1443 // Test error handling reading built-in float type
1445 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1446 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1447 std::vector<float> seq;
1455 suppressErrorMessages);
1458 EXPECT_TRUE(yin.error());
1462 // Test error handling reading built-in float type
1464 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1465 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1466 std::vector<double> seq;
1474 suppressErrorMessages);
1477 EXPECT_TRUE(yin.error());
1481 // Test error handling reading built-in Hex8 type
1483 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1484 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1485 std::vector<Hex8> seq;
1492 suppressErrorMessages);
1495 EXPECT_TRUE(yin.error());
1500 // Test error handling reading built-in Hex16 type
1502 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1503 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1504 std::vector<Hex16> seq;
1511 suppressErrorMessages);
1514 EXPECT_TRUE(yin.error());
1518 // Test error handling reading built-in Hex32 type
1520 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1521 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1522 std::vector<Hex32> seq;
1529 suppressErrorMessages);
1532 EXPECT_TRUE(yin.error());
1536 // Test error handling reading built-in Hex64 type
1538 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1539 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1540 std::vector<Hex64> seq;
1543 "- 0xFFEEDDCCBBAA9988\n"
1544 "- 0x12345567890ABCDEF0\n"
1547 suppressErrorMessages);
1550 EXPECT_TRUE(yin.error());
1553 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
1556 // We pass the suppressErrorMessages handler to handle the error
1557 // message generated in the constructor of Input.
1558 Input yin("{foo:3, bar: 5}", /*Ctxt=*/NULL, suppressErrorMessages);
1560 EXPECT_TRUE(yin.error());
1564 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/NULL, suppressErrorMessages);
1566 EXPECT_TRUE(yin.error());
1570 struct OptionalTest {
1571 std::vector<int> Numbers;
1574 struct OptionalTestSeq {
1575 std::vector<OptionalTest> Tests;
1578 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1582 struct MappingTraits<OptionalTest> {
1583 static void mapping(IO& IO, OptionalTest &OT) {
1584 IO.mapOptional("Numbers", OT.Numbers);
1589 struct MappingTraits<OptionalTestSeq> {
1590 static void mapping(IO &IO, OptionalTestSeq &OTS) {
1591 IO.mapOptional("Tests", OTS.Tests);
1597 TEST(YAMLIO, SequenceElideTest) {
1598 // Test that writing out a purely optional structure with its fields set to
1599 // default followed by other data is properly read back in.
1600 OptionalTestSeq Seq;
1601 OptionalTest One, Two, Three, Four;
1602 int N[] = {1, 2, 3};
1603 Three.Numbers.assign(N, N + 3);
1604 Seq.Tests.push_back(One);
1605 Seq.Tests.push_back(Two);
1606 Seq.Tests.push_back(Three);
1607 Seq.Tests.push_back(Four);
1609 std::string intermediate;
1611 llvm::raw_string_ostream ostr(intermediate);
1616 Input yin(intermediate);
1617 OptionalTestSeq Seq2;
1620 EXPECT_FALSE(yin.error());
1622 EXPECT_EQ(4UL, Seq2.Tests.size());
1624 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1625 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1627 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1628 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1629 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1631 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
1634 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
1638 EXPECT_TRUE(yin.error());
1641 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
1645 EXPECT_FALSE(yin.error());
1648 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
1649 std::vector<uint8_t> seq;
1650 Input yin("", /*Ctxt=*/NULL, suppressErrorMessages);
1653 EXPECT_FALSE(yin.error());
1654 EXPECT_TRUE(seq.empty());