ConfigYAMLTests.cpp
3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//===-- ConfigYAMLTests.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "ConfigFragment.h"
#include "ConfigTesting.h"
#include "Protocol.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/SourceMgr.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace config {
template <typename T> void PrintTo(const Located<T> &V, std::ostream *OS) {
*OS << ::testing::PrintToString(*V);
}
namespace {
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
MATCHER_P(Val, Value, "") {
if (*arg == Value)
return true;
*result_listener << "value is " << *arg;
return false;
}
TEST(ParseYAML, SyntacticForms) {
CapturedDiags Diags;
const char *YAML = R"yaml(
If:
PathMatch:
- 'abc'
CompileFlags: { Add: [foo, bar] }
---
CompileFlags:
Add: |
b
az
---
Index:
Background: Skip
)yaml";
auto Results = Fragment::parseYAML(YAML, "config.yaml", Diags.callback());
EXPECT_THAT(Diags.Diagnostics, IsEmpty());
ASSERT_EQ(Results.size(), 3u);
EXPECT_FALSE(Results[0].If.HasUnrecognizedCondition);
EXPECT_THAT(Results[0].If.PathMatch, ElementsAre(Val("abc")));
EXPECT_THAT(Results[0].CompileFlags.Add, ElementsAre(Val("foo"), Val("bar")));
EXPECT_THAT(Results[1].CompileFlags.Add, ElementsAre(Val("b\naz\n")));
ASSERT_TRUE(Results[2].Index.Background);
EXPECT_EQ("Skip", *Results[2].Index.Background.getValue());
}
TEST(ParseYAML, Locations) {
CapturedDiags Diags;
Annotations YAML(R"yaml(
If:
PathMatch: [['???bad***regex(((']]
)yaml");
auto Results =
Fragment::parseYAML(YAML.code(), "config.yaml", Diags.callback());
EXPECT_THAT(Diags.Diagnostics, IsEmpty());
ASSERT_EQ(Results.size(), 1u);
ASSERT_NE(Results.front().Source.Manager, nullptr);
EXPECT_EQ(toRange(Results.front().If.PathMatch.front().Range,
*Results.front().Source.Manager),
YAML.range());
}
TEST(ParseYAML, Diagnostics) {
CapturedDiags Diags;
Annotations YAML(R"yaml(
If:
[[UnknownCondition]]: "foo"
CompileFlags:
Add: 'first'
---
CompileFlags: {^
)yaml");
auto Results =
Fragment::parseYAML(YAML.code(), "config.yaml", Diags.callback());
ASSERT_THAT(
Diags.Diagnostics,
ElementsAre(AllOf(DiagMessage("Unknown If key UnknownCondition"),
DiagKind(llvm::SourceMgr::DK_Warning),
DiagPos(YAML.range().start), DiagRange(YAML.range())),
AllOf(DiagMessage("Unexpected token. Expected Key, Flow "
"Entry, or Flow Mapping End."),
DiagKind(llvm::SourceMgr::DK_Error),
DiagPos(YAML.point()), DiagRange(llvm::None))));
ASSERT_EQ(Results.size(), 1u); // invalid fragment discarded.
EXPECT_THAT(Results.front().CompileFlags.Add, ElementsAre(Val("first")));
EXPECT_TRUE(Results.front().If.HasUnrecognizedCondition);
}
TEST(ParseYAML, Invalid) {
CapturedDiags Diags;
const char *YAML = R"yaml(
If:
horrible
---
- 1
)yaml";
auto Results = Fragment::parseYAML(YAML, "config.yaml", Diags.callback());
EXPECT_THAT(Diags.Diagnostics,
ElementsAre(DiagMessage("If should be a dictionary"),
DiagMessage("Config should be a dictionary")));
ASSERT_THAT(Results, IsEmpty());
}
} // namespace
} // namespace config
} // namespace clangd
} // namespace clang