Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
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
23using namespace std;
24using namespace toolbox;
25
27
29{
30 const string text{R"(
31# comment
32 # indented comment
33ab
34cd=
35ef=gh
36=ij
37
38kl = mn
39 op = qr
40st = = uv =
41
42)"};
43
44 istringstream is{text};
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
59{
60 const string text{R"(
61# comment
62 # indented comment
63ab
64cd=
65
66[foo]
67ef=gh
68=ij
69
70 [ bar ]
71kl = mn
72 op = qr
73st = = uv =
74
75)"};
76
77 istringstream is{text};
79
80 string next;
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();
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();
100 is, [&conf](const auto& key, const auto& val) { conf.emplace(key, val); }, &next);
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
110{
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
129{
130 const string text{R"(
131foo=101
132bar=202
133
134[session]
135
136bar=303
137baz=404
138)"};
139
140 istringstream is{text};
141 string next;
142
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
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
174{
175 const string text{R"(
176foo=\a\b\c
177)"};
178
179 istringstream is{text};
180
183
184 BOOST_CHECK_EQUAL(config.size(), 1U);
185 BOOST_CHECK_EQUAL(config.get("foo"), "abc");
186}
187
BOOST_AUTO_TEST_CASE(ParseSectionSingleCase)
Definition Config.ut.cpp:28
BOOST_CHECK_EQUAL(v.size(), 10U)
Simple config reader with environment variable substitution.
Definition Config.hpp:67
std::istream & read_section(std::istream &is)
Definition Config.hpp:127
STL namespace.
std::istream & parse_section(std::istream &is, FnT fn, std::string *name=nullptr)
Definition Config.hpp:33
std::string_view sv
Definition Tokeniser.hpp:26
constexpr auto bind() noexcept
Definition Slot.hpp:92
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))