Skip to content

Commit 59e3c5c

Browse files
author
root
committed
done
1 parent 1743b0b commit 59e3c5c

5 files changed

Lines changed: 47 additions & 13 deletions

File tree

source/parser/config_reader/config_node_.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ std::runtime_error ConfigNode::create_parsing_error(
3333
return std::runtime_error(ss.str());
3434
}
3535

36-
const std::optional<std::string>& ConfigNode::get_path_node() {
36+
const std::optional<std::string>& ConfigNode::get_path_node() const{
3737
return m_path_node;
3838
}
3939

@@ -129,7 +129,10 @@ ConfigNode::Iterator ConfigNode::end() const { return Iterator(m_node.end(), thi
129129

130130
ConfigNodeExpected ConfigNode::operator[](std::string_view key) const {
131131
const YAML::Node child_node = m_node[key];
132-
const std::string current_path = m_path_node.value() + "\\" + std::string(key);
132+
std::string current_path = std::string(key);
133+
if (m_path_node.has_value()){
134+
current_path = m_path_node.value() + "\\" + std::string(key);
135+
}
133136
if (!child_node) {
134137
std::stringstream ss;
135138
ss << "Key error: node " << *this << "which is located by path:" << current_path << ":\n";
@@ -146,7 +149,7 @@ ConfigNodeExpected ConfigNode::operator[](std::string_view key) const {
146149
};
147150

148151
ConfigNode load_file(std::filesystem::path path) {
149-
return ConfigNode(YAML::LoadFile(path.string()), path.string());
152+
return ConfigNode(YAML::LoadFile(path.string()), std::nullopt, path.string());
150153
}
151154

152155
} // namespace sim

source/parser/config_reader/config_node_.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ConfigNode {
3030
[[nodiscard]] std::runtime_error create_parsing_error(
3131
std::string_view error) const;
3232

33-
const std::optional<std::string>& get_path_node();
33+
const std::optional<std::string>& get_path_node() const;
3434

3535
friend std::ostream& operator<<(std::ostream& out, const ConfigNode& node);
3636

@@ -98,7 +98,7 @@ class ConfigNode {
9898

9999
const std::optional<std::string> m_name;
100100

101-
std::optional<std::string> m_path_node;
101+
mutable std::optional<std::string> m_path_node;
102102
};
103103

104104
ConfigNode load_file(std::filesystem::path path);

source/parser/config_reader/config_node_with_preset_.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ std::ostream& operator<<(std::ostream& out, const ConfigNodeWithPreset& node) {
8484
return out << node.get_node();
8585
}
8686

87+
const std::optional<std::string>& ConfigNodeWithPreset::get_path_node() const{
88+
return m_node.get_path_node();
89+
}
90+
8791
const std::string& ConfigNodeWithPreset::get_name_or_throw() const {
8892
return m_node.get_name_or_throw();
8993
}

source/parser/config_reader/config_node_with_preset_.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ConfigNodeWithPreset {
3636
return as<T>().value_or_throw();
3737
}
3838

39+
const std::optional<std::string>& get_path_node() const;
40+
3941
[[nodiscard]] const std::string& get_name_or_throw() const;
4042

4143
const std::optional<ConfigNode> get_presets_node() const noexcept;

test/parser/topology/check_paths.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#pragma once
2-
31
#include <gtest/gtest.h>
42

53
#include "parser/config_reader/config_node_with_preset.hpp"
@@ -16,9 +14,9 @@ TEST(PathConfigNode, InsertedPath){
1614
)");
1715
ConfigNode node(root, std::nullopt, "root");
1816
auto a = node["a"].value();
19-
auto b = node["b"].value();
20-
auto c = node["c"].value();
21-
ASSERT_EQ(a.get_path_node(), "root\\a");
17+
auto b = a["b"].value();
18+
auto c = b["c"].value();
19+
2220
ASSERT_EQ(b.get_path_node(), "root\\a\\b");
2321
ASSERT_EQ(c.get_path_node(), "root\\a\\b\\c");
2422
}
@@ -27,9 +25,36 @@ TEST(PathConfigNode, MissingKey){
2725
std::filesystem::path bus_topology_path =
2826
std::filesystem::path(__FILE__).parent_path() /
2927
"bus_network.yml";
30-
ConfigNode node = load_file(path);
31-
auto result = node["connection-1->1"].value();
32-
ASSERT_EQ(result.get_path_node(), bus_topology_path.string() + "connections\\connection-1->1");
28+
ConfigNode node = load_file(bus_topology_path);
29+
auto result = node["connections"]["connection-1->1"].value();
30+
31+
ASSERT_EQ(result.get_path_node(), bus_topology_path.string() + "\\connections\\connection-1->1");
32+
}
33+
34+
TEST(PathConfigNodeWithPreset, InsertedPath){
35+
YAML::Node root = YAML::Load(R"(
36+
a:
37+
b:
38+
c: 1
39+
)");
40+
ConfigNode tmp(root, std::nullopt, "root");
41+
ConfigNodeWithPreset node(tmp);
42+
auto a = node["a"].value();
43+
auto b = a["b"].value();
44+
auto c = b["c"].value();
45+
46+
ASSERT_EQ(b.get_path_node(), "root\\a\\b");
47+
ASSERT_EQ(c.get_path_node(), "root\\a\\b\\c");
48+
}
49+
50+
TEST(PathConfigNodeWithPreset, MissingKey){
51+
std::filesystem::path bus_topology_path =
52+
std::filesystem::path(__FILE__).parent_path() /
53+
"bus_network.yml";
54+
ConfigNodeWithPreset node = load_file_with_presets(bus_topology_path);
55+
auto result = node["connections"]["connection-1->1"].value();
56+
57+
ASSERT_EQ(result.get_path_node(), bus_topology_path.string() + "\\connections\\connection-1->1");
3358
}
3459

3560
}

0 commit comments

Comments
 (0)