Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
util
Config.ut.cpp
Go to the documentation of this file.
1
// The Reactive C++ Toolbox.
2
// Copyright (C) 2013-2019 Swirly Cloud Limited
3
// Copyright (C) 2022 Reactive Markets Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "
Config.hpp
"
18
19
#include <boost/test/unit_test.hpp>
20
21
#include <map>
22
23
using namespace
std
;
24
using namespace
toolbox
;
25
26
BOOST_AUTO_TEST_SUITE
(
ConfigSuite
)
27
28
BOOST_AUTO_TEST_CASE
(
ParseSectionSingleCase
)
29
{
30
const
string
text
{R
"(
31
# comment
32
# indented comment
33
ab
34
cd=
35
ef=gh
36
=ij
37
38
kl = mn
39
op = qr
40
st = = uv =
41
42
)"};
43
44
istringstream
is
{
text
};
45
map<string, string>
conf
;
46
parse_section
(
is
, [&
conf
](
const
auto
&
key
,
const
auto
&
val
) {
conf
.emplace(
key
,
val
); });
47
BOOST_CHECK_EQUAL
(
conf
.size(), 7U);
48
BOOST_CHECK_EQUAL
(
conf
[
"ab"
],
""
);
49
BOOST_CHECK_EQUAL
(
conf
[
"cd"
],
""
);
50
BOOST_CHECK_EQUAL
(
conf
[
"ef"
],
"gh"
);
51
BOOST_CHECK_EQUAL
(
conf
[
""
],
"ij"
);
52
BOOST_CHECK_EQUAL
(
conf
[
"kl"
],
"mn"
);
53
BOOST_CHECK_EQUAL
(
conf
[
"op"
],
"qr"
);
54
BOOST_CHECK_EQUAL
(
conf
[
"st"
],
"= uv ="
);
55
BOOST_CHECK
(
is
.eof());
56
}
57
58
BOOST_AUTO_TEST_CASE
(
ParseSectionMultiCase
)
59
{
60
const
string
text
{R
"(
61
# comment
62
# indented comment
63
ab
64
cd=
65
66
[foo]
67
ef=gh
68
=ij
69
70
[ bar ]
71
kl = mn
72
op = qr
73
st = = uv =
74
75
)"};
76
77
istringstream
is
{
text
};
78
map<string, string>
conf
;
79
80
string
next;
81
parse_section
(
82
is
, [&
conf
](
const
auto
&
key
,
const
auto
&
val
) {
conf
.emplace(
key
,
val
); }, &next);
83
BOOST_CHECK_EQUAL
(
conf
.size(), 2U);
84
BOOST_CHECK_EQUAL
(
conf
[
"ab"
],
""
);
85
BOOST_CHECK_EQUAL
(
conf
[
"cd"
],
""
);
86
BOOST_CHECK_EQUAL
(next,
"foo"
);
87
BOOST_CHECK
(!
is
.fail());
88
89
conf
.clear();
90
parse_section
(
91
is
, [&
conf
](
const
auto
&
key
,
const
auto
&
val
) {
conf
.emplace(
key
,
val
); }, &next);
92
BOOST_CHECK_EQUAL
(
conf
.size(), 2U);
93
BOOST_CHECK_EQUAL
(
conf
[
"ef"
],
"gh"
);
94
BOOST_CHECK_EQUAL
(
conf
[
""
],
"ij"
);
95
BOOST_CHECK_EQUAL
(next,
"bar"
);
96
BOOST_CHECK
(!
is
.fail());
97
98
conf
.clear();
99
parse_section
(
100
is
, [&
conf
](
const
auto
&
key
,
const
auto
&
val
) {
conf
.emplace(
key
,
val
); }, &next);
101
BOOST_CHECK_EQUAL
(
conf
.size(), 3U);
102
BOOST_CHECK_EQUAL
(
conf
[
"kl"
],
"mn"
);
103
BOOST_CHECK_EQUAL
(
conf
[
"op"
],
"qr"
);
104
BOOST_CHECK_EQUAL
(
conf
[
"st"
],
"= uv ="
);
105
BOOST_CHECK
(next.empty());
106
BOOST_CHECK
(
is
.eof());
107
}
108
109
BOOST_AUTO_TEST_CASE
(
ConfigSetAndGetCase
)
110
{
111
Config
config
;
112
113
BOOST_CHECK_EQUAL
(
config
.get<
int
>(
"foo"
, 101), 101);
114
BOOST_CHECK_EQUAL
(
config
.get<
int
>(
"bar"
, 202), 202);
115
116
config
.set(
"foo"
,
"101"
);
117
config
.set(
"bar"
,
"202"
);
118
119
BOOST_CHECK_EQUAL
(
config
.get<
int
>(
"foo"
, 0), 101);
120
BOOST_CHECK_EQUAL
(
config
.get<
int
>(
"bar"
, 0), 202);
121
122
config
.set(
"foo"
,
"303"
);
123
124
BOOST_CHECK_EQUAL
(
config
.get<
int
>(
"foo"
, 0), 303);
125
BOOST_CHECK_EQUAL
(
config
.get<
int
>(
"bar"
, 0), 202);
126
}
127
128
BOOST_AUTO_TEST_CASE
(
ConfigOverrideCase
)
129
{
130
const
string
text
{R
"(
131
foo=101
132
bar=202
133
134
[session]
135
136
bar=303
137
baz=404
138
)"};
139
140
istringstream
is
{
text
};
141
string
next;
142
143
Config
parent
;
144
parent
.
read_section
(
is
, next);
145
146
BOOST_CHECK_EQUAL
(
parent
.size(), 2U);
147
BOOST_CHECK_EQUAL
(
parent
.get<
int
>(
"foo"
, 0), 101);
148
BOOST_CHECK_EQUAL
(
parent
.get<
int
>(
"bar"
, 0), 202);
149
BOOST_CHECK_EQUAL
(next,
"session"
);
150
BOOST_CHECK
(!
is
.fail());
151
152
// Verify that getter with nullptr default compiles.
153
BOOST_CHECK
(
parent
.get(
"foo"
,
nullptr
));
154
155
// Conversion from internal std::string to std::string_view is a special case.
156
BOOST_CHECK_EQUAL
(
parent
.get<string_view>(
"foo"
),
"101"
sv
);
157
158
Config
child
;
159
child
.
read_section
(
is
, next);
160
child
.set_parent(
parent
);
161
162
BOOST_CHECK_EQUAL
(
child
.size(), 2U);
163
BOOST_CHECK_EQUAL
(
child
.get<
int
>(
"foo"
, 0), 101);
164
BOOST_CHECK_EQUAL
(
child
.get<
int
>(
"bar"
, 0), 303);
165
BOOST_CHECK_EQUAL
(
child
.get<
int
>(
"baz"
, 0), 404);
166
BOOST_CHECK
(next.empty());
167
BOOST_CHECK
(
is
.eof());
168
169
BOOST_CHECK_THROW
(
child
.get(
"bad"
), runtime_error);
170
BOOST_CHECK_THROW
(
child
.get<
int
>(
"bad"
), runtime_error);
171
}
172
173
BOOST_AUTO_TEST_CASE
(
ConfigEscapeCase
)
174
{
175
const
string
text
{R
"(
176
foo=\a\b\c
177
)"};
178
179
istringstream
is
{
text
};
180
181
Config
config
;
182
config
.
read_section
(
is
);
183
184
BOOST_CHECK_EQUAL
(
config
.size(), 1U);
185
BOOST_CHECK_EQUAL
(
config
.get(
"foo"
),
"abc"
);
186
}
187
188
BOOST_AUTO_TEST_SUITE_END
()
Config.hpp
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(ParseSectionSingleCase)
Definition
Config.ut.cpp:28
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(v.size(), 10U)
toolbox::Config
Simple config reader with environment variable substitution.
Definition
Config.hpp:67
toolbox::util::Config::read_section
std::istream & read_section(std::istream &is)
Definition
Config.hpp:127
std
STL namespace.
toolbox::util::parse_section
std::istream & parse_section(std::istream &is, FnT fn, std::string *name=nullptr)
Definition
Config.hpp:33
toolbox::util::sv
std::string_view sv
Definition
Tokeniser.hpp:26
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
BOOST_CHECK
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))
Generated by
1.9.8