|
1 | 1 | #include "yaml-cpp/yaml.h" // IWYU pragma: keep |
| 2 | +#include <fstream> |
2 | 3 |
|
3 | 4 | #include "gtest/gtest.h" |
4 | 5 |
|
@@ -78,6 +79,45 @@ TEST(LoadNodeTest, IterateMap) { |
78 | 79 | EXPECT_EQ(3, i); |
79 | 80 | } |
80 | 81 |
|
| 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 | + |
81 | 121 | #ifdef BOOST_FOREACH |
82 | 122 | TEST(LoadNodeTest, ForEach) { |
83 | 123 | Node node = Load("[1, 3, 5, 7]"); |
|
0 commit comments