1 | //===- YAMLTest.cpp - Tests for Object YAML -------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/ObjectYAML/YAML.h" |
10 | #include "llvm/Support/YAMLTraits.h" |
11 | #include "gtest/gtest.h" |
12 | |
13 | using namespace llvm; |
14 | |
15 | struct BinaryHolder { |
16 | yaml::BinaryRef Binary; |
17 | }; |
18 | |
19 | namespace llvm { |
20 | namespace yaml { |
21 | template <> |
22 | struct MappingTraits<BinaryHolder> { |
23 | static void mapping(IO &IO, BinaryHolder &BH) { |
24 | IO.mapRequired(Key: "Binary" , Val&: BH.Binary); |
25 | } |
26 | }; |
27 | } // end namespace yaml |
28 | } // end namespace llvm |
29 | |
30 | TEST(ObjectYAML, BinaryRef) { |
31 | BinaryHolder BH; |
32 | SmallVector<char, 32> Buf; |
33 | llvm::raw_svector_ostream OS(Buf); |
34 | yaml::Output YOut(OS); |
35 | YOut << BH; |
36 | EXPECT_NE(OS.str().find("''" ), StringRef::npos); |
37 | } |
38 | |
39 | TEST(ObjectYAML, UnknownOption) { |
40 | StringRef InputYAML = "InvalidKey: InvalidValue\n" |
41 | "Binary: AAAA\n" ; |
42 | BinaryHolder BH; |
43 | yaml::Input Input(InputYAML); |
44 | // test 1: default in trying to parse invalid key is an error case. |
45 | Input >> BH; |
46 | EXPECT_EQ(Input.error().value(), 22); |
47 | |
48 | // test 2: only warn about invalid key if actively set. |
49 | yaml::Input Input2(InputYAML); |
50 | BinaryHolder BH2; |
51 | Input2.setAllowUnknownKeys(true); |
52 | Input2 >> BH2; |
53 | EXPECT_EQ(BH2.Binary, yaml::BinaryRef("AAAA" )); |
54 | EXPECT_EQ(Input2.error().value(), 0); |
55 | } |
56 | |