netxsimdg
Loading...
Searching...
No Matches
ConfiguredModule_test.cpp
1/*
2 * @file ConfiguredModule_test.cpp
3 *
4 * @date Oct 21, 2021
5 * @author Tim Spain <timothy.spain@nersc.no>
6 */
7
9
10#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
11#include <doctest/doctest.h>
12
13#include "ArgV.hpp"
14#include "moduleTestClasses.hpp"
16#include "include/Module.hpp"
17
18#include <istream>
19#include <memory>
20
21// Module classes for the test classes
22namespace Module {
23class ITestModule : public Module<ITest>
24{
25};
26
27const std::string IMPL1 = "Impl1";
28const std::string IMPL2 = "Impl2";
29
30template <>
31Module<ITest>::map Module<ITest>::functionMap = {
32 {IMPL1, newImpl<ITest, Impl1>},
33 {IMPL2, newImpl<ITest, Impl2>},
34};
35template <> Module<ITest>::fn Module<ITest>::spf = functionMap.at(IMPL1);
36template <> std::unique_ptr<ITest> Module<ITest>::staticInstance = std::move(Module<ITest>::spf());
37template <> std::string Module<ITest>::moduleName() { return "ITest"; };
38template <> std::unique_ptr<ITest> getInstance<ITest>() { return getInstTemplate<ITest, ITestModule>(); };
39template <> ITest& getImplementation<ITest>() { return getImplTemplate<ITest, ITestModule>(); };
40template <> void setImplementation<ITest>(const std::string& implName)
41{
42 setImplTemplate<ITestModule>(implName);
43};
44}
45
46namespace Nextsim {
47
48TEST_SUITE_BEGIN("ConfiguredModule");
49TEST_CASE("Configure a module")
50{
52
53 // Create the fake command line, selecting the Impl1 implementation
54 ArgV argvee({"cmtest", "--Modules.ITest=Impl1"});
55
56 Configurator::setCommandLine(argvee.argc(), argvee());
57
59 {Module::Module<ITest>::moduleName(), {Module::setImplementation<ITest>, Module::implementation<ITest>}},
60 });
62
63 ITest& impler = Module::getImplementation<ITest>();
64
65 REQUIRE(impler() == 1);
66}
67
68TEST_CASE("Configure a module from a stream")
69{
71 std::stringstream config;
72 config << "[Modules]" << std::endl
73 << "ITest = Impl2" << std::endl;
74
75 std::unique_ptr<std::istream> pcstream(new std::stringstream(config.str()));
76 Configurator::addStream(std::move(pcstream));
77
79 {Module::Module<ITest>::moduleName(), {Module::setImplementation<ITest>, Module::implementation<ITest>}},
80 });
82
83 ITest& impler = Module::getImplementation<ITest>();
84 REQUIRE(impler() == 2);
85}
86
87TEST_CASE("Don't configure a module from a stream")
88{
90 std::stringstream config;
91 config << "[Modules]" << std::endl
92 << "ITestNotReally = NotImpl2" << std::endl;
93
94 Module::setImplementation<ITest>("Impl2");
95
96 std::unique_ptr<std::istream> pcstream(new std::stringstream(config.str()));
97 Configurator::addStream(std::move(pcstream));
98
100 {Module::Module<ITest>::moduleName(), {Module::setImplementation<ITest>, Module::implementation<ITest>}},
101 });
102 // Parse the available modules. This should not change the implementation
103 // to the default.
105
106 ITest& impler = Module::getImplementation<ITest>();
107 REQUIRE(impler() == 2);
108}
109
110TEST_CASE("Configure a module with an incorrect name")
111{
113 std::stringstream config;
114 config << "[Modules]" << std::endl
115 << "ITest = Optometry" << std::endl;
116
117 std::unique_ptr<std::istream> pcstream(new std::stringstream(config.str()));
118 Configurator::addStream(std::move(pcstream));
119
121 {Module::Module<ITest>::moduleName(), {Module::setImplementation<ITest>, Module::implementation<ITest>}},
122 });
123 // Should throw a domain_error as "Optometry" is not a valid implementation.
124 REQUIRE_THROWS(ConfiguredModule::parseConfigurator());
125
126}
127TEST_SUITE_END();
128
129}
static void addStream(std::unique_ptr< std::istream > pis)
Adds a istream source of configuration data.
static void setCommandLine(int argc, char *argv[])
Sets the command line data to be parsed.
static void parseConfigurator()
Parse the configuration for all of the modules defined in ModuleLoader.
static void setConfiguredModules(const map &ls)