1 //===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/BitmaskEnum.h"
10 #include "llvm/ADT/StringMap.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Support/Casting.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/YAMLTraits.h"
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
21 using llvm::yaml::Hex16
;
22 using llvm::yaml::Hex32
;
23 using llvm::yaml::Hex64
;
24 using llvm::yaml::Hex8
;
25 using llvm::yaml::Input
;
26 using llvm::yaml::isNumeric
;
27 using llvm::yaml::MappingNormalization
;
28 using llvm::yaml::MappingTraits
;
29 using llvm::yaml::Output
;
30 using llvm::yaml::ScalarTraits
;
31 using ::testing::StartsWith
;
36 static void suppressErrorMessages(const llvm::SMDiagnostic
&, void *) {
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
49 typedef std::vector
<FooBar
> FooBarSequence
;
51 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar
)
53 struct FooBarContainer
{
60 struct MappingTraits
<FooBar
> {
61 static void mapping(IO
&io
, FooBar
& fb
) {
62 io
.mapRequired("foo", fb
.foo
);
63 io
.mapRequired("bar", fb
.bar
);
67 template <> struct MappingTraits
<FooBarContainer
> {
68 static void mapping(IO
&io
, FooBarContainer
&fb
) {
69 io
.mapRequired("fbs", fb
.fbs
);
77 // Test the reading of a yaml mapping
79 TEST(YAMLIO
, TestMapRead
) {
82 Input
yin("---\nfoo: 3\nbar: 5\n...\n");
85 EXPECT_FALSE(yin
.error());
86 EXPECT_EQ(doc
.foo
, 3);
87 EXPECT_EQ(doc
.bar
, 5);
91 Input
yin("{foo: 3, bar: 5}");
94 EXPECT_FALSE(yin
.error());
95 EXPECT_EQ(doc
.foo
, 3);
96 EXPECT_EQ(doc
.bar
, 5);
100 Input
yin("{\"foo\": 3\n, \"bar\": 5}");
103 EXPECT_FALSE(yin
.error());
104 EXPECT_EQ(doc
.foo
, 3);
105 EXPECT_EQ(doc
.bar
, 5);
109 TEST(YAMLIO
, TestMalformedMapRead
) {
111 Input
yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages
);
113 EXPECT_TRUE(!!yin
.error());
116 TEST(YAMLIO
, TestMapDuplicatedKeysRead
) {
117 auto testDiagnostic
= [](const llvm::SMDiagnostic
&Error
, void *) {
118 EXPECT_EQ(Error
.getMessage(), "duplicated mapping key 'foo'");
121 Input
yin("{foo: 3, bar: 5, foo: 4}", nullptr, testDiagnostic
);
123 EXPECT_TRUE(!!yin
.error());
127 // Test the reading of a yaml sequence of mappings
129 TEST(YAMLIO
, TestSequenceMapRead
) {
131 Input
yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
134 EXPECT_FALSE(yin
.error());
135 EXPECT_EQ(seq
.size(), 2UL);
136 FooBar
& map1
= seq
[0];
137 FooBar
& map2
= seq
[1];
138 EXPECT_EQ(map1
.foo
, 3);
139 EXPECT_EQ(map1
.bar
, 5);
140 EXPECT_EQ(map2
.foo
, 7);
141 EXPECT_EQ(map2
.bar
, 9);
145 // Test the reading of a map containing a yaml sequence of mappings
147 TEST(YAMLIO
, TestContainerSequenceMapRead
) {
149 FooBarContainer cont
;
150 Input
yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
153 EXPECT_FALSE(yin2
.error());
154 EXPECT_EQ(cont
.fbs
.size(), 2UL);
155 EXPECT_EQ(cont
.fbs
[0].foo
, 3);
156 EXPECT_EQ(cont
.fbs
[0].bar
, 5);
157 EXPECT_EQ(cont
.fbs
[1].foo
, 7);
158 EXPECT_EQ(cont
.fbs
[1].bar
, 9);
162 FooBarContainer cont
;
163 Input
yin("---\nfbs:\n...\n");
165 // Okay: Empty node represents an empty array.
166 EXPECT_FALSE(yin
.error());
167 EXPECT_EQ(cont
.fbs
.size(), 0UL);
171 FooBarContainer cont
;
172 Input
yin("---\nfbs: !!null null\n...\n");
174 // Okay: null represents an empty array.
175 EXPECT_FALSE(yin
.error());
176 EXPECT_EQ(cont
.fbs
.size(), 0UL);
180 FooBarContainer cont
;
181 Input
yin("---\nfbs: ~\n...\n");
183 // Okay: null represents an empty array.
184 EXPECT_FALSE(yin
.error());
185 EXPECT_EQ(cont
.fbs
.size(), 0UL);
189 FooBarContainer cont
;
190 Input
yin("---\nfbs: null\n...\n");
192 // Okay: null represents an empty array.
193 EXPECT_FALSE(yin
.error());
194 EXPECT_EQ(cont
.fbs
.size(), 0UL);
199 // Test the reading of a map containing a malformed yaml sequence
201 TEST(YAMLIO
, TestMalformedContainerSequenceMapRead
) {
203 FooBarContainer cont
;
204 Input
yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
205 suppressErrorMessages
);
207 // Error: fbs is not a sequence.
208 EXPECT_TRUE(!!yin
.error());
209 EXPECT_EQ(cont
.fbs
.size(), 0UL);
213 FooBarContainer cont
;
214 Input
yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages
);
216 // This should be an error.
217 EXPECT_TRUE(!!yin
.error());
218 EXPECT_EQ(cont
.fbs
.size(), 0UL);
223 // Test writing then reading back a sequence of mappings
225 TEST(YAMLIO
, TestSequenceMapWriteAndRead
) {
226 std::string intermediate
;
235 seq
.push_back(entry1
);
236 seq
.push_back(entry2
);
238 llvm::raw_string_ostream
ostr(intermediate
);
244 Input
yin(intermediate
);
248 EXPECT_FALSE(yin
.error());
249 EXPECT_EQ(seq2
.size(), 2UL);
250 FooBar
& map1
= seq2
[0];
251 FooBar
& map2
= seq2
[1];
252 EXPECT_EQ(map1
.foo
, 10);
253 EXPECT_EQ(map1
.bar
, -3);
254 EXPECT_EQ(map2
.foo
, 257);
255 EXPECT_EQ(map2
.bar
, 0);
260 // Test reading the entire struct as an enum.
266 bool operator==(const FooBarEnum
&R
) const {
267 return Foo
== R
.Foo
&& Bar
== R
.Bar
;
273 template <> struct MappingTraits
<FooBarEnum
> {
274 static void enumInput(IO
&io
, FooBarEnum
&Val
) {
275 io
.enumCase(Val
, "OnlyFoo", FooBarEnum({1, 0}));
276 io
.enumCase(Val
, "OnlyBar", FooBarEnum({0, 1}));
278 static void mapping(IO
&io
, FooBarEnum
&Val
) {
279 io
.mapOptional("Foo", Val
.Foo
);
280 io
.mapOptional("Bar", Val
.Bar
);
286 TEST(YAMLIO
, TestMapEnumRead
) {
289 Input
Yin("OnlyFoo");
291 EXPECT_FALSE(Yin
.error());
292 EXPECT_EQ(Doc
.Foo
, 1);
293 EXPECT_EQ(Doc
.Bar
, 0);
296 Input
Yin("OnlyBar");
298 EXPECT_FALSE(Yin
.error());
299 EXPECT_EQ(Doc
.Foo
, 0);
300 EXPECT_EQ(Doc
.Bar
, 1);
303 Input
Yin("{Foo: 3, Bar: 5}");
305 EXPECT_FALSE(Yin
.error());
306 EXPECT_EQ(Doc
.Foo
, 3);
307 EXPECT_EQ(Doc
.Bar
, 5);
312 // Test YAML filename handling.
314 static void testErrorFilename(const llvm::SMDiagnostic
&Error
, void *) {
315 EXPECT_EQ(Error
.getFilename(), "foo.yaml");
318 TEST(YAMLIO
, TestGivenFilename
) {
319 auto Buffer
= llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
320 Input
yin(*Buffer
, nullptr, testErrorFilename
);
324 EXPECT_TRUE(!!yin
.error());
327 struct WithStringField
{
335 template <> struct MappingTraits
<WithStringField
> {
336 static void mapping(IO
&io
, WithStringField
&fb
) {
337 io
.mapRequired("str1", fb
.str1
);
338 io
.mapRequired("str2", fb
.str2
);
339 io
.mapRequired("str3", fb
.str3
);
345 TEST(YAMLIO
, MultilineStrings
) {
346 WithStringField Original
;
347 Original
.str1
= "a multiline string\nfoobarbaz";
348 Original
.str2
= "another one\rfoobarbaz";
349 Original
.str3
= "a one-line string";
351 std::string Serialized
;
353 llvm::raw_string_ostream
OS(Serialized
);
357 auto Expected
= "---\n"
358 "str1: \"a multiline string\\nfoobarbaz\"\n"
359 "str2: \"another one\\rfoobarbaz\"\n"
360 "str3: a one-line string\n"
362 ASSERT_EQ(Serialized
, Expected
);
364 // Also check it parses back without the errors.
365 WithStringField Deserialized
;
367 Input
YIn(Serialized
);
369 ASSERT_FALSE(YIn
.error())
370 << "Parsing error occurred during deserialization. Serialized string:\n"
373 EXPECT_EQ(Original
.str1
, Deserialized
.str1
);
374 EXPECT_EQ(Original
.str2
, Deserialized
.str2
);
375 EXPECT_EQ(Original
.str3
, Deserialized
.str3
);
378 TEST(YAMLIO
, NoQuotesForTab
) {
379 WithStringField WithTab
;
380 WithTab
.str1
= "aba\tcaba";
381 std::string Serialized
;
383 llvm::raw_string_ostream
OS(Serialized
);
387 auto ExpectedPrefix
= "---\n"
389 EXPECT_THAT(Serialized
, StartsWith(ExpectedPrefix
));
392 //===----------------------------------------------------------------------===//
393 // Test built-in types
394 //===----------------------------------------------------------------------===//
396 struct BuiltInTypes
{
419 struct MappingTraits
<BuiltInTypes
> {
420 static void mapping(IO
&io
, BuiltInTypes
& bt
) {
421 io
.mapRequired("str", bt
.str
);
422 io
.mapRequired("stdstr", bt
.stdstr
);
423 io
.mapRequired("u64", bt
.u64
);
424 io
.mapRequired("u32", bt
.u32
);
425 io
.mapRequired("u16", bt
.u16
);
426 io
.mapRequired("u8", bt
.u8
);
427 io
.mapRequired("b", bt
.b
);
428 io
.mapRequired("s64", bt
.s64
);
429 io
.mapRequired("s32", bt
.s32
);
430 io
.mapRequired("s16", bt
.s16
);
431 io
.mapRequired("s8", bt
.s8
);
432 io
.mapRequired("f", bt
.f
);
433 io
.mapRequired("d", bt
.d
);
434 io
.mapRequired("h8", bt
.h8
);
435 io
.mapRequired("h16", bt
.h16
);
436 io
.mapRequired("h32", bt
.h32
);
437 io
.mapRequired("h64", bt
.h64
);
445 // Test the reading of all built-in scalar conversions
447 TEST(YAMLIO
, TestReadBuiltInTypes
) {
451 "stdstr: hello where?\n"
466 "h64: 0xFEDCBA9876543210\n"
470 EXPECT_FALSE(yin
.error());
471 EXPECT_EQ(map
.str
, "hello there");
472 EXPECT_EQ(map
.stdstr
, "hello where?");
473 EXPECT_EQ(map
.u64
, 5000000000ULL);
474 EXPECT_EQ(map
.u32
, 4000000000U);
475 EXPECT_EQ(map
.u16
, 65000);
476 EXPECT_EQ(map
.u8
, 255);
477 EXPECT_EQ(map
.b
, false);
478 EXPECT_EQ(map
.s64
, -5000000000LL);
479 EXPECT_EQ(map
.s32
, -2000000000L);
480 EXPECT_EQ(map
.s16
, -32000);
481 EXPECT_EQ(map
.s8
, -127);
482 EXPECT_EQ(map
.f
, 137.125);
483 EXPECT_EQ(map
.d
, -2.8625);
484 EXPECT_EQ(map
.h8
, Hex8(255));
485 EXPECT_EQ(map
.h16
, Hex16(0x8765));
486 EXPECT_EQ(map
.h32
, Hex32(0xFEDCBA98));
487 EXPECT_EQ(map
.h64
, Hex64(0xFEDCBA9876543210LL
));
492 // Test writing then reading back all built-in scalar types
494 TEST(YAMLIO
, TestReadWriteBuiltInTypes
) {
495 std::string intermediate
;
499 map
.stdstr
= "three four";
500 map
.u64
= 6000000000ULL;
501 map
.u32
= 3000000000U;
505 map
.s64
= -6000000000LL;
506 map
.s32
= -2000000000;
513 map
.h32
= 3000000000U;
514 map
.h64
= 6000000000LL;
516 llvm::raw_string_ostream
ostr(intermediate
);
522 Input
yin(intermediate
);
526 EXPECT_FALSE(yin
.error());
527 EXPECT_EQ(map
.str
, "one two");
528 EXPECT_EQ(map
.stdstr
, "three four");
529 EXPECT_EQ(map
.u64
, 6000000000ULL);
530 EXPECT_EQ(map
.u32
, 3000000000U);
531 EXPECT_EQ(map
.u16
, 50000);
532 EXPECT_EQ(map
.u8
, 254);
533 EXPECT_EQ(map
.b
, true);
534 EXPECT_EQ(map
.s64
, -6000000000LL);
535 EXPECT_EQ(map
.s32
, -2000000000L);
536 EXPECT_EQ(map
.s16
, -32000);
537 EXPECT_EQ(map
.s8
, -128);
538 EXPECT_EQ(map
.f
, 3.25);
539 EXPECT_EQ(map
.d
, -2.8625);
540 EXPECT_EQ(map
.h8
, Hex8(254));
541 EXPECT_EQ(map
.h16
, Hex16(50000));
542 EXPECT_EQ(map
.h32
, Hex32(3000000000U));
543 EXPECT_EQ(map
.h64
, Hex64(6000000000LL));
547 //===----------------------------------------------------------------------===//
548 // Test endian-aware types
549 //===----------------------------------------------------------------------===//
552 typedef llvm::support::detail::packed_endian_specific_integral
<
553 float, llvm::endianness::little
, llvm::support::unaligned
>
555 typedef llvm::support::detail::packed_endian_specific_integral
<
556 double, llvm::endianness::little
, llvm::support::unaligned
>
559 llvm::support::ulittle64_t u64
;
560 llvm::support::ulittle32_t u32
;
561 llvm::support::ulittle16_t u16
;
562 llvm::support::little64_t s64
;
563 llvm::support::little32_t s32
;
564 llvm::support::little16_t s16
;
571 template <> struct MappingTraits
<EndianTypes
> {
572 static void mapping(IO
&io
, EndianTypes
&et
) {
573 io
.mapRequired("u64", et
.u64
);
574 io
.mapRequired("u32", et
.u32
);
575 io
.mapRequired("u16", et
.u16
);
576 io
.mapRequired("s64", et
.s64
);
577 io
.mapRequired("s32", et
.s32
);
578 io
.mapRequired("s16", et
.s16
);
579 io
.mapRequired("f", et
.f
);
580 io
.mapRequired("d", et
.d
);
587 // Test the reading of all endian scalar conversions
589 TEST(YAMLIO
, TestReadEndianTypes
) {
603 EXPECT_FALSE(yin
.error());
604 EXPECT_EQ(map
.u64
, 5000000000ULL);
605 EXPECT_EQ(map
.u32
, 4000000000U);
606 EXPECT_EQ(map
.u16
, 65000);
607 EXPECT_EQ(map
.s64
, -5000000000LL);
608 EXPECT_EQ(map
.s32
, -2000000000L);
609 EXPECT_EQ(map
.s16
, -32000);
610 EXPECT_EQ(map
.f
, 3.25f
);
611 EXPECT_EQ(map
.d
, -2.8625);
615 // Test writing then reading back all endian-aware scalar types
617 TEST(YAMLIO
, TestReadWriteEndianTypes
) {
618 std::string intermediate
;
621 map
.u64
= 6000000000ULL;
622 map
.u32
= 3000000000U;
624 map
.s64
= -6000000000LL;
625 map
.s32
= -2000000000;
630 llvm::raw_string_ostream
ostr(intermediate
);
636 Input
yin(intermediate
);
640 EXPECT_FALSE(yin
.error());
641 EXPECT_EQ(map
.u64
, 6000000000ULL);
642 EXPECT_EQ(map
.u32
, 3000000000U);
643 EXPECT_EQ(map
.u16
, 50000);
644 EXPECT_EQ(map
.s64
, -6000000000LL);
645 EXPECT_EQ(map
.s32
, -2000000000L);
646 EXPECT_EQ(map
.s16
, -32000);
647 EXPECT_EQ(map
.f
, 3.25f
);
648 EXPECT_EQ(map
.d
, -2.8625);
652 enum class Enum
: uint16_t { One
, Two
};
653 enum class BitsetEnum
: uint16_t {
656 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ OneZero
),
658 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
660 llvm::support::little_t
<Enum
> LittleEnum
;
661 llvm::support::big_t
<Enum
> BigEnum
;
662 llvm::support::little_t
<BitsetEnum
> LittleBitset
;
663 llvm::support::big_t
<BitsetEnum
> BigBitset
;
667 template <> struct ScalarEnumerationTraits
<Enum
> {
668 static void enumeration(IO
&io
, Enum
&E
) {
669 io
.enumCase(E
, "One", Enum::One
);
670 io
.enumCase(E
, "Two", Enum::Two
);
674 template <> struct ScalarBitSetTraits
<BitsetEnum
> {
675 static void bitset(IO
&io
, BitsetEnum
&E
) {
676 io
.bitSetCase(E
, "ZeroOne", BitsetEnum::ZeroOne
);
677 io
.bitSetCase(E
, "OneZero", BitsetEnum::OneZero
);
681 template <> struct MappingTraits
<EndianEnums
> {
682 static void mapping(IO
&io
, EndianEnums
&EE
) {
683 io
.mapRequired("LittleEnum", EE
.LittleEnum
);
684 io
.mapRequired("BigEnum", EE
.BigEnum
);
685 io
.mapRequired("LittleBitset", EE
.LittleBitset
);
686 io
.mapRequired("BigBitset", EE
.BigBitset
);
692 TEST(YAMLIO
, TestReadEndianEnums
) {
697 "LittleBitset: [ ZeroOne ]\n"
698 "BigBitset: [ ZeroOne, OneZero ]\n"
702 EXPECT_FALSE(yin
.error());
703 EXPECT_EQ(Enum::One
, map
.LittleEnum
);
704 EXPECT_EQ(Enum::Two
, map
.BigEnum
);
705 EXPECT_EQ(BitsetEnum::ZeroOne
, map
.LittleBitset
);
706 EXPECT_EQ(BitsetEnum::ZeroOne
| BitsetEnum::OneZero
, map
.BigBitset
);
709 TEST(YAMLIO
, TestReadWriteEndianEnums
) {
710 std::string intermediate
;
713 map
.LittleEnum
= Enum::Two
;
714 map
.BigEnum
= Enum::One
;
715 map
.LittleBitset
= BitsetEnum::OneZero
| BitsetEnum::ZeroOne
;
716 map
.BigBitset
= BitsetEnum::OneZero
;
718 llvm::raw_string_ostream
ostr(intermediate
);
724 Input
yin(intermediate
);
728 EXPECT_FALSE(yin
.error());
729 EXPECT_EQ(Enum::Two
, map
.LittleEnum
);
730 EXPECT_EQ(Enum::One
, map
.BigEnum
);
731 EXPECT_EQ(BitsetEnum::OneZero
| BitsetEnum::ZeroOne
, map
.LittleBitset
);
732 EXPECT_EQ(BitsetEnum::OneZero
, map
.BigBitset
);
737 llvm::StringRef str1
;
738 llvm::StringRef str2
;
739 llvm::StringRef str3
;
740 llvm::StringRef str4
;
741 llvm::StringRef str5
;
742 llvm::StringRef str6
;
743 llvm::StringRef str7
;
744 llvm::StringRef str8
;
745 llvm::StringRef str9
;
746 llvm::StringRef str10
;
747 llvm::StringRef str11
;
757 std::string stdstr10
;
758 std::string stdstr11
;
759 std::string stdstr12
;
760 std::string stdstr13
;
766 struct MappingTraits
<StringTypes
> {
767 static void mapping(IO
&io
, StringTypes
& st
) {
768 io
.mapRequired("str1", st
.str1
);
769 io
.mapRequired("str2", st
.str2
);
770 io
.mapRequired("str3", st
.str3
);
771 io
.mapRequired("str4", st
.str4
);
772 io
.mapRequired("str5", st
.str5
);
773 io
.mapRequired("str6", st
.str6
);
774 io
.mapRequired("str7", st
.str7
);
775 io
.mapRequired("str8", st
.str8
);
776 io
.mapRequired("str9", st
.str9
);
777 io
.mapRequired("str10", st
.str10
);
778 io
.mapRequired("str11", st
.str11
);
779 io
.mapRequired("stdstr1", st
.stdstr1
);
780 io
.mapRequired("stdstr2", st
.stdstr2
);
781 io
.mapRequired("stdstr3", st
.stdstr3
);
782 io
.mapRequired("stdstr4", st
.stdstr4
);
783 io
.mapRequired("stdstr5", st
.stdstr5
);
784 io
.mapRequired("stdstr6", st
.stdstr6
);
785 io
.mapRequired("stdstr7", st
.stdstr7
);
786 io
.mapRequired("stdstr8", st
.stdstr8
);
787 io
.mapRequired("stdstr9", st
.stdstr9
);
788 io
.mapRequired("stdstr10", st
.stdstr10
);
789 io
.mapRequired("stdstr11", st
.stdstr11
);
790 io
.mapRequired("stdstr12", st
.stdstr12
);
791 io
.mapRequired("stdstr13", st
.stdstr13
);
797 TEST(YAMLIO
, TestReadWriteStringTypes
) {
798 std::string intermediate
;
806 map
.str6
= "0000000004000000";
810 map
.str10
= "0.2e20";
812 map
.stdstr1
= "'eee";
813 map
.stdstr2
= "\"fff";
814 map
.stdstr3
= "`ggg";
815 map
.stdstr4
= "@hhh";
817 map
.stdstr6
= "0000000004000000";
818 map
.stdstr7
= "true";
819 map
.stdstr8
= "FALSE";
821 map
.stdstr10
= "0.2e20";
822 map
.stdstr11
= "0x30";
823 map
.stdstr12
= "- match";
824 map
.stdstr13
.assign("\0a\0b\0", 5);
826 llvm::raw_string_ostream
ostr(intermediate
);
831 llvm::StringRef
flowOut(intermediate
);
832 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'''aaa"));
833 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'\"bbb'"));
834 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'`ccc'"));
835 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'@ddd'"));
836 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("''\n"));
837 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0000000004000000'\n"));
838 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'true'\n"));
839 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'FALSE'\n"));
840 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'~'\n"));
841 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0.2e20'\n"));
842 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0x30'\n"));
843 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'- match'\n"));
844 EXPECT_NE(std::string::npos
, flowOut
.find("'''eee"));
845 EXPECT_NE(std::string::npos
, flowOut
.find("'\"fff'"));
846 EXPECT_NE(std::string::npos
, flowOut
.find("'`ggg'"));
847 EXPECT_NE(std::string::npos
, flowOut
.find("'@hhh'"));
848 EXPECT_NE(std::string::npos
, flowOut
.find("''\n"));
849 EXPECT_NE(std::string::npos
, flowOut
.find("'0000000004000000'\n"));
850 EXPECT_NE(std::string::npos
, flowOut
.find("\"\\0a\\0b\\0\""));
853 Input
yin(intermediate
);
857 EXPECT_FALSE(yin
.error());
858 EXPECT_EQ(map
.str1
, "'aaa");
859 EXPECT_EQ(map
.str2
, "\"bbb");
860 EXPECT_EQ(map
.str3
, "`ccc");
861 EXPECT_EQ(map
.str4
, "@ddd");
862 EXPECT_EQ(map
.str5
, "");
863 EXPECT_EQ(map
.str6
, "0000000004000000");
864 EXPECT_EQ(map
.stdstr1
, "'eee");
865 EXPECT_EQ(map
.stdstr2
, "\"fff");
866 EXPECT_EQ(map
.stdstr3
, "`ggg");
867 EXPECT_EQ(map
.stdstr4
, "@hhh");
868 EXPECT_EQ(map
.stdstr5
, "");
869 EXPECT_EQ(map
.stdstr6
, "0000000004000000");
870 EXPECT_EQ(std::string("\0a\0b\0", 5), map
.stdstr13
);
874 //===----------------------------------------------------------------------===//
875 // Test ScalarEnumerationTraits
876 //===----------------------------------------------------------------------===//
897 struct ScalarEnumerationTraits
<Colors
> {
898 static void enumeration(IO
&io
, Colors
&value
) {
899 io
.enumCase(value
, "red", cRed
);
900 io
.enumCase(value
, "blue", cBlue
);
901 io
.enumCase(value
, "green", cGreen
);
902 io
.enumCase(value
, "yellow",cYellow
);
906 struct MappingTraits
<ColorMap
> {
907 static void mapping(IO
&io
, ColorMap
& c
) {
908 io
.mapRequired("c1", c
.c1
);
909 io
.mapRequired("c2", c
.c2
);
910 io
.mapRequired("c3", c
.c3
);
911 io
.mapOptional("c4", c
.c4
, cBlue
); // supplies default
912 io
.mapOptional("c5", c
.c5
, cYellow
); // supplies default
913 io
.mapOptional("c6", c
.c6
, cRed
); // supplies default
921 // Test reading enumerated scalars
923 TEST(YAMLIO
, TestEnumRead
) {
933 EXPECT_FALSE(yin
.error());
934 EXPECT_EQ(cBlue
, map
.c1
);
935 EXPECT_EQ(cRed
, map
.c2
);
936 EXPECT_EQ(cGreen
, map
.c3
);
937 EXPECT_EQ(cBlue
, map
.c4
); // tests default
938 EXPECT_EQ(cYellow
,map
.c5
); // tests overridden
939 EXPECT_EQ(cRed
, map
.c6
); // tests default
944 //===----------------------------------------------------------------------===//
945 // Test ScalarBitSetTraits
946 //===----------------------------------------------------------------------===//
955 inline MyFlags
operator|(MyFlags a
, MyFlags b
) {
956 return static_cast<MyFlags
>(
957 static_cast<uint32_t>(a
) | static_cast<uint32_t>(b
));
971 struct ScalarBitSetTraits
<MyFlags
> {
972 static void bitset(IO
&io
, MyFlags
&value
) {
973 io
.bitSetCase(value
, "big", flagBig
);
974 io
.bitSetCase(value
, "flat", flagFlat
);
975 io
.bitSetCase(value
, "round", flagRound
);
976 io
.bitSetCase(value
, "pointy",flagPointy
);
980 struct MappingTraits
<FlagsMap
> {
981 static void mapping(IO
&io
, FlagsMap
& c
) {
982 io
.mapRequired("f1", c
.f1
);
983 io
.mapRequired("f2", c
.f2
);
984 io
.mapRequired("f3", c
.f3
);
985 io
.mapOptional("f4", c
.f4
, flagRound
);
993 // Test reading flow sequence representing bit-mask values
995 TEST(YAMLIO
, TestFlagsRead
) {
999 "f2: [ round, flat ]\n"
1004 EXPECT_FALSE(yin
.error());
1005 EXPECT_EQ(flagBig
, map
.f1
);
1006 EXPECT_EQ(flagRound
|flagFlat
, map
.f2
);
1007 EXPECT_EQ(flagNone
, map
.f3
); // check empty set
1008 EXPECT_EQ(flagRound
, map
.f4
); // check optional key
1013 // Test writing then reading back bit-mask values
1015 TEST(YAMLIO
, TestReadWriteFlags
) {
1016 std::string intermediate
;
1020 map
.f2
= flagRound
| flagFlat
;
1024 llvm::raw_string_ostream
ostr(intermediate
);
1030 Input
yin(intermediate
);
1034 EXPECT_FALSE(yin
.error());
1035 EXPECT_EQ(flagBig
, map2
.f1
);
1036 EXPECT_EQ(flagRound
|flagFlat
, map2
.f2
);
1037 EXPECT_EQ(flagNone
, map2
.f3
);
1038 //EXPECT_EQ(flagRound, map2.f4); // check optional key
1044 //===----------------------------------------------------------------------===//
1045 // Test ScalarTraits
1046 //===----------------------------------------------------------------------===//
1048 struct MyCustomType
{
1053 struct MyCustomTypeMap
{
1063 struct MappingTraits
<MyCustomTypeMap
> {
1064 static void mapping(IO
&io
, MyCustomTypeMap
& s
) {
1065 io
.mapRequired("f1", s
.f1
);
1066 io
.mapRequired("f2", s
.f2
);
1067 io
.mapRequired("f3", s
.f3
);
1070 // MyCustomType is formatted as a yaml scalar. A value of
1071 // {length=3, width=4} would be represented in yaml as "3 by 4".
1073 struct ScalarTraits
<MyCustomType
> {
1074 static void output(const MyCustomType
&value
, void* ctxt
, llvm::raw_ostream
&out
) {
1075 out
<< llvm::format("%d by %d", value
.length
, value
.width
);
1077 static StringRef
input(StringRef scalar
, void* ctxt
, MyCustomType
&value
) {
1078 size_t byStart
= scalar
.find("by");
1079 if ( byStart
!= StringRef::npos
) {
1080 StringRef lenStr
= scalar
.slice(0, byStart
);
1081 lenStr
= lenStr
.rtrim();
1082 if ( lenStr
.getAsInteger(0, value
.length
) ) {
1083 return "malformed length";
1085 StringRef widthStr
= scalar
.drop_front(byStart
+2);
1086 widthStr
= widthStr
.ltrim();
1087 if ( widthStr
.getAsInteger(0, value
.width
) ) {
1088 return "malformed width";
1093 return "malformed by";
1096 static QuotingType
mustQuote(StringRef
) { return QuotingType::Single
; }
1103 // Test writing then reading back custom values
1105 TEST(YAMLIO
, TestReadWriteMyCustomType
) {
1106 std::string intermediate
;
1108 MyCustomTypeMap map
;
1111 map
.f2
.length
= 100;
1115 llvm::raw_string_ostream
ostr(intermediate
);
1121 Input
yin(intermediate
);
1122 MyCustomTypeMap map2
;
1125 EXPECT_FALSE(yin
.error());
1126 EXPECT_EQ(1, map2
.f1
.length
);
1127 EXPECT_EQ(4, map2
.f1
.width
);
1128 EXPECT_EQ(100, map2
.f2
.length
);
1129 EXPECT_EQ(400, map2
.f2
.width
);
1130 EXPECT_EQ(10, map2
.f3
);
1135 //===----------------------------------------------------------------------===//
1136 // Test BlockScalarTraits
1137 //===----------------------------------------------------------------------===//
1139 struct MultilineStringType
{
1143 struct MultilineStringTypeMap
{
1144 MultilineStringType name
;
1145 MultilineStringType description
;
1146 MultilineStringType ingredients
;
1147 MultilineStringType recipes
;
1148 MultilineStringType warningLabels
;
1149 MultilineStringType documentation
;
1156 struct MappingTraits
<MultilineStringTypeMap
> {
1157 static void mapping(IO
&io
, MultilineStringTypeMap
& s
) {
1158 io
.mapRequired("name", s
.name
);
1159 io
.mapRequired("description", s
.description
);
1160 io
.mapRequired("ingredients", s
.ingredients
);
1161 io
.mapRequired("recipes", s
.recipes
);
1162 io
.mapRequired("warningLabels", s
.warningLabels
);
1163 io
.mapRequired("documentation", s
.documentation
);
1164 io
.mapRequired("price", s
.price
);
1168 // MultilineStringType is formatted as a yaml block literal scalar. A value of
1169 // "Hello\nWorld" would be represented in yaml as
1174 struct BlockScalarTraits
<MultilineStringType
> {
1175 static void output(const MultilineStringType
&value
, void *ctxt
,
1176 llvm::raw_ostream
&out
) {
1179 static StringRef
input(StringRef scalar
, void *ctxt
,
1180 MultilineStringType
&value
) {
1181 value
.str
= scalar
.str();
1188 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType
)
1191 // Test writing then reading back custom values
1193 TEST(YAMLIO
, TestReadWriteMultilineStringType
) {
1194 std::string intermediate
;
1196 MultilineStringTypeMap map
;
1197 map
.name
.str
= "An Item";
1198 map
.description
.str
= "Hello\nWorld";
1199 map
.ingredients
.str
= "SubItem 1\nSub Item 2\n\nSub Item 3\n";
1200 map
.recipes
.str
= "\n\nTest 1\n\n\n";
1201 map
.warningLabels
.str
= "";
1202 map
.documentation
.str
= "\n\n";
1205 llvm::raw_string_ostream
ostr(intermediate
);
1210 Input
yin(intermediate
);
1211 MultilineStringTypeMap map2
;
1214 EXPECT_FALSE(yin
.error());
1215 EXPECT_EQ(map2
.name
.str
, "An Item\n");
1216 EXPECT_EQ(map2
.description
.str
, "Hello\nWorld\n");
1217 EXPECT_EQ(map2
.ingredients
.str
, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
1218 EXPECT_EQ(map2
.recipes
.str
, "\n\nTest 1\n");
1219 EXPECT_TRUE(map2
.warningLabels
.str
.empty());
1220 EXPECT_TRUE(map2
.documentation
.str
.empty());
1221 EXPECT_EQ(map2
.price
, 350);
1226 // Test writing then reading back custom values
1228 TEST(YAMLIO
, TestReadWriteBlockScalarDocuments
) {
1229 std::string intermediate
;
1231 std::vector
<MultilineStringType
> documents
;
1232 MultilineStringType doc
;
1233 doc
.str
= "Hello\nWorld";
1234 documents
.push_back(doc
);
1236 llvm::raw_string_ostream
ostr(intermediate
);
1240 // Verify that the block scalar header was written out on the same line
1241 // as the document marker.
1242 EXPECT_NE(llvm::StringRef::npos
,
1243 llvm::StringRef(intermediate
).find("--- |"));
1246 Input
yin(intermediate
);
1247 std::vector
<MultilineStringType
> documents2
;
1250 EXPECT_FALSE(yin
.error());
1251 EXPECT_EQ(documents2
.size(), size_t(1));
1252 EXPECT_EQ(documents2
[0].str
, "Hello\nWorld\n");
1256 TEST(YAMLIO
, TestReadWriteBlockScalarValue
) {
1257 std::string intermediate
;
1259 MultilineStringType doc
;
1260 doc
.str
= "Just a block\nscalar doc";
1262 llvm::raw_string_ostream
ostr(intermediate
);
1267 Input
yin(intermediate
);
1268 MultilineStringType doc
;
1271 EXPECT_FALSE(yin
.error());
1272 EXPECT_EQ(doc
.str
, "Just a block\nscalar doc\n");
1276 //===----------------------------------------------------------------------===//
1277 // Test flow sequences
1278 //===----------------------------------------------------------------------===//
1280 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber
)
1281 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber
)
1282 LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef
, MyString
)
1283 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString
)
1288 struct ScalarTraits
<MyNumber
> {
1289 static void output(const MyNumber
&value
, void *, llvm::raw_ostream
&out
) {
1293 static StringRef
input(StringRef scalar
, void *, MyNumber
&value
) {
1295 if ( getAsSignedInteger(scalar
, 0, n
) )
1296 return "invalid number";
1301 static QuotingType
mustQuote(StringRef
) { return QuotingType::None
; }
1304 template <> struct ScalarTraits
<MyString
> {
1305 using Impl
= ScalarTraits
<StringRef
>;
1306 static void output(const MyString
&V
, void *Ctx
, raw_ostream
&OS
) {
1307 Impl::output(V
, Ctx
, OS
);
1309 static StringRef
input(StringRef S
, void *Ctx
, MyString
&V
) {
1310 return Impl::input(S
, Ctx
, V
.value
);
1312 static QuotingType
mustQuote(StringRef S
) {
1313 return Impl::mustQuote(S
);
1319 struct NameAndNumbers
{
1320 llvm::StringRef name
;
1321 std::vector
<MyString
> strings
;
1322 std::vector
<MyNumber
> single
;
1323 std::vector
<MyNumber
> numbers
;
1329 struct MappingTraits
<NameAndNumbers
> {
1330 static void mapping(IO
&io
, NameAndNumbers
& nn
) {
1331 io
.mapRequired("name", nn
.name
);
1332 io
.mapRequired("strings", nn
.strings
);
1333 io
.mapRequired("single", nn
.single
);
1334 io
.mapRequired("numbers", nn
.numbers
);
1340 typedef std::vector
<MyNumber
> MyNumberFlowSequence
;
1342 LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence
)
1344 struct NameAndNumbersFlow
{
1345 llvm::StringRef name
;
1346 std::vector
<MyNumberFlowSequence
> sequenceOfNumbers
;
1352 struct MappingTraits
<NameAndNumbersFlow
> {
1353 static void mapping(IO
&io
, NameAndNumbersFlow
& nn
) {
1354 io
.mapRequired("name", nn
.name
);
1355 io
.mapRequired("sequenceOfNumbers", nn
.sequenceOfNumbers
);
1362 // Test writing then reading back custom values
1364 TEST(YAMLIO
, TestReadWriteMyFlowSequence
) {
1365 std::string intermediate
;
1369 map
.strings
.push_back(llvm::StringRef("one"));
1370 map
.strings
.push_back(llvm::StringRef("two"));
1371 map
.single
.push_back(1);
1372 map
.numbers
.push_back(10);
1373 map
.numbers
.push_back(-30);
1374 map
.numbers
.push_back(1024);
1376 llvm::raw_string_ostream
ostr(intermediate
);
1380 // Verify sequences were written in flow style
1381 llvm::StringRef
flowOut(intermediate
);
1382 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("one, two"));
1383 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("10, -30, 1024"));
1387 Input
yin(intermediate
);
1388 NameAndNumbers map2
;
1391 EXPECT_FALSE(yin
.error());
1392 EXPECT_TRUE(map2
.name
== "hello");
1393 EXPECT_EQ(map2
.strings
.size(), 2UL);
1394 EXPECT_TRUE(map2
.strings
[0].value
== "one");
1395 EXPECT_TRUE(map2
.strings
[1].value
== "two");
1396 EXPECT_EQ(map2
.single
.size(), 1UL);
1397 EXPECT_EQ(1, map2
.single
[0]);
1398 EXPECT_EQ(map2
.numbers
.size(), 3UL);
1399 EXPECT_EQ(10, map2
.numbers
[0]);
1400 EXPECT_EQ(-30, map2
.numbers
[1]);
1401 EXPECT_EQ(1024, map2
.numbers
[2]);
1407 // Test writing then reading back a sequence of flow sequences.
1409 TEST(YAMLIO
, TestReadWriteSequenceOfMyFlowSequence
) {
1410 std::string intermediate
;
1412 NameAndNumbersFlow map
;
1414 MyNumberFlowSequence single
= { 0 };
1415 MyNumberFlowSequence numbers
= { 12, 1, -512 };
1416 map
.sequenceOfNumbers
.push_back(single
);
1417 map
.sequenceOfNumbers
.push_back(numbers
);
1418 map
.sequenceOfNumbers
.push_back(MyNumberFlowSequence());
1420 llvm::raw_string_ostream
ostr(intermediate
);
1424 // Verify sequences were written in flow style
1425 // and that the parent sequence used '-'.
1426 llvm::StringRef
flowOut(intermediate
);
1427 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ 0 ]"));
1428 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ 12, 1, -512 ]"));
1429 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ ]"));
1433 Input
yin(intermediate
);
1434 NameAndNumbersFlow map2
;
1437 EXPECT_FALSE(yin
.error());
1438 EXPECT_TRUE(map2
.name
== "hello");
1439 EXPECT_EQ(map2
.sequenceOfNumbers
.size(), 3UL);
1440 EXPECT_EQ(map2
.sequenceOfNumbers
[0].size(), 1UL);
1441 EXPECT_EQ(0, map2
.sequenceOfNumbers
[0][0]);
1442 EXPECT_EQ(map2
.sequenceOfNumbers
[1].size(), 3UL);
1443 EXPECT_EQ(12, map2
.sequenceOfNumbers
[1][0]);
1444 EXPECT_EQ(1, map2
.sequenceOfNumbers
[1][1]);
1445 EXPECT_EQ(-512, map2
.sequenceOfNumbers
[1][2]);
1446 EXPECT_TRUE(map2
.sequenceOfNumbers
[2].empty());
1450 //===----------------------------------------------------------------------===//
1451 // Test normalizing/denormalizing
1452 //===----------------------------------------------------------------------===//
1454 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds
)
1456 typedef std::vector
<TotalSeconds
> SecondsSequence
;
1458 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds
)
1464 struct MappingTraits
<TotalSeconds
> {
1466 class NormalizedSeconds
{
1468 NormalizedSeconds(IO
&io
)
1469 : hours(0), minutes(0), seconds(0) {
1471 NormalizedSeconds(IO
&, TotalSeconds
&secs
)
1473 minutes((secs
- (hours
*3600))/60),
1474 seconds(secs
% 60) {
1476 TotalSeconds
denormalize(IO
&) {
1477 return TotalSeconds(hours
*3600 + minutes
*60 + seconds
);
1485 static void mapping(IO
&io
, TotalSeconds
&secs
) {
1486 MappingNormalization
<NormalizedSeconds
, TotalSeconds
> keys(io
, secs
);
1488 io
.mapOptional("hours", keys
->hours
, 0);
1489 io
.mapOptional("minutes", keys
->minutes
, 0);
1490 io
.mapRequired("seconds", keys
->seconds
);
1498 // Test the reading of a yaml sequence of mappings
1500 TEST(YAMLIO
, TestReadMySecondsSequence
) {
1501 SecondsSequence seq
;
1502 Input
yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1505 EXPECT_FALSE(yin
.error());
1506 EXPECT_EQ(seq
.size(), 2UL);
1507 EXPECT_EQ(seq
[0], 3605U);
1508 EXPECT_EQ(seq
[1], 59U);
1513 // Test writing then reading back custom values
1515 TEST(YAMLIO
, TestReadWriteMySecondsSequence
) {
1516 std::string intermediate
;
1518 SecondsSequence seq
;
1519 seq
.push_back(4000);
1523 llvm::raw_string_ostream
ostr(intermediate
);
1528 Input
yin(intermediate
);
1529 SecondsSequence seq2
;
1532 EXPECT_FALSE(yin
.error());
1533 EXPECT_EQ(seq2
.size(), 3UL);
1534 EXPECT_EQ(seq2
[0], 4000U);
1535 EXPECT_EQ(seq2
[1], 500U);
1536 EXPECT_EQ(seq2
[2], 59U);
1541 //===----------------------------------------------------------------------===//
1542 // Test dynamic typing
1543 //===----------------------------------------------------------------------===//
1562 struct KindAndFlags
{
1563 KindAndFlags() : kind(kindA
), flags(0) { }
1564 KindAndFlags(Kind k
, uint32_t f
) : kind(k
), flags(f
) { }
1569 typedef std::vector
<KindAndFlags
> KindAndFlagsSequence
;
1571 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags
)
1576 struct ScalarEnumerationTraits
<AFlags
> {
1577 static void enumeration(IO
&io
, AFlags
&value
) {
1578 io
.enumCase(value
, "a1", a1
);
1579 io
.enumCase(value
, "a2", a2
);
1580 io
.enumCase(value
, "a3", a3
);
1584 struct ScalarEnumerationTraits
<BFlags
> {
1585 static void enumeration(IO
&io
, BFlags
&value
) {
1586 io
.enumCase(value
, "b1", b1
);
1587 io
.enumCase(value
, "b2", b2
);
1588 io
.enumCase(value
, "b3", b3
);
1592 struct ScalarEnumerationTraits
<Kind
> {
1593 static void enumeration(IO
&io
, Kind
&value
) {
1594 io
.enumCase(value
, "A", kindA
);
1595 io
.enumCase(value
, "B", kindB
);
1599 struct MappingTraits
<KindAndFlags
> {
1600 static void mapping(IO
&io
, KindAndFlags
& kf
) {
1601 io
.mapRequired("kind", kf
.kind
);
1602 // Type of "flags" field varies depending on "kind" field.
1603 // Use memcpy here to avoid breaking strict aliasing rules.
1604 if (kf
.kind
== kindA
) {
1605 AFlags aflags
= static_cast<AFlags
>(kf
.flags
);
1606 io
.mapRequired("flags", aflags
);
1609 BFlags bflags
= static_cast<BFlags
>(kf
.flags
);
1610 io
.mapRequired("flags", bflags
);
1620 // Test the reading of a yaml sequence dynamic types
1622 TEST(YAMLIO
, TestReadKindAndFlagsSequence
) {
1623 KindAndFlagsSequence seq
;
1624 Input
yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1627 EXPECT_FALSE(yin
.error());
1628 EXPECT_EQ(seq
.size(), 2UL);
1629 EXPECT_EQ(seq
[0].kind
, kindA
);
1630 EXPECT_EQ(seq
[0].flags
, (uint32_t)a2
);
1631 EXPECT_EQ(seq
[1].kind
, kindB
);
1632 EXPECT_EQ(seq
[1].flags
, (uint32_t)b1
);
1636 // Test writing then reading back dynamic types
1638 TEST(YAMLIO
, TestReadWriteKindAndFlagsSequence
) {
1639 std::string intermediate
;
1641 KindAndFlagsSequence seq
;
1642 seq
.push_back(KindAndFlags(kindA
,a1
));
1643 seq
.push_back(KindAndFlags(kindB
,b1
));
1644 seq
.push_back(KindAndFlags(kindA
,a2
));
1645 seq
.push_back(KindAndFlags(kindB
,b2
));
1646 seq
.push_back(KindAndFlags(kindA
,a3
));
1648 llvm::raw_string_ostream
ostr(intermediate
);
1653 Input
yin(intermediate
);
1654 KindAndFlagsSequence seq2
;
1657 EXPECT_FALSE(yin
.error());
1658 EXPECT_EQ(seq2
.size(), 5UL);
1659 EXPECT_EQ(seq2
[0].kind
, kindA
);
1660 EXPECT_EQ(seq2
[0].flags
, (uint32_t)a1
);
1661 EXPECT_EQ(seq2
[1].kind
, kindB
);
1662 EXPECT_EQ(seq2
[1].flags
, (uint32_t)b1
);
1663 EXPECT_EQ(seq2
[2].kind
, kindA
);
1664 EXPECT_EQ(seq2
[2].flags
, (uint32_t)a2
);
1665 EXPECT_EQ(seq2
[3].kind
, kindB
);
1666 EXPECT_EQ(seq2
[3].flags
, (uint32_t)b2
);
1667 EXPECT_EQ(seq2
[4].kind
, kindA
);
1668 EXPECT_EQ(seq2
[4].flags
, (uint32_t)a3
);
1673 //===----------------------------------------------------------------------===//
1674 // Test document list
1675 //===----------------------------------------------------------------------===//
1681 typedef std::vector
<FooBarMap
> FooBarMapDocumentList
;
1683 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap
)
1689 struct MappingTraits
<FooBarMap
> {
1690 static void mapping(IO
&io
, FooBarMap
& fb
) {
1691 io
.mapRequired("foo", fb
.foo
);
1692 io
.mapRequired("bar", fb
.bar
);
1700 // Test the reading of a yaml mapping
1702 TEST(YAMLIO
, TestDocRead
) {
1704 Input
yin("---\nfoo: 3\nbar: 5\n...\n");
1707 EXPECT_FALSE(yin
.error());
1708 EXPECT_EQ(doc
.foo
, 3);
1709 EXPECT_EQ(doc
.bar
,5);
1715 // Test writing then reading back a sequence of mappings
1717 TEST(YAMLIO
, TestSequenceDocListWriteAndRead
) {
1718 std::string intermediate
;
1726 std::vector
<FooBarMap
> docList
;
1727 docList
.push_back(doc1
);
1728 docList
.push_back(doc2
);
1730 llvm::raw_string_ostream
ostr(intermediate
);
1737 Input
yin(intermediate
);
1738 std::vector
<FooBarMap
> docList2
;
1741 EXPECT_FALSE(yin
.error());
1742 EXPECT_EQ(docList2
.size(), 2UL);
1743 FooBarMap
& map1
= docList2
[0];
1744 FooBarMap
& map2
= docList2
[1];
1745 EXPECT_EQ(map1
.foo
, 10);
1746 EXPECT_EQ(map1
.bar
, -3);
1747 EXPECT_EQ(map2
.foo
, 257);
1748 EXPECT_EQ(map2
.bar
, 0);
1752 //===----------------------------------------------------------------------===//
1753 // Test document tags
1754 //===----------------------------------------------------------------------===//
1757 MyDouble() : value(0.0) { }
1758 MyDouble(double x
) : value(x
) { }
1762 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble
)
1768 struct MappingTraits
<MyDouble
> {
1769 static void mapping(IO
&io
, MyDouble
&d
) {
1770 if (io
.mapTag("!decimal", true)) {
1771 mappingDecimal(io
, d
);
1772 } else if (io
.mapTag("!fraction")) {
1773 mappingFraction(io
, d
);
1776 static void mappingDecimal(IO
&io
, MyDouble
&d
) {
1777 io
.mapRequired("value", d
.value
);
1779 static void mappingFraction(IO
&io
, MyDouble
&d
) {
1781 io
.mapRequired("numerator", num
);
1782 io
.mapRequired("denominator", denom
);
1783 // convert fraction to double
1784 d
.value
= num
/denom
;
1792 // Test the reading of two different tagged yaml documents.
1794 TEST(YAMLIO
, TestTaggedDocuments
) {
1795 std::vector
<MyDouble
> docList
;
1796 Input
yin("--- !decimal\nvalue: 3.0\n"
1797 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1799 EXPECT_FALSE(yin
.error());
1800 EXPECT_EQ(docList
.size(), 2UL);
1801 EXPECT_EQ(docList
[0].value
, 3.0);
1802 EXPECT_EQ(docList
[1].value
, 4.5);
1808 // Test writing then reading back tagged documents
1810 TEST(YAMLIO
, TestTaggedDocumentsWriteAndRead
) {
1811 std::string intermediate
;
1815 std::vector
<MyDouble
> docList
;
1816 docList
.push_back(a
);
1817 docList
.push_back(b
);
1819 llvm::raw_string_ostream
ostr(intermediate
);
1825 Input
yin(intermediate
);
1826 std::vector
<MyDouble
> docList2
;
1829 EXPECT_FALSE(yin
.error());
1830 EXPECT_EQ(docList2
.size(), 2UL);
1831 EXPECT_EQ(docList2
[0].value
, 10.25);
1832 EXPECT_EQ(docList2
[1].value
, -3.75);
1837 //===----------------------------------------------------------------------===//
1838 // Test mapping validation
1839 //===----------------------------------------------------------------------===//
1841 struct MyValidation
{
1845 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation
)
1850 struct MappingTraits
<MyValidation
> {
1851 static void mapping(IO
&io
, MyValidation
&d
) {
1852 io
.mapRequired("value", d
.value
);
1854 static std::string
validate(IO
&io
, MyValidation
&d
) {
1856 return "negative value";
1865 // Test that validate() is called and complains about the negative value.
1867 TEST(YAMLIO
, TestValidatingInput
) {
1868 std::vector
<MyValidation
> docList
;
1869 Input
yin("--- \nvalue: 3.0\n"
1870 "--- \nvalue: -1.0\n...\n",
1871 nullptr, suppressErrorMessages
);
1873 EXPECT_TRUE(!!yin
.error());
1876 //===----------------------------------------------------------------------===//
1877 // Test flow mapping
1878 //===----------------------------------------------------------------------===//
1884 FlowFooBar() : foo(0), bar(0) {}
1885 FlowFooBar(int foo
, int bar
) : foo(foo
), bar(bar
) {}
1888 typedef std::vector
<FlowFooBar
> FlowFooBarSequence
;
1890 LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar
)
1892 struct FlowFooBarDoc
{
1893 FlowFooBar attribute
;
1894 FlowFooBarSequence seq
;
1900 struct MappingTraits
<FlowFooBar
> {
1901 static void mapping(IO
&io
, FlowFooBar
&fb
) {
1902 io
.mapRequired("foo", fb
.foo
);
1903 io
.mapRequired("bar", fb
.bar
);
1906 static const bool flow
= true;
1910 struct MappingTraits
<FlowFooBarDoc
> {
1911 static void mapping(IO
&io
, FlowFooBarDoc
&fb
) {
1912 io
.mapRequired("attribute", fb
.attribute
);
1913 io
.mapRequired("seq", fb
.seq
);
1920 // Test writing then reading back custom mappings
1922 TEST(YAMLIO
, TestReadWriteMyFlowMapping
) {
1923 std::string intermediate
;
1926 doc
.attribute
= FlowFooBar(42, 907);
1927 doc
.seq
.push_back(FlowFooBar(1, 2));
1928 doc
.seq
.push_back(FlowFooBar(0, 0));
1929 doc
.seq
.push_back(FlowFooBar(-1, 1024));
1931 llvm::raw_string_ostream
ostr(intermediate
);
1935 // Verify that mappings were written in flow style
1936 llvm::StringRef
flowOut(intermediate
);
1937 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("{ foo: 42, bar: 907 }"));
1938 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: 1, bar: 2 }"));
1939 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: 0, bar: 0 }"));
1940 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: -1, bar: 1024 }"));
1944 Input
yin(intermediate
);
1948 EXPECT_FALSE(yin
.error());
1949 EXPECT_EQ(doc2
.attribute
.foo
, 42);
1950 EXPECT_EQ(doc2
.attribute
.bar
, 907);
1951 EXPECT_EQ(doc2
.seq
.size(), 3UL);
1952 EXPECT_EQ(doc2
.seq
[0].foo
, 1);
1953 EXPECT_EQ(doc2
.seq
[0].bar
, 2);
1954 EXPECT_EQ(doc2
.seq
[1].foo
, 0);
1955 EXPECT_EQ(doc2
.seq
[1].bar
, 0);
1956 EXPECT_EQ(doc2
.seq
[2].foo
, -1);
1957 EXPECT_EQ(doc2
.seq
[2].bar
, 1024);
1961 //===----------------------------------------------------------------------===//
1962 // Test error handling
1963 //===----------------------------------------------------------------------===//
1966 // Test error handling of unknown enumerated scalar
1968 TEST(YAMLIO
, TestColorsReadError
) {
1976 suppressErrorMessages
);
1978 EXPECT_TRUE(!!yin
.error());
1983 // Test error handling of flow sequence with unknown value
1985 TEST(YAMLIO
, TestFlagsReadError
) {
1989 "f2: [ round, hollow ]\n"
1993 suppressErrorMessages
);
1996 EXPECT_TRUE(!!yin
.error());
2001 // Test error handling reading built-in uint8_t type
2003 TEST(YAMLIO
, TestReadBuiltInTypesUint8Error
) {
2004 std::vector
<uint8_t> seq
;
2011 suppressErrorMessages
);
2014 EXPECT_TRUE(!!yin
.error());
2019 // Test error handling reading built-in uint16_t type
2021 TEST(YAMLIO
, TestReadBuiltInTypesUint16Error
) {
2022 std::vector
<uint16_t> seq
;
2029 suppressErrorMessages
);
2032 EXPECT_TRUE(!!yin
.error());
2037 // Test error handling reading built-in uint32_t type
2039 TEST(YAMLIO
, TestReadBuiltInTypesUint32Error
) {
2040 std::vector
<uint32_t> seq
;
2047 suppressErrorMessages
);
2050 EXPECT_TRUE(!!yin
.error());
2055 // Test error handling reading built-in uint64_t type
2057 TEST(YAMLIO
, TestReadBuiltInTypesUint64Error
) {
2058 std::vector
<uint64_t> seq
;
2060 "- 18446744073709551615\n"
2062 "- 19446744073709551615\n"
2065 suppressErrorMessages
);
2068 EXPECT_TRUE(!!yin
.error());
2073 // Test error handling reading built-in int8_t type
2075 TEST(YAMLIO
, TestReadBuiltInTypesint8OverError
) {
2076 std::vector
<int8_t> seq
;
2084 suppressErrorMessages
);
2087 EXPECT_TRUE(!!yin
.error());
2091 // Test error handling reading built-in int8_t type
2093 TEST(YAMLIO
, TestReadBuiltInTypesint8UnderError
) {
2094 std::vector
<int8_t> seq
;
2102 suppressErrorMessages
);
2105 EXPECT_TRUE(!!yin
.error());
2110 // Test error handling reading built-in int16_t type
2112 TEST(YAMLIO
, TestReadBuiltInTypesint16UnderError
) {
2113 std::vector
<int16_t> seq
;
2121 suppressErrorMessages
);
2124 EXPECT_TRUE(!!yin
.error());
2129 // Test error handling reading built-in int16_t type
2131 TEST(YAMLIO
, TestReadBuiltInTypesint16OverError
) {
2132 std::vector
<int16_t> seq
;
2140 suppressErrorMessages
);
2143 EXPECT_TRUE(!!yin
.error());
2148 // Test error handling reading built-in int32_t type
2150 TEST(YAMLIO
, TestReadBuiltInTypesint32UnderError
) {
2151 std::vector
<int32_t> seq
;
2159 suppressErrorMessages
);
2162 EXPECT_TRUE(!!yin
.error());
2166 // Test error handling reading built-in int32_t type
2168 TEST(YAMLIO
, TestReadBuiltInTypesint32OverError
) {
2169 std::vector
<int32_t> seq
;
2177 suppressErrorMessages
);
2180 EXPECT_TRUE(!!yin
.error());
2185 // Test error handling reading built-in int64_t type
2187 TEST(YAMLIO
, TestReadBuiltInTypesint64UnderError
) {
2188 std::vector
<int64_t> seq
;
2190 "- -9223372036854775808\n"
2192 "- 9223372036854775807\n"
2193 "- -9223372036854775809\n"
2196 suppressErrorMessages
);
2199 EXPECT_TRUE(!!yin
.error());
2203 // Test error handling reading built-in int64_t type
2205 TEST(YAMLIO
, TestReadBuiltInTypesint64OverError
) {
2206 std::vector
<int64_t> seq
;
2208 "- -9223372036854775808\n"
2210 "- 9223372036854775807\n"
2211 "- 9223372036854775809\n"
2214 suppressErrorMessages
);
2217 EXPECT_TRUE(!!yin
.error());
2221 // Test error handling reading built-in float type
2223 TEST(YAMLIO
, TestReadBuiltInTypesFloatError
) {
2224 std::vector
<float> seq
;
2232 suppressErrorMessages
);
2235 EXPECT_TRUE(!!yin
.error());
2239 // Test error handling reading built-in float type
2241 TEST(YAMLIO
, TestReadBuiltInTypesDoubleError
) {
2242 std::vector
<double> seq
;
2250 suppressErrorMessages
);
2253 EXPECT_TRUE(!!yin
.error());
2257 // Test error handling reading built-in Hex8 type
2259 TEST(YAMLIO
, TestReadBuiltInTypesHex8Error
) {
2260 std::vector
<Hex8
> seq
;
2267 suppressErrorMessages
);
2269 EXPECT_TRUE(!!yin
.error());
2271 std::vector
<Hex8
> seq2
;
2273 "[ 0x12, 0xFE, 0x123 ]\n"
2275 /*Ctxt=*/nullptr, suppressErrorMessages
);
2277 EXPECT_TRUE(!!yin2
.error());
2279 EXPECT_EQ(seq
.size(), 3u);
2280 EXPECT_EQ(seq
.size(), seq2
.size());
2281 for (size_t i
= 0; i
< seq
.size(); ++i
)
2282 EXPECT_EQ(seq
[i
], seq2
[i
]);
2287 // Test error handling reading built-in Hex16 type
2289 TEST(YAMLIO
, TestReadBuiltInTypesHex16Error
) {
2290 std::vector
<Hex16
> seq
;
2297 suppressErrorMessages
);
2299 EXPECT_TRUE(!!yin
.error());
2301 std::vector
<Hex16
> seq2
;
2303 "[ 0x0012, 0xFEFF, 0x12345 ]\n"
2305 /*Ctxt=*/nullptr, suppressErrorMessages
);
2307 EXPECT_TRUE(!!yin2
.error());
2309 EXPECT_EQ(seq
.size(), 3u);
2310 EXPECT_EQ(seq
.size(), seq2
.size());
2311 for (size_t i
= 0; i
< seq
.size(); ++i
)
2312 EXPECT_EQ(seq
[i
], seq2
[i
]);
2316 // Test error handling reading built-in Hex32 type
2318 TEST(YAMLIO
, TestReadBuiltInTypesHex32Error
) {
2319 std::vector
<Hex32
> seq
;
2326 suppressErrorMessages
);
2329 EXPECT_TRUE(!!yin
.error());
2331 std::vector
<Hex32
> seq2
;
2333 "[ 0x0012, 0xFEFF0000, 0x1234556789 ]\n"
2335 /*Ctxt=*/nullptr, suppressErrorMessages
);
2337 EXPECT_TRUE(!!yin2
.error());
2339 EXPECT_EQ(seq
.size(), 3u);
2340 EXPECT_EQ(seq
.size(), seq2
.size());
2341 for (size_t i
= 0; i
< seq
.size(); ++i
)
2342 EXPECT_EQ(seq
[i
], seq2
[i
]);
2346 // Test error handling reading built-in Hex64 type
2348 TEST(YAMLIO
, TestReadBuiltInTypesHex64Error
) {
2349 std::vector
<Hex64
> seq
;
2352 "- 0xFFEEDDCCBBAA9988\n"
2353 "- 0x12345567890ABCDEF0\n"
2356 suppressErrorMessages
);
2358 EXPECT_TRUE(!!yin
.error());
2360 std::vector
<Hex64
> seq2
;
2362 "[ 0x0012, 0xFFEEDDCCBBAA9988, 0x12345567890ABCDEF0 ]\n"
2364 /*Ctxt=*/nullptr, suppressErrorMessages
);
2366 EXPECT_TRUE(!!yin2
.error());
2368 EXPECT_EQ(seq
.size(), 3u);
2369 EXPECT_EQ(seq
.size(), seq2
.size());
2370 for (size_t i
= 0; i
< seq
.size(); ++i
)
2371 EXPECT_EQ(seq
[i
], seq2
[i
]);
2374 TEST(YAMLIO
, TestMalformedMapFailsGracefully
) {
2377 // We pass the suppressErrorMessages handler to handle the error
2378 // message generated in the constructor of Input.
2379 Input
yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages
);
2381 EXPECT_TRUE(!!yin
.error());
2385 Input
yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages
);
2387 EXPECT_TRUE(!!yin
.error());
2391 struct OptionalTest
{
2392 std::vector
<int> Numbers
;
2393 std::optional
<int> MaybeNumber
;
2396 struct OptionalTestSeq
{
2397 std::vector
<OptionalTest
> Tests
;
2400 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest
)
2404 struct MappingTraits
<OptionalTest
> {
2405 static void mapping(IO
& IO
, OptionalTest
&OT
) {
2406 IO
.mapOptional("Numbers", OT
.Numbers
);
2407 IO
.mapOptional("MaybeNumber", OT
.MaybeNumber
);
2412 struct MappingTraits
<OptionalTestSeq
> {
2413 static void mapping(IO
&IO
, OptionalTestSeq
&OTS
) {
2414 IO
.mapOptional("Tests", OTS
.Tests
);
2420 TEST(YAMLIO
, SequenceElideTest
) {
2421 // Test that writing out a purely optional structure with its fields set to
2422 // default followed by other data is properly read back in.
2423 OptionalTestSeq Seq
;
2424 OptionalTest One
, Two
, Three
, Four
;
2425 int N
[] = {1, 2, 3};
2426 Three
.Numbers
.assign(N
, N
+ 3);
2427 Seq
.Tests
.push_back(One
);
2428 Seq
.Tests
.push_back(Two
);
2429 Seq
.Tests
.push_back(Three
);
2430 Seq
.Tests
.push_back(Four
);
2432 std::string intermediate
;
2434 llvm::raw_string_ostream
ostr(intermediate
);
2439 Input
yin(intermediate
);
2440 OptionalTestSeq Seq2
;
2443 EXPECT_FALSE(yin
.error());
2445 EXPECT_EQ(4UL, Seq2
.Tests
.size());
2447 EXPECT_TRUE(Seq2
.Tests
[0].Numbers
.empty());
2448 EXPECT_TRUE(Seq2
.Tests
[1].Numbers
.empty());
2450 EXPECT_EQ(1, Seq2
.Tests
[2].Numbers
[0]);
2451 EXPECT_EQ(2, Seq2
.Tests
[2].Numbers
[1]);
2452 EXPECT_EQ(3, Seq2
.Tests
[2].Numbers
[2]);
2454 EXPECT_TRUE(Seq2
.Tests
[3].Numbers
.empty());
2457 TEST(YAMLIO
, TestEmptyStringFailsForMapWithRequiredFields
) {
2461 EXPECT_TRUE(!!yin
.error());
2464 TEST(YAMLIO
, TestEmptyStringSucceedsForMapWithOptionalFields
) {
2468 EXPECT_FALSE(yin
.error());
2469 EXPECT_FALSE(doc
.MaybeNumber
.has_value());
2472 TEST(YAMLIO
, TestEmptyStringSucceedsForSequence
) {
2473 std::vector
<uint8_t> seq
;
2474 Input
yin("", /*Ctxt=*/nullptr, suppressErrorMessages
);
2477 EXPECT_FALSE(yin
.error());
2478 EXPECT_TRUE(seq
.empty());
2482 llvm::StringRef str1
, str2
, str3
;
2483 FlowMap(llvm::StringRef str1
, llvm::StringRef str2
, llvm::StringRef str3
)
2484 : str1(str1
), str2(str2
), str3(str3
) {}
2488 llvm::StringRef str
;
2489 FlowSeq(llvm::StringRef S
) : str(S
) {}
2490 FlowSeq() = default;
2496 struct MappingTraits
<FlowMap
> {
2497 static void mapping(IO
&io
, FlowMap
&fm
) {
2498 io
.mapRequired("str1", fm
.str1
);
2499 io
.mapRequired("str2", fm
.str2
);
2500 io
.mapRequired("str3", fm
.str3
);
2503 static const bool flow
= true;
2507 struct ScalarTraits
<FlowSeq
> {
2508 static void output(const FlowSeq
&value
, void*, llvm::raw_ostream
&out
) {
2511 static StringRef
input(StringRef scalar
, void*, FlowSeq
&value
) {
2516 static QuotingType
mustQuote(StringRef S
) { return QuotingType::None
; }
2521 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq
)
2523 TEST(YAMLIO
, TestWrapFlow
) {
2525 llvm::raw_string_ostream
ostr(out
);
2526 FlowMap
Map("This is str1", "This is str2", "This is str3");
2527 std::vector
<FlowSeq
> Seq
;
2528 Seq
.emplace_back("This is str1");
2529 Seq
.emplace_back("This is str2");
2530 Seq
.emplace_back("This is str3");
2533 // 20 is just bellow the total length of the first mapping field.
2534 // We should wreap at every element.
2535 Output
yout(ostr
, nullptr, 15);
2540 "{ str1: This is str1, \n"
2541 " str2: This is str2, \n"
2542 " str3: This is str3 }\n"
2549 "[ This is str1, \n"
2556 // 25 will allow the second field to be output on the first line.
2557 Output
yout(ostr
, nullptr, 25);
2562 "{ str1: This is str1, str2: This is str2, \n"
2563 " str3: This is str3 }\n"
2570 "[ This is str1, This is str2, \n"
2576 // 0 means no wrapping.
2577 Output
yout(ostr
, nullptr, 0);
2582 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2589 "[ This is str1, This is str2, This is str3 ]\n"
2595 struct MappingContext
{
2604 NestedMap(MappingContext
&Context
) : Context(Context
) {}
2606 MappingContext
&Context
;
2611 template <> struct MappingContextTraits
<SimpleMap
, MappingContext
> {
2612 static void mapping(IO
&io
, SimpleMap
&sm
, MappingContext
&Context
) {
2613 io
.mapRequired("B", sm
.B
);
2614 io
.mapRequired("C", sm
.C
);
2616 io
.mapRequired("Context", Context
.A
);
2618 static std::string
validate(IO
&io
, SimpleMap
&sm
, MappingContext
&Context
) {
2623 template <> struct MappingTraits
<NestedMap
> {
2624 static void mapping(IO
&io
, NestedMap
&nm
) {
2625 io
.mapRequired("Simple", nm
.Simple
, nm
.Context
);
2631 TEST(YAMLIO
, TestMapWithContext
) {
2632 MappingContext Context
;
2633 NestedMap
Nested(Context
);
2635 llvm::raw_string_ostream
ostr(out
);
2637 Output
yout(ostr
, nullptr, 15);
2640 EXPECT_EQ(1, Context
.A
);
2651 Nested
.Simple
.B
= 2;
2652 Nested
.Simple
.C
= 3;
2654 EXPECT_EQ(2, Context
.A
);
2665 LLVM_YAML_IS_STRING_MAP(int)
2667 TEST(YAMLIO
, TestCustomMapping
) {
2668 std::map
<std::string
, int> x
;
2671 llvm::raw_string_ostream
ostr(out
);
2672 Output
xout(ostr
, nullptr, 0);
2692 std::map
<std::string
, int> y
;
2694 EXPECT_EQ(2ul, y
.size());
2695 EXPECT_EQ(1, y
["foo"]);
2696 EXPECT_EQ(2, y
["bar"]);
2699 LLVM_YAML_IS_STRING_MAP(FooBar
)
2701 TEST(YAMLIO
, TestCustomMappingStruct
) {
2702 std::map
<std::string
, FooBar
> x
;
2709 llvm::raw_string_ostream
ostr(out
);
2710 Output
xout(ostr
, nullptr, 0);
2724 std::map
<std::string
, FooBar
> y
;
2726 EXPECT_EQ(2ul, y
.size());
2727 EXPECT_EQ(1, y
["foo"].foo
);
2728 EXPECT_EQ(2, y
["foo"].bar
);
2729 EXPECT_EQ(3, y
["bar"].foo
);
2730 EXPECT_EQ(4, y
["bar"].bar
);
2733 struct FooBarMapMap
{
2734 std::map
<std::string
, FooBar
> fbm
;
2739 template <> struct MappingTraits
<FooBarMapMap
> {
2740 static void mapping(IO
&io
, FooBarMapMap
&x
) {
2741 io
.mapRequired("fbm", x
.fbm
);
2747 TEST(YAMLIO
, TestEmptyMapWrite
) {
2750 llvm::raw_string_ostream
OS(str
);
2753 EXPECT_EQ(str
, "---\nfbm: {}\n...\n");
2756 TEST(YAMLIO
, TestEmptySequenceWrite
) {
2758 FooBarContainer cont
;
2760 llvm::raw_string_ostream
OS(str
);
2763 EXPECT_EQ(str
, "---\nfbs: []\n...\n");
2769 llvm::raw_string_ostream
OS(str
);
2772 EXPECT_EQ(str
, "---\n[]\n...\n");
2776 static void TestEscaped(llvm::StringRef Input
, llvm::StringRef Expected
) {
2778 llvm::raw_string_ostream
ostr(out
);
2779 Output
xout(ostr
, nullptr, 0);
2781 llvm::yaml::EmptyContext Ctx
;
2782 yamlize(xout
, Input
, true, Ctx
);
2784 // Make a separate StringRef so we get nice byte-by-byte output.
2785 llvm::StringRef
Got(out
);
2786 EXPECT_EQ(Expected
, Got
);
2789 TEST(YAMLIO
, TestEscaped
) {
2791 TestEscaped("@abc@", "'@abc@'");
2793 TestEscaped("abc", "abc");
2794 // Forward slash quoted
2795 TestEscaped("abc/", "'abc/'");
2796 // Double quote non-printable
2797 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2798 // Double quote inside single quote
2799 TestEscaped("abc\"fdf", "'abc\"fdf'");
2800 // Double quote inside double quote
2801 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2802 // Single quote inside single quote
2803 TestEscaped("abc'fdf", "'abc''fdf'");
2805 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2806 // UTF8 with single quote inside double quote
2807 TestEscaped("parameter 'параметр' is unused",
2808 "\"parameter 'параметр' is unused\"");
2810 // String with embedded non-printable multibyte UTF-8 sequence (U+200B
2811 // zero-width space). The thing to test here is that we emit a
2812 // unicode-scalar level escape like \uNNNN (at the YAML level), and don't
2813 // just pass the UTF-8 byte sequence through as with quoted printables.
2815 const unsigned char foobar
[10] = {'f', 'o', 'o',
2816 0xE2, 0x80, 0x8B, // UTF-8 of U+200B
2819 TestEscaped((char const *)foobar
, "\"foo\\u200Bbar\"");
2823 TEST(YAMLIO
, Numeric
) {
2824 EXPECT_TRUE(isNumeric(".inf"));
2825 EXPECT_TRUE(isNumeric(".INF"));
2826 EXPECT_TRUE(isNumeric(".Inf"));
2827 EXPECT_TRUE(isNumeric("-.inf"));
2828 EXPECT_TRUE(isNumeric("+.inf"));
2830 EXPECT_TRUE(isNumeric(".nan"));
2831 EXPECT_TRUE(isNumeric(".NaN"));
2832 EXPECT_TRUE(isNumeric(".NAN"));
2834 EXPECT_TRUE(isNumeric("0"));
2835 EXPECT_TRUE(isNumeric("0."));
2836 EXPECT_TRUE(isNumeric("0.0"));
2837 EXPECT_TRUE(isNumeric("-0.0"));
2838 EXPECT_TRUE(isNumeric("+0.0"));
2840 EXPECT_TRUE(isNumeric("12345"));
2841 EXPECT_TRUE(isNumeric("012345"));
2842 EXPECT_TRUE(isNumeric("+12.0"));
2843 EXPECT_TRUE(isNumeric(".5"));
2844 EXPECT_TRUE(isNumeric("+.5"));
2845 EXPECT_TRUE(isNumeric("-1.0"));
2847 EXPECT_TRUE(isNumeric("2.3e4"));
2848 EXPECT_TRUE(isNumeric("-2E+05"));
2849 EXPECT_TRUE(isNumeric("+12e03"));
2850 EXPECT_TRUE(isNumeric("6.8523015e+5"));
2852 EXPECT_TRUE(isNumeric("1.e+1"));
2853 EXPECT_TRUE(isNumeric(".0e+1"));
2855 EXPECT_TRUE(isNumeric("0x2aF3"));
2856 EXPECT_TRUE(isNumeric("0o01234567"));
2858 EXPECT_FALSE(isNumeric("not a number"));
2859 EXPECT_FALSE(isNumeric("."));
2860 EXPECT_FALSE(isNumeric(".e+1"));
2861 EXPECT_FALSE(isNumeric(".1e"));
2862 EXPECT_FALSE(isNumeric(".1e+"));
2863 EXPECT_FALSE(isNumeric(".1e++1"));
2865 EXPECT_FALSE(isNumeric("ABCD"));
2866 EXPECT_FALSE(isNumeric("+0x2AF3"));
2867 EXPECT_FALSE(isNumeric("-0x2AF3"));
2868 EXPECT_FALSE(isNumeric("0x2AF3Z"));
2869 EXPECT_FALSE(isNumeric("0o012345678"));
2870 EXPECT_FALSE(isNumeric("0xZ"));
2871 EXPECT_FALSE(isNumeric("-0o012345678"));
2872 EXPECT_FALSE(isNumeric("000003A8229434B839616A25C16B0291F77A438B"));
2874 EXPECT_FALSE(isNumeric(""));
2875 EXPECT_FALSE(isNumeric("."));
2876 EXPECT_FALSE(isNumeric(".e+1"));
2877 EXPECT_FALSE(isNumeric(".e+"));
2878 EXPECT_FALSE(isNumeric(".e"));
2879 EXPECT_FALSE(isNumeric("e1"));
2881 // Deprecated formats: as for YAML 1.2 specification, the following are not
2882 // valid numbers anymore:
2884 // * Sexagecimal numbers
2885 // * Decimal numbers with comma s the delimiter
2886 // * "inf", "nan" without '.' prefix
2887 EXPECT_FALSE(isNumeric("3:25:45"));
2888 EXPECT_FALSE(isNumeric("+12,345"));
2889 EXPECT_FALSE(isNumeric("-inf"));
2890 EXPECT_FALSE(isNumeric("1,230.15"));
2893 //===----------------------------------------------------------------------===//
2894 // Test writing and reading escaped keys
2895 //===----------------------------------------------------------------------===//
2897 // Struct with dynamic string key
2898 struct QuotedKeyStruct
{
2901 int unquoted_numeric
;
2910 template <> struct MappingTraits
<QuotedKeyStruct
> {
2911 static void mapping(IO
&io
, QuotedKeyStruct
&map
) {
2912 io
.mapRequired("true", map
.unquoted_bool
);
2913 io
.mapRequired("null", map
.unquoted_null
);
2914 io
.mapRequired("42", map
.unquoted_numeric
);
2915 io
.mapRequired("unquoted", map
.unquoted_str
);
2916 io
.mapRequired(":", map
.colon
);
2917 io
.mapRequired(" ", map
.just_space
);
2918 char unprintableKey
[] = {/* \f, form-feed */ 0xC, 0};
2919 io
.mapRequired(unprintableKey
, map
.unprintable
);
2925 TEST(YAMLIO
, TestQuotedKeyRead
) {
2926 QuotedKeyStruct map
= {};
2927 Input
yin("---\ntrue: 1\nnull: 2\n42: 3\nunquoted: 4\n':': 5\n' ': "
2928 "6\n\"\\f\": 7\n...\n");
2931 EXPECT_FALSE(yin
.error());
2932 EXPECT_EQ(map
.unquoted_bool
, 1);
2933 EXPECT_EQ(map
.unquoted_null
, 2);
2934 EXPECT_EQ(map
.unquoted_numeric
, 3);
2935 EXPECT_EQ(map
.unquoted_str
, 4);
2936 EXPECT_EQ(map
.colon
, 5);
2937 EXPECT_EQ(map
.just_space
, 6);
2938 EXPECT_EQ(map
.unprintable
, 7);
2941 TEST(YAMLIO
, TestQuotedKeyWriteRead
) {
2942 std::string intermediate
;
2944 QuotedKeyStruct map
= {1, 2, 3, 4, 5, 6, 7};
2945 llvm::raw_string_ostream
ostr(intermediate
);
2950 EXPECT_NE(std::string::npos
, intermediate
.find("true:"));
2951 EXPECT_NE(std::string::npos
, intermediate
.find("null:"));
2952 EXPECT_NE(std::string::npos
, intermediate
.find("42:"));
2953 EXPECT_NE(std::string::npos
, intermediate
.find("unquoted:"));
2954 EXPECT_NE(std::string::npos
, intermediate
.find("':':"));
2955 EXPECT_NE(std::string::npos
, intermediate
.find("' '"));
2956 EXPECT_NE(std::string::npos
, intermediate
.find("\"\\f\":"));
2959 Input
yin(intermediate
);
2960 QuotedKeyStruct map
;
2963 EXPECT_FALSE(yin
.error());
2964 EXPECT_EQ(map
.unquoted_bool
, 1);
2965 EXPECT_EQ(map
.unquoted_null
, 2);
2966 EXPECT_EQ(map
.unquoted_numeric
, 3);
2967 EXPECT_EQ(map
.unquoted_str
, 4);
2968 EXPECT_EQ(map
.colon
, 5);
2969 EXPECT_EQ(map
.just_space
, 6);
2970 EXPECT_EQ(map
.unprintable
, 7);
2974 //===----------------------------------------------------------------------===//
2975 // Test PolymorphicTraits and TaggedScalarTraits
2976 //===----------------------------------------------------------------------===//
2985 Poly(NodeKind Kind
) : Kind(Kind
) {}
2987 virtual ~Poly() = default;
2989 NodeKind
getKind() const { return Kind
; }
2992 struct Scalar
: Poly
{
3004 Scalar() : Poly(NK_Scalar
), SKind(SK_Unknown
) {}
3005 Scalar(double DoubleValue
)
3006 : Poly(NK_Scalar
), SKind(SK_Double
), DoubleValue(DoubleValue
) {}
3007 Scalar(bool BoolValue
)
3008 : Poly(NK_Scalar
), SKind(SK_Bool
), BoolValue(BoolValue
) {}
3010 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Scalar
; }
3013 struct Seq
: Poly
, std::vector
<std::unique_ptr
<Poly
>> {
3014 Seq() : Poly(NK_Seq
) {}
3016 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Seq
; }
3019 struct Map
: Poly
, llvm::StringMap
<std::unique_ptr
<Poly
>> {
3020 Map() : Poly(NK_Map
) {}
3022 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Map
; }
3028 template <> struct PolymorphicTraits
<std::unique_ptr
<Poly
>> {
3029 static NodeKind
getKind(const std::unique_ptr
<Poly
> &N
) {
3030 if (isa
<Scalar
>(*N
))
3031 return NodeKind::Scalar
;
3033 return NodeKind::Sequence
;
3035 return NodeKind::Map
;
3036 llvm_unreachable("unsupported node type");
3039 static Scalar
&getAsScalar(std::unique_ptr
<Poly
> &N
) {
3040 if (!N
|| !isa
<Scalar
>(*N
))
3041 N
= std::make_unique
<Scalar
>();
3042 return *cast
<Scalar
>(N
.get());
3045 static Seq
&getAsSequence(std::unique_ptr
<Poly
> &N
) {
3046 if (!N
|| !isa
<Seq
>(*N
))
3047 N
= std::make_unique
<Seq
>();
3048 return *cast
<Seq
>(N
.get());
3051 static Map
&getAsMap(std::unique_ptr
<Poly
> &N
) {
3052 if (!N
|| !isa
<Map
>(*N
))
3053 N
= std::make_unique
<Map
>();
3054 return *cast
<Map
>(N
.get());
3058 template <> struct TaggedScalarTraits
<Scalar
> {
3059 static void output(const Scalar
&S
, void *Ctxt
, raw_ostream
&ScalarOS
,
3060 raw_ostream
&TagOS
) {
3062 case Scalar::SK_Unknown
:
3063 report_fatal_error("output unknown scalar");
3065 case Scalar::SK_Double
:
3067 ScalarTraits
<double>::output(S
.DoubleValue
, Ctxt
, ScalarOS
);
3069 case Scalar::SK_Bool
:
3071 ScalarTraits
<bool>::output(S
.BoolValue
, Ctxt
, ScalarOS
);
3076 static StringRef
input(StringRef ScalarStr
, StringRef Tag
, void *Ctxt
,
3078 S
.SKind
= StringSwitch
<Scalar::ScalarKind
>(Tag
)
3079 .Case("!double", Scalar::SK_Double
)
3080 .Case("!bool", Scalar::SK_Bool
)
3081 .Default(Scalar::SK_Unknown
);
3083 case Scalar::SK_Unknown
:
3084 return StringRef("unknown scalar tag");
3085 case Scalar::SK_Double
:
3086 return ScalarTraits
<double>::input(ScalarStr
, Ctxt
, S
.DoubleValue
);
3087 case Scalar::SK_Bool
:
3088 return ScalarTraits
<bool>::input(ScalarStr
, Ctxt
, S
.BoolValue
);
3090 llvm_unreachable("unknown scalar kind");
3093 static QuotingType
mustQuote(const Scalar
&S
, StringRef Str
) {
3095 case Scalar::SK_Unknown
:
3096 report_fatal_error("quote unknown scalar");
3097 case Scalar::SK_Double
:
3098 return ScalarTraits
<double>::mustQuote(Str
);
3099 case Scalar::SK_Bool
:
3100 return ScalarTraits
<bool>::mustQuote(Str
);
3102 llvm_unreachable("unknown scalar kind");
3106 template <> struct CustomMappingTraits
<Map
> {
3107 static void inputOne(IO
&IO
, StringRef Key
, Map
&M
) {
3108 IO
.mapRequired(Key
.str().c_str(), M
[Key
]);
3111 static void output(IO
&IO
, Map
&M
) {
3113 IO
.mapRequired(N
.getKey().str().c_str(), N
.getValue());
3117 template <> struct SequenceTraits
<Seq
> {
3118 static size_t size(IO
&IO
, Seq
&A
) { return A
.size(); }
3120 static std::unique_ptr
<Poly
> &element(IO
&IO
, Seq
&A
, size_t Index
) {
3121 if (Index
>= A
.size())
3122 A
.resize(Index
+ 1);
3130 TEST(YAMLIO
, TestReadWritePolymorphicScalar
) {
3131 std::string intermediate
;
3132 std::unique_ptr
<Poly
> node
= std::make_unique
<Scalar
>(true);
3134 llvm::raw_string_ostream
ostr(intermediate
);
3136 #ifdef GTEST_HAS_DEATH_TEST
3138 EXPECT_DEATH(yout
<< node
, "plain scalar documents are not supported");
3143 TEST(YAMLIO
, TestReadWritePolymorphicSeq
) {
3144 std::string intermediate
;
3146 auto seq
= std::make_unique
<Seq
>();
3147 seq
->push_back(std::make_unique
<Scalar
>(true));
3148 seq
->push_back(std::make_unique
<Scalar
>(1.0));
3149 auto node
= llvm::unique_dyn_cast
<Poly
>(seq
);
3151 llvm::raw_string_ostream
ostr(intermediate
);
3156 Input
yin(intermediate
);
3157 std::unique_ptr
<Poly
> node
;
3160 EXPECT_FALSE(yin
.error());
3161 auto seq
= llvm::dyn_cast
<Seq
>(node
.get());
3163 ASSERT_EQ(seq
->size(), 2u);
3164 auto first
= llvm::dyn_cast
<Scalar
>((*seq
)[0].get());
3166 EXPECT_EQ(first
->SKind
, Scalar::SK_Bool
);
3167 EXPECT_TRUE(first
->BoolValue
);
3168 auto second
= llvm::dyn_cast
<Scalar
>((*seq
)[1].get());
3169 ASSERT_TRUE(second
);
3170 EXPECT_EQ(second
->SKind
, Scalar::SK_Double
);
3171 EXPECT_EQ(second
->DoubleValue
, 1.0);
3175 TEST(YAMLIO
, TestReadWritePolymorphicMap
) {
3176 std::string intermediate
;
3178 auto map
= std::make_unique
<Map
>();
3179 (*map
)["foo"] = std::make_unique
<Scalar
>(false);
3180 (*map
)["bar"] = std::make_unique
<Scalar
>(2.0);
3181 std::unique_ptr
<Poly
> node
= llvm::unique_dyn_cast
<Poly
>(map
);
3183 llvm::raw_string_ostream
ostr(intermediate
);
3188 Input
yin(intermediate
);
3189 std::unique_ptr
<Poly
> node
;
3192 EXPECT_FALSE(yin
.error());
3193 auto map
= llvm::dyn_cast
<Map
>(node
.get());
3195 auto foo
= llvm::dyn_cast
<Scalar
>((*map
)["foo"].get());
3197 EXPECT_EQ(foo
->SKind
, Scalar::SK_Bool
);
3198 EXPECT_FALSE(foo
->BoolValue
);
3199 auto bar
= llvm::dyn_cast
<Scalar
>((*map
)["bar"].get());
3201 EXPECT_EQ(bar
->SKind
, Scalar::SK_Double
);
3202 EXPECT_EQ(bar
->DoubleValue
, 2.0);
3206 TEST(YAMLIO
, TestAnchorMapError
) {
3207 Input
yin("& & &: ");
3208 yin
.setCurrentDocument();
3209 EXPECT_TRUE(yin
.error());
3212 TEST(YAMLIO
, TestFlowSequenceTokenErrors
) {
3214 EXPECT_FALSE(yin
.setCurrentDocument());
3215 EXPECT_TRUE(yin
.error());
3218 EXPECT_FALSE(yin2
.setCurrentDocument());
3219 EXPECT_TRUE(yin2
.error());
3222 EXPECT_FALSE(yin3
.setCurrentDocument());
3223 EXPECT_TRUE(yin3
.error());
3226 TEST(YAMLIO
, TestDirectiveMappingNoValue
) {
3227 Input
yin("%YAML\n{5:");
3228 yin
.setCurrentDocument();
3229 EXPECT_TRUE(yin
.error());
3231 Input
yin2("%TAG\n'\x98!< :\n");
3232 yin2
.setCurrentDocument();
3233 EXPECT_TRUE(yin2
.error());
3236 TEST(YAMLIO
, TestUnescapeInfiniteLoop
) {
3237 Input
yin("\"\\u\\^#\\\\\"");
3238 yin
.setCurrentDocument();
3239 EXPECT_TRUE(yin
.error());
3242 TEST(YAMLIO
, TestScannerUnexpectedCharacter
) {
3243 Input
yin("!<$\x9F.");
3244 EXPECT_FALSE(yin
.setCurrentDocument());
3245 EXPECT_TRUE(yin
.error());
3248 TEST(YAMLIO
, TestUnknownDirective
) {
3250 EXPECT_FALSE(yin
.setCurrentDocument());
3251 EXPECT_TRUE(yin
.error());
3254 EXPECT_FALSE(yin2
.setCurrentDocument());
3255 EXPECT_TRUE(yin2
.error());
3258 TEST(YAMLIO
, TestEmptyAlias
) {
3260 EXPECT_FALSE(yin
.setCurrentDocument());
3261 EXPECT_TRUE(yin
.error());
3264 TEST(YAMLIO
, TestEmptyAnchor
) {
3266 EXPECT_FALSE(yin
.setCurrentDocument());
3269 TEST(YAMLIO
, TestScannerNoNullEmpty
) {
3270 std::vector
<char> str
{};
3271 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3272 yin
.setCurrentDocument();
3273 EXPECT_FALSE(yin
.error());
3276 TEST(YAMLIO
, TestScannerNoNullSequenceOfNull
) {
3277 std::vector
<char> str
{'-'};
3278 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3279 yin
.setCurrentDocument();
3280 EXPECT_FALSE(yin
.error());
3283 TEST(YAMLIO
, TestScannerNoNullSimpleSequence
) {
3284 std::vector
<char> str
{'-', ' ', 'a'};
3285 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3286 yin
.setCurrentDocument();
3287 EXPECT_FALSE(yin
.error());
3290 TEST(YAMLIO
, TestScannerNoNullUnbalancedMap
) {
3291 std::vector
<char> str
{'{'};
3292 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3293 yin
.setCurrentDocument();
3294 EXPECT_TRUE(yin
.error());
3297 TEST(YAMLIO
, TestScannerNoNullEmptyMap
) {
3298 std::vector
<char> str
{'{', '}'};
3299 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3300 yin
.setCurrentDocument();
3301 EXPECT_FALSE(yin
.error());
3304 TEST(YAMLIO
, TestScannerNoNullUnbalancedSequence
) {
3305 std::vector
<char> str
{'['};
3306 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3307 yin
.setCurrentDocument();
3308 EXPECT_TRUE(yin
.error());
3311 TEST(YAMLIO
, TestScannerNoNullEmptySequence
) {
3312 std::vector
<char> str
{'[', ']'};
3313 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3314 yin
.setCurrentDocument();
3315 EXPECT_FALSE(yin
.error());
3318 TEST(YAMLIO
, TestScannerNoNullScalarUnbalancedDoubleQuote
) {
3319 std::vector
<char> str
{'"'};
3320 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3321 yin
.setCurrentDocument();
3322 EXPECT_TRUE(yin
.error());
3325 TEST(YAMLIO
, TestScannerNoNullScalarUnbalancedSingleQuote
) {
3326 std::vector
<char> str
{'\''};
3327 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3328 yin
.setCurrentDocument();
3329 EXPECT_TRUE(yin
.error());
3332 TEST(YAMLIO
, TestScannerNoNullEmptyAlias
) {
3333 std::vector
<char> str
{'&'};
3334 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3335 yin
.setCurrentDocument();
3336 EXPECT_TRUE(yin
.error());
3339 TEST(YAMLIO
, TestScannerNoNullEmptyAnchor
) {
3340 std::vector
<char> str
{'*'};
3341 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3342 yin
.setCurrentDocument();
3343 EXPECT_TRUE(yin
.error());
3346 TEST(YAMLIO
, TestScannerNoNullDecodeInvalidUTF8
) {
3347 std::vector
<char> str
{'\xef'};
3348 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3349 yin
.setCurrentDocument();
3350 EXPECT_TRUE(yin
.error());
3353 TEST(YAMLIO
, TestScannerNoNullScanPlainScalarInFlow
) {
3354 std::vector
<char> str
{'{', 'a', ':'};
3355 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3356 yin
.setCurrentDocument();
3357 EXPECT_TRUE(yin
.error());
3362 // Initialize to int max as a sentinel value.
3363 for (auto &v
: values
)
3364 v
= std::numeric_limits
<int>::max();
3371 // Initialize to int max as a sentinel value.
3372 for (auto &v
: values
)
3373 v
= std::numeric_limits
<int>::max();
3375 std::array
<int, 4> values
;
3380 template <> struct MappingTraits
<FixedArray
> {
3381 static void mapping(IO
&io
, FixedArray
&st
) {
3382 MutableArrayRef
<int> array
= st
.values
;
3383 io
.mapRequired("Values", array
);
3386 template <> struct MappingTraits
<StdArray
> {
3387 static void mapping(IO
&io
, StdArray
&st
) {
3388 io
.mapRequired("Values", st
.values
);
3394 using TestTypes
= ::testing::Types
<FixedArray
, StdArray
>;
3396 template <typename T
> class YAMLIO
: public testing::Test
{};
3397 TYPED_TEST_SUITE(YAMLIO
, TestTypes
, );
3399 TYPED_TEST(YAMLIO
, FixedSizeArray
) {
3401 Input
yin("---\nValues: [ 1, 2, 3, 4 ]\n...\n");
3404 EXPECT_FALSE(yin
.error());
3405 EXPECT_EQ(faval
.values
[0], 1);
3406 EXPECT_EQ(faval
.values
[1], 2);
3407 EXPECT_EQ(faval
.values
[2], 3);
3408 EXPECT_EQ(faval
.values
[3], 4);
3410 std::string serialized
;
3412 llvm::raw_string_ostream
os(serialized
);
3416 auto expected
= "---\n"
3417 "Values: [ 1, 2, 3, 4 ]\n"
3419 ASSERT_EQ(serialized
, expected
);
3422 TYPED_TEST(YAMLIO
, FixedSizeArrayMismatch
) {
3425 Input
yin("---\nValues: [ 1, 2, 3 ]\n...\n");
3428 // No error for too small, leaves the default initialized value
3429 EXPECT_FALSE(yin
.error());
3430 EXPECT_EQ(faval
.values
[0], 1);
3431 EXPECT_EQ(faval
.values
[1], 2);
3432 EXPECT_EQ(faval
.values
[2], 3);
3433 EXPECT_EQ(faval
.values
[3], std::numeric_limits
<int>::max());
3438 Input
yin("---\nValues: [ 1, 2, 3, 4, 5 ]\n...\n");
3441 // Error for too many elements.
3442 EXPECT_TRUE(!!yin
.error());