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/Twine.h"
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/YAMLTraits.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
20 using llvm::yaml::Hex16
;
21 using llvm::yaml::Hex32
;
22 using llvm::yaml::Hex64
;
23 using llvm::yaml::Hex8
;
24 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 TEST(YAMLIO
, TestMalformedMapRead
) {
102 Input
yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages
);
104 EXPECT_TRUE(!!yin
.error());
108 // Test the reading of a yaml sequence of mappings
110 TEST(YAMLIO
, TestSequenceMapRead
) {
112 Input
yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
115 EXPECT_FALSE(yin
.error());
116 EXPECT_EQ(seq
.size(), 2UL);
117 FooBar
& map1
= seq
[0];
118 FooBar
& map2
= seq
[1];
119 EXPECT_EQ(map1
.foo
, 3);
120 EXPECT_EQ(map1
.bar
, 5);
121 EXPECT_EQ(map2
.foo
, 7);
122 EXPECT_EQ(map2
.bar
, 9);
126 // Test the reading of a map containing a yaml sequence of mappings
128 TEST(YAMLIO
, TestContainerSequenceMapRead
) {
130 FooBarContainer cont
;
131 Input
yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
134 EXPECT_FALSE(yin2
.error());
135 EXPECT_EQ(cont
.fbs
.size(), 2UL);
136 EXPECT_EQ(cont
.fbs
[0].foo
, 3);
137 EXPECT_EQ(cont
.fbs
[0].bar
, 5);
138 EXPECT_EQ(cont
.fbs
[1].foo
, 7);
139 EXPECT_EQ(cont
.fbs
[1].bar
, 9);
143 FooBarContainer cont
;
144 Input
yin("---\nfbs:\n...\n");
146 // Okay: Empty node represents an empty array.
147 EXPECT_FALSE(yin
.error());
148 EXPECT_EQ(cont
.fbs
.size(), 0UL);
152 FooBarContainer cont
;
153 Input
yin("---\nfbs: !!null null\n...\n");
155 // Okay: null represents an empty array.
156 EXPECT_FALSE(yin
.error());
157 EXPECT_EQ(cont
.fbs
.size(), 0UL);
161 FooBarContainer cont
;
162 Input
yin("---\nfbs: ~\n...\n");
164 // Okay: null represents an empty array.
165 EXPECT_FALSE(yin
.error());
166 EXPECT_EQ(cont
.fbs
.size(), 0UL);
170 FooBarContainer cont
;
171 Input
yin("---\nfbs: null\n...\n");
173 // Okay: null represents an empty array.
174 EXPECT_FALSE(yin
.error());
175 EXPECT_EQ(cont
.fbs
.size(), 0UL);
180 // Test the reading of a map containing a malformed yaml sequence
182 TEST(YAMLIO
, TestMalformedContainerSequenceMapRead
) {
184 FooBarContainer cont
;
185 Input
yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
186 suppressErrorMessages
);
188 // Error: fbs is not a sequence.
189 EXPECT_TRUE(!!yin
.error());
190 EXPECT_EQ(cont
.fbs
.size(), 0UL);
194 FooBarContainer cont
;
195 Input
yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages
);
197 // This should be an error.
198 EXPECT_TRUE(!!yin
.error());
199 EXPECT_EQ(cont
.fbs
.size(), 0UL);
204 // Test writing then reading back a sequence of mappings
206 TEST(YAMLIO
, TestSequenceMapWriteAndRead
) {
207 std::string intermediate
;
216 seq
.push_back(entry1
);
217 seq
.push_back(entry2
);
219 llvm::raw_string_ostream
ostr(intermediate
);
225 Input
yin(intermediate
);
229 EXPECT_FALSE(yin
.error());
230 EXPECT_EQ(seq2
.size(), 2UL);
231 FooBar
& map1
= seq2
[0];
232 FooBar
& map2
= seq2
[1];
233 EXPECT_EQ(map1
.foo
, 10);
234 EXPECT_EQ(map1
.bar
, -3);
235 EXPECT_EQ(map2
.foo
, 257);
236 EXPECT_EQ(map2
.bar
, 0);
241 // Test YAML filename handling.
243 static void testErrorFilename(const llvm::SMDiagnostic
&Error
, void *) {
244 EXPECT_EQ(Error
.getFilename(), "foo.yaml");
247 TEST(YAMLIO
, TestGivenFilename
) {
248 auto Buffer
= llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
249 Input
yin(*Buffer
, nullptr, testErrorFilename
);
253 EXPECT_TRUE(!!yin
.error());
256 struct WithStringField
{
264 template <> struct MappingTraits
<WithStringField
> {
265 static void mapping(IO
&io
, WithStringField
&fb
) {
266 io
.mapRequired("str1", fb
.str1
);
267 io
.mapRequired("str2", fb
.str2
);
268 io
.mapRequired("str3", fb
.str3
);
274 TEST(YAMLIO
, MultilineStrings
) {
275 WithStringField Original
;
276 Original
.str1
= "a multiline string\nfoobarbaz";
277 Original
.str2
= "another one\rfoobarbaz";
278 Original
.str3
= "a one-line string";
280 std::string Serialized
;
282 llvm::raw_string_ostream
OS(Serialized
);
286 auto Expected
= "---\n"
287 "str1: 'a multiline string\n"
289 "str2: 'another one\r"
291 "str3: a one-line string\n"
293 ASSERT_EQ(Serialized
, Expected
);
295 // Also check it parses back without the errors.
296 WithStringField Deserialized
;
298 Input
YIn(Serialized
);
300 ASSERT_FALSE(YIn
.error())
301 << "Parsing error occurred during deserialization. Serialized string:\n"
304 EXPECT_EQ(Original
.str1
, Deserialized
.str1
);
305 EXPECT_EQ(Original
.str2
, Deserialized
.str2
);
306 EXPECT_EQ(Original
.str3
, Deserialized
.str3
);
309 TEST(YAMLIO
, NoQuotesForTab
) {
310 WithStringField WithTab
;
311 WithTab
.str1
= "aba\tcaba";
312 std::string Serialized
;
314 llvm::raw_string_ostream
OS(Serialized
);
318 auto ExpectedPrefix
= "---\n"
320 EXPECT_THAT(Serialized
, StartsWith(ExpectedPrefix
));
323 //===----------------------------------------------------------------------===//
324 // Test built-in types
325 //===----------------------------------------------------------------------===//
327 struct BuiltInTypes
{
350 struct MappingTraits
<BuiltInTypes
> {
351 static void mapping(IO
&io
, BuiltInTypes
& bt
) {
352 io
.mapRequired("str", bt
.str
);
353 io
.mapRequired("stdstr", bt
.stdstr
);
354 io
.mapRequired("u64", bt
.u64
);
355 io
.mapRequired("u32", bt
.u32
);
356 io
.mapRequired("u16", bt
.u16
);
357 io
.mapRequired("u8", bt
.u8
);
358 io
.mapRequired("b", bt
.b
);
359 io
.mapRequired("s64", bt
.s64
);
360 io
.mapRequired("s32", bt
.s32
);
361 io
.mapRequired("s16", bt
.s16
);
362 io
.mapRequired("s8", bt
.s8
);
363 io
.mapRequired("f", bt
.f
);
364 io
.mapRequired("d", bt
.d
);
365 io
.mapRequired("h8", bt
.h8
);
366 io
.mapRequired("h16", bt
.h16
);
367 io
.mapRequired("h32", bt
.h32
);
368 io
.mapRequired("h64", bt
.h64
);
376 // Test the reading of all built-in scalar conversions
378 TEST(YAMLIO
, TestReadBuiltInTypes
) {
382 "stdstr: hello where?\n"
397 "h64: 0xFEDCBA9876543210\n"
401 EXPECT_FALSE(yin
.error());
402 EXPECT_TRUE(map
.str
.equals("hello there"));
403 EXPECT_TRUE(map
.stdstr
== "hello where?");
404 EXPECT_EQ(map
.u64
, 5000000000ULL);
405 EXPECT_EQ(map
.u32
, 4000000000U);
406 EXPECT_EQ(map
.u16
, 65000);
407 EXPECT_EQ(map
.u8
, 255);
408 EXPECT_EQ(map
.b
, false);
409 EXPECT_EQ(map
.s64
, -5000000000LL);
410 EXPECT_EQ(map
.s32
, -2000000000L);
411 EXPECT_EQ(map
.s16
, -32000);
412 EXPECT_EQ(map
.s8
, -127);
413 EXPECT_EQ(map
.f
, 137.125);
414 EXPECT_EQ(map
.d
, -2.8625);
415 EXPECT_EQ(map
.h8
, Hex8(255));
416 EXPECT_EQ(map
.h16
, Hex16(0x8765));
417 EXPECT_EQ(map
.h32
, Hex32(0xFEDCBA98));
418 EXPECT_EQ(map
.h64
, Hex64(0xFEDCBA9876543210LL
));
423 // Test writing then reading back all built-in scalar types
425 TEST(YAMLIO
, TestReadWriteBuiltInTypes
) {
426 std::string intermediate
;
430 map
.stdstr
= "three four";
431 map
.u64
= 6000000000ULL;
432 map
.u32
= 3000000000U;
436 map
.s64
= -6000000000LL;
437 map
.s32
= -2000000000;
444 map
.h32
= 3000000000U;
445 map
.h64
= 6000000000LL;
447 llvm::raw_string_ostream
ostr(intermediate
);
453 Input
yin(intermediate
);
457 EXPECT_FALSE(yin
.error());
458 EXPECT_TRUE(map
.str
.equals("one two"));
459 EXPECT_TRUE(map
.stdstr
== "three four");
460 EXPECT_EQ(map
.u64
, 6000000000ULL);
461 EXPECT_EQ(map
.u32
, 3000000000U);
462 EXPECT_EQ(map
.u16
, 50000);
463 EXPECT_EQ(map
.u8
, 254);
464 EXPECT_EQ(map
.b
, true);
465 EXPECT_EQ(map
.s64
, -6000000000LL);
466 EXPECT_EQ(map
.s32
, -2000000000L);
467 EXPECT_EQ(map
.s16
, -32000);
468 EXPECT_EQ(map
.s8
, -128);
469 EXPECT_EQ(map
.f
, 3.25);
470 EXPECT_EQ(map
.d
, -2.8625);
471 EXPECT_EQ(map
.h8
, Hex8(254));
472 EXPECT_EQ(map
.h16
, Hex16(50000));
473 EXPECT_EQ(map
.h32
, Hex32(3000000000U));
474 EXPECT_EQ(map
.h64
, Hex64(6000000000LL));
478 //===----------------------------------------------------------------------===//
479 // Test endian-aware types
480 //===----------------------------------------------------------------------===//
483 typedef llvm::support::detail::packed_endian_specific_integral
<
484 float, llvm::support::little
, llvm::support::unaligned
>
486 typedef llvm::support::detail::packed_endian_specific_integral
<
487 double, llvm::support::little
, llvm::support::unaligned
>
490 llvm::support::ulittle64_t u64
;
491 llvm::support::ulittle32_t u32
;
492 llvm::support::ulittle16_t u16
;
493 llvm::support::little64_t s64
;
494 llvm::support::little32_t s32
;
495 llvm::support::little16_t s16
;
502 template <> struct MappingTraits
<EndianTypes
> {
503 static void mapping(IO
&io
, EndianTypes
&et
) {
504 io
.mapRequired("u64", et
.u64
);
505 io
.mapRequired("u32", et
.u32
);
506 io
.mapRequired("u16", et
.u16
);
507 io
.mapRequired("s64", et
.s64
);
508 io
.mapRequired("s32", et
.s32
);
509 io
.mapRequired("s16", et
.s16
);
510 io
.mapRequired("f", et
.f
);
511 io
.mapRequired("d", et
.d
);
518 // Test the reading of all endian scalar conversions
520 TEST(YAMLIO
, TestReadEndianTypes
) {
534 EXPECT_FALSE(yin
.error());
535 EXPECT_EQ(map
.u64
, 5000000000ULL);
536 EXPECT_EQ(map
.u32
, 4000000000U);
537 EXPECT_EQ(map
.u16
, 65000);
538 EXPECT_EQ(map
.s64
, -5000000000LL);
539 EXPECT_EQ(map
.s32
, -2000000000L);
540 EXPECT_EQ(map
.s16
, -32000);
541 EXPECT_EQ(map
.f
, 3.25f
);
542 EXPECT_EQ(map
.d
, -2.8625);
546 // Test writing then reading back all endian-aware scalar types
548 TEST(YAMLIO
, TestReadWriteEndianTypes
) {
549 std::string intermediate
;
552 map
.u64
= 6000000000ULL;
553 map
.u32
= 3000000000U;
555 map
.s64
= -6000000000LL;
556 map
.s32
= -2000000000;
561 llvm::raw_string_ostream
ostr(intermediate
);
567 Input
yin(intermediate
);
571 EXPECT_FALSE(yin
.error());
572 EXPECT_EQ(map
.u64
, 6000000000ULL);
573 EXPECT_EQ(map
.u32
, 3000000000U);
574 EXPECT_EQ(map
.u16
, 50000);
575 EXPECT_EQ(map
.s64
, -6000000000LL);
576 EXPECT_EQ(map
.s32
, -2000000000L);
577 EXPECT_EQ(map
.s16
, -32000);
578 EXPECT_EQ(map
.f
, 3.25f
);
579 EXPECT_EQ(map
.d
, -2.8625);
583 enum class Enum
: uint16_t { One
, Two
};
584 enum class BitsetEnum
: uint16_t {
587 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ OneZero
),
589 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
591 llvm::support::little_t
<Enum
> LittleEnum
;
592 llvm::support::big_t
<Enum
> BigEnum
;
593 llvm::support::little_t
<BitsetEnum
> LittleBitset
;
594 llvm::support::big_t
<BitsetEnum
> BigBitset
;
598 template <> struct ScalarEnumerationTraits
<Enum
> {
599 static void enumeration(IO
&io
, Enum
&E
) {
600 io
.enumCase(E
, "One", Enum::One
);
601 io
.enumCase(E
, "Two", Enum::Two
);
605 template <> struct ScalarBitSetTraits
<BitsetEnum
> {
606 static void bitset(IO
&io
, BitsetEnum
&E
) {
607 io
.bitSetCase(E
, "ZeroOne", BitsetEnum::ZeroOne
);
608 io
.bitSetCase(E
, "OneZero", BitsetEnum::OneZero
);
612 template <> struct MappingTraits
<EndianEnums
> {
613 static void mapping(IO
&io
, EndianEnums
&EE
) {
614 io
.mapRequired("LittleEnum", EE
.LittleEnum
);
615 io
.mapRequired("BigEnum", EE
.BigEnum
);
616 io
.mapRequired("LittleBitset", EE
.LittleBitset
);
617 io
.mapRequired("BigBitset", EE
.BigBitset
);
623 TEST(YAMLIO
, TestReadEndianEnums
) {
628 "LittleBitset: [ ZeroOne ]\n"
629 "BigBitset: [ ZeroOne, OneZero ]\n"
633 EXPECT_FALSE(yin
.error());
634 EXPECT_EQ(Enum::One
, map
.LittleEnum
);
635 EXPECT_EQ(Enum::Two
, map
.BigEnum
);
636 EXPECT_EQ(BitsetEnum::ZeroOne
, map
.LittleBitset
);
637 EXPECT_EQ(BitsetEnum::ZeroOne
| BitsetEnum::OneZero
, map
.BigBitset
);
640 TEST(YAMLIO
, TestReadWriteEndianEnums
) {
641 std::string intermediate
;
644 map
.LittleEnum
= Enum::Two
;
645 map
.BigEnum
= Enum::One
;
646 map
.LittleBitset
= BitsetEnum::OneZero
| BitsetEnum::ZeroOne
;
647 map
.BigBitset
= BitsetEnum::OneZero
;
649 llvm::raw_string_ostream
ostr(intermediate
);
655 Input
yin(intermediate
);
659 EXPECT_FALSE(yin
.error());
660 EXPECT_EQ(Enum::Two
, map
.LittleEnum
);
661 EXPECT_EQ(Enum::One
, map
.BigEnum
);
662 EXPECT_EQ(BitsetEnum::OneZero
| BitsetEnum::ZeroOne
, map
.LittleBitset
);
663 EXPECT_EQ(BitsetEnum::OneZero
, map
.BigBitset
);
668 llvm::StringRef str1
;
669 llvm::StringRef str2
;
670 llvm::StringRef str3
;
671 llvm::StringRef str4
;
672 llvm::StringRef str5
;
673 llvm::StringRef str6
;
674 llvm::StringRef str7
;
675 llvm::StringRef str8
;
676 llvm::StringRef str9
;
677 llvm::StringRef str10
;
678 llvm::StringRef str11
;
688 std::string stdstr10
;
689 std::string stdstr11
;
690 std::string stdstr12
;
691 std::string stdstr13
;
697 struct MappingTraits
<StringTypes
> {
698 static void mapping(IO
&io
, StringTypes
& st
) {
699 io
.mapRequired("str1", st
.str1
);
700 io
.mapRequired("str2", st
.str2
);
701 io
.mapRequired("str3", st
.str3
);
702 io
.mapRequired("str4", st
.str4
);
703 io
.mapRequired("str5", st
.str5
);
704 io
.mapRequired("str6", st
.str6
);
705 io
.mapRequired("str7", st
.str7
);
706 io
.mapRequired("str8", st
.str8
);
707 io
.mapRequired("str9", st
.str9
);
708 io
.mapRequired("str10", st
.str10
);
709 io
.mapRequired("str11", st
.str11
);
710 io
.mapRequired("stdstr1", st
.stdstr1
);
711 io
.mapRequired("stdstr2", st
.stdstr2
);
712 io
.mapRequired("stdstr3", st
.stdstr3
);
713 io
.mapRequired("stdstr4", st
.stdstr4
);
714 io
.mapRequired("stdstr5", st
.stdstr5
);
715 io
.mapRequired("stdstr6", st
.stdstr6
);
716 io
.mapRequired("stdstr7", st
.stdstr7
);
717 io
.mapRequired("stdstr8", st
.stdstr8
);
718 io
.mapRequired("stdstr9", st
.stdstr9
);
719 io
.mapRequired("stdstr10", st
.stdstr10
);
720 io
.mapRequired("stdstr11", st
.stdstr11
);
721 io
.mapRequired("stdstr12", st
.stdstr12
);
722 io
.mapRequired("stdstr13", st
.stdstr13
);
728 TEST(YAMLIO
, TestReadWriteStringTypes
) {
729 std::string intermediate
;
737 map
.str6
= "0000000004000000";
741 map
.str10
= "0.2e20";
743 map
.stdstr1
= "'eee";
744 map
.stdstr2
= "\"fff";
745 map
.stdstr3
= "`ggg";
746 map
.stdstr4
= "@hhh";
748 map
.stdstr6
= "0000000004000000";
749 map
.stdstr7
= "true";
750 map
.stdstr8
= "FALSE";
752 map
.stdstr10
= "0.2e20";
753 map
.stdstr11
= "0x30";
754 map
.stdstr12
= "- match";
755 map
.stdstr13
.assign("\0a\0b\0", 5);
757 llvm::raw_string_ostream
ostr(intermediate
);
762 llvm::StringRef
flowOut(intermediate
);
763 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'''aaa"));
764 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'\"bbb'"));
765 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'`ccc'"));
766 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'@ddd'"));
767 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("''\n"));
768 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0000000004000000'\n"));
769 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'true'\n"));
770 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'FALSE'\n"));
771 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'~'\n"));
772 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0.2e20'\n"));
773 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'0x30'\n"));
774 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("'- match'\n"));
775 EXPECT_NE(std::string::npos
, flowOut
.find("'''eee"));
776 EXPECT_NE(std::string::npos
, flowOut
.find("'\"fff'"));
777 EXPECT_NE(std::string::npos
, flowOut
.find("'`ggg'"));
778 EXPECT_NE(std::string::npos
, flowOut
.find("'@hhh'"));
779 EXPECT_NE(std::string::npos
, flowOut
.find("''\n"));
780 EXPECT_NE(std::string::npos
, flowOut
.find("'0000000004000000'\n"));
781 EXPECT_NE(std::string::npos
, flowOut
.find("\"\\0a\\0b\\0\""));
784 Input
yin(intermediate
);
788 EXPECT_FALSE(yin
.error());
789 EXPECT_TRUE(map
.str1
.equals("'aaa"));
790 EXPECT_TRUE(map
.str2
.equals("\"bbb"));
791 EXPECT_TRUE(map
.str3
.equals("`ccc"));
792 EXPECT_TRUE(map
.str4
.equals("@ddd"));
793 EXPECT_TRUE(map
.str5
.equals(""));
794 EXPECT_TRUE(map
.str6
.equals("0000000004000000"));
795 EXPECT_TRUE(map
.stdstr1
== "'eee");
796 EXPECT_TRUE(map
.stdstr2
== "\"fff");
797 EXPECT_TRUE(map
.stdstr3
== "`ggg");
798 EXPECT_TRUE(map
.stdstr4
== "@hhh");
799 EXPECT_TRUE(map
.stdstr5
== "");
800 EXPECT_TRUE(map
.stdstr6
== "0000000004000000");
801 EXPECT_EQ(std::string("\0a\0b\0", 5), map
.stdstr13
);
805 //===----------------------------------------------------------------------===//
806 // Test ScalarEnumerationTraits
807 //===----------------------------------------------------------------------===//
828 struct ScalarEnumerationTraits
<Colors
> {
829 static void enumeration(IO
&io
, Colors
&value
) {
830 io
.enumCase(value
, "red", cRed
);
831 io
.enumCase(value
, "blue", cBlue
);
832 io
.enumCase(value
, "green", cGreen
);
833 io
.enumCase(value
, "yellow",cYellow
);
837 struct MappingTraits
<ColorMap
> {
838 static void mapping(IO
&io
, ColorMap
& c
) {
839 io
.mapRequired("c1", c
.c1
);
840 io
.mapRequired("c2", c
.c2
);
841 io
.mapRequired("c3", c
.c3
);
842 io
.mapOptional("c4", c
.c4
, cBlue
); // supplies default
843 io
.mapOptional("c5", c
.c5
, cYellow
); // supplies default
844 io
.mapOptional("c6", c
.c6
, cRed
); // supplies default
852 // Test reading enumerated scalars
854 TEST(YAMLIO
, TestEnumRead
) {
864 EXPECT_FALSE(yin
.error());
865 EXPECT_EQ(cBlue
, map
.c1
);
866 EXPECT_EQ(cRed
, map
.c2
);
867 EXPECT_EQ(cGreen
, map
.c3
);
868 EXPECT_EQ(cBlue
, map
.c4
); // tests default
869 EXPECT_EQ(cYellow
,map
.c5
); // tests overridden
870 EXPECT_EQ(cRed
, map
.c6
); // tests default
875 //===----------------------------------------------------------------------===//
876 // Test ScalarBitSetTraits
877 //===----------------------------------------------------------------------===//
886 inline MyFlags
operator|(MyFlags a
, MyFlags b
) {
887 return static_cast<MyFlags
>(
888 static_cast<uint32_t>(a
) | static_cast<uint32_t>(b
));
902 struct ScalarBitSetTraits
<MyFlags
> {
903 static void bitset(IO
&io
, MyFlags
&value
) {
904 io
.bitSetCase(value
, "big", flagBig
);
905 io
.bitSetCase(value
, "flat", flagFlat
);
906 io
.bitSetCase(value
, "round", flagRound
);
907 io
.bitSetCase(value
, "pointy",flagPointy
);
911 struct MappingTraits
<FlagsMap
> {
912 static void mapping(IO
&io
, FlagsMap
& c
) {
913 io
.mapRequired("f1", c
.f1
);
914 io
.mapRequired("f2", c
.f2
);
915 io
.mapRequired("f3", c
.f3
);
916 io
.mapOptional("f4", c
.f4
, flagRound
);
924 // Test reading flow sequence representing bit-mask values
926 TEST(YAMLIO
, TestFlagsRead
) {
930 "f2: [ round, flat ]\n"
935 EXPECT_FALSE(yin
.error());
936 EXPECT_EQ(flagBig
, map
.f1
);
937 EXPECT_EQ(flagRound
|flagFlat
, map
.f2
);
938 EXPECT_EQ(flagNone
, map
.f3
); // check empty set
939 EXPECT_EQ(flagRound
, map
.f4
); // check optional key
944 // Test writing then reading back bit-mask values
946 TEST(YAMLIO
, TestReadWriteFlags
) {
947 std::string intermediate
;
951 map
.f2
= flagRound
| flagFlat
;
955 llvm::raw_string_ostream
ostr(intermediate
);
961 Input
yin(intermediate
);
965 EXPECT_FALSE(yin
.error());
966 EXPECT_EQ(flagBig
, map2
.f1
);
967 EXPECT_EQ(flagRound
|flagFlat
, map2
.f2
);
968 EXPECT_EQ(flagNone
, map2
.f3
);
969 //EXPECT_EQ(flagRound, map2.f4); // check optional key
975 //===----------------------------------------------------------------------===//
977 //===----------------------------------------------------------------------===//
979 struct MyCustomType
{
984 struct MyCustomTypeMap
{
994 struct MappingTraits
<MyCustomTypeMap
> {
995 static void mapping(IO
&io
, MyCustomTypeMap
& s
) {
996 io
.mapRequired("f1", s
.f1
);
997 io
.mapRequired("f2", s
.f2
);
998 io
.mapRequired("f3", s
.f3
);
1001 // MyCustomType is formatted as a yaml scalar. A value of
1002 // {length=3, width=4} would be represented in yaml as "3 by 4".
1004 struct ScalarTraits
<MyCustomType
> {
1005 static void output(const MyCustomType
&value
, void* ctxt
, llvm::raw_ostream
&out
) {
1006 out
<< llvm::format("%d by %d", value
.length
, value
.width
);
1008 static StringRef
input(StringRef scalar
, void* ctxt
, MyCustomType
&value
) {
1009 size_t byStart
= scalar
.find("by");
1010 if ( byStart
!= StringRef::npos
) {
1011 StringRef lenStr
= scalar
.slice(0, byStart
);
1012 lenStr
= lenStr
.rtrim();
1013 if ( lenStr
.getAsInteger(0, value
.length
) ) {
1014 return "malformed length";
1016 StringRef widthStr
= scalar
.drop_front(byStart
+2);
1017 widthStr
= widthStr
.ltrim();
1018 if ( widthStr
.getAsInteger(0, value
.width
) ) {
1019 return "malformed width";
1024 return "malformed by";
1027 static QuotingType
mustQuote(StringRef
) { return QuotingType::Single
; }
1034 // Test writing then reading back custom values
1036 TEST(YAMLIO
, TestReadWriteMyCustomType
) {
1037 std::string intermediate
;
1039 MyCustomTypeMap map
;
1042 map
.f2
.length
= 100;
1046 llvm::raw_string_ostream
ostr(intermediate
);
1052 Input
yin(intermediate
);
1053 MyCustomTypeMap map2
;
1056 EXPECT_FALSE(yin
.error());
1057 EXPECT_EQ(1, map2
.f1
.length
);
1058 EXPECT_EQ(4, map2
.f1
.width
);
1059 EXPECT_EQ(100, map2
.f2
.length
);
1060 EXPECT_EQ(400, map2
.f2
.width
);
1061 EXPECT_EQ(10, map2
.f3
);
1066 //===----------------------------------------------------------------------===//
1067 // Test BlockScalarTraits
1068 //===----------------------------------------------------------------------===//
1070 struct MultilineStringType
{
1074 struct MultilineStringTypeMap
{
1075 MultilineStringType name
;
1076 MultilineStringType description
;
1077 MultilineStringType ingredients
;
1078 MultilineStringType recipes
;
1079 MultilineStringType warningLabels
;
1080 MultilineStringType documentation
;
1087 struct MappingTraits
<MultilineStringTypeMap
> {
1088 static void mapping(IO
&io
, MultilineStringTypeMap
& s
) {
1089 io
.mapRequired("name", s
.name
);
1090 io
.mapRequired("description", s
.description
);
1091 io
.mapRequired("ingredients", s
.ingredients
);
1092 io
.mapRequired("recipes", s
.recipes
);
1093 io
.mapRequired("warningLabels", s
.warningLabels
);
1094 io
.mapRequired("documentation", s
.documentation
);
1095 io
.mapRequired("price", s
.price
);
1099 // MultilineStringType is formatted as a yaml block literal scalar. A value of
1100 // "Hello\nWorld" would be represented in yaml as
1105 struct BlockScalarTraits
<MultilineStringType
> {
1106 static void output(const MultilineStringType
&value
, void *ctxt
,
1107 llvm::raw_ostream
&out
) {
1110 static StringRef
input(StringRef scalar
, void *ctxt
,
1111 MultilineStringType
&value
) {
1112 value
.str
= scalar
.str();
1119 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType
)
1122 // Test writing then reading back custom values
1124 TEST(YAMLIO
, TestReadWriteMultilineStringType
) {
1125 std::string intermediate
;
1127 MultilineStringTypeMap map
;
1128 map
.name
.str
= "An Item";
1129 map
.description
.str
= "Hello\nWorld";
1130 map
.ingredients
.str
= "SubItem 1\nSub Item 2\n\nSub Item 3\n";
1131 map
.recipes
.str
= "\n\nTest 1\n\n\n";
1132 map
.warningLabels
.str
= "";
1133 map
.documentation
.str
= "\n\n";
1136 llvm::raw_string_ostream
ostr(intermediate
);
1141 Input
yin(intermediate
);
1142 MultilineStringTypeMap map2
;
1145 EXPECT_FALSE(yin
.error());
1146 EXPECT_EQ(map2
.name
.str
, "An Item\n");
1147 EXPECT_EQ(map2
.description
.str
, "Hello\nWorld\n");
1148 EXPECT_EQ(map2
.ingredients
.str
, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
1149 EXPECT_EQ(map2
.recipes
.str
, "\n\nTest 1\n");
1150 EXPECT_TRUE(map2
.warningLabels
.str
.empty());
1151 EXPECT_TRUE(map2
.documentation
.str
.empty());
1152 EXPECT_EQ(map2
.price
, 350);
1157 // Test writing then reading back custom values
1159 TEST(YAMLIO
, TestReadWriteBlockScalarDocuments
) {
1160 std::string intermediate
;
1162 std::vector
<MultilineStringType
> documents
;
1163 MultilineStringType doc
;
1164 doc
.str
= "Hello\nWorld";
1165 documents
.push_back(doc
);
1167 llvm::raw_string_ostream
ostr(intermediate
);
1171 // Verify that the block scalar header was written out on the same line
1172 // as the document marker.
1173 EXPECT_NE(llvm::StringRef::npos
, llvm::StringRef(ostr
.str()).find("--- |"));
1176 Input
yin(intermediate
);
1177 std::vector
<MultilineStringType
> documents2
;
1180 EXPECT_FALSE(yin
.error());
1181 EXPECT_EQ(documents2
.size(), size_t(1));
1182 EXPECT_EQ(documents2
[0].str
, "Hello\nWorld\n");
1186 TEST(YAMLIO
, TestReadWriteBlockScalarValue
) {
1187 std::string intermediate
;
1189 MultilineStringType doc
;
1190 doc
.str
= "Just a block\nscalar doc";
1192 llvm::raw_string_ostream
ostr(intermediate
);
1197 Input
yin(intermediate
);
1198 MultilineStringType doc
;
1201 EXPECT_FALSE(yin
.error());
1202 EXPECT_EQ(doc
.str
, "Just a block\nscalar doc\n");
1206 //===----------------------------------------------------------------------===//
1207 // Test flow sequences
1208 //===----------------------------------------------------------------------===//
1210 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber
)
1211 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber
)
1212 LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef
, MyString
)
1213 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyString
)
1218 struct ScalarTraits
<MyNumber
> {
1219 static void output(const MyNumber
&value
, void *, llvm::raw_ostream
&out
) {
1223 static StringRef
input(StringRef scalar
, void *, MyNumber
&value
) {
1225 if ( getAsSignedInteger(scalar
, 0, n
) )
1226 return "invalid number";
1231 static QuotingType
mustQuote(StringRef
) { return QuotingType::None
; }
1234 template <> struct ScalarTraits
<MyString
> {
1235 using Impl
= ScalarTraits
<StringRef
>;
1236 static void output(const MyString
&V
, void *Ctx
, raw_ostream
&OS
) {
1237 Impl::output(V
, Ctx
, OS
);
1239 static StringRef
input(StringRef S
, void *Ctx
, MyString
&V
) {
1240 return Impl::input(S
, Ctx
, V
.value
);
1242 static QuotingType
mustQuote(StringRef S
) {
1243 return Impl::mustQuote(S
);
1249 struct NameAndNumbers
{
1250 llvm::StringRef name
;
1251 std::vector
<MyString
> strings
;
1252 std::vector
<MyNumber
> single
;
1253 std::vector
<MyNumber
> numbers
;
1259 struct MappingTraits
<NameAndNumbers
> {
1260 static void mapping(IO
&io
, NameAndNumbers
& nn
) {
1261 io
.mapRequired("name", nn
.name
);
1262 io
.mapRequired("strings", nn
.strings
);
1263 io
.mapRequired("single", nn
.single
);
1264 io
.mapRequired("numbers", nn
.numbers
);
1270 typedef std::vector
<MyNumber
> MyNumberFlowSequence
;
1272 LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence
)
1274 struct NameAndNumbersFlow
{
1275 llvm::StringRef name
;
1276 std::vector
<MyNumberFlowSequence
> sequenceOfNumbers
;
1282 struct MappingTraits
<NameAndNumbersFlow
> {
1283 static void mapping(IO
&io
, NameAndNumbersFlow
& nn
) {
1284 io
.mapRequired("name", nn
.name
);
1285 io
.mapRequired("sequenceOfNumbers", nn
.sequenceOfNumbers
);
1292 // Test writing then reading back custom values
1294 TEST(YAMLIO
, TestReadWriteMyFlowSequence
) {
1295 std::string intermediate
;
1299 map
.strings
.push_back(llvm::StringRef("one"));
1300 map
.strings
.push_back(llvm::StringRef("two"));
1301 map
.single
.push_back(1);
1302 map
.numbers
.push_back(10);
1303 map
.numbers
.push_back(-30);
1304 map
.numbers
.push_back(1024);
1306 llvm::raw_string_ostream
ostr(intermediate
);
1310 // Verify sequences were written in flow style
1312 llvm::StringRef
flowOut(intermediate
);
1313 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("one, two"));
1314 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("10, -30, 1024"));
1318 Input
yin(intermediate
);
1319 NameAndNumbers map2
;
1322 EXPECT_FALSE(yin
.error());
1323 EXPECT_TRUE(map2
.name
.equals("hello"));
1324 EXPECT_EQ(map2
.strings
.size(), 2UL);
1325 EXPECT_TRUE(map2
.strings
[0].value
.equals("one"));
1326 EXPECT_TRUE(map2
.strings
[1].value
.equals("two"));
1327 EXPECT_EQ(map2
.single
.size(), 1UL);
1328 EXPECT_EQ(1, map2
.single
[0]);
1329 EXPECT_EQ(map2
.numbers
.size(), 3UL);
1330 EXPECT_EQ(10, map2
.numbers
[0]);
1331 EXPECT_EQ(-30, map2
.numbers
[1]);
1332 EXPECT_EQ(1024, map2
.numbers
[2]);
1338 // Test writing then reading back a sequence of flow sequences.
1340 TEST(YAMLIO
, TestReadWriteSequenceOfMyFlowSequence
) {
1341 std::string intermediate
;
1343 NameAndNumbersFlow map
;
1345 MyNumberFlowSequence single
= { 0 };
1346 MyNumberFlowSequence numbers
= { 12, 1, -512 };
1347 map
.sequenceOfNumbers
.push_back(single
);
1348 map
.sequenceOfNumbers
.push_back(numbers
);
1349 map
.sequenceOfNumbers
.push_back(MyNumberFlowSequence());
1351 llvm::raw_string_ostream
ostr(intermediate
);
1355 // Verify sequences were written in flow style
1356 // and that the parent sequence used '-'.
1358 llvm::StringRef
flowOut(intermediate
);
1359 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ 0 ]"));
1360 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ 12, 1, -512 ]"));
1361 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- [ ]"));
1365 Input
yin(intermediate
);
1366 NameAndNumbersFlow map2
;
1369 EXPECT_FALSE(yin
.error());
1370 EXPECT_TRUE(map2
.name
.equals("hello"));
1371 EXPECT_EQ(map2
.sequenceOfNumbers
.size(), 3UL);
1372 EXPECT_EQ(map2
.sequenceOfNumbers
[0].size(), 1UL);
1373 EXPECT_EQ(0, map2
.sequenceOfNumbers
[0][0]);
1374 EXPECT_EQ(map2
.sequenceOfNumbers
[1].size(), 3UL);
1375 EXPECT_EQ(12, map2
.sequenceOfNumbers
[1][0]);
1376 EXPECT_EQ(1, map2
.sequenceOfNumbers
[1][1]);
1377 EXPECT_EQ(-512, map2
.sequenceOfNumbers
[1][2]);
1378 EXPECT_TRUE(map2
.sequenceOfNumbers
[2].empty());
1382 //===----------------------------------------------------------------------===//
1383 // Test normalizing/denormalizing
1384 //===----------------------------------------------------------------------===//
1386 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds
)
1388 typedef std::vector
<TotalSeconds
> SecondsSequence
;
1390 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds
)
1396 struct MappingTraits
<TotalSeconds
> {
1398 class NormalizedSeconds
{
1400 NormalizedSeconds(IO
&io
)
1401 : hours(0), minutes(0), seconds(0) {
1403 NormalizedSeconds(IO
&, TotalSeconds
&secs
)
1405 minutes((secs
- (hours
*3600))/60),
1406 seconds(secs
% 60) {
1408 TotalSeconds
denormalize(IO
&) {
1409 return TotalSeconds(hours
*3600 + minutes
*60 + seconds
);
1417 static void mapping(IO
&io
, TotalSeconds
&secs
) {
1418 MappingNormalization
<NormalizedSeconds
, TotalSeconds
> keys(io
, secs
);
1420 io
.mapOptional("hours", keys
->hours
, 0);
1421 io
.mapOptional("minutes", keys
->minutes
, 0);
1422 io
.mapRequired("seconds", keys
->seconds
);
1430 // Test the reading of a yaml sequence of mappings
1432 TEST(YAMLIO
, TestReadMySecondsSequence
) {
1433 SecondsSequence seq
;
1434 Input
yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
1437 EXPECT_FALSE(yin
.error());
1438 EXPECT_EQ(seq
.size(), 2UL);
1439 EXPECT_EQ(seq
[0], 3605U);
1440 EXPECT_EQ(seq
[1], 59U);
1445 // Test writing then reading back custom values
1447 TEST(YAMLIO
, TestReadWriteMySecondsSequence
) {
1448 std::string intermediate
;
1450 SecondsSequence seq
;
1451 seq
.push_back(4000);
1455 llvm::raw_string_ostream
ostr(intermediate
);
1460 Input
yin(intermediate
);
1461 SecondsSequence seq2
;
1464 EXPECT_FALSE(yin
.error());
1465 EXPECT_EQ(seq2
.size(), 3UL);
1466 EXPECT_EQ(seq2
[0], 4000U);
1467 EXPECT_EQ(seq2
[1], 500U);
1468 EXPECT_EQ(seq2
[2], 59U);
1473 //===----------------------------------------------------------------------===//
1474 // Test dynamic typing
1475 //===----------------------------------------------------------------------===//
1494 struct KindAndFlags
{
1495 KindAndFlags() : kind(kindA
), flags(0) { }
1496 KindAndFlags(Kind k
, uint32_t f
) : kind(k
), flags(f
) { }
1501 typedef std::vector
<KindAndFlags
> KindAndFlagsSequence
;
1503 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags
)
1508 struct ScalarEnumerationTraits
<AFlags
> {
1509 static void enumeration(IO
&io
, AFlags
&value
) {
1510 io
.enumCase(value
, "a1", a1
);
1511 io
.enumCase(value
, "a2", a2
);
1512 io
.enumCase(value
, "a3", a3
);
1516 struct ScalarEnumerationTraits
<BFlags
> {
1517 static void enumeration(IO
&io
, BFlags
&value
) {
1518 io
.enumCase(value
, "b1", b1
);
1519 io
.enumCase(value
, "b2", b2
);
1520 io
.enumCase(value
, "b3", b3
);
1524 struct ScalarEnumerationTraits
<Kind
> {
1525 static void enumeration(IO
&io
, Kind
&value
) {
1526 io
.enumCase(value
, "A", kindA
);
1527 io
.enumCase(value
, "B", kindB
);
1531 struct MappingTraits
<KindAndFlags
> {
1532 static void mapping(IO
&io
, KindAndFlags
& kf
) {
1533 io
.mapRequired("kind", kf
.kind
);
1534 // Type of "flags" field varies depending on "kind" field.
1535 // Use memcpy here to avoid breaking strict aliasing rules.
1536 if (kf
.kind
== kindA
) {
1537 AFlags aflags
= static_cast<AFlags
>(kf
.flags
);
1538 io
.mapRequired("flags", aflags
);
1541 BFlags bflags
= static_cast<BFlags
>(kf
.flags
);
1542 io
.mapRequired("flags", bflags
);
1552 // Test the reading of a yaml sequence dynamic types
1554 TEST(YAMLIO
, TestReadKindAndFlagsSequence
) {
1555 KindAndFlagsSequence seq
;
1556 Input
yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1559 EXPECT_FALSE(yin
.error());
1560 EXPECT_EQ(seq
.size(), 2UL);
1561 EXPECT_EQ(seq
[0].kind
, kindA
);
1562 EXPECT_EQ(seq
[0].flags
, (uint32_t)a2
);
1563 EXPECT_EQ(seq
[1].kind
, kindB
);
1564 EXPECT_EQ(seq
[1].flags
, (uint32_t)b1
);
1568 // Test writing then reading back dynamic types
1570 TEST(YAMLIO
, TestReadWriteKindAndFlagsSequence
) {
1571 std::string intermediate
;
1573 KindAndFlagsSequence seq
;
1574 seq
.push_back(KindAndFlags(kindA
,a1
));
1575 seq
.push_back(KindAndFlags(kindB
,b1
));
1576 seq
.push_back(KindAndFlags(kindA
,a2
));
1577 seq
.push_back(KindAndFlags(kindB
,b2
));
1578 seq
.push_back(KindAndFlags(kindA
,a3
));
1580 llvm::raw_string_ostream
ostr(intermediate
);
1585 Input
yin(intermediate
);
1586 KindAndFlagsSequence seq2
;
1589 EXPECT_FALSE(yin
.error());
1590 EXPECT_EQ(seq2
.size(), 5UL);
1591 EXPECT_EQ(seq2
[0].kind
, kindA
);
1592 EXPECT_EQ(seq2
[0].flags
, (uint32_t)a1
);
1593 EXPECT_EQ(seq2
[1].kind
, kindB
);
1594 EXPECT_EQ(seq2
[1].flags
, (uint32_t)b1
);
1595 EXPECT_EQ(seq2
[2].kind
, kindA
);
1596 EXPECT_EQ(seq2
[2].flags
, (uint32_t)a2
);
1597 EXPECT_EQ(seq2
[3].kind
, kindB
);
1598 EXPECT_EQ(seq2
[3].flags
, (uint32_t)b2
);
1599 EXPECT_EQ(seq2
[4].kind
, kindA
);
1600 EXPECT_EQ(seq2
[4].flags
, (uint32_t)a3
);
1605 //===----------------------------------------------------------------------===//
1606 // Test document list
1607 //===----------------------------------------------------------------------===//
1613 typedef std::vector
<FooBarMap
> FooBarMapDocumentList
;
1615 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap
)
1621 struct MappingTraits
<FooBarMap
> {
1622 static void mapping(IO
&io
, FooBarMap
& fb
) {
1623 io
.mapRequired("foo", fb
.foo
);
1624 io
.mapRequired("bar", fb
.bar
);
1632 // Test the reading of a yaml mapping
1634 TEST(YAMLIO
, TestDocRead
) {
1636 Input
yin("---\nfoo: 3\nbar: 5\n...\n");
1639 EXPECT_FALSE(yin
.error());
1640 EXPECT_EQ(doc
.foo
, 3);
1641 EXPECT_EQ(doc
.bar
,5);
1647 // Test writing then reading back a sequence of mappings
1649 TEST(YAMLIO
, TestSequenceDocListWriteAndRead
) {
1650 std::string intermediate
;
1658 std::vector
<FooBarMap
> docList
;
1659 docList
.push_back(doc1
);
1660 docList
.push_back(doc2
);
1662 llvm::raw_string_ostream
ostr(intermediate
);
1669 Input
yin(intermediate
);
1670 std::vector
<FooBarMap
> docList2
;
1673 EXPECT_FALSE(yin
.error());
1674 EXPECT_EQ(docList2
.size(), 2UL);
1675 FooBarMap
& map1
= docList2
[0];
1676 FooBarMap
& map2
= docList2
[1];
1677 EXPECT_EQ(map1
.foo
, 10);
1678 EXPECT_EQ(map1
.bar
, -3);
1679 EXPECT_EQ(map2
.foo
, 257);
1680 EXPECT_EQ(map2
.bar
, 0);
1684 //===----------------------------------------------------------------------===//
1685 // Test document tags
1686 //===----------------------------------------------------------------------===//
1689 MyDouble() : value(0.0) { }
1690 MyDouble(double x
) : value(x
) { }
1694 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble
)
1700 struct MappingTraits
<MyDouble
> {
1701 static void mapping(IO
&io
, MyDouble
&d
) {
1702 if (io
.mapTag("!decimal", true)) {
1703 mappingDecimal(io
, d
);
1704 } else if (io
.mapTag("!fraction")) {
1705 mappingFraction(io
, d
);
1708 static void mappingDecimal(IO
&io
, MyDouble
&d
) {
1709 io
.mapRequired("value", d
.value
);
1711 static void mappingFraction(IO
&io
, MyDouble
&d
) {
1713 io
.mapRequired("numerator", num
);
1714 io
.mapRequired("denominator", denom
);
1715 // convert fraction to double
1716 d
.value
= num
/denom
;
1724 // Test the reading of two different tagged yaml documents.
1726 TEST(YAMLIO
, TestTaggedDocuments
) {
1727 std::vector
<MyDouble
> docList
;
1728 Input
yin("--- !decimal\nvalue: 3.0\n"
1729 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1731 EXPECT_FALSE(yin
.error());
1732 EXPECT_EQ(docList
.size(), 2UL);
1733 EXPECT_EQ(docList
[0].value
, 3.0);
1734 EXPECT_EQ(docList
[1].value
, 4.5);
1740 // Test writing then reading back tagged documents
1742 TEST(YAMLIO
, TestTaggedDocumentsWriteAndRead
) {
1743 std::string intermediate
;
1747 std::vector
<MyDouble
> docList
;
1748 docList
.push_back(a
);
1749 docList
.push_back(b
);
1751 llvm::raw_string_ostream
ostr(intermediate
);
1757 Input
yin(intermediate
);
1758 std::vector
<MyDouble
> docList2
;
1761 EXPECT_FALSE(yin
.error());
1762 EXPECT_EQ(docList2
.size(), 2UL);
1763 EXPECT_EQ(docList2
[0].value
, 10.25);
1764 EXPECT_EQ(docList2
[1].value
, -3.75);
1769 //===----------------------------------------------------------------------===//
1770 // Test mapping validation
1771 //===----------------------------------------------------------------------===//
1773 struct MyValidation
{
1777 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation
)
1782 struct MappingTraits
<MyValidation
> {
1783 static void mapping(IO
&io
, MyValidation
&d
) {
1784 io
.mapRequired("value", d
.value
);
1786 static StringRef
validate(IO
&io
, MyValidation
&d
) {
1788 return "negative value";
1797 // Test that validate() is called and complains about the negative value.
1799 TEST(YAMLIO
, TestValidatingInput
) {
1800 std::vector
<MyValidation
> docList
;
1801 Input
yin("--- \nvalue: 3.0\n"
1802 "--- \nvalue: -1.0\n...\n",
1803 nullptr, suppressErrorMessages
);
1805 EXPECT_TRUE(!!yin
.error());
1808 //===----------------------------------------------------------------------===//
1809 // Test flow mapping
1810 //===----------------------------------------------------------------------===//
1816 FlowFooBar() : foo(0), bar(0) {}
1817 FlowFooBar(int foo
, int bar
) : foo(foo
), bar(bar
) {}
1820 typedef std::vector
<FlowFooBar
> FlowFooBarSequence
;
1822 LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar
)
1824 struct FlowFooBarDoc
{
1825 FlowFooBar attribute
;
1826 FlowFooBarSequence seq
;
1832 struct MappingTraits
<FlowFooBar
> {
1833 static void mapping(IO
&io
, FlowFooBar
&fb
) {
1834 io
.mapRequired("foo", fb
.foo
);
1835 io
.mapRequired("bar", fb
.bar
);
1838 static const bool flow
= true;
1842 struct MappingTraits
<FlowFooBarDoc
> {
1843 static void mapping(IO
&io
, FlowFooBarDoc
&fb
) {
1844 io
.mapRequired("attribute", fb
.attribute
);
1845 io
.mapRequired("seq", fb
.seq
);
1852 // Test writing then reading back custom mappings
1854 TEST(YAMLIO
, TestReadWriteMyFlowMapping
) {
1855 std::string intermediate
;
1858 doc
.attribute
= FlowFooBar(42, 907);
1859 doc
.seq
.push_back(FlowFooBar(1, 2));
1860 doc
.seq
.push_back(FlowFooBar(0, 0));
1861 doc
.seq
.push_back(FlowFooBar(-1, 1024));
1863 llvm::raw_string_ostream
ostr(intermediate
);
1867 // Verify that mappings were written in flow style
1869 llvm::StringRef
flowOut(intermediate
);
1870 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("{ foo: 42, bar: 907 }"));
1871 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: 1, bar: 2 }"));
1872 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: 0, bar: 0 }"));
1873 EXPECT_NE(llvm::StringRef::npos
, flowOut
.find("- { foo: -1, bar: 1024 }"));
1877 Input
yin(intermediate
);
1881 EXPECT_FALSE(yin
.error());
1882 EXPECT_EQ(doc2
.attribute
.foo
, 42);
1883 EXPECT_EQ(doc2
.attribute
.bar
, 907);
1884 EXPECT_EQ(doc2
.seq
.size(), 3UL);
1885 EXPECT_EQ(doc2
.seq
[0].foo
, 1);
1886 EXPECT_EQ(doc2
.seq
[0].bar
, 2);
1887 EXPECT_EQ(doc2
.seq
[1].foo
, 0);
1888 EXPECT_EQ(doc2
.seq
[1].bar
, 0);
1889 EXPECT_EQ(doc2
.seq
[2].foo
, -1);
1890 EXPECT_EQ(doc2
.seq
[2].bar
, 1024);
1894 //===----------------------------------------------------------------------===//
1895 // Test error handling
1896 //===----------------------------------------------------------------------===//
1899 // Test error handling of unknown enumerated scalar
1901 TEST(YAMLIO
, TestColorsReadError
) {
1909 suppressErrorMessages
);
1911 EXPECT_TRUE(!!yin
.error());
1916 // Test error handling of flow sequence with unknown value
1918 TEST(YAMLIO
, TestFlagsReadError
) {
1922 "f2: [ round, hollow ]\n"
1926 suppressErrorMessages
);
1929 EXPECT_TRUE(!!yin
.error());
1934 // Test error handling reading built-in uint8_t type
1936 TEST(YAMLIO
, TestReadBuiltInTypesUint8Error
) {
1937 std::vector
<uint8_t> seq
;
1944 suppressErrorMessages
);
1947 EXPECT_TRUE(!!yin
.error());
1952 // Test error handling reading built-in uint16_t type
1954 TEST(YAMLIO
, TestReadBuiltInTypesUint16Error
) {
1955 std::vector
<uint16_t> seq
;
1962 suppressErrorMessages
);
1965 EXPECT_TRUE(!!yin
.error());
1970 // Test error handling reading built-in uint32_t type
1972 TEST(YAMLIO
, TestReadBuiltInTypesUint32Error
) {
1973 std::vector
<uint32_t> seq
;
1980 suppressErrorMessages
);
1983 EXPECT_TRUE(!!yin
.error());
1988 // Test error handling reading built-in uint64_t type
1990 TEST(YAMLIO
, TestReadBuiltInTypesUint64Error
) {
1991 std::vector
<uint64_t> seq
;
1993 "- 18446744073709551615\n"
1995 "- 19446744073709551615\n"
1998 suppressErrorMessages
);
2001 EXPECT_TRUE(!!yin
.error());
2006 // Test error handling reading built-in int8_t type
2008 TEST(YAMLIO
, TestReadBuiltInTypesint8OverError
) {
2009 std::vector
<int8_t> seq
;
2017 suppressErrorMessages
);
2020 EXPECT_TRUE(!!yin
.error());
2024 // Test error handling reading built-in int8_t type
2026 TEST(YAMLIO
, TestReadBuiltInTypesint8UnderError
) {
2027 std::vector
<int8_t> seq
;
2035 suppressErrorMessages
);
2038 EXPECT_TRUE(!!yin
.error());
2043 // Test error handling reading built-in int16_t type
2045 TEST(YAMLIO
, TestReadBuiltInTypesint16UnderError
) {
2046 std::vector
<int16_t> seq
;
2054 suppressErrorMessages
);
2057 EXPECT_TRUE(!!yin
.error());
2062 // Test error handling reading built-in int16_t type
2064 TEST(YAMLIO
, TestReadBuiltInTypesint16OverError
) {
2065 std::vector
<int16_t> seq
;
2073 suppressErrorMessages
);
2076 EXPECT_TRUE(!!yin
.error());
2081 // Test error handling reading built-in int32_t type
2083 TEST(YAMLIO
, TestReadBuiltInTypesint32UnderError
) {
2084 std::vector
<int32_t> seq
;
2092 suppressErrorMessages
);
2095 EXPECT_TRUE(!!yin
.error());
2099 // Test error handling reading built-in int32_t type
2101 TEST(YAMLIO
, TestReadBuiltInTypesint32OverError
) {
2102 std::vector
<int32_t> seq
;
2110 suppressErrorMessages
);
2113 EXPECT_TRUE(!!yin
.error());
2118 // Test error handling reading built-in int64_t type
2120 TEST(YAMLIO
, TestReadBuiltInTypesint64UnderError
) {
2121 std::vector
<int64_t> seq
;
2123 "- -9223372036854775808\n"
2125 "- 9223372036854775807\n"
2126 "- -9223372036854775809\n"
2129 suppressErrorMessages
);
2132 EXPECT_TRUE(!!yin
.error());
2136 // Test error handling reading built-in int64_t type
2138 TEST(YAMLIO
, TestReadBuiltInTypesint64OverError
) {
2139 std::vector
<int64_t> seq
;
2141 "- -9223372036854775808\n"
2143 "- 9223372036854775807\n"
2144 "- 9223372036854775809\n"
2147 suppressErrorMessages
);
2150 EXPECT_TRUE(!!yin
.error());
2154 // Test error handling reading built-in float type
2156 TEST(YAMLIO
, TestReadBuiltInTypesFloatError
) {
2157 std::vector
<float> seq
;
2165 suppressErrorMessages
);
2168 EXPECT_TRUE(!!yin
.error());
2172 // Test error handling reading built-in float type
2174 TEST(YAMLIO
, TestReadBuiltInTypesDoubleError
) {
2175 std::vector
<double> seq
;
2183 suppressErrorMessages
);
2186 EXPECT_TRUE(!!yin
.error());
2190 // Test error handling reading built-in Hex8 type
2192 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8
)
2193 TEST(YAMLIO
, TestReadBuiltInTypesHex8Error
) {
2194 std::vector
<Hex8
> seq
;
2201 suppressErrorMessages
);
2204 EXPECT_TRUE(!!yin
.error());
2209 // Test error handling reading built-in Hex16 type
2211 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16
)
2212 TEST(YAMLIO
, TestReadBuiltInTypesHex16Error
) {
2213 std::vector
<Hex16
> seq
;
2220 suppressErrorMessages
);
2223 EXPECT_TRUE(!!yin
.error());
2227 // Test error handling reading built-in Hex32 type
2229 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32
)
2230 TEST(YAMLIO
, TestReadBuiltInTypesHex32Error
) {
2231 std::vector
<Hex32
> seq
;
2238 suppressErrorMessages
);
2241 EXPECT_TRUE(!!yin
.error());
2245 // Test error handling reading built-in Hex64 type
2247 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64
)
2248 TEST(YAMLIO
, TestReadBuiltInTypesHex64Error
) {
2249 std::vector
<Hex64
> seq
;
2252 "- 0xFFEEDDCCBBAA9988\n"
2253 "- 0x12345567890ABCDEF0\n"
2256 suppressErrorMessages
);
2259 EXPECT_TRUE(!!yin
.error());
2262 TEST(YAMLIO
, TestMalformedMapFailsGracefully
) {
2265 // We pass the suppressErrorMessages handler to handle the error
2266 // message generated in the constructor of Input.
2267 Input
yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages
);
2269 EXPECT_TRUE(!!yin
.error());
2273 Input
yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages
);
2275 EXPECT_TRUE(!!yin
.error());
2279 struct OptionalTest
{
2280 std::vector
<int> Numbers
;
2283 struct OptionalTestSeq
{
2284 std::vector
<OptionalTest
> Tests
;
2287 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest
)
2291 struct MappingTraits
<OptionalTest
> {
2292 static void mapping(IO
& IO
, OptionalTest
&OT
) {
2293 IO
.mapOptional("Numbers", OT
.Numbers
);
2298 struct MappingTraits
<OptionalTestSeq
> {
2299 static void mapping(IO
&IO
, OptionalTestSeq
&OTS
) {
2300 IO
.mapOptional("Tests", OTS
.Tests
);
2306 TEST(YAMLIO
, SequenceElideTest
) {
2307 // Test that writing out a purely optional structure with its fields set to
2308 // default followed by other data is properly read back in.
2309 OptionalTestSeq Seq
;
2310 OptionalTest One
, Two
, Three
, Four
;
2311 int N
[] = {1, 2, 3};
2312 Three
.Numbers
.assign(N
, N
+ 3);
2313 Seq
.Tests
.push_back(One
);
2314 Seq
.Tests
.push_back(Two
);
2315 Seq
.Tests
.push_back(Three
);
2316 Seq
.Tests
.push_back(Four
);
2318 std::string intermediate
;
2320 llvm::raw_string_ostream
ostr(intermediate
);
2325 Input
yin(intermediate
);
2326 OptionalTestSeq Seq2
;
2329 EXPECT_FALSE(yin
.error());
2331 EXPECT_EQ(4UL, Seq2
.Tests
.size());
2333 EXPECT_TRUE(Seq2
.Tests
[0].Numbers
.empty());
2334 EXPECT_TRUE(Seq2
.Tests
[1].Numbers
.empty());
2336 EXPECT_EQ(1, Seq2
.Tests
[2].Numbers
[0]);
2337 EXPECT_EQ(2, Seq2
.Tests
[2].Numbers
[1]);
2338 EXPECT_EQ(3, Seq2
.Tests
[2].Numbers
[2]);
2340 EXPECT_TRUE(Seq2
.Tests
[3].Numbers
.empty());
2343 TEST(YAMLIO
, TestEmptyStringFailsForMapWithRequiredFields
) {
2347 EXPECT_TRUE(!!yin
.error());
2350 TEST(YAMLIO
, TestEmptyStringSucceedsForMapWithOptionalFields
) {
2354 EXPECT_FALSE(yin
.error());
2357 TEST(YAMLIO
, TestEmptyStringSucceedsForSequence
) {
2358 std::vector
<uint8_t> seq
;
2359 Input
yin("", /*Ctxt=*/nullptr, suppressErrorMessages
);
2362 EXPECT_FALSE(yin
.error());
2363 EXPECT_TRUE(seq
.empty());
2367 llvm::StringRef str1
, str2
, str3
;
2368 FlowMap(llvm::StringRef str1
, llvm::StringRef str2
, llvm::StringRef str3
)
2369 : str1(str1
), str2(str2
), str3(str3
) {}
2373 llvm::StringRef str
;
2374 FlowSeq(llvm::StringRef S
) : str(S
) {}
2375 FlowSeq() = default;
2381 struct MappingTraits
<FlowMap
> {
2382 static void mapping(IO
&io
, FlowMap
&fm
) {
2383 io
.mapRequired("str1", fm
.str1
);
2384 io
.mapRequired("str2", fm
.str2
);
2385 io
.mapRequired("str3", fm
.str3
);
2388 static const bool flow
= true;
2392 struct ScalarTraits
<FlowSeq
> {
2393 static void output(const FlowSeq
&value
, void*, llvm::raw_ostream
&out
) {
2396 static StringRef
input(StringRef scalar
, void*, FlowSeq
&value
) {
2401 static QuotingType
mustQuote(StringRef S
) { return QuotingType::None
; }
2406 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq
)
2408 TEST(YAMLIO
, TestWrapFlow
) {
2410 llvm::raw_string_ostream
ostr(out
);
2411 FlowMap
Map("This is str1", "This is str2", "This is str3");
2412 std::vector
<FlowSeq
> Seq
;
2413 Seq
.emplace_back("This is str1");
2414 Seq
.emplace_back("This is str2");
2415 Seq
.emplace_back("This is str3");
2418 // 20 is just bellow the total length of the first mapping field.
2419 // We should wreap at every element.
2420 Output
yout(ostr
, nullptr, 15);
2426 "{ str1: This is str1, \n"
2427 " str2: This is str2, \n"
2428 " str3: This is str3 }\n"
2436 "[ This is str1, \n"
2443 // 25 will allow the second field to be output on the first line.
2444 Output
yout(ostr
, nullptr, 25);
2450 "{ str1: This is str1, str2: This is str2, \n"
2451 " str3: This is str3 }\n"
2459 "[ This is str1, This is str2, \n"
2465 // 0 means no wrapping.
2466 Output
yout(ostr
, nullptr, 0);
2472 "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2480 "[ This is str1, This is str2, This is str3 ]\n"
2486 struct MappingContext
{
2495 NestedMap(MappingContext
&Context
) : Context(Context
) {}
2497 MappingContext
&Context
;
2502 template <> struct MappingContextTraits
<SimpleMap
, MappingContext
> {
2503 static void mapping(IO
&io
, SimpleMap
&sm
, MappingContext
&Context
) {
2504 io
.mapRequired("B", sm
.B
);
2505 io
.mapRequired("C", sm
.C
);
2507 io
.mapRequired("Context", Context
.A
);
2511 template <> struct MappingTraits
<NestedMap
> {
2512 static void mapping(IO
&io
, NestedMap
&nm
) {
2513 io
.mapRequired("Simple", nm
.Simple
, nm
.Context
);
2519 TEST(YAMLIO
, TestMapWithContext
) {
2520 MappingContext Context
;
2521 NestedMap
Nested(Context
);
2523 llvm::raw_string_ostream
ostr(out
);
2525 Output
yout(ostr
, nullptr, 15);
2529 EXPECT_EQ(1, Context
.A
);
2540 Nested
.Simple
.B
= 2;
2541 Nested
.Simple
.C
= 3;
2544 EXPECT_EQ(2, Context
.A
);
2555 LLVM_YAML_IS_STRING_MAP(int)
2557 TEST(YAMLIO
, TestCustomMapping
) {
2558 std::map
<std::string
, int> x
;
2561 llvm::raw_string_ostream
ostr(out
);
2562 Output
xout(ostr
, nullptr, 0);
2584 std::map
<std::string
, int> y
;
2586 EXPECT_EQ(2ul, y
.size());
2587 EXPECT_EQ(1, y
["foo"]);
2588 EXPECT_EQ(2, y
["bar"]);
2591 LLVM_YAML_IS_STRING_MAP(FooBar
)
2593 TEST(YAMLIO
, TestCustomMappingStruct
) {
2594 std::map
<std::string
, FooBar
> x
;
2601 llvm::raw_string_ostream
ostr(out
);
2602 Output
xout(ostr
, nullptr, 0);
2617 std::map
<std::string
, FooBar
> y
;
2619 EXPECT_EQ(2ul, y
.size());
2620 EXPECT_EQ(1, y
["foo"].foo
);
2621 EXPECT_EQ(2, y
["foo"].bar
);
2622 EXPECT_EQ(3, y
["bar"].foo
);
2623 EXPECT_EQ(4, y
["bar"].bar
);
2626 struct FooBarMapMap
{
2627 std::map
<std::string
, FooBar
> fbm
;
2632 template <> struct MappingTraits
<FooBarMapMap
> {
2633 static void mapping(IO
&io
, FooBarMapMap
&x
) {
2634 io
.mapRequired("fbm", x
.fbm
);
2640 TEST(YAMLIO
, TestEmptyMapWrite
) {
2643 llvm::raw_string_ostream
OS(str
);
2646 EXPECT_EQ(OS
.str(), "---\nfbm: {}\n...\n");
2649 TEST(YAMLIO
, TestEmptySequenceWrite
) {
2650 FooBarContainer cont
;
2652 llvm::raw_string_ostream
OS(str
);
2655 EXPECT_EQ(OS
.str(), "---\nfbs: []\n...\n");
2658 static void TestEscaped(llvm::StringRef Input
, llvm::StringRef Expected
) {
2660 llvm::raw_string_ostream
ostr(out
);
2661 Output
xout(ostr
, nullptr, 0);
2663 llvm::yaml::EmptyContext Ctx
;
2664 yamlize(xout
, Input
, true, Ctx
);
2668 // Make a separate StringRef so we get nice byte-by-byte output.
2669 llvm::StringRef
Got(out
);
2670 EXPECT_EQ(Expected
, Got
);
2673 TEST(YAMLIO
, TestEscaped
) {
2675 TestEscaped("@abc@", "'@abc@'");
2677 TestEscaped("abc", "abc");
2678 // Forward slash quoted
2679 TestEscaped("abc/", "'abc/'");
2680 // Double quote non-printable
2681 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2682 // Double quote inside single quote
2683 TestEscaped("abc\"fdf", "'abc\"fdf'");
2684 // Double quote inside double quote
2685 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2686 // Single quote inside single quote
2687 TestEscaped("abc'fdf", "'abc''fdf'");
2689 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2690 // UTF8 with single quote inside double quote
2691 TestEscaped("parameter 'параметр' is unused",
2692 "\"parameter 'параметр' is unused\"");
2694 // String with embedded non-printable multibyte UTF-8 sequence (U+200B
2695 // zero-width space). The thing to test here is that we emit a
2696 // unicode-scalar level escape like \uNNNN (at the YAML level), and don't
2697 // just pass the UTF-8 byte sequence through as with quoted printables.
2699 const unsigned char foobar
[10] = {'f', 'o', 'o',
2700 0xE2, 0x80, 0x8B, // UTF-8 of U+200B
2703 TestEscaped((char const *)foobar
, "\"foo\\u200Bbar\"");
2707 TEST(YAMLIO
, Numeric
) {
2708 EXPECT_TRUE(isNumeric(".inf"));
2709 EXPECT_TRUE(isNumeric(".INF"));
2710 EXPECT_TRUE(isNumeric(".Inf"));
2711 EXPECT_TRUE(isNumeric("-.inf"));
2712 EXPECT_TRUE(isNumeric("+.inf"));
2714 EXPECT_TRUE(isNumeric(".nan"));
2715 EXPECT_TRUE(isNumeric(".NaN"));
2716 EXPECT_TRUE(isNumeric(".NAN"));
2718 EXPECT_TRUE(isNumeric("0"));
2719 EXPECT_TRUE(isNumeric("0."));
2720 EXPECT_TRUE(isNumeric("0.0"));
2721 EXPECT_TRUE(isNumeric("-0.0"));
2722 EXPECT_TRUE(isNumeric("+0.0"));
2724 EXPECT_TRUE(isNumeric("12345"));
2725 EXPECT_TRUE(isNumeric("012345"));
2726 EXPECT_TRUE(isNumeric("+12.0"));
2727 EXPECT_TRUE(isNumeric(".5"));
2728 EXPECT_TRUE(isNumeric("+.5"));
2729 EXPECT_TRUE(isNumeric("-1.0"));
2731 EXPECT_TRUE(isNumeric("2.3e4"));
2732 EXPECT_TRUE(isNumeric("-2E+05"));
2733 EXPECT_TRUE(isNumeric("+12e03"));
2734 EXPECT_TRUE(isNumeric("6.8523015e+5"));
2736 EXPECT_TRUE(isNumeric("1.e+1"));
2737 EXPECT_TRUE(isNumeric(".0e+1"));
2739 EXPECT_TRUE(isNumeric("0x2aF3"));
2740 EXPECT_TRUE(isNumeric("0o01234567"));
2742 EXPECT_FALSE(isNumeric("not a number"));
2743 EXPECT_FALSE(isNumeric("."));
2744 EXPECT_FALSE(isNumeric(".e+1"));
2745 EXPECT_FALSE(isNumeric(".1e"));
2746 EXPECT_FALSE(isNumeric(".1e+"));
2747 EXPECT_FALSE(isNumeric(".1e++1"));
2749 EXPECT_FALSE(isNumeric("ABCD"));
2750 EXPECT_FALSE(isNumeric("+0x2AF3"));
2751 EXPECT_FALSE(isNumeric("-0x2AF3"));
2752 EXPECT_FALSE(isNumeric("0x2AF3Z"));
2753 EXPECT_FALSE(isNumeric("0o012345678"));
2754 EXPECT_FALSE(isNumeric("0xZ"));
2755 EXPECT_FALSE(isNumeric("-0o012345678"));
2756 EXPECT_FALSE(isNumeric("000003A8229434B839616A25C16B0291F77A438B"));
2758 EXPECT_FALSE(isNumeric(""));
2759 EXPECT_FALSE(isNumeric("."));
2760 EXPECT_FALSE(isNumeric(".e+1"));
2761 EXPECT_FALSE(isNumeric(".e+"));
2762 EXPECT_FALSE(isNumeric(".e"));
2763 EXPECT_FALSE(isNumeric("e1"));
2765 // Deprecated formats: as for YAML 1.2 specification, the following are not
2766 // valid numbers anymore:
2768 // * Sexagecimal numbers
2769 // * Decimal numbers with comma s the delimiter
2770 // * "inf", "nan" without '.' prefix
2771 EXPECT_FALSE(isNumeric("3:25:45"));
2772 EXPECT_FALSE(isNumeric("+12,345"));
2773 EXPECT_FALSE(isNumeric("-inf"));
2774 EXPECT_FALSE(isNumeric("1,230.15"));
2777 //===----------------------------------------------------------------------===//
2778 // Test PolymorphicTraits and TaggedScalarTraits
2779 //===----------------------------------------------------------------------===//
2788 Poly(NodeKind Kind
) : Kind(Kind
) {}
2790 virtual ~Poly() = default;
2792 NodeKind
getKind() const { return Kind
; }
2795 struct Scalar
: Poly
{
2807 Scalar() : Poly(NK_Scalar
), SKind(SK_Unknown
) {}
2808 Scalar(double DoubleValue
)
2809 : Poly(NK_Scalar
), SKind(SK_Double
), DoubleValue(DoubleValue
) {}
2810 Scalar(bool BoolValue
)
2811 : Poly(NK_Scalar
), SKind(SK_Bool
), BoolValue(BoolValue
) {}
2813 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Scalar
; }
2816 struct Seq
: Poly
, std::vector
<std::unique_ptr
<Poly
>> {
2817 Seq() : Poly(NK_Seq
) {}
2819 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Seq
; }
2822 struct Map
: Poly
, llvm::StringMap
<std::unique_ptr
<Poly
>> {
2823 Map() : Poly(NK_Map
) {}
2825 static bool classof(const Poly
*N
) { return N
->getKind() == NK_Map
; }
2831 template <> struct PolymorphicTraits
<std::unique_ptr
<Poly
>> {
2832 static NodeKind
getKind(const std::unique_ptr
<Poly
> &N
) {
2833 if (isa
<Scalar
>(*N
))
2834 return NodeKind::Scalar
;
2836 return NodeKind::Sequence
;
2838 return NodeKind::Map
;
2839 llvm_unreachable("unsupported node type");
2842 static Scalar
&getAsScalar(std::unique_ptr
<Poly
> &N
) {
2843 if (!N
|| !isa
<Scalar
>(*N
))
2844 N
= std::make_unique
<Scalar
>();
2845 return *cast
<Scalar
>(N
.get());
2848 static Seq
&getAsSequence(std::unique_ptr
<Poly
> &N
) {
2849 if (!N
|| !isa
<Seq
>(*N
))
2850 N
= std::make_unique
<Seq
>();
2851 return *cast
<Seq
>(N
.get());
2854 static Map
&getAsMap(std::unique_ptr
<Poly
> &N
) {
2855 if (!N
|| !isa
<Map
>(*N
))
2856 N
= std::make_unique
<Map
>();
2857 return *cast
<Map
>(N
.get());
2861 template <> struct TaggedScalarTraits
<Scalar
> {
2862 static void output(const Scalar
&S
, void *Ctxt
, raw_ostream
&ScalarOS
,
2863 raw_ostream
&TagOS
) {
2865 case Scalar::SK_Unknown
:
2866 report_fatal_error("output unknown scalar");
2868 case Scalar::SK_Double
:
2870 ScalarTraits
<double>::output(S
.DoubleValue
, Ctxt
, ScalarOS
);
2872 case Scalar::SK_Bool
:
2874 ScalarTraits
<bool>::output(S
.BoolValue
, Ctxt
, ScalarOS
);
2879 static StringRef
input(StringRef ScalarStr
, StringRef Tag
, void *Ctxt
,
2881 S
.SKind
= StringSwitch
<Scalar::ScalarKind
>(Tag
)
2882 .Case("!double", Scalar::SK_Double
)
2883 .Case("!bool", Scalar::SK_Bool
)
2884 .Default(Scalar::SK_Unknown
);
2886 case Scalar::SK_Unknown
:
2887 return StringRef("unknown scalar tag");
2888 case Scalar::SK_Double
:
2889 return ScalarTraits
<double>::input(ScalarStr
, Ctxt
, S
.DoubleValue
);
2890 case Scalar::SK_Bool
:
2891 return ScalarTraits
<bool>::input(ScalarStr
, Ctxt
, S
.BoolValue
);
2893 llvm_unreachable("unknown scalar kind");
2896 static QuotingType
mustQuote(const Scalar
&S
, StringRef Str
) {
2898 case Scalar::SK_Unknown
:
2899 report_fatal_error("quote unknown scalar");
2900 case Scalar::SK_Double
:
2901 return ScalarTraits
<double>::mustQuote(Str
);
2902 case Scalar::SK_Bool
:
2903 return ScalarTraits
<bool>::mustQuote(Str
);
2905 llvm_unreachable("unknown scalar kind");
2909 template <> struct CustomMappingTraits
<Map
> {
2910 static void inputOne(IO
&IO
, StringRef Key
, Map
&M
) {
2911 IO
.mapRequired(Key
.str().c_str(), M
[Key
]);
2914 static void output(IO
&IO
, Map
&M
) {
2916 IO
.mapRequired(N
.getKey().str().c_str(), N
.getValue());
2920 template <> struct SequenceTraits
<Seq
> {
2921 static size_t size(IO
&IO
, Seq
&A
) { return A
.size(); }
2923 static std::unique_ptr
<Poly
> &element(IO
&IO
, Seq
&A
, size_t Index
) {
2924 if (Index
>= A
.size())
2925 A
.resize(Index
+ 1);
2933 TEST(YAMLIO
, TestReadWritePolymorphicScalar
) {
2934 std::string intermediate
;
2935 std::unique_ptr
<Poly
> node
= std::make_unique
<Scalar
>(true);
2937 llvm::raw_string_ostream
ostr(intermediate
);
2939 #ifdef GTEST_HAS_DEATH_TEST
2941 EXPECT_DEATH(yout
<< node
, "plain scalar documents are not supported");
2946 TEST(YAMLIO
, TestReadWritePolymorphicSeq
) {
2947 std::string intermediate
;
2949 auto seq
= std::make_unique
<Seq
>();
2950 seq
->push_back(std::make_unique
<Scalar
>(true));
2951 seq
->push_back(std::make_unique
<Scalar
>(1.0));
2952 auto node
= llvm::unique_dyn_cast
<Poly
>(seq
);
2954 llvm::raw_string_ostream
ostr(intermediate
);
2959 Input
yin(intermediate
);
2960 std::unique_ptr
<Poly
> node
;
2963 EXPECT_FALSE(yin
.error());
2964 auto seq
= llvm::dyn_cast
<Seq
>(node
.get());
2966 ASSERT_EQ(seq
->size(), 2u);
2967 auto first
= llvm::dyn_cast
<Scalar
>((*seq
)[0].get());
2969 EXPECT_EQ(first
->SKind
, Scalar::SK_Bool
);
2970 EXPECT_TRUE(first
->BoolValue
);
2971 auto second
= llvm::dyn_cast
<Scalar
>((*seq
)[1].get());
2972 ASSERT_TRUE(second
);
2973 EXPECT_EQ(second
->SKind
, Scalar::SK_Double
);
2974 EXPECT_EQ(second
->DoubleValue
, 1.0);
2978 TEST(YAMLIO
, TestReadWritePolymorphicMap
) {
2979 std::string intermediate
;
2981 auto map
= std::make_unique
<Map
>();
2982 (*map
)["foo"] = std::make_unique
<Scalar
>(false);
2983 (*map
)["bar"] = std::make_unique
<Scalar
>(2.0);
2984 std::unique_ptr
<Poly
> node
= llvm::unique_dyn_cast
<Poly
>(map
);
2986 llvm::raw_string_ostream
ostr(intermediate
);
2991 Input
yin(intermediate
);
2992 std::unique_ptr
<Poly
> node
;
2995 EXPECT_FALSE(yin
.error());
2996 auto map
= llvm::dyn_cast
<Map
>(node
.get());
2998 auto foo
= llvm::dyn_cast
<Scalar
>((*map
)["foo"].get());
3000 EXPECT_EQ(foo
->SKind
, Scalar::SK_Bool
);
3001 EXPECT_FALSE(foo
->BoolValue
);
3002 auto bar
= llvm::dyn_cast
<Scalar
>((*map
)["bar"].get());
3004 EXPECT_EQ(bar
->SKind
, Scalar::SK_Double
);
3005 EXPECT_EQ(bar
->DoubleValue
, 2.0);