Skip to content

Commit

Permalink
add integration tests & fix logger segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
jayv committed Oct 22, 2024
1 parent 5644946 commit 0619ab6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
12 changes: 8 additions & 4 deletions include/flexi_cfg/config/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include "flexi_cfg/utils.h"
#include "flexi_cfg/logger.h"
#include "flexi_cfg/utils.h"

#define DEBUG_CLASSES 0
#define PRINT_SRC 0 // NOLINT(cppcoreguidelines-macro-usage)
Expand Down Expand Up @@ -178,10 +179,13 @@ class ConfigList : public ConfigBaseClonable<ConfigValue, ConfigList> {

void stream(std::ostream& os) const override {
os << "[";
for (size_t i = 0; i < data.size() - 1; ++i) {
os << data[i] << ", ";
for (auto it = data.begin(); it != data.end(); it = std::next(it)) {
os << *it;
if (std::next(it) != data.end()) {
os << ", ";
}
}
os << data.back() << "]";
os << "]";
}

std::vector<std::shared_ptr<ConfigBase>> data;
Expand Down
43 changes: 31 additions & 12 deletions tests/config_parse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST_P(InputString, ParseTree) {
}

TEST_P(InputString, Parse) {
flexi_cfg::logger::setLevel(flexi_cfg::logger::Severity::INFO);
setLevel(flexi_cfg::logger::Severity::INFO);
auto parse = []() {
peg::memory_input in(GetParam(), "From content");
flexi_cfg::config::ActionData out;
Expand All @@ -39,7 +39,7 @@ TEST_P(InputString, Parse) {
}

TEST_P(InputString, Reader) {
flexi_cfg::logger::setLevel(flexi_cfg::logger::Severity::INFO);
setLevel(flexi_cfg::logger::Severity::INFO);
flexi_cfg::Reader cfg({}, ""); // Nominally, we wouldn't do this, but we need a mechanism to
// capture the output of 'parse' from within the "try/catch" block

Expand All @@ -63,8 +63,19 @@ TEST_P(InputString, Reader) {
EXPECT_EQ(cfg.getValue<bool>("test2.n_key"), true);
EXPECT_EQ(cfg.getType("test2.n_key"), flexi_cfg::config::types::Type::kBoolean);
EXPECT_TRUE(cfg.exists("test2.inner.list"));
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.list"), std::vector<int>({1, 2, 3, 4}));
EXPECT_EQ(cfg.getType("test2.inner.list"), flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.list"), std::vector({1, 2, 3, 4}));
EXPECT_TRUE(cfg.exists("test2.inner.emptyList"));
EXPECT_EQ(cfg.getType("test2.inner.emptyList"), flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.emptyList"), std::vector<int>({}));
EXPECT_TRUE(cfg.exists("test2.inner.listWithComment"));
EXPECT_EQ(cfg.getType("test2.inner.listWithComment"), flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.listWithComment"), std::vector({0, 2}));
EXPECT_TRUE(cfg.exists("test2.inner.listWithTrailingComment"));
EXPECT_EQ(cfg.getType("test2.inner.listWithTrailingComment"),
flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.listWithTrailingComment"),
std::vector({0, 2}));
EXPECT_TRUE(cfg.exists("test2"));
EXPECT_EQ(cfg.getType("test2"), flexi_cfg::config::types::Type::kStruct);
EXPECT_TRUE(cfg.exists("test1"));
Expand Down Expand Up @@ -111,6 +122,15 @@ struct test2 {
struct inner {
list = [1, 2, 3, 4]
emptyList = []
listWithComment = [
# I don't matter
0, 2
]
listWithTrailingComment = [
0,
2# I don't matter
]
}
}
Expand Down Expand Up @@ -139,16 +159,15 @@ auto baseDir() -> const std::filesystem::path& {

auto filenameGenerator() -> std::vector<std::filesystem::path> {
std::vector<std::filesystem::path> files;
auto genFilename = [](std::size_t idx) -> std::filesystem::path {
return baseDir() / ("config_example" + std::to_string(idx) + ".cfg");
};
for (size_t idx = 1;; ++idx) {
const auto cfg_file = genFilename(idx);
if (!std::filesystem::exists(cfg_file)) {
break;
for (const auto& entry : std::filesystem::directory_iterator(baseDir())) {
if (entry.is_regular_file()) {
if (const auto& file = entry.path().filename().string();
file.starts_with("config_example") && file.ends_with(".cfg")) {
files.emplace_back(file);
}
}
files.emplace_back(cfg_file.filename());
}
std::ranges::sort(files);
return files;
}
} // namespace
Expand Down Expand Up @@ -189,7 +208,7 @@ TEST_P(FileInput, ConfigReaderParseRootDir) {
INSTANTIATE_TEST_SUITE_P(ConfigParse, FileInput, testing::ValuesIn(filenameGenerator()));

TEST(ConfigParse, ConfigRoot) {
flexi_cfg::logger::setLevel(flexi_cfg::logger::Severity::DEBUG);
setLevel(flexi_cfg::logger::Severity::DEBUG);
EXPECT_NO_THROW(flexi_cfg::Parser::parse(
std::filesystem::path("config_root/test/config_example_base.cfg"), baseDir()));
}

0 comments on commit 0619ab6

Please sign in to comment.