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
;
27 using llvm::yaml::isNumeric
;
28 using llvm::yaml::MappingNormalization
;
29 using llvm::yaml::MappingTraits
;
30 using llvm::yaml::Output
;
31 using llvm::yaml::ScalarTraits
;
32 using ::testing::StartsWith
;
37 static void suppressErrorMessages(const llvm::SMDiagnostic
&, void *) {
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
50 typedef std::vector
<FooBar
> FooBarSequence
;
52 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar
)
54 struct FooBarContainer
{
61 struct MappingTraits
<FooBar
> {
62 static void mapping(IO
&io
, FooBar
& fb
) {
63 io
.mapRequired("foo", fb
.foo
);
64 io
.mapRequired("bar", fb
.bar
);
68 template <> struct MappingTraits
<FooBarContainer
> {
69 static void mapping(IO
&io
, FooBarContainer
&fb
) {
70 io
.mapRequired("fbs", fb
.fbs
);
78 // Test the reading of a yaml mapping
80 TEST(YAMLIO
, TestMapRead
) {
83 Input
yin("---\nfoo: 3\nbar: 5\n...\n");
86 EXPECT_FALSE(yin
.error());
87 EXPECT_EQ(doc
.foo
, 3);
88 EXPECT_EQ(doc
.bar
, 5);
92 Input
yin("{foo: 3, bar: 5}");
95 EXPECT_FALSE(yin
.error());
96 EXPECT_EQ(doc
.foo
, 3);
97 EXPECT_EQ(doc
.bar
, 5);
101 TEST(YAMLIO
, TestMalformedMapRead
) {
103 Input
yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages
);
105 EXPECT_TRUE(!!yin
.error());
109 // Test the reading of a yaml sequence of mappings
111 TEST(YAMLIO
, TestSequenceMapRead
) {
113 Input
yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
116 EXPECT_FALSE(yin
.error());
117 EXPECT_EQ(seq
.size(), 2UL);
118 FooBar
& map1
= seq
[0];
119 FooBar
& map2
= seq
[1];
120 EXPECT_EQ(map1
.foo
, 3);
121 EXPECT_EQ(map1
.bar
, 5);
122 EXPECT_EQ(map2
.foo
, 7);
123 EXPECT_EQ(map2
.bar
, 9);
127 // Test the reading of a map containing a yaml sequence of mappings
129 TEST(YAMLIO
, TestContainerSequenceMapRead
) {
131 FooBarContainer cont
;
132 Input
yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
135 EXPECT_FALSE(yin2
.error());
136 EXPECT_EQ(cont
.fbs
.size(), 2UL);
137 EXPECT_EQ(cont
.fbs
[0].foo
, 3);
138 EXPECT_EQ(cont
.fbs
[0].bar
, 5);
139 EXPECT_EQ(cont
.fbs
[1].foo
, 7);
140 EXPECT_EQ(cont
.fbs
[1].bar
, 9);
144 FooBarContainer cont
;
145 Input
yin("---\nfbs:\n...\n");
147 // Okay: Empty node represents an empty array.
148 EXPECT_FALSE(yin
.error());
149 EXPECT_EQ(cont
.fbs
.size(), 0UL);
153 FooBarContainer cont
;
154 Input
yin("---\nfbs: !!null null\n...\n");
156 // Okay: null represents an empty array.
157 EXPECT_FALSE(yin
.error());
158 EXPECT_EQ(cont
.fbs
.size(), 0UL);
162 FooBarContainer cont
;
163 Input
yin("---\nfbs: ~\n...\n");
165 // Okay: null represents an empty array.
166 EXPECT_FALSE(yin
.error());
167 EXPECT_EQ(cont
.fbs
.size(), 0UL);
171 FooBarContainer cont
;
172 Input
yin("---\nfbs: null\n...\n");
174 // Okay: null represents an empty array.
175 EXPECT_FALSE(yin
.error());
176 EXPECT_EQ(cont
.fbs
.size(), 0UL);
181 // Test the reading of a map containing a malformed yaml sequence
183 TEST(YAMLIO
, TestMalformedContainerSequenceMapRead
) {
185 FooBarContainer cont
;
186 Input
yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
187 suppressErrorMessages
);
189 // Error: fbs is not a sequence.
190 EXPECT_TRUE(!!yin
.error());
191 EXPECT_EQ(cont
.fbs
.size(), 0UL);
195 FooBarContainer cont
;
196 Input
yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages
);
198 // This should be an error.
199 EXPECT_TRUE(!!yin
.error());
200 EXPECT_EQ(cont
.fbs
.size(), 0UL);
205 // Test writing then reading back a sequence of mappings
207 TEST(YAMLIO
, TestSequenceMapWriteAndRead
) {
208 std::string intermediate
;
217 seq
.push_back(entry1
);
218 seq
.push_back(entry2
);
220 llvm::raw_string_ostream
ostr(intermediate
);
226 Input
yin(intermediate
);
230 EXPECT_FALSE(yin
.error());
231 EXPECT_EQ(seq2
.size(), 2UL);
232 FooBar
& map1
= seq2
[0];
233 FooBar
& map2
= seq2
[1];
234 EXPECT_EQ(map1
.foo
, 10);
235 EXPECT_EQ(map1
.bar
, -3);
236 EXPECT_EQ(map2
.foo
, 257);
237 EXPECT_EQ(map2
.bar
, 0);
242 // Test YAML filename handling.
244 static void testErrorFilename(const llvm::SMDiagnostic
&Error
, void *) {
245 EXPECT_EQ(Error
.getFilename(), "foo.yaml");
248 TEST(YAMLIO
, TestGivenFilename
) {
249 auto Buffer
= llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
250 Input
yin(*Buffer
, nullptr, testErrorFilename
);
254 EXPECT_TRUE(!!yin
.error());
257 struct WithStringField
{
265 template <> struct MappingTraits
<WithStringField
> {
266 static void mapping(IO
&io
, WithStringField
&fb
) {
267 io
.mapRequired("str1", fb
.str1
);
268 io
.mapRequired("str2", fb
.str2
);
269 io
.mapRequired("str3", fb
.str3
);
275 TEST(YAMLIO
, MultilineStrings
) {
276 WithStringField Original
;
277 Original
.str1
= "a multiline string\nfoobarbaz";
278 Original
.str2
= "another one\rfoobarbaz";
279 Original
.str3
= "a one-line string";
281 std::string Serialized
;
283 llvm::raw_string_ostream
OS(Serialized
);
287 auto Expected
= "---\n"
288 "str1: \"a multiline string\\nfoobarbaz\"\n"
289 "str2: \"another one\\rfoobarbaz\"\n"
290 "str3: a one-line string\n"
292 ASSERT_EQ(Serialized
, Expected
);
294 // Also check it parses back without the errors.
295 WithStringField Deserialized
;
297 Input
YIn(Serialized
);
299 ASSERT_FALSE(YIn
.error())
300 << "Parsing error occurred during deserialization. Serialized string:\n"
303 EXPECT_EQ(Original
.str1
, Deserialized
.str1
);
304 EXPECT_EQ(Original
.str2
, Deserialized
.str2
);
305 EXPECT_EQ(Original
.str3
, Deserialized
.str3
);
308 TEST(YAMLIO
, NoQuotesForTab
) {
309 WithStringField WithTab
;
310 WithTab
.str1
= "aba\tcaba";
311 std::string Serialized
;
313 llvm::raw_string_ostream
OS(Serialized
);
317 auto ExpectedPrefix
= "---\n"
319 EXPECT_THAT(Serialized
, StartsWith(ExpectedPrefix
));
322 //===----------------------------------------------------------------------===//
323 // Test built-in types
324 //===----------------------------------------------------------------------===//
326 struct BuiltInTypes
{
349 struct MappingTraits
<BuiltInTypes
> {
350 static void mapping(IO
&io
, BuiltInTypes
& bt
) {
351 io
.mapRequired("str", bt
.str
);
352 io
.mapRequired("stdstr", bt
.stdstr
);
353 io
.mapRequired("u64", bt
.u64
);
354 io
.mapRequired("u32", bt
.u32
);
355 io
.mapRequired("u16", bt
.u16
);
356 io
.mapRequired("u8", bt
.u8
);
357 io
.mapRequired("b", bt
.b
);
358 io
.mapRequired("s64", bt
.s64
);
359 io
.mapRequired("s32", bt
.s32
);
360 io
.mapRequired("s16", bt
.s16
);
361 io
.mapRequired("s8", bt
.s8
);
362 io
.mapRequired("f", bt
.f
);
363 io
.mapRequired("d", bt
.d
);
364 io
.mapRequired("h8", bt
.h8
);
365 io
.mapRequired("h16", bt
.h16
);
366 io
.mapRequired("h32", bt
.h32
);
367 io
.mapRequired("h64", bt
.h64
);
375 // Test the reading of all built-in scalar conversions
377 TEST(YAMLIO
, TestReadBuiltInTypes
) {
381 "stdstr: hello where?\n"
396 "h64: 0xFEDCBA9876543210\n"
400 EXPECT_FALSE(yin
.error());
401 EXPECT_TRUE(map
.str
.equals("hello there"));
402 EXPECT_TRUE(map
.stdstr
== "hello where?");
403 EXPECT_EQ(map
.u64
, 5000000000ULL);
404 EXPECT_EQ(map
.u32
, 4000000000U);
405 EXPECT_EQ(map
.u16
, 65000);
406 EXPECT_EQ(map
.u8
, 255);
407 EXPECT_EQ(map
.b
, false);
408 EXPECT_EQ(map
.s64
, -5000000000LL);
409 EXPECT_EQ(map
.s32
, -2000000000L);
410 EXPECT_EQ(map
.s16
, -32000);
411 EXPECT_EQ(map
.s8
, -127);
412 EXPECT_EQ(map
.f
, 137.125);
413 EXPECT_EQ(map
.d
, -2.8625);
414 EXPECT_EQ(map
.h8
, Hex8(255));
415 EXPECT_EQ(map
.h16
, Hex16(0x8765));
416 EXPECT_EQ(map
.h32
, Hex32(0xFEDCBA98));
417 EXPECT_EQ(map
.h64
, Hex64(0xFEDCBA9876543210LL
));
422 // Test writing then reading back all built-in scalar types
424 TEST(YAMLIO
, TestReadWriteBuiltInTypes
) {
425 std::string intermediate
;
429 map
.stdstr
= "three four";
430 map
.u64
= 6000000000ULL;
431 map
.u32
= 3000000000U;
435 map
.s64
= -6000000000LL;
436 map
.s32
= -2000000000;
443 map
.h32
= 3000000000U;
444 map
.h64
= 6000000000LL;
446 llvm::raw_string_ostream
ostr(intermediate
);
452 Input
yin(intermediate
);
456 EXPECT_FALSE(yin
.error());
457 EXPECT_TRUE(map
.str
.equals("one two"));
458 EXPECT_TRUE(map
.stdstr
== "three four");
459 EXPECT_EQ(map
.u64
, 6000000000ULL);
460 EXPECT_EQ(map
.u32
, 3000000000U);
461 EXPECT_EQ(map
.u16
, 50000);
462 EXPECT_EQ(map
.u8
, 254);
463 EXPECT_EQ(map
.b
, true);
464 EXPECT_EQ(map
.s64
, -6000000000LL);
465 EXPECT_EQ(map
.s32
, -2000000000L);
466 EXPECT_EQ(map
.s16
, -32000);
467 EXPECT_EQ(map
.s8
, -128);
468 EXPECT_EQ(map
.f
, 3.25);
469 EXPECT_EQ(map
.d
, -2.8625);
470 EXPECT_EQ(map
.h8
, Hex8(254));
471 EXPECT_EQ(map
.h16
, Hex16(50000));
472 EXPECT_EQ(map
.h32
, Hex32(3000000000U));
473 EXPECT_EQ(map
.h64
, Hex64(6000000000LL));
477 //===----------------------------------------------------------------------===//
478 // Test endian-aware types
479 //===----------------------------------------------------------------------===//
482 typedef llvm::support::detail::packed_endian_specific_integral
<
483 float, llvm::support::little
, llvm::support::unaligned
>
485 typedef llvm::support::detail::packed_endian_specific_integral
<
486 double, llvm::support::little
, llvm::support::unaligned
>
489 llvm::support::ulittle64_t u64
;
490 llvm::support::ulittle32_t u32
;
491 llvm::support::ulittle16_t u16
;
492 llvm::support::little64_t s64
;
493 llvm::support::little32_t s32
;
494 llvm::support::little16_t s16
;
501 template <> struct MappingTraits
<EndianTypes
> {
502 static void mapping(IO
&io
, EndianTypes
&et
) {
503 io
.mapRequired("u64", et
.u64
);
504 io
.mapRequired("u32", et
.u32
);
505 io
.mapRequired("u16", et
.u16
);
506 io
.mapRequired("s64", et
.s64
);
507 io
.mapRequired("s32", et
.s32
);
508 io
.mapRequired("s16", et
.s16
);
509 io
.mapRequired("f", et
.f
);
510 io
.mapRequired("d", et
.d
);
517 // Test the reading of all endian scalar conversions
519 TEST(YAMLIO
, TestReadEndianTypes
) {
533 EXPECT_FALSE(yin
.error());
534 EXPECT_EQ(map
.u64
, 5000000000ULL);
535 EXPECT_EQ(map
.u32
, 4000000000U);
536 EXPECT_EQ(map
.u16
, 65000);
537 EXPECT_EQ(map
.s64
, -5000000000LL);
538 EXPECT_EQ(map
.s32
, -2000000000L);
539 EXPECT_EQ(map
.s16
, -32000);
540 EXPECT_EQ(map
.f
, 3.25f
);
541 EXPECT_EQ(map
.d
, -2.8625);
545 // Test writing then reading back all endian-aware scalar types
547 TEST(YAMLIO
, TestReadWriteEndianTypes
) {
548 std::string intermediate
;
551 map
.u64
= 6000000000ULL;
552 map
.u32
= 3000000000U;
554 map
.s64
= -6000000000LL;
555 map
.s32
= -2000000000;
560 llvm::raw_string_ostream
ostr(intermediate
);
566 Input
yin(intermediate
);
570 EXPECT_FALSE(yin
.error());
571 EXPECT_EQ(map
.u64
, 6000000000ULL);
572 EXPECT_EQ(map
.u32
, 3000000000U);
573 EXPECT_EQ(map
.u16
, 50000);
574 EXPECT_EQ(map
.s64
, -6000000000LL);
575 EXPECT_EQ(map
.s32
, -2000000000L);
576 EXPECT_EQ(map
.s16
, -32000);
577 EXPECT_EQ(map
.f
, 3.25f
);
578 EXPECT_EQ(map
.d
, -2.8625);
582 enum class Enum
: uint16_t { One
, Two
};
583 enum class BitsetEnum
: uint16_t {
586 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ OneZero
),
588 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
590 llvm::support::little_t
<Enum
> LittleEnum
;
591 llvm::support::big_t
<Enum
> BigEnum
;
592 llvm::support::little_t
<BitsetEnum
> LittleBitset
;
593 llvm::support::big_t
<BitsetEnum
> BigBitset
;
597 template <> struct ScalarEnumerationTraits
<Enum
> {
598 static void enumeration(IO
&io
, Enum
&E
) {
599 io
.enumCase(E
, "One", Enum::One
);
600 io
.enumCase(E
, "Two", Enum::Two
);
604 template <> struct ScalarBitSetTraits
<BitsetEnum
> {
605 static void bitset(IO
&io
, BitsetEnum
&E
) {
606 io
.bitSetCase(E
, "ZeroOne", BitsetEnum::ZeroOne
);
607 io
.bitSetCase(E
, "OneZero", BitsetEnum::OneZero
);
611 template <> struct MappingTraits
<EndianEnums
> {
612 static void mapping(IO
&io
, EndianEnums
&EE
) {
613 io
.mapRequired("LittleEnum", EE
.LittleEnum
);
614 io
.mapRequired("BigEnum", EE
.BigEnum
);
615 io
.mapRequired("LittleBitset", EE
.LittleBitset
);
616 io
.mapRequired("BigBitset", EE
.BigBitset
);
622 TEST(YAMLIO
, TestReadEndianEnums
) {
627 "LittleBitset: [ ZeroOne ]\n"
628 "BigBitset: [ ZeroOne, OneZero ]\n"
632 EXPECT_FALSE(yin
.error());
633 EXPECT_EQ(Enum::One
, map
.LittleEnum
);
634 EXPECT_EQ(Enum::Two
, map
.BigEnum
);
635 EXPECT_EQ(BitsetEnum::ZeroOne
, map
.LittleBitset
);
636 EXPECT_EQ(BitsetEnum::ZeroOne
| BitsetEnum::OneZero
, map
.BigBitset
);
639 TEST(YAMLIO
, TestReadWriteEndianEnums
) {
640 std::string intermediate
;
643 map
.LittleEnum
= Enum::Two
;
644 map
.BigEnum
= Enum::One
;
645 map
.LittleBitset
= BitsetEnum::OneZero
| BitsetEnum::ZeroOne
;
646 map
.BigBitset
= BitsetEnum::OneZero
;
648 llvm::raw_string_ostream
ostr(intermediate
);
654 Input
yin(intermediate
);
658 EXPECT_FALSE(yin
.error());
659 EXPECT_EQ(Enum::Two
, map
.LittleEnum
);
660 EXPECT_EQ(Enum::One
, map
.BigEnum
);
661 EXPECT_EQ(BitsetEnum::OneZero
| BitsetEnum::ZeroOne
, map
.LittleBitset
);
662 EXPECT_EQ(BitsetEnum::OneZero
, map
.BigBitset
);
667 llvm::StringRef str1
;
668 llvm::StringRef str2
;
669 llvm::StringRef str3
;
670 llvm::StringRef str4
;
671 llvm::StringRef str5
;
672 llvm::StringRef str6
;
673 llvm::StringRef str7
;
674 llvm::StringRef str8
;
675 llvm::StringRef str9
;
676 llvm::StringRef str10
;
677 llvm::StringRef str11
;
687 std::string stdstr10
;
688 std::string stdstr11
;
689 std::string stdstr12
;
690 std::string stdstr13
;
696 struct MappingTraits
<StringTypes
> {
697 static void mapping(IO
&io
, StringTypes
& st
) {
698 io
.mapRequired("str1", st
.str1
);
699 io
.mapRequired("str2", st
.str2
);
700 io
.mapRequired("str3", st
.str3
);
701 io
.mapRequired("str4", st
.str4
);
702 io
.mapRequired("str5", st
.str5
);
703 io
.mapRequired("str6", st
.str6
);
704 io
.mapRequired("str7", st
.str7
);
705 io
.mapRequired("str8", st
.str8
);
706 io
.mapRequired("str9", st
.str9
);
707 io
.mapRequired("str10", st
.str10
);
708 io
.mapRequired("str11", st
.str11
);
709 io
.mapRequired("stdstr1", st
.stdstr1
);
710 io
.mapRequired("stdstr2", st
.stdstr2
);
711 io
.mapRequired("stdstr3", st
.stdstr3
);
712 io
.mapRequired("stdstr4", st
.stdstr4
);
713 io
.mapRequired("stdstr5", st
.stdstr5
);
714 io
.mapRequired("stdstr6", st
.stdstr6
);
715 io
.mapRequired("stdstr7", st
.stdstr7
);
716 io
.mapRequired("stdstr8", st
.stdstr8
);
717 io
.mapRequired("stdstr9", st
.stdstr9
);
718 io
.mapRequired("stdstr10", st
.stdstr10
);
719 io
.mapRequired("stdstr11", st
.stdstr11
);
720 io
.mapRequired("stdstr12", st
.stdstr12
);
721 io
.mapRequired("stdstr13", st
.stdstr13
);
727 TEST(YAMLIO
, TestReadWriteStringTypes
) {
728 std::string intermediate
;
736 map
.str6
= "0000000004000000";
740 map
.str10
= "0.2e20";
742 map
.stdstr1
= "'eee";
743 map
.stdstr2
= "\"fff";
744 map
.stdstr3
= "`ggg";
745 map
.stdstr4
= "@hhh";
747 map
.stdstr6
= "0000000004000000";
748 map
.stdstr7
= "true";
749 map
.stdstr8
= "FALSE";
751 map
.stdstr10
= "0.2e20";
752 map
.stdstr11
= "0x30";
753 map
.stdstr12
= "- match";
754 map
.stdstr13
.assign("\0a\0b\0", 5);
756 llvm::raw_string_ostream
ostr(intermediate
);
761 llvm::StringRef
flowOut(intermediate
);
762 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'''aaa"));
763 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'\"bbb'"));
764 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'`ccc'"));
765 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'@ddd'"));
766 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("''\n"));
767 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0000000004000000'\n"));
768 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'true'\n"));
769 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'FALSE'\n"));
770 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'~'\n"));
771 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0.2e20'\n"));
772 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0x30'\n"));
773 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'- match'\n"));
774 EXPECT_NE(std::string::npos
, flowOut
.find("'''eee"));
775 EXPECT_NE(std::string::npos
, flowOut
.find("'\"fff'"));
776 EXPECT_NE(std::string::npos
, flowOut
.find("'`ggg'"));
777 EXPECT_NE(std::string::npos
, flowOut
.find("'@hhh'"));
778 EXPECT_NE(std::string::npos
, flowOut
.find("''\n"));
779 EXPECT_NE(std::string::npos
, flowOut
.find("'0000000004000000'\n"));
780 EXPECT_NE(std::string::npos
, flowOut
.find("\"\\0a\\0b\\0\""));
783 Input
yin(intermediate
);
787 EXPECT_FALSE(yin
.error());
788 EXPECT_TRUE(map
.str1
.equals("'aaa"));
789 EXPECT_TRUE(map
.str2
.equals("\"bbb"));
790 EXPECT_TRUE(map
.str3
.equals("`ccc"));
791 EXPECT_TRUE(map
.str4
.equals("@ddd"));
792 EXPECT_TRUE(map
.str5
.equals(""));
793 EXPECT_TRUE(map
.str6
.equals("0000000004000000"));
794 EXPECT_TRUE(map
.stdstr1
== "'eee");
795 EXPECT_TRUE(map
.stdstr2
== "\"fff");
796 EXPECT_TRUE(map
.stdstr3
== "`ggg");
797 EXPECT_TRUE(map
.stdstr4
== "@hhh");
798 EXPECT_TRUE(map
.stdstr5
== "");
799 EXPECT_TRUE(map
.stdstr6
== "0000000004000000");
800 EXPECT_EQ(std::string("\0a\0b\0", 5), map
.stdstr13
);
804 //===----------------------------------------------------------------------===//
805 // Test ScalarEnumerationTraits
806 //===----------------------------------------------------------------------===//
827 struct ScalarEnumerationTraits
<Colors
> {
828 static void enumeration(IO
&io
, Colors
&value
) {
829 io
.enumCase(value
, "red", cRed
);
830 io
.enumCase(value
, "blue", cBlue
);
831 io
.enumCase(value
, "green", cGreen
);
832 io
.enumCase(value
, "yellow",cYellow
);
836 struct MappingTraits
<ColorMap
> {
837 static void mapping(IO
&io
, ColorMap
& c
) {
838 io
.mapRequired("c1", c
.c1
);
839 io
.mapRequired("c2", c
.c2
);
840 io
.mapRequired("c3", c
.c3
);
841 io
.mapOptional("c4", c
.c4
, cBlue
); // supplies default
842 io
.mapOptional("c5", c
.c5
, cYellow
); // supplies default
843 io
.mapOptional("c6", c
.c6
, cRed
); // supplies default
851 // Test reading enumerated scalars
853 TEST(YAMLIO
, TestEnumRead
) {
863 EXPECT_FALSE(yin
.error());
864 EXPECT_EQ(cBlue
, map
.c1
);
865 EXPECT_EQ(cRed
, map
.c2
);
866 EXPECT_EQ(cGreen
, map
.c3
);
867 EXPECT_EQ(cBlue
, map
.c4
); // tests default
868 EXPECT_EQ(cYellow
,map
.c5
); // tests overridden
869 EXPECT_EQ(cRed
, map
.c6
); // tests default
874 //===----------------------------------------------------------------------===//
875 // Test ScalarBitSetTraits
876 //===----------------------------------------------------------------------===//
885 inline MyFlags
operator|(MyFlags a
, MyFlags b
) {
886 return static_cast<MyFlags
>(
887 static_cast<uint32_t>(a
) | static_cast<uint32_t>(b
));
901 struct ScalarBitSetTraits
<MyFlags
> {
902 static void bitset(IO
&io
, MyFlags
&value
) {
903 io
.bitSetCase(value
, "big", flagBig
);
904 io
.bitSetCase(value
, "flat", flagFlat
);
905 io
.bitSetCase(value
, "round", flagRound
);
906 io
.bitSetCase(value
, "pointy",flagPointy
);
910 struct MappingTraits
<FlagsMap
> {
911 static void mapping(IO
&io
, FlagsMap
& c
) {
912 io
.mapRequired("f1", c
.f1
);
913 io
.mapRequired("f2", c
.f2
);
914 io
.mapRequired("f3", c
.f3
);
915 io
.mapOptional("f4", c
.f4
, flagRound
);
923 // Test reading flow sequence representing bit-mask values
925 TEST(YAMLIO
, TestFlagsRead
) {
929 "f2: [ round, flat ]\n"
934 EXPECT_FALSE(yin
.error());
935 EXPECT_EQ(flagBig
, map
.f1
);
936 EXPECT_EQ(flagRound
|flagFlat
, map
.f2
);
937 EXPECT_EQ(flagNone
, map
.f3
); // check empty set
938 EXPECT_EQ(flagRound
, map
.f4
); // check optional key
943 // Test writing then reading back bit-mask values
945 TEST(YAMLIO
, TestReadWriteFlags
) {
946 std::string intermediate
;
950 map
.f2
= flagRound
| flagFlat
;
954 llvm::raw_string_ostream
ostr(intermediate
);
960 Input
yin(intermediate
);
964 EXPECT_FALSE(yin
.error());
965 EXPECT_EQ(flagBig
, map2
.f1
);
966 EXPECT_EQ(flagRound
|flagFlat
, map2
.f2
);
967 EXPECT_EQ(flagNone
, map2
.f3
);
968 //EXPECT_EQ(flagRound, map2.f4); // check optional key
974 //===----------------------------------------------------------------------===//
976 //===----------------------------------------------------------------------===//
978 struct MyCustomType
{
983 struct MyCustomTypeMap
{
993 struct MappingTraits
<MyCustomTypeMap
> {
994 static void mapping(IO
&io
, MyCustomTypeMap
& s
) {
995 io
.mapRequired("f1", s
.f1
);
996 io
.mapRequired("f2", s
.f2
);
997 io
.mapRequired("f3", s
.f3
);
1000 // MyCustomType is formatted as a yaml scalar. A value of
1001 // {length=3, width=4} would be represented in yaml as "3 by 4".
1003 struct ScalarTraits
<MyCustomType
> {
1004 static void output(const MyCustomType
&value
, void* ctxt
, llvm::raw_ostream
&out
) {
1005 out
<< llvm::format("%d by %d", value
.length
, value
.width
);
1007 static StringRef
input(StringRef scalar
, void* ctxt
, MyCustomType
&value
) {
1008 size_t byStart
= scalar
.find("by");
1009 if ( byStart
!= StringRef::npos
) {
1010 StringRef lenStr
= scalar
.slice(0, byStart
);
1011 lenStr
= lenStr
.rtrim();
1012 if ( lenStr
.getAsInteger(0, value
.length
) ) {
1013 return "malformed length";
1015 StringRef widthStr
= scalar
.drop_front(byStart
+2);
1016 widthStr
= widthStr
.ltrim();
1017 if ( widthStr
.getAsInteger(0, value
.width
) ) {
1018 return "malformed width";
1023 return "malformed by";
1026 static QuotingType
mustQuote(StringRef
) { return QuotingType::Single
; }
1033 // Test writing then reading back custom values
1035 TEST(YAMLIO
, TestReadWriteMyCustomType
) {
1036 std::string intermediate
;
1038 MyCustomTypeMap map
;
1041 map
.f2
.length
= 100;
1045 llvm::raw_string_ostream
ostr(intermediate
);
1051 Input
yin(intermediate
);
1052 MyCustomTypeMap map2
;
1055 EXPECT_FALSE(yin
.error());
1056 EXPECT_EQ(1, map2
.f1
.length
);
1057 EXPECT_EQ(4, map2
.f1
.width
);
1058 EXPECT_EQ(100, map2
.f2
.length
);
1059 EXPECT_EQ(400, map2
.f2
.width
);
1060 EXPECT_EQ(10, map2
.f3
);
1065 //===----------------------------------------------------------------------===//
1066 // Test BlockScalarTraits
1067 //===----------------------------------------------------------------------===//
1069 struct MultilineStringType
{
1073 struct MultilineStringTypeMap
{
1074 MultilineStringType name
;
1075 MultilineStringType description
;
1076 MultilineStringType ingredients
;
1077 MultilineStringType recipes
;
1078 MultilineStringType warningLabels
;
1079 MultilineStringType documentation
;
1086 struct MappingTraits
<MultilineStringTypeMap
> {
1087 static void mapping(IO
&io
, MultilineStringTypeMap
& s
) {
1088 io
.mapRequired("name", s
.name
);
1089 io
.mapRequired("description", s
.description
);
1090 io
.mapRequired("ingredients", s
.ingredients
);
1091 io
.mapRequired("recipes", s
.recipes
);
1092 io
.mapRequired("warningLabels", s
.warningLabels
);
1093 io
.mapRequired("documentation", s
.documentation
);
1094 io
.mapRequired("price", s
.price
);
1098 // MultilineStringType is formatted as a yaml block literal scalar. A value of
1099 // "Hello\nWorld" would be represented in yaml as
1104 struct BlockScalarTraits
<MultilineStringType
> {
1105 static void output(const MultilineStringType
&value
, void *ctxt
,
1106 llvm::raw_ostream
&out
) {
1109 static StringRef
input(StringRef scalar
, void *ctxt
,
1110 MultilineStringType
&value
) {
1111 value
.str
= scalar
.str();
1118 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType
)
1121 // Test writing then reading back custom values
1123 TEST(YAMLIO
, TestReadWriteMultilineStringType
) {
1124 std::string intermediate
;
1126 MultilineStringTypeMap map
;
1127 map
.name
.str
= "An Item";
1128 map
.description
.str
= "Hello\nWorld";
1129 map
.ingredients
.str
= "SubItem 1\nSub Item 2\n\nSub Item 3\n";
1130 map
.recipes
.str
= "\n\nTest 1\n\n\n";
1131 map
.warningLabels
.str
= "";
1132 map
.documentation
.str
= "\n\n";
1135 llvm::raw_string_ostream
ostr(intermediate
);
1140 Input
yin(intermediate
);
1141 MultilineStringTypeMap map2
;
1144 EXPECT_FALSE(yin
.error());
1145 EXPECT_EQ(map2
.name
.str
, "An Item\n");
1146 EXPECT_EQ(map2
.description
.str
, "Hello\nWorld\n");
1147 EXPECT_EQ(map2
.ingredients
.str
, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
1148 EXPECT_EQ(map2
.recipes
.str
, "\n\nTest 1\n");
1149 EXPECT_TRUE(map2
.warningLabels
.str
.empty());
1150 EXPECT_TRUE(map2
.documentation
.str
.empty());
1151 EXPECT_EQ(map2
.price
, 350);
1156 // Test writing then reading back custom values
1158 TEST(YAMLIO
, TestReadWriteBlockScalarDocuments
) {
1159 std::string intermediate
;
1161 std::vector
<MultilineStringType
> documents
;
1162 MultilineStringType doc
;
1163 doc
.str
= "Hello\nWorld";
1164 documents
.push_back(doc
);
1166 llvm::raw_string_ostream
ostr(intermediate
);
1170 // Verify that the block scalar header was written out on the same line
1171 // as the document marker.
1172 EXPECT_NE(llvm::StringRef::npos
, llvm::StringRef(ostr
.str()).find("--- |"));
1175 Input
yin(intermediate
);
1176 std::vector
<MultilineStringType
> documents2
;
1179 EXPECT_FALSE(yin
.error());
1180 EXPECT_EQ(documents2
.size(), size_t(1));
1181 EXPECT_EQ(documents2
[0].str
, "Hello\nWorld\n");
1185 TEST(YAMLIO
, TestReadWriteBlockScalarValue
) {
1186 std::string intermediate
;
1188 MultilineStringType doc
;
1189 doc
.str
= "Just a block\nscalar doc";
1191 llvm::raw_string_ostream
ostr(intermediate
);
1196 Input
yin(intermediate
);
1197 MultilineStringType doc
;
1200 EXPECT_FALSE(yin
.error());
1201 EXPECT_EQ(doc
.str
, "Just a block\nscalar doc\n");
1205 //===----------------------------------------------------------------------===//
1206 // Test flow sequences
1207 //===----------------------------------------------------------------------===//
1209 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber
)
1210 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber
)
1211 LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef
, MyString
)
1212 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString
)
1217 struct ScalarTraits
<MyNumber
> {
1218 static void output(const MyNumber
&value
, void *, llvm::raw_ostream
&out
) {
1222 static StringRef
input(StringRef scalar
, void *, MyNumber
&value
) {
1224 if ( getAsSignedInteger(scalar
, 0, n
) )
1225 return "invalid number";
1230 static QuotingType
mustQuote(StringRef
) { return QuotingType::None
; }
1233 template <> struct ScalarTraits
<MyString
> {
1234 using Impl
= ScalarTraits
<StringRef
>;
1235 static void output(const MyString
&V
, void *Ctx
, raw_ostream
&OS
) {
1236 Impl::output(V
, Ctx
, OS
);
1238 static StringRef
input(StringRef S
, void *Ctx
, MyString
&V
) {
1239 return Impl::input(S
, Ctx
, V
.value
);
1241 static QuotingType
mustQuote(StringRef S
) {
1242 return Impl::mustQuote(S
);
1248 struct NameAndNumbers
{
1249 llvm::StringRef name
;
1250 std::vector
<MyString
> strings
;
1251 std::vector
<MyNumber
> single
;
1252 std::vector
<MyNumber
> numbers
;
1258 struct MappingTraits
<NameAndNumbers
> {
1259 static void mapping(IO
&io
, NameAndNumbers
& nn
) {
1260 io
.mapRequired("name", nn
.name
);
1261 io
.mapRequired("strings", nn
.strings
);
1262 io
.mapRequired("single", nn
.single
);
1263 io
.mapRequired("numbers", nn
.numbers
);
1269 typedef std::vector
<MyNumber
> MyNumberFlowSequence
;
1271 LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence
)
1273 struct NameAndNumbersFlow
{
1274 llvm::StringRef name
;
1275 std::vector
<MyNumberFlowSequence
> sequenceOfNumbers
;
1281 struct MappingTraits
<NameAndNumbersFlow
> {
1282 static void mapping(IO
&io
, NameAndNumbersFlow
& nn
) {
1283 io
.mapRequired("name", nn
.name
);
1284 io
.mapRequired("sequenceOfNumbers", nn
.sequenceOfNumbers
);
1291 // Test writing then reading back custom values
1293 TEST(YAMLIO
, TestReadWriteMyFlowSequence
) {
1294 std::string intermediate
;
1298 map
.strings
.push_back(llvm::StringRef("one"));
1299 map
.strings
.push_back(llvm::StringRef("two"));
1300 map
.single
.push_back(1);
1301 map
.numbers
.push_back(10);
1302 map
.numbers
.push_back(-30);
1303 map
.numbers
.push_back(1024);
1305 llvm::raw_string_ostream
ostr(intermediate
);
1309 // Verify sequences were written in flow style
1311 llvm::StringRef
flowOut(intermediate
);
1312 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("one, two"));
1313 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("10, -30, 1024"));
1317 Input
yin(intermediate
);
1318 NameAndNumbers map2
;
1321 EXPECT_FALSE(yin
.error());
1322 EXPECT_TRUE(map2
.name
.equals("hello"));
1323 EXPECT_EQ(map2
.strings
.size(), 2UL);
1324 EXPECT_TRUE(map2
.strings
[0].value
.equals("one"));
1325 EXPECT_TRUE(map2
.strings
[1].value
.equals("two"));
1326 EXPECT_EQ(map2
.single
.size(), 1UL);
1327 EXPECT_EQ(1, map2
.single
[0]);
1328 EXPECT_EQ(map2
.numbers
.size(), 3UL);
1329 EXPECT_EQ(10, map2
.numbers
[0]);
1330 EXPECT_EQ(-30, map2
.numbers
[1]);
1331 EXPECT_EQ(1024, map2
.numbers
[2]);
1337 // Test writing then reading back a sequence of flow sequences.
1339 TEST(YAMLIO
, TestReadWriteSequenceOfMyFlowSequence
) {
1340 std::string intermediate
;
1342 NameAndNumbersFlow map
;
1344 MyNumberFlowSequence single
= { 0 };
1345 MyNumberFlowSequence numbers
= { 12, 1, -512 };
1346 map
.sequenceOfNumbers
.push_back(single
);
1347 map
.sequenceOfNumbers
.push_back(numbers
);
1348 map
.sequenceOfNumbers
.push_back(MyNumberFlowSequence());
1350 llvm::raw_string_ostream
ostr(intermediate
);
1354 // Verify sequences were written in flow style
1355 // and that the parent sequence used '-'.
1357 llvm::StringRef
flowOut(intermediate
);
1358 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ 0 ]"));
1359 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ 12, 1, -512 ]"));
1360 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ ]"));
1364 Input
yin(intermediate
);
1365 NameAndNumbersFlow map2
;
1368 EXPECT_FALSE(yin
.error());
1369 EXPECT_TRUE(map2
.name
.equals("hello"));
1370 EXPECT_EQ(map2
.sequenceOfNumbers
.size(), 3UL);
1371 EXPECT_EQ(map2
.sequenceOfNumbers
[0].size(), 1UL);
1372 EXPECT_EQ(0, map2
.sequenceOfNumbers
[0][0]);
1373 EXPECT_EQ(map2
.sequenceOfNumbers
[1].size(), 3UL);
1374 EXPECT_EQ(12, map2
.sequenceOfNumbers
[1][0]);
1375 EXPECT_EQ(1, map2
.sequenceOfNumbers
[1][1]);
1376 EXPECT_EQ(-512, map2
.sequenceOfNumbers
[1][2]);
1377 EXPECT_TRUE(map2
.sequenceOfNumbers
[2].empty());
1381 //===----------------------------------------------------------------------===//
1382 // Test normalizing/denormalizing
1383 //===----------------------------------------------------------------------===//
1385 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds
)
1387 typedef std::vector
<TotalSeconds
> SecondsSequence
;
1389 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds
)
1395 struct MappingTraits
<TotalSeconds
> {
1397 class NormalizedSeconds
{
1399 NormalizedSeconds(IO
&io
)
1400 : hours(0), minutes(0), seconds(0) {
1402 NormalizedSeconds(IO
&, TotalSeconds
&secs
)
1404 minutes((secs
- (hours
*3600))/60),
1405 seconds(secs
% 60) {
1407 TotalSeconds
denormalize(IO
&) {
1408 return TotalSeconds(hours
*3600 + minutes
*60 + seconds
);
1416 static void mapping(IO
&io
, TotalSeconds
&secs
) {
1417 MappingNormalization
<NormalizedSeconds
, TotalSeconds
> keys(io
, secs
);
1419 io
.mapOptional("hours", keys
->hours
, 0);
1420 io
.mapOptional("minutes", keys
->minutes
, 0);
1421 io
.mapRequired("seconds", keys
->seconds
);
1429 // Test the reading of a yaml sequence of mappings
1431 TEST(YAMLIO
, TestReadMySecondsSequence
) {
1432 SecondsSequence seq
;
1433 Input
yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1436 EXPECT_FALSE(yin
.error());
1437 EXPECT_EQ(seq
.size(), 2UL);
1438 EXPECT_EQ(seq
[0], 3605U);
1439 EXPECT_EQ(seq
[1], 59U);
1444 // Test writing then reading back custom values
1446 TEST(YAMLIO
, TestReadWriteMySecondsSequence
) {
1447 std::string intermediate
;
1449 SecondsSequence seq
;
1450 seq
.push_back(4000);
1454 llvm::raw_string_ostream
ostr(intermediate
);
1459 Input
yin(intermediate
);
1460 SecondsSequence seq2
;
1463 EXPECT_FALSE(yin
.error());
1464 EXPECT_EQ(seq2
.size(), 3UL);
1465 EXPECT_EQ(seq2
[0], 4000U);
1466 EXPECT_EQ(seq2
[1], 500U);
1467 EXPECT_EQ(seq2
[2], 59U);
1472 //===----------------------------------------------------------------------===//
1473 // Test dynamic typing
1474 //===----------------------------------------------------------------------===//
1493 struct KindAndFlags
{
1494 KindAndFlags() : kind(kindA
), flags(0) { }
1495 KindAndFlags(Kind k
, uint32_t f
) : kind(k
), flags(f
) { }
1500 typedef std::vector
<KindAndFlags
> KindAndFlagsSequence
;
1502 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags
)
1507 struct ScalarEnumerationTraits
<AFlags
> {
1508 static void enumeration(IO
&io
, AFlags
&value
) {
1509 io
.enumCase(value
, "a1", a1
);
1510 io
.enumCase(value
, "a2", a2
);
1511 io
.enumCase(value
, "a3", a3
);
1515 struct ScalarEnumerationTraits
<BFlags
> {
1516 static void enumeration(IO
&io
, BFlags
&value
) {
1517 io
.enumCase(value
, "b1", b1
);
1518 io
.enumCase(value
, "b2", b2
);
1519 io
.enumCase(value
, "b3", b3
);
1523 struct ScalarEnumerationTraits
<Kind
> {
1524 static void enumeration(IO
&io
, Kind
&value
) {
1525 io
.enumCase(value
, "A", kindA
);
1526 io
.enumCase(value
, "B", kindB
);
1530 struct MappingTraits
<KindAndFlags
> {
1531 static void mapping(IO
&io
, KindAndFlags
& kf
) {
1532 io
.mapRequired("kind", kf
.kind
);
1533 // Type of "flags" field varies depending on "kind" field.
1534 // Use memcpy here to avoid breaking strict aliasing rules.
1535 if (kf
.kind
== kindA
) {
1536 AFlags aflags
= static_cast<AFlags
>(kf
.flags
);
1537 io
.mapRequired("flags", aflags
);
1540 BFlags bflags
= static_cast<BFlags
>(kf
.flags
);
1541 io
.mapRequired("flags", bflags
);
1551 // Test the reading of a yaml sequence dynamic types
1553 TEST(YAMLIO
, TestReadKindAndFlagsSequence
) {
1554 KindAndFlagsSequence seq
;
1555 Input
yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1558 EXPECT_FALSE(yin
.error());
1559 EXPECT_EQ(seq
.size(), 2UL);
1560 EXPECT_EQ(seq
[0].kind
, kindA
);
1561 EXPECT_EQ(seq
[0].flags
, (uint32_t)a2
);
1562 EXPECT_EQ(seq
[1].kind
, kindB
);
1563 EXPECT_EQ(seq
[1].flags
, (uint32_t)b1
);
1567 // Test writing then reading back dynamic types
1569 TEST(YAMLIO
, TestReadWriteKindAndFlagsSequence
) {
1570 std::string intermediate
;
1572 KindAndFlagsSequence seq
;
1573 seq
.push_back(KindAndFlags(kindA
,a1
));
1574 seq
.push_back(KindAndFlags(kindB
,b1
));
1575 seq
.push_back(KindAndFlags(kindA
,a2
));
1576 seq
.push_back(KindAndFlags(kindB
,b2
));
1577 seq
.push_back(KindAndFlags(kindA
,a3
));
1579 llvm::raw_string_ostream
ostr(intermediate
);
1584 Input
yin(intermediate
);
1585 KindAndFlagsSequence seq2
;
1588 EXPECT_FALSE(yin
.error());
1589 EXPECT_EQ(seq2
.size(), 5UL);
1590 EXPECT_EQ(seq2
[0].kind
, kindA
);
1591 EXPECT_EQ(seq2
[0].flags
, (uint32_t)a1
);
1592 EXPECT_EQ(seq2
[1].kind
, kindB
);
1593 EXPECT_EQ(seq2
[1].flags
, (uint32_t)b1
);
1594 EXPECT_EQ(seq2
[2].kind
, kindA
);
1595 EXPECT_EQ(seq2
[2].flags
, (uint32_t)a2
);
1596 EXPECT_EQ(seq2
[3].kind
, kindB
);
1597 EXPECT_EQ(seq2
[3].flags
, (uint32_t)b2
);
1598 EXPECT_EQ(seq2
[4].kind
, kindA
);
1599 EXPECT_EQ(seq2
[4].flags
, (uint32_t)a3
);
1604 //===----------------------------------------------------------------------===//
1605 // Test document list
1606 //===----------------------------------------------------------------------===//
1612 typedef std::vector
<FooBarMap
> FooBarMapDocumentList
;
1614 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap
)
1620 struct MappingTraits
<FooBarMap
> {
1621 static void mapping(IO
&io
, FooBarMap
& fb
) {
1622 io
.mapRequired("foo", fb
.foo
);
1623 io
.mapRequired("bar", fb
.bar
);
1631 // Test the reading of a yaml mapping
1633 TEST(YAMLIO
, TestDocRead
) {
1635 Input
yin("---\nfoo: 3\nbar: 5\n...\n");
1638 EXPECT_FALSE(yin
.error());
1639 EXPECT_EQ(doc
.foo
, 3);
1640 EXPECT_EQ(doc
.bar
,5);
1646 // Test writing then reading back a sequence of mappings
1648 TEST(YAMLIO
, TestSequenceDocListWriteAndRead
) {
1649 std::string intermediate
;
1657 std::vector
<FooBarMap
> docList
;
1658 docList
.push_back(doc1
);
1659 docList
.push_back(doc2
);
1661 llvm::raw_string_ostream
ostr(intermediate
);
1668 Input
yin(intermediate
);
1669 std::vector
<FooBarMap
> docList2
;
1672 EXPECT_FALSE(yin
.error());
1673 EXPECT_EQ(docList2
.size(), 2UL);
1674 FooBarMap
& map1
= docList2
[0];
1675 FooBarMap
& map2
= docList2
[1];
1676 EXPECT_EQ(map1
.foo
, 10);
1677 EXPECT_EQ(map1
.bar
, -3);
1678 EXPECT_EQ(map2
.foo
, 257);
1679 EXPECT_EQ(map2
.bar
, 0);
1683 //===----------------------------------------------------------------------===//
1684 // Test document tags
1685 //===----------------------------------------------------------------------===//
1688 MyDouble() : value(0.0) { }
1689 MyDouble(double x
) : value(x
) { }
1693 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble
)
1699 struct MappingTraits
<MyDouble
> {
1700 static void mapping(IO
&io
, MyDouble
&d
) {
1701 if (io
.mapTag("!decimal", true)) {
1702 mappingDecimal(io
, d
);
1703 } else if (io
.mapTag("!fraction")) {
1704 mappingFraction(io
, d
);
1707 static void mappingDecimal(IO
&io
, MyDouble
&d
) {
1708 io
.mapRequired("value", d
.value
);
1710 static void mappingFraction(IO
&io
, MyDouble
&d
) {
1712 io
.mapRequired("numerator", num
);
1713 io
.mapRequired("denominator", denom
);
1714 // convert fraction to double
1715 d
.value
= num
/denom
;
1723 // Test the reading of two different tagged yaml documents.
1725 TEST(YAMLIO
, TestTaggedDocuments
) {
1726 std::vector
<MyDouble
> docList
;
1727 Input
yin("--- !decimal\nvalue: 3.0\n"
1728 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1730 EXPECT_FALSE(yin
.error());
1731 EXPECT_EQ(docList
.size(), 2UL);
1732 EXPECT_EQ(docList
[0].value
, 3.0);
1733 EXPECT_EQ(docList
[1].value
, 4.5);
1739 // Test writing then reading back tagged documents
1741 TEST(YAMLIO
, TestTaggedDocumentsWriteAndRead
) {
1742 std::string intermediate
;
1746 std::vector
<MyDouble
> docList
;
1747 docList
.push_back(a
);
1748 docList
.push_back(b
);
1750 llvm::raw_string_ostream
ostr(intermediate
);
1756 Input
yin(intermediate
);
1757 std::vector
<MyDouble
> docList2
;
1760 EXPECT_FALSE(yin
.error());
1761 EXPECT_EQ(docList2
.size(), 2UL);
1762 EXPECT_EQ(docList2
[0].value
, 10.25);
1763 EXPECT_EQ(docList2
[1].value
, -3.75);
1768 //===----------------------------------------------------------------------===//
1769 // Test mapping validation
1770 //===----------------------------------------------------------------------===//
1772 struct MyValidation
{
1776 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation
)
1781 struct MappingTraits
<MyValidation
> {
1782 static void mapping(IO
&io
, MyValidation
&d
) {
1783 io
.mapRequired("value", d
.value
);
1785 static std::string
validate(IO
&io
, MyValidation
&d
) {
1787 return "negative value";
1796 // Test that validate() is called and complains about the negative value.
1798 TEST(YAMLIO
, TestValidatingInput
) {
1799 std::vector
<MyValidation
> docList
;
1800 Input
yin("--- \nvalue: 3.0\n"
1801 "--- \nvalue: -1.0\n...\n",
1802 nullptr, suppressErrorMessages
);
1804 EXPECT_TRUE(!!yin
.error());
1807 //===----------------------------------------------------------------------===//
1808 // Test flow mapping
1809 //===----------------------------------------------------------------------===//
1815 FlowFooBar() : foo(0), bar(0) {}
1816 FlowFooBar(int foo
, int bar
) : foo(foo
), bar(bar
) {}
1819 typedef std::vector
<FlowFooBar
> FlowFooBarSequence
;
1821 LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar
)
1823 struct FlowFooBarDoc
{
1824 FlowFooBar attribute
;
1825 FlowFooBarSequence seq
;
1831 struct MappingTraits
<FlowFooBar
> {
1832 static void mapping(IO
&io
, FlowFooBar
&fb
) {
1833 io
.mapRequired("foo", fb
.foo
);
1834 io
.mapRequired("bar", fb
.bar
);
1837 static const bool flow
= true;
1841 struct MappingTraits
<FlowFooBarDoc
> {
1842 static void mapping(IO
&io
, FlowFooBarDoc
&fb
) {
1843 io
.mapRequired("attribute", fb
.attribute
);
1844 io
.mapRequired("seq", fb
.seq
);
1851 // Test writing then reading back custom mappings
1853 TEST(YAMLIO
, TestReadWriteMyFlowMapping
) {
1854 std::string intermediate
;
1857 doc
.attribute
= FlowFooBar(42, 907);
1858 doc
.seq
.push_back(FlowFooBar(1, 2));
1859 doc
.seq
.push_back(FlowFooBar(0, 0));
1860 doc
.seq
.push_back(FlowFooBar(-1, 1024));
1862 llvm::raw_string_ostream
ostr(intermediate
);
1866 // Verify that mappings were written in flow style
1868 llvm::StringRef
flowOut(intermediate
);
1869 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("{ foo: 42, bar: 907 }"));
1870 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: 1, bar: 2 }"));
1871 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: 0, bar: 0 }"));
1872 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: -1, bar: 1024 }"));
1876 Input
yin(intermediate
);
1880 EXPECT_FALSE(yin
.error());
1881 EXPECT_EQ(doc2
.attribute
.foo
, 42);
1882 EXPECT_EQ(doc2
.attribute
.bar
, 907);
1883 EXPECT_EQ(doc2
.seq
.size(), 3UL);
1884 EXPECT_EQ(doc2
.seq
[0].foo
, 1);
1885 EXPECT_EQ(doc2
.seq
[0].bar
, 2);
1886 EXPECT_EQ(doc2
.seq
[1].foo
, 0);
1887 EXPECT_EQ(doc2
.seq
[1].bar
, 0);
1888 EXPECT_EQ(doc2
.seq
[2].foo
, -1);
1889 EXPECT_EQ(doc2
.seq
[2].bar
, 1024);
1893 //===----------------------------------------------------------------------===//
1894 // Test error handling
1895 //===----------------------------------------------------------------------===//
1898 // Test error handling of unknown enumerated scalar
1900 TEST(YAMLIO
, TestColorsReadError
) {
1908 suppressErrorMessages
);
1910 EXPECT_TRUE(!!yin
.error());
1915 // Test error handling of flow sequence with unknown value
1917 TEST(YAMLIO
, TestFlagsReadError
) {
1921 "f2: [ round, hollow ]\n"
1925 suppressErrorMessages
);
1928 EXPECT_TRUE(!!yin
.error());
1933 // Test error handling reading built-in uint8_t type
1935 TEST(YAMLIO
, TestReadBuiltInTypesUint8Error
) {
1936 std::vector
<uint8_t> seq
;
1943 suppressErrorMessages
);
1946 EXPECT_TRUE(!!yin
.error());
1951 // Test error handling reading built-in uint16_t type
1953 TEST(YAMLIO
, TestReadBuiltInTypesUint16Error
) {
1954 std::vector
<uint16_t> seq
;
1961 suppressErrorMessages
);
1964 EXPECT_TRUE(!!yin
.error());
1969 // Test error handling reading built-in uint32_t type
1971 TEST(YAMLIO
, TestReadBuiltInTypesUint32Error
) {
1972 std::vector
<uint32_t> seq
;
1979 suppressErrorMessages
);
1982 EXPECT_TRUE(!!yin
.error());
1987 // Test error handling reading built-in uint64_t type
1989 TEST(YAMLIO
, TestReadBuiltInTypesUint64Error
) {
1990 std::vector
<uint64_t> seq
;
1992 "- 18446744073709551615\n"
1994 "- 19446744073709551615\n"
1997 suppressErrorMessages
);
2000 EXPECT_TRUE(!!yin
.error());
2005 // Test error handling reading built-in int8_t type
2007 TEST(YAMLIO
, TestReadBuiltInTypesint8OverError
) {
2008 std::vector
<int8_t> seq
;
2016 suppressErrorMessages
);
2019 EXPECT_TRUE(!!yin
.error());
2023 // Test error handling reading built-in int8_t type
2025 TEST(YAMLIO
, TestReadBuiltInTypesint8UnderError
) {
2026 std::vector
<int8_t> seq
;
2034 suppressErrorMessages
);
2037 EXPECT_TRUE(!!yin
.error());
2042 // Test error handling reading built-in int16_t type
2044 TEST(YAMLIO
, TestReadBuiltInTypesint16UnderError
) {
2045 std::vector
<int16_t> seq
;
2053 suppressErrorMessages
);
2056 EXPECT_TRUE(!!yin
.error());
2061 // Test error handling reading built-in int16_t type
2063 TEST(YAMLIO
, TestReadBuiltInTypesint16OverError
) {
2064 std::vector
<int16_t> seq
;
2072 suppressErrorMessages
);
2075 EXPECT_TRUE(!!yin
.error());
2080 // Test error handling reading built-in int32_t type
2082 TEST(YAMLIO
, TestReadBuiltInTypesint32UnderError
) {
2083 std::vector
<int32_t> seq
;
2091 suppressErrorMessages
);
2094 EXPECT_TRUE(!!yin
.error());
2098 // Test error handling reading built-in int32_t type
2100 TEST(YAMLIO
, TestReadBuiltInTypesint32OverError
) {
2101 std::vector
<int32_t> seq
;
2109 suppressErrorMessages
);
2112 EXPECT_TRUE(!!yin
.error());
2117 // Test error handling reading built-in int64_t type
2119 TEST(YAMLIO
, TestReadBuiltInTypesint64UnderError
) {
2120 std::vector
<int64_t> seq
;
2122 "- -9223372036854775808\n"
2124 "- 9223372036854775807\n"
2125 "- -9223372036854775809\n"
2128 suppressErrorMessages
);
2131 EXPECT_TRUE(!!yin
.error());
2135 // Test error handling reading built-in int64_t type
2137 TEST(YAMLIO
, TestReadBuiltInTypesint64OverError
) {
2138 std::vector
<int64_t> seq
;
2140 "- -9223372036854775808\n"
2142 "- 9223372036854775807\n"
2143 "- 9223372036854775809\n"
2146 suppressErrorMessages
);
2149 EXPECT_TRUE(!!yin
.error());
2153 // Test error handling reading built-in float type
2155 TEST(YAMLIO
, TestReadBuiltInTypesFloatError
) {
2156 std::vector
<float> seq
;
2164 suppressErrorMessages
);
2167 EXPECT_TRUE(!!yin
.error());
2171 // Test error handling reading built-in float type
2173 TEST(YAMLIO
, TestReadBuiltInTypesDoubleError
) {
2174 std::vector
<double> seq
;
2182 suppressErrorMessages
);
2185 EXPECT_TRUE(!!yin
.error());
2189 // Test error handling reading built-in Hex8 type
2191 TEST(YAMLIO
, TestReadBuiltInTypesHex8Error
) {
2192 std::vector
<Hex8
> seq
;
2199 suppressErrorMessages
);
2201 EXPECT_TRUE(!!yin
.error());
2203 std::vector
<Hex8
> seq2
;
2205 "[ 0x12, 0xFE, 0x123 ]\n"
2207 /*Ctxt=*/nullptr, suppressErrorMessages
);
2209 EXPECT_TRUE(!!yin2
.error());
2211 EXPECT_TRUE(seq
.size() == 3);
2212 EXPECT_TRUE(seq
.size() == seq2
.size());
2213 for (size_t i
= 0; i
< seq
.size(); ++i
)
2214 EXPECT_TRUE(seq
[i
] == seq2
[i
]);
2219 // Test error handling reading built-in Hex16 type
2221 TEST(YAMLIO
, TestReadBuiltInTypesHex16Error
) {
2222 std::vector
<Hex16
> seq
;
2229 suppressErrorMessages
);
2231 EXPECT_TRUE(!!yin
.error());
2233 std::vector
<Hex16
> seq2
;
2235 "[ 0x0012, 0xFEFF, 0x12345 ]\n"
2237 /*Ctxt=*/nullptr, suppressErrorMessages
);
2239 EXPECT_TRUE(!!yin2
.error());
2241 EXPECT_TRUE(seq
.size() == 3);
2242 EXPECT_TRUE(seq
.size() == seq2
.size());
2243 for (size_t i
= 0; i
< seq
.size(); ++i
)
2244 EXPECT_TRUE(seq
[i
] == seq2
[i
]);
2248 // Test error handling reading built-in Hex32 type
2250 TEST(YAMLIO
, TestReadBuiltInTypesHex32Error
) {
2251 std::vector
<Hex32
> seq
;
2258 suppressErrorMessages
);
2261 EXPECT_TRUE(!!yin
.error());
2263 std::vector
<Hex32
> seq2
;
2265 "[ 0x0012, 0xFEFF0000, 0x1234556789 ]\n"
2267 /*Ctxt=*/nullptr, suppressErrorMessages
);
2269 EXPECT_TRUE(!!yin2
.error());
2271 EXPECT_TRUE(seq
.size() == 3);
2272 EXPECT_TRUE(seq
.size() == seq2
.size());
2273 for (size_t i
= 0; i
< seq
.size(); ++i
)
2274 EXPECT_TRUE(seq
[i
] == seq2
[i
]);
2278 // Test error handling reading built-in Hex64 type
2280 TEST(YAMLIO
, TestReadBuiltInTypesHex64Error
) {
2281 std::vector
<Hex64
> seq
;
2284 "- 0xFFEEDDCCBBAA9988\n"
2285 "- 0x12345567890ABCDEF0\n"
2288 suppressErrorMessages
);
2290 EXPECT_TRUE(!!yin
.error());
2292 std::vector
<Hex64
> seq2
;
2294 "[ 0x0012, 0xFFEEDDCCBBAA9988, 0x12345567890ABCDEF0 ]\n"
2296 /*Ctxt=*/nullptr, suppressErrorMessages
);
2298 EXPECT_TRUE(!!yin2
.error());
2300 EXPECT_TRUE(seq
.size() == 3);
2301 EXPECT_TRUE(seq
.size() == seq2
.size());
2302 for (size_t i
= 0; i
< seq
.size(); ++i
)
2303 EXPECT_TRUE(seq
[i
] == seq2
[i
]);
2306 TEST(YAMLIO
, TestMalformedMapFailsGracefully
) {
2309 // We pass the suppressErrorMessages handler to handle the error
2310 // message generated in the constructor of Input.
2311 Input
yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages
);
2313 EXPECT_TRUE(!!yin
.error());
2317 Input
yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages
);
2319 EXPECT_TRUE(!!yin
.error());
2323 struct OptionalTest
{
2324 std::vector
<int> Numbers
;
2327 struct OptionalTestSeq
{
2328 std::vector
<OptionalTest
> Tests
;
2331 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest
)
2335 struct MappingTraits
<OptionalTest
> {
2336 static void mapping(IO
& IO
, OptionalTest
&OT
) {
2337 IO
.mapOptional("Numbers", OT
.Numbers
);
2342 struct MappingTraits
<OptionalTestSeq
> {
2343 static void mapping(IO
&IO
, OptionalTestSeq
&OTS
) {
2344 IO
.mapOptional("Tests", OTS
.Tests
);
2350 TEST(YAMLIO
, SequenceElideTest
) {
2351 // Test that writing out a purely optional structure with its fields set to
2352 // default followed by other data is properly read back in.
2353 OptionalTestSeq Seq
;
2354 OptionalTest One
, Two
, Three
, Four
;
2355 int N
[] = {1, 2, 3};
2356 Three
.Numbers
.assign(N
, N
+ 3);
2357 Seq
.Tests
.push_back(One
);
2358 Seq
.Tests
.push_back(Two
);
2359 Seq
.Tests
.push_back(Three
);
2360 Seq
.Tests
.push_back(Four
);
2362 std::string intermediate
;
2364 llvm::raw_string_ostream
ostr(intermediate
);
2369 Input
yin(intermediate
);
2370 OptionalTestSeq Seq2
;
2373 EXPECT_FALSE(yin
.error());
2375 EXPECT_EQ(4UL, Seq2
.Tests
.size());
2377 EXPECT_TRUE(Seq2
.Tests
[0].Numbers
.empty());
2378 EXPECT_TRUE(Seq2
.Tests
[1].Numbers
.empty());
2380 EXPECT_EQ(1, Seq2
.Tests
[2].Numbers
[0]);
2381 EXPECT_EQ(2, Seq2
.Tests
[2].Numbers
[1]);
2382 EXPECT_EQ(3, Seq2
.Tests
[2].Numbers
[2]);
2384 EXPECT_TRUE(Seq2
.Tests
[3].Numbers
.empty());
2387 TEST(YAMLIO
, TestEmptyStringFailsForMapWithRequiredFields
) {
2391 EXPECT_TRUE(!!yin
.error());
2394 TEST(YAMLIO
, TestEmptyStringSucceedsForMapWithOptionalFields
) {
2398 EXPECT_FALSE(yin
.error());
2401 TEST(YAMLIO
, TestEmptyStringSucceedsForSequence
) {
2402 std::vector
<uint8_t> seq
;
2403 Input
yin("", /*Ctxt=*/nullptr, suppressErrorMessages
);
2406 EXPECT_FALSE(yin
.error());
2407 EXPECT_TRUE(seq
.empty());
2411 llvm::StringRef str1
, str2
, str3
;
2412 FlowMap(llvm::StringRef str1
, llvm::StringRef str2
, llvm::StringRef str3
)
2413 : str1(str1
), str2(str2
), str3(str3
) {}
2417 llvm::StringRef str
;
2418 FlowSeq(llvm::StringRef S
) : str(S
) {}
2419 FlowSeq() = default;
2425 struct MappingTraits
<FlowMap
> {
2426 static void mapping(IO
&io
, FlowMap
&fm
) {
2427 io
.mapRequired("str1", fm
.str1
);
2428 io
.mapRequired("str2", fm
.str2
);
2429 io
.mapRequired("str3", fm
.str3
);
2432 static const bool flow
= true;
2436 struct ScalarTraits
<FlowSeq
> {
2437 static void output(const FlowSeq
&value
, void*, llvm::raw_ostream
&out
) {
2440 static StringRef
input(StringRef scalar
, void*, FlowSeq
&value
) {
2445 static QuotingType
mustQuote(StringRef S
) { return QuotingType::None
; }
2450 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq
)
2452 TEST(YAMLIO
, TestWrapFlow
) {
2454 llvm::raw_string_ostream
ostr(out
);
2455 FlowMap
Map("This is str1", "This is str2", "This is str3");
2456 std::vector
<FlowSeq
> Seq
;
2457 Seq
.emplace_back("This is str1");
2458 Seq
.emplace_back("This is str2");
2459 Seq
.emplace_back("This is str3");
2462 // 20 is just bellow the total length of the first mapping field.
2463 // We should wreap at every element.
2464 Output
yout(ostr
, nullptr, 15);
2470 "{ str1: This is str1, \n"
2471 " str2: This is str2, \n"
2472 " str3: This is str3 }\n"
2480 "[ This is str1, \n"
2487 // 25 will allow the second field to be output on the first line.
2488 Output
yout(ostr
, nullptr, 25);
2494 "{ str1: This is str1, str2: This is str2, \n"
2495 " str3: This is str3 }\n"
2503 "[ This is str1, This is str2, \n"
2509 // 0 means no wrapping.
2510 Output
yout(ostr
, nullptr, 0);
2516 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2524 "[ This is str1, This is str2, This is str3 ]\n"
2530 struct MappingContext
{
2539 NestedMap(MappingContext
&Context
) : Context(Context
) {}
2541 MappingContext
&Context
;
2546 template <> struct MappingContextTraits
<SimpleMap
, MappingContext
> {
2547 static void mapping(IO
&io
, SimpleMap
&sm
, MappingContext
&Context
) {
2548 io
.mapRequired("B", sm
.B
);
2549 io
.mapRequired("C", sm
.C
);
2551 io
.mapRequired("Context", Context
.A
);
2555 template <> struct MappingTraits
<NestedMap
> {
2556 static void mapping(IO
&io
, NestedMap
&nm
) {
2557 io
.mapRequired("Simple", nm
.Simple
, nm
.Context
);
2563 TEST(YAMLIO
, TestMapWithContext
) {
2564 MappingContext Context
;
2565 NestedMap
Nested(Context
);
2567 llvm::raw_string_ostream
ostr(out
);
2569 Output
yout(ostr
, nullptr, 15);
2573 EXPECT_EQ(1, Context
.A
);
2584 Nested
.Simple
.B
= 2;
2585 Nested
.Simple
.C
= 3;
2588 EXPECT_EQ(2, Context
.A
);
2599 LLVM_YAML_IS_STRING_MAP(int)
2601 TEST(YAMLIO
, TestCustomMapping
) {
2602 std::map
<std::string
, int> x
;
2605 llvm::raw_string_ostream
ostr(out
);
2606 Output
xout(ostr
, nullptr, 0);
2628 std::map
<std::string
, int> y
;
2630 EXPECT_EQ(2ul, y
.size());
2631 EXPECT_EQ(1, y
["foo"]);
2632 EXPECT_EQ(2, y
["bar"]);
2635 LLVM_YAML_IS_STRING_MAP(FooBar
)
2637 TEST(YAMLIO
, TestCustomMappingStruct
) {
2638 std::map
<std::string
, FooBar
> x
;
2645 llvm::raw_string_ostream
ostr(out
);
2646 Output
xout(ostr
, nullptr, 0);
2661 std::map
<std::string
, FooBar
> y
;
2663 EXPECT_EQ(2ul, y
.size());
2664 EXPECT_EQ(1, y
["foo"].foo
);
2665 EXPECT_EQ(2, y
["foo"].bar
);
2666 EXPECT_EQ(3, y
["bar"].foo
);
2667 EXPECT_EQ(4, y
["bar"].bar
);
2670 struct FooBarMapMap
{
2671 std::map
<std::string
, FooBar
> fbm
;
2676 template <> struct MappingTraits
<FooBarMapMap
> {
2677 static void mapping(IO
&io
, FooBarMapMap
&x
) {
2678 io
.mapRequired("fbm", x
.fbm
);
2684 TEST(YAMLIO
, TestEmptyMapWrite
) {
2687 llvm::raw_string_ostream
OS(str
);
2690 EXPECT_EQ(OS
.str(), "---\nfbm: {}\n...\n");
2693 TEST(YAMLIO
, TestEmptySequenceWrite
) {
2695 FooBarContainer cont
;
2697 llvm::raw_string_ostream
OS(str
);
2700 EXPECT_EQ(OS
.str(), "---\nfbs: []\n...\n");
2706 llvm::raw_string_ostream
OS(str
);
2709 EXPECT_EQ(OS
.str(), "---\n[]\n...\n");
2713 static void TestEscaped(llvm::StringRef Input
, llvm::StringRef Expected
) {
2715 llvm::raw_string_ostream
ostr(out
);
2716 Output
xout(ostr
, nullptr, 0);
2718 llvm::yaml::EmptyContext Ctx
;
2719 yamlize(xout
, Input
, true, Ctx
);
2723 // Make a separate StringRef so we get nice byte-by-byte output.
2724 llvm::StringRef
Got(out
);
2725 EXPECT_EQ(Expected
, Got
);
2728 TEST(YAMLIO
, TestEscaped
) {
2730 TestEscaped("@abc@", "'@abc@'");
2732 TestEscaped("abc", "abc");
2733 // Forward slash quoted
2734 TestEscaped("abc/", "'abc/'");
2735 // Double quote non-printable
2736 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2737 // Double quote inside single quote
2738 TestEscaped("abc\"fdf", "'abc\"fdf'");
2739 // Double quote inside double quote
2740 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2741 // Single quote inside single quote
2742 TestEscaped("abc'fdf", "'abc''fdf'");
2744 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2745 // UTF8 with single quote inside double quote
2746 TestEscaped("parameter 'параметр' is unused",
2747 "\"parameter 'параметр' is unused\"");
2749 // String with embedded non-printable multibyte UTF-8 sequence (U+200B
2750 // zero-width space). The thing to test here is that we emit a
2751 // unicode-scalar level escape like \uNNNN (at the YAML level), and don't
2752 // just pass the UTF-8 byte sequence through as with quoted printables.
2754 const unsigned char foobar
[10] = {'f', 'o', 'o',
2755 0xE2, 0x80, 0x8B, // UTF-8 of U+200B
2758 TestEscaped((char const *)foobar
, "\"foo\\u200Bbar\"");
2762 TEST(YAMLIO
, Numeric
) {
2763 EXPECT_TRUE(isNumeric(".inf"));
2764 EXPECT_TRUE(isNumeric(".INF"));
2765 EXPECT_TRUE(isNumeric(".Inf"));
2766 EXPECT_TRUE(isNumeric("-.inf"));
2767 EXPECT_TRUE(isNumeric("+.inf"));
2769 EXPECT_TRUE(isNumeric(".nan"));
2770 EXPECT_TRUE(isNumeric(".NaN"));
2771 EXPECT_TRUE(isNumeric(".NAN"));
2773 EXPECT_TRUE(isNumeric("0"));
2774 EXPECT_TRUE(isNumeric("0."));
2775 EXPECT_TRUE(isNumeric("0.0"));
2776 EXPECT_TRUE(isNumeric("-0.0"));
2777 EXPECT_TRUE(isNumeric("+0.0"));
2779 EXPECT_TRUE(isNumeric("12345"));
2780 EXPECT_TRUE(isNumeric("012345"));
2781 EXPECT_TRUE(isNumeric("+12.0"));
2782 EXPECT_TRUE(isNumeric(".5"));
2783 EXPECT_TRUE(isNumeric("+.5"));
2784 EXPECT_TRUE(isNumeric("-1.0"));
2786 EXPECT_TRUE(isNumeric("2.3e4"));
2787 EXPECT_TRUE(isNumeric("-2E+05"));
2788 EXPECT_TRUE(isNumeric("+12e03"));
2789 EXPECT_TRUE(isNumeric("6.8523015e+5"));
2791 EXPECT_TRUE(isNumeric("1.e+1"));
2792 EXPECT_TRUE(isNumeric(".0e+1"));
2794 EXPECT_TRUE(isNumeric("0x2aF3"));
2795 EXPECT_TRUE(isNumeric("0o01234567"));
2797 EXPECT_FALSE(isNumeric("not a number"));
2798 EXPECT_FALSE(isNumeric("."));
2799 EXPECT_FALSE(isNumeric(".e+1"));
2800 EXPECT_FALSE(isNumeric(".1e"));
2801 EXPECT_FALSE(isNumeric(".1e+"));
2802 EXPECT_FALSE(isNumeric(".1e++1"));
2804 EXPECT_FALSE(isNumeric("ABCD"));
2805 EXPECT_FALSE(isNumeric("+0x2AF3"));
2806 EXPECT_FALSE(isNumeric("-0x2AF3"));
2807 EXPECT_FALSE(isNumeric("0x2AF3Z"));
2808 EXPECT_FALSE(isNumeric("0o012345678"));
2809 EXPECT_FALSE(isNumeric("0xZ"));
2810 EXPECT_FALSE(isNumeric("-0o012345678"));
2811 EXPECT_FALSE(isNumeric("000003A8229434B839616A25C16B0291F77A438B"));
2813 EXPECT_FALSE(isNumeric(""));
2814 EXPECT_FALSE(isNumeric("."));
2815 EXPECT_FALSE(isNumeric(".e+1"));
2816 EXPECT_FALSE(isNumeric(".e+"));
2817 EXPECT_FALSE(isNumeric(".e"));
2818 EXPECT_FALSE(isNumeric("e1"));
2820 // Deprecated formats: as for YAML 1.2 specification, the following are not
2821 // valid numbers anymore:
2823 // * Sexagecimal numbers
2824 // * Decimal numbers with comma s the delimiter
2825 // * "inf", "nan" without '.' prefix
2826 EXPECT_FALSE(isNumeric("3:25:45"));
2827 EXPECT_FALSE(isNumeric("+12,345"));
2828 EXPECT_FALSE(isNumeric("-inf"));
2829 EXPECT_FALSE(isNumeric("1,230.15"));
2832 //===----------------------------------------------------------------------===//
2833 // Test PolymorphicTraits and TaggedScalarTraits
2834 //===----------------------------------------------------------------------===//
2843 Poly(NodeKind Kind
) : Kind(Kind
) {}
2845 virtual ~Poly() = default;
2847 NodeKind
getKind() const { return Kind
; }
2850 struct Scalar
: Poly
{
2862 Scalar() : Poly(NK_Scalar
), SKind(SK_Unknown
) {}
2863 Scalar(double DoubleValue
)
2864 : Poly(NK_Scalar
), SKind(SK_Double
), DoubleValue(DoubleValue
) {}
2865 Scalar(bool BoolValue
)
2866 : Poly(NK_Scalar
), SKind(SK_Bool
), BoolValue(BoolValue
) {}
2868 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Scalar
; }
2871 struct Seq
: Poly
, std::vector
<std::unique_ptr
<Poly
>> {
2872 Seq() : Poly(NK_Seq
) {}
2874 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Seq
; }
2877 struct Map
: Poly
, llvm::StringMap
<std::unique_ptr
<Poly
>> {
2878 Map() : Poly(NK_Map
) {}
2880 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Map
; }
2886 template <> struct PolymorphicTraits
<std::unique_ptr
<Poly
>> {
2887 static NodeKind
getKind(const std::unique_ptr
<Poly
> &N
) {
2888 if (isa
<Scalar
>(*N
))
2889 return NodeKind::Scalar
;
2891 return NodeKind::Sequence
;
2893 return NodeKind::Map
;
2894 llvm_unreachable("unsupported node type");
2897 static Scalar
&getAsScalar(std::unique_ptr
<Poly
> &N
) {
2898 if (!N
|| !isa
<Scalar
>(*N
))
2899 N
= std::make_unique
<Scalar
>();
2900 return *cast
<Scalar
>(N
.get());
2903 static Seq
&getAsSequence(std::unique_ptr
<Poly
> &N
) {
2904 if (!N
|| !isa
<Seq
>(*N
))
2905 N
= std::make_unique
<Seq
>();
2906 return *cast
<Seq
>(N
.get());
2909 static Map
&getAsMap(std::unique_ptr
<Poly
> &N
) {
2910 if (!N
|| !isa
<Map
>(*N
))
2911 N
= std::make_unique
<Map
>();
2912 return *cast
<Map
>(N
.get());
2916 template <> struct TaggedScalarTraits
<Scalar
> {
2917 static void output(const Scalar
&S
, void *Ctxt
, raw_ostream
&ScalarOS
,
2918 raw_ostream
&TagOS
) {
2920 case Scalar::SK_Unknown
:
2921 report_fatal_error("output unknown scalar");
2923 case Scalar::SK_Double
:
2925 ScalarTraits
<double>::output(S
.DoubleValue
, Ctxt
, ScalarOS
);
2927 case Scalar::SK_Bool
:
2929 ScalarTraits
<bool>::output(S
.BoolValue
, Ctxt
, ScalarOS
);
2934 static StringRef
input(StringRef ScalarStr
, StringRef Tag
, void *Ctxt
,
2936 S
.SKind
= StringSwitch
<Scalar::ScalarKind
>(Tag
)
2937 .Case("!double", Scalar::SK_Double
)
2938 .Case("!bool", Scalar::SK_Bool
)
2939 .Default(Scalar::SK_Unknown
);
2941 case Scalar::SK_Unknown
:
2942 return StringRef("unknown scalar tag");
2943 case Scalar::SK_Double
:
2944 return ScalarTraits
<double>::input(ScalarStr
, Ctxt
, S
.DoubleValue
);
2945 case Scalar::SK_Bool
:
2946 return ScalarTraits
<bool>::input(ScalarStr
, Ctxt
, S
.BoolValue
);
2948 llvm_unreachable("unknown scalar kind");
2951 static QuotingType
mustQuote(const Scalar
&S
, StringRef Str
) {
2953 case Scalar::SK_Unknown
:
2954 report_fatal_error("quote unknown scalar");
2955 case Scalar::SK_Double
:
2956 return ScalarTraits
<double>::mustQuote(Str
);
2957 case Scalar::SK_Bool
:
2958 return ScalarTraits
<bool>::mustQuote(Str
);
2960 llvm_unreachable("unknown scalar kind");
2964 template <> struct CustomMappingTraits
<Map
> {
2965 static void inputOne(IO
&IO
, StringRef Key
, Map
&M
) {
2966 IO
.mapRequired(Key
.str().c_str(), M
[Key
]);
2969 static void output(IO
&IO
, Map
&M
) {
2971 IO
.mapRequired(N
.getKey().str().c_str(), N
.getValue());
2975 template <> struct SequenceTraits
<Seq
> {
2976 static size_t size(IO
&IO
, Seq
&A
) { return A
.size(); }
2978 static std::unique_ptr
<Poly
> &element(IO
&IO
, Seq
&A
, size_t Index
) {
2979 if (Index
>= A
.size())
2980 A
.resize(Index
+ 1);
2988 TEST(YAMLIO
, TestReadWritePolymorphicScalar
) {
2989 std::string intermediate
;
2990 std::unique_ptr
<Poly
> node
= std::make_unique
<Scalar
>(true);
2992 llvm::raw_string_ostream
ostr(intermediate
);
2994 #ifdef GTEST_HAS_DEATH_TEST
2996 EXPECT_DEATH(yout
<< node
, "plain scalar documents are not supported");
3001 TEST(YAMLIO
, TestReadWritePolymorphicSeq
) {
3002 std::string intermediate
;
3004 auto seq
= std::make_unique
<Seq
>();
3005 seq
->push_back(std::make_unique
<Scalar
>(true));
3006 seq
->push_back(std::make_unique
<Scalar
>(1.0));
3007 auto node
= llvm::unique_dyn_cast
<Poly
>(seq
);
3009 llvm::raw_string_ostream
ostr(intermediate
);
3014 Input
yin(intermediate
);
3015 std::unique_ptr
<Poly
> node
;
3018 EXPECT_FALSE(yin
.error());
3019 auto seq
= llvm::dyn_cast
<Seq
>(node
.get());
3021 ASSERT_EQ(seq
->size(), 2u);
3022 auto first
= llvm::dyn_cast
<Scalar
>((*seq
)[0].get());
3024 EXPECT_EQ(first
->SKind
, Scalar::SK_Bool
);
3025 EXPECT_TRUE(first
->BoolValue
);
3026 auto second
= llvm::dyn_cast
<Scalar
>((*seq
)[1].get());
3027 ASSERT_TRUE(second
);
3028 EXPECT_EQ(second
->SKind
, Scalar::SK_Double
);
3029 EXPECT_EQ(second
->DoubleValue
, 1.0);
3033 TEST(YAMLIO
, TestReadWritePolymorphicMap
) {
3034 std::string intermediate
;
3036 auto map
= std::make_unique
<Map
>();
3037 (*map
)["foo"] = std::make_unique
<Scalar
>(false);
3038 (*map
)["bar"] = std::make_unique
<Scalar
>(2.0);
3039 std::unique_ptr
<Poly
> node
= llvm::unique_dyn_cast
<Poly
>(map
);
3041 llvm::raw_string_ostream
ostr(intermediate
);
3046 Input
yin(intermediate
);
3047 std::unique_ptr
<Poly
> node
;
3050 EXPECT_FALSE(yin
.error());
3051 auto map
= llvm::dyn_cast
<Map
>(node
.get());
3053 auto foo
= llvm::dyn_cast
<Scalar
>((*map
)["foo"].get());
3055 EXPECT_EQ(foo
->SKind
, Scalar::SK_Bool
);
3056 EXPECT_FALSE(foo
->BoolValue
);
3057 auto bar
= llvm::dyn_cast
<Scalar
>((*map
)["bar"].get());
3059 EXPECT_EQ(bar
->SKind
, Scalar::SK_Double
);
3060 EXPECT_EQ(bar
->DoubleValue
, 2.0);
3064 TEST(YAMLIO
, TestAnchorMapError
) {
3065 Input
yin("& & &: ");
3066 yin
.setCurrentDocument();
3067 EXPECT_TRUE(yin
.error());
3070 TEST(YAMLIO
, TestFlowSequenceTokenErrors
) {
3072 EXPECT_FALSE(yin
.setCurrentDocument());
3073 EXPECT_TRUE(yin
.error());
3076 EXPECT_FALSE(yin2
.setCurrentDocument());
3077 EXPECT_TRUE(yin2
.error());
3080 EXPECT_FALSE(yin3
.setCurrentDocument());
3081 EXPECT_TRUE(yin3
.error());
3084 TEST(YAMLIO
, TestDirectiveMappingNoValue
) {
3085 Input
yin("%YAML\n{5:");
3086 EXPECT_FALSE(yin
.setCurrentDocument());
3087 EXPECT_TRUE(yin
.error());
3089 Input
yin2("%TAG\n'\x98!< :\n");
3090 yin2
.setCurrentDocument();
3091 EXPECT_TRUE(yin2
.error());
3094 TEST(YAMLIO
, TestUnescapeInfiniteLoop
) {
3095 Input
yin("\"\\u\\^#\\\\\"");
3096 yin
.setCurrentDocument();
3097 EXPECT_TRUE(yin
.error());
3100 TEST(YAMLIO
, TestScannerUnexpectedCharacter
) {
3101 Input
yin("!<$\x9F.");
3102 EXPECT_FALSE(yin
.setCurrentDocument());
3103 EXPECT_TRUE(yin
.error());
3106 TEST(YAMLIO
, TestUnknownDirective
) {
3108 EXPECT_FALSE(yin
.setCurrentDocument());
3109 EXPECT_TRUE(yin
.error());
3112 EXPECT_FALSE(yin2
.setCurrentDocument());
3113 EXPECT_TRUE(yin2
.error());
3116 TEST(YAMLIO
, TestEmptyAlias
) {
3118 EXPECT_FALSE(yin
.setCurrentDocument());
3119 EXPECT_TRUE(yin
.error());
3122 TEST(YAMLIO
, TestEmptyAnchor
) {
3124 EXPECT_FALSE(yin
.setCurrentDocument());
3127 TEST(YAMLIO
, TestScannerNoNullEmpty
) {
3128 std::vector
<char> str
{};
3129 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3130 yin
.setCurrentDocument();
3131 EXPECT_FALSE(yin
.error());
3134 TEST(YAMLIO
, TestScannerNoNullSequenceOfNull
) {
3135 std::vector
<char> str
{'-'};
3136 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3137 yin
.setCurrentDocument();
3138 EXPECT_FALSE(yin
.error());
3141 TEST(YAMLIO
, TestScannerNoNullSimpleSequence
) {
3142 std::vector
<char> str
{'-', ' ', 'a'};
3143 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3144 yin
.setCurrentDocument();
3145 EXPECT_FALSE(yin
.error());
3148 TEST(YAMLIO
, TestScannerNoNullUnbalancedMap
) {
3149 std::vector
<char> str
{'{'};
3150 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3151 yin
.setCurrentDocument();
3152 EXPECT_TRUE(yin
.error());
3155 TEST(YAMLIO
, TestScannerNoNullEmptyMap
) {
3156 std::vector
<char> str
{'{', '}'};
3157 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3158 yin
.setCurrentDocument();
3159 EXPECT_FALSE(yin
.error());
3162 TEST(YAMLIO
, TestScannerNoNullUnbalancedSequence
) {
3163 std::vector
<char> str
{'['};
3164 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3165 yin
.setCurrentDocument();
3166 EXPECT_TRUE(yin
.error());
3169 TEST(YAMLIO
, TestScannerNoNullEmptySequence
) {
3170 std::vector
<char> str
{'[', ']'};
3171 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3172 yin
.setCurrentDocument();
3173 EXPECT_FALSE(yin
.error());
3176 TEST(YAMLIO
, TestScannerNoNullScalarUnbalancedDoubleQuote
) {
3177 std::vector
<char> str
{'"'};
3178 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3179 yin
.setCurrentDocument();
3180 EXPECT_TRUE(yin
.error());
3183 TEST(YAMLIO
, TestScannerNoNullScalarUnbalancedSingleQuote
) {
3184 std::vector
<char> str
{'\''};
3185 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3186 yin
.setCurrentDocument();
3187 EXPECT_TRUE(yin
.error());
3190 TEST(YAMLIO
, TestScannerNoNullEmptyAlias
) {
3191 std::vector
<char> str
{'&'};
3192 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3193 yin
.setCurrentDocument();
3194 EXPECT_TRUE(yin
.error());
3197 TEST(YAMLIO
, TestScannerNoNullEmptyAnchor
) {
3198 std::vector
<char> str
{'*'};
3199 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3200 yin
.setCurrentDocument();
3201 EXPECT_TRUE(yin
.error());
3204 TEST(YAMLIO
, TestScannerNoNullDecodeInvalidUTF8
) {
3205 std::vector
<char> str
{'\xef'};
3206 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3207 yin
.setCurrentDocument();
3208 EXPECT_TRUE(yin
.error());
3211 TEST(YAMLIO
, TestScannerNoNullScanPlainScalarInFlow
) {
3212 std::vector
<char> str
{'{', 'a', ':'};
3213 Input
yin(llvm::StringRef(str
.data(), str
.size()));
3214 yin
.setCurrentDocument();
3215 EXPECT_TRUE(yin
.error());