forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_utils_test.cc
174 lines (150 loc) · 5.67 KB
/
config_utils_test.cc
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/text/config_utils.h"
#include <cstdint>
#include <string>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
namespace verible {
namespace config {
TEST(ConfigUtilsTest, ComplainInvalidParameter) {
absl::Status s;
// singular ...
s = ParseNameValues("baz:123", {{"foo", nullptr}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(),
"baz: unknown parameter; supported "
"parameter is 'foo'");
// plural.
s = ParseNameValues("baz:123", {{"foo", nullptr}, {"bar", nullptr}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(),
"baz: unknown parameter; supported "
"parameters are 'foo', 'bar'");
s = ParseNameValues("foo:123", {{"foo", nullptr}, {"bar", nullptr}});
EXPECT_TRUE(s.ok());
}
TEST(ConfigUtilsTest, ParseInteger) {
absl::Status s;
int value = -1;
s = ParseNameValues("baz:42", {{"baz", SetInt(&value, 0, 100)}});
EXPECT_TRUE(s.ok());
EXPECT_EQ(value, 42);
s = ParseNameValues("baz:fourtytwo", {{"baz", SetInt(&value, 0, 100)}});
EXPECT_FALSE(s.ok());
// would be cool though :)
EXPECT_EQ(s.message(), "baz: 'fourtytwo': Cannot parse integer");
s = ParseNameValues("baz:142", {{"baz", SetInt(&value, 0, 100)}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(), "baz: 142 out of range [0...100]");
s = ParseNameValues("baz:-1", {{"baz", SetInt(&value, 0, 100)}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(), "baz: -1 out of range [0...100]");
s = ParseNameValues("baz:-12345", {{"baz", SetInt(&value)}});
EXPECT_TRUE(s.ok());
EXPECT_EQ(value, -12345);
}
TEST(ConfigUtilsTest, ParseBool) {
absl::Status s;
bool value = false;
for (auto config : {"baz", "baz:TrUe", "baz:on", "baz:1"}) {
s = ParseNameValues(config, {{"baz", SetBool(&value)}});
EXPECT_TRUE(s.ok());
EXPECT_TRUE(value);
}
for (auto config : {"baz:fAlse", "baz:off", "baz:0"}) {
s = ParseNameValues(config, {{"baz", SetBool(&value)}});
EXPECT_TRUE(s.ok());
EXPECT_FALSE(value);
}
s = ParseNameValues("baz:foobar", {{"baz", SetBool(&value)}});
EXPECT_FALSE(s.ok());
EXPECT_TRUE(
absl::StartsWith(s.message(), "baz: Boolean value should be one of"));
}
TEST(ConfigUtilsTest, ParseString) {
absl::Status s;
std::string str;
s = ParseNameValues("baz:hello", {{"baz", SetString(&str)}});
EXPECT_TRUE(s.ok());
EXPECT_EQ(str, "hello");
s = ParseNameValues("baz:hello",
{{"baz", SetStringOneOf(&str, {"hello", "world"})}});
EXPECT_TRUE(s.ok());
EXPECT_EQ(str, "hello");
// Selection from multiple strings
s = ParseNameValues("baz:greetings",
{{"baz", SetStringOneOf(&str, {"hello", "world"})}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(),
"baz: Value can only be one of ['hello', 'world']; "
"got 'greetings'");
// Selection from one string
s = ParseNameValues("baz:greetings",
{{"baz", SetStringOneOf(&str, {"hello"})}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(), "baz: Value can only be 'hello'; got 'greetings'");
}
TEST(ConfigUtilsTest, ParseNamedBitmap) {
const std::vector<absl::string_view> kBitNames = {"ZERO", "ONE", "TWO"};
const std::pair<absl::string_view, uint32_t> kTestCases[] = {
{"baz:ONE", 1 << 1},
{"baz:", 0},
{"baz:ZERO|TWO", (1 << 0) | (1 << 2)},
{"baz:ZERO||TWO", (1 << 0) | (1 << 2)}, // Allowing empty value
{"baz:ZERO| |TWO", (1 << 0) | (1 << 2)}, // .. even with space
{"baz: ZERO | TWO", (1 << 0) | (1 << 2)}, // Whitespace around ok.
{"baz:zErO|TwO", (1 << 0) | (1 << 2)}, // case-insensitive
{"baz:TWO|ZERO", (1 << 0) | (1 << 2)}, // Sequence does not matter
{"baz:ZERO|ONE|TWO", (1 << 0) | (1 << 1) | (1 << 2)},
};
absl::Status s;
for (const auto& testcase : kTestCases) {
uint32_t bitmap = 0x12345678;
s = ParseNameValues(testcase.first,
{{"baz", SetNamedBits(&bitmap, kBitNames)}});
EXPECT_TRUE(s.ok()) << "case: '" << testcase.first << "' ->" << s.message();
EXPECT_EQ(bitmap, testcase.second);
}
{
uint32_t bitmap = 0x12345678;
s = ParseNameValues("baz:ONE|invalid",
{{"baz", SetNamedBits(&bitmap, kBitNames)}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(),
"baz: 'invalid' is not in the available "
"choices {ZERO, ONE, TWO}");
EXPECT_EQ(bitmap, 0x12345678); // on parse failure, variable not modified.
}
}
TEST(ConfigUtilsTest, ParseMultipleParameters) {
absl::Status s;
int answer;
bool panic;
s = ParseNameValues("answer:42;panic:off", {{"answer", SetInt(&answer)},
{"panic", SetBool(&panic)}});
EXPECT_TRUE(s.ok());
EXPECT_FALSE(panic);
EXPECT_EQ(answer, 42);
s = ParseNameValues("answer:43;panic:on", {{"answer", SetInt(&answer)},
{"panic", SetBool(&panic)}});
EXPECT_TRUE(s.ok()) << s.message();
EXPECT_TRUE(panic);
EXPECT_EQ(answer, 43);
}
} // namespace config
} // namespace verible