Skip to content

Commit 341827f

Browse files
committed
unittest for loading / accessing aliases
1 parent 562aefc commit 341827f

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

test/integration/alias.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
A: &DEFAULT
2+
str: string
3+
float: 3.1415
4+
int: 42
5+
6+
B: *DEFAULT

test/integration/load_node_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "yaml-cpp/yaml.h" // IWYU pragma: keep
2+
#include <fstream>
23

34
#include "gtest/gtest.h"
45

@@ -78,6 +79,45 @@ TEST(LoadNodeTest, IterateMap) {
7879
EXPECT_EQ(3, i);
7980
}
8081

82+
static void alias_access(Node&& doc) {
83+
Node A = doc["A"];
84+
Node B = doc["B"];
85+
86+
// A and B have the same content
87+
ASSERT_TRUE(A);
88+
ASSERT_TRUE(A.IsMap());
89+
ASSERT_TRUE(B);
90+
ASSERT_TRUE(B.IsMap());
91+
92+
// A and B have the same content
93+
std::map<std::string, std::string> values = {{"str", "string"}, {"float", "3.1415"}, {"int", "42"}};
94+
for (YAML::const_iterator it = A.begin(); it != A.end(); ++it) {
95+
const std::string& key = it->first.as<std::string>();
96+
SCOPED_TRACE("key " + key);
97+
Node a = A[key];
98+
Node b = B[key];
99+
EXPECT_TRUE(a);
100+
EXPECT_TRUE(b);
101+
EXPECT_TRUE(a.IsScalar());
102+
EXPECT_TRUE(b.IsScalar());
103+
104+
// a and b should be identical
105+
EXPECT_STREQ(a.as<std::string>().c_str(), b.as<std::string>().c_str());
106+
// ... and have the values given in the map
107+
EXPECT_STREQ(a.as<std::string>().c_str(), values[key].c_str());
108+
}
109+
}
110+
111+
TEST(LoadNodeTest, AliasAccess) {
112+
alias_access(Load("{A: &DEFAULT {str: string, int: 42, float: 3.1415}, B: *DEFAULT}"));
113+
}
114+
115+
TEST(LoadNodeTest, AliasAccessStream) {
116+
std::ifstream input("integration/alias.yaml");
117+
ASSERT_TRUE(input.good());
118+
alias_access(Load(input));
119+
}
120+
81121
#ifdef BOOST_FOREACH
82122
TEST(LoadNodeTest, ForEach) {
83123
Node node = Load("[1, 3, 5, 7]");

0 commit comments

Comments
 (0)