Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
fixtures.hh
Go to the documentation of this file.
1#ifndef UTOPIA_CORE_TESTTOOLS_FIXTURES_HH
2#define UTOPIA_CORE_TESTTOOLS_FIXTURES_HH
3
4#include <memory>
5#include <string_view>
6#include <random>
7
8#include <yaml-cpp/yaml.h>
9#include <spdlog/spdlog.h>
10#include <spdlog/sinks/stdout_color_sinks.h>
11
12#include "../types.hh"
13#include "../../data_io/cfg_utils.hh"
14
20namespace Utopia::TestTools {
21
23
26template<class RNGType=Utopia::DefaultRNG>
29 using RNG = RNGType;
30
33
35
40 const Config cfg;
41
43 std::shared_ptr<spdlog::logger> log;
44
46 std::shared_ptr<RNG> rng;
47
49 BaseInfrastructure (const std::string& config_file_path = "")
50 :
51 // Load a configuration file, if a file path was given
53 if (config_file_path.size()) {
54 return YAML::LoadFile(config_file_path);
55 }
56 return Config{};
57 }()),
58
59 // Set up a test logger
60 log([](){
61 auto logger = spdlog::get("test");
62
63 // Create it only if it does not already exist
64 if (not logger) {
65 logger = spdlog::stdout_color_mt("test");
66 }
67
68 // Set level and global logging pattern
69 logger->set_level(spdlog::level::trace);
70 spdlog::set_pattern("[%T.%e] [%^%l%$] [%n] %v");
71 // "[HH:MM:SS.mmm] [level(colored)] [logger] <message>"
72
73 return logger;
74 }()),
75
76 // Set up random number generator (with random seed)
77 rng(std::make_shared<RNG>(std::random_device()()))
78 {
79 log->info("BaseInfrastructure fixture set up.");
80 if (config_file_path.size()) {
81 log->info("Test configuration loaded from: {}", config_file_path);
82 }
83 else {
84 log->info("No test configuration file loaded.");
85 }
86 }
87
90 spdlog::drop("test");
91 }
92};
93
94
95} // namespace Utopia::TestTools
96
97// end group TestTools
102#endif // UTOPIA_CORE_TESTTOOLS_FIXTURES_HH
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:71
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
Definition testtools.hh:15
A base class for an infrastructure fixture.
Definition fixtures.hh:27
DataIO::Config Config
Make the config type more easily available.
Definition fixtures.hh:32
const Config cfg
The test configuration.
Definition fixtures.hh:40
std::shared_ptr< spdlog::logger > log
A logger to use during the test or to pass to entities that need it.
Definition fixtures.hh:43
BaseInfrastructure(const std::string &config_file_path="")
Construct the BaseInfrastructure fixture.
Definition fixtures.hh:49
~BaseInfrastructure()
Destruct the BaseInfrastructure fixture, tearing down the test logger.
Definition fixtures.hh:89
RNGType RNG
The random number generator type to use.
Definition fixtures.hh:29
std::shared_ptr< RNG > rng
The shared random number generator instance, randomly seeded.
Definition fixtures.hh:46