|
6 | 6 | #include <gtest/gtest.h> |
7 | 7 |
|
8 | 8 | #include <formats/common/value_test.hpp> |
| 9 | +#include <userver/formats/common/utils.hpp> |
9 | 10 | #include <userver/formats/json/serialize.hpp> |
10 | 11 | #include <userver/formats/json/value.hpp> |
| 12 | +#include <userver/formats/json/value_builder.hpp> |
11 | 13 | #include <userver/formats/yaml/serialize.hpp> |
12 | 14 | #include <userver/formats/yaml/value.hpp> |
13 | 15 | #include <userver/formats/yaml/value_builder.hpp> |
@@ -869,4 +871,40 @@ bar: baz |
869 | 871 | ::unsetenv("ANOTHER_ENV_VARIABLE"); |
870 | 872 | } |
871 | 873 |
|
| 874 | +template <typename Value> |
| 875 | +std::size_t CountDepth(const Value& value) { |
| 876 | + std::size_t depth = 0; |
| 877 | + auto value_ref = value; |
| 878 | + while (value_ref.IsObject()) { |
| 879 | + value_ref = value_ref["obj"]; |
| 880 | + ++depth; |
| 881 | + } |
| 882 | + return depth; |
| 883 | +} |
| 884 | + |
| 885 | +TEST(YamlConfig, DeepYamlToJson) { |
| 886 | + constexpr std::size_t kDepth = 30; |
| 887 | + std::vector<std::string> path(kDepth, "obj"); |
| 888 | + path.push_back("key"); |
| 889 | + |
| 890 | + formats::yaml::ValueBuilder yaml_builder; |
| 891 | + formats::common::SetAtPath(yaml_builder, std::vector(path), formats::yaml::ValueBuilder("value").ExtractValue()); |
| 892 | + const auto node = yaml_builder.ExtractValue(); |
| 893 | + |
| 894 | + formats::json::ValueBuilder expected_json_builder; |
| 895 | + formats::common::SetAtPath( |
| 896 | + expected_json_builder, std::vector(path), formats::json::ValueBuilder("value").ExtractValue() |
| 897 | + ); |
| 898 | + const auto expected_json = expected_json_builder.ExtractValue(); |
| 899 | + |
| 900 | + const yaml_config::YamlConfig yaml{node, {}}; |
| 901 | + const auto json = yaml.As<formats::json::Value>(); |
| 902 | + |
| 903 | + EXPECT_EQ(json, expected_json) << "Actual json value:\n" |
| 904 | + << ToString(json) << "\n actual json depth: " << CountDepth(json) |
| 905 | + << "\n expected json value:\n" |
| 906 | + << ToString(expected_json) |
| 907 | + << "\n expected json depth: " << CountDepth(expected_json); |
| 908 | +} |
| 909 | + |
872 | 910 | USERVER_NAMESPACE_END |
0 commit comments