17#include <boost/program_options.hpp>
19#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
20#include <doctest/doctest.h>
30 const std::string valueKey =
"config.value";
32 boost::program_options::options_description opt(
"Options");
33 opt.add_options()(valueKey.c_str(), boost::program_options::value<int>()->default_value(-1),
37 value = vm[valueKey].as<
int>();
40 bool checkValue(
int target) {
return value == target; }
54 int getValue() {
return value; }
55 std::string getName() {
return name; }
66 {Config2::VALUE_KEY,
"config.value"},
67 {Config2::NAME_KEY,
"config.name"},
95 int getValue() {
return value; }
96 double getWeight() {
return weight; }
107 {Config3::VALUE_KEY,
"config.value"},
108 {Config3::WEIGHT_KEY,
"data.weight"},
113 value = Configured::getConfiguration(
keyMap.at(VALUE_KEY), -1);
114 weight = Configured::getConfiguration(
keyMap.at(WEIGHT_KEY), 1.);
119TEST_SUITE_BEGIN(
"Configurator");
120TEST_CASE(
"Parse one config stream using the raw configurator")
127 std::stringstream text;
128 text <<
"[config]" << std::endl <<
"value = " << target << std::endl;
131 REQUIRE(config.checkValue(0));
135 REQUIRE(config.checkValue(-1));
137 std::unique_ptr<std::istream> pcstream(
new std::stringstream(text.str()));
144 REQUIRE(config.checkValue(target));
147TEST_CASE(
"Parse one config stream using the pointer configuration function")
155 std::string targetName =
"Zork";
156 std::stringstream text;
157 text <<
"[config]" << std::endl
158 <<
"value = " << target << std::endl
159 <<
"name = " << targetName << std::endl;
165 REQUIRE(config.getValue() == target);
166 REQUIRE(config.getName() == targetName);
169TEST_CASE(
"Parse two config streams for one class, reference helper function")
177 std::string targetName =
"Zork";
178 std::stringstream text1;
179 text1 <<
"[config]" << std::endl <<
"value = " << target << std::endl;
182 std::stringstream text2;
183 text2 <<
"[config]" << std::endl <<
"name = " << targetName << std::endl;
189 REQUIRE(config.getValue() == target);
190 REQUIRE(config.getName() == targetName);
193TEST_CASE(
"Parse config streams for two overlapping class, try")
204 std::string targetName =
"Zork II";
205 std::stringstream text1;
206 text1 <<
"[config]" << std::endl
207 <<
"value = " << target << std::endl
208 <<
"name = " << targetName << std::endl;
211 double targetWeight = 0.467836;
212 std::stringstream text2;
213 text2 <<
"[data]" << std::endl <<
"weight = " << targetWeight << std::endl;
217 REQUIRE(config.getValue() != target);
218 REQUIRE(config.getName() != targetName);
220 REQUIRE(confih.getValue() != target);
221 REQUIRE(confih.getWeight() != targetWeight);
226 REQUIRE(config.getValue() == target);
227 REQUIRE(config.getName() == targetName);
229 REQUIRE(confih.getValue() == target);
230 REQUIRE(confih.getWeight() == targetWeight);
void configure() override
The configuration function.
void configure() override
The configuration function.
static void addStream(std::unique_ptr< std::istream > pis)
Adds a istream source of configuration data.
static void clearStreams()
static boost::program_options::variables_map parse(const boost::program_options::options_description &opt)
Parses all configuration sources.