mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
refactor: Replace all old instances of Config with New Config (#1627)
Fixes #1184 Previous PR's found [here](https://github.com/XRPLF/clio/pull/1593) and [here](https://github.com/XRPLF/clio/pull/1544)
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "util/newconfig/ConfigConstraints.hpp"
|
||||
#include "util/newconfig/ConfigDefinition.hpp"
|
||||
#include "util/newconfig/ConfigValue.hpp"
|
||||
#include "util/newconfig/FakeConfigData.hpp"
|
||||
@@ -26,6 +27,8 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
using namespace util::config;
|
||||
|
||||
@@ -45,7 +48,7 @@ TEST_F(ValueViewTest, ValueView)
|
||||
|
||||
TEST_F(ValueViewTest, DifferentIntegerTest)
|
||||
{
|
||||
auto const vv = configData.getValue("header.port");
|
||||
auto const vv = configData.getValueView("header.port");
|
||||
auto const uint32 = vv.asIntType<uint32_t>();
|
||||
auto const uint64 = vv.asIntType<uint64_t>();
|
||||
auto const int32 = vv.asIntType<int32_t>();
|
||||
@@ -65,27 +68,58 @@ TEST_F(ValueViewTest, DifferentIntegerTest)
|
||||
EXPECT_NEAR(doubleVal, sameDouble, precision);
|
||||
EXPECT_NEAR(floatVal, sameFloat, precision);
|
||||
|
||||
auto const ipVal = configData.getValue("ip");
|
||||
auto const ipVal = configData.getValueView("ip");
|
||||
auto const ipDouble = ipVal.asDouble();
|
||||
auto const ipFloat = ipVal.asFloat();
|
||||
EXPECT_NEAR(ipDouble, 444.22, precision);
|
||||
EXPECT_NEAR(ipFloat, 444.22f, precision);
|
||||
}
|
||||
|
||||
TEST_F(ValueViewTest, IntegerAsDoubleTypeValue)
|
||||
{
|
||||
auto const cv = ConfigValue{ConfigType::Double}.defaultValue(432).withConstraint(validatePositiveDouble);
|
||||
ValueView const vv{cv};
|
||||
auto const doubleVal = vv.asFloat();
|
||||
auto const floatVal = vv.asDouble();
|
||||
|
||||
auto const precision = 1e-9;
|
||||
EXPECT_NEAR(doubleVal, 432, precision);
|
||||
EXPECT_NEAR(floatVal, 432, precision);
|
||||
}
|
||||
|
||||
TEST_F(ValueViewTest, OptionalValues)
|
||||
{
|
||||
auto const cv = ConfigValue{ConfigType::Integer}.defaultValue(432).optional();
|
||||
auto const cv2 = ConfigValue{ConfigType::Double}.optional();
|
||||
auto const cv3 = ConfigValue{ConfigType::String}.optional();
|
||||
auto const cv4 = ConfigValue{ConfigType::String}.defaultValue("hello").optional();
|
||||
|
||||
ValueView const vv{cv};
|
||||
ValueView const vv2{cv2};
|
||||
ValueView const vv3{cv3};
|
||||
ValueView const vv4{cv4};
|
||||
|
||||
EXPECT_EQ(vv.asOptional<uint32_t>().value(), 432);
|
||||
EXPECT_EQ(vv.asOptional<uint64_t>().value(), 432);
|
||||
EXPECT_EQ(vv2.asOptional<uint64_t>(), std::nullopt);
|
||||
EXPECT_EQ(vv3.asOptional<std::string>(), std::nullopt);
|
||||
EXPECT_EQ(vv4.asOptional<std::string>(), "hello");
|
||||
}
|
||||
|
||||
struct ValueDeathTest : ValueViewTest {};
|
||||
|
||||
TEST_F(ValueDeathTest, WrongTypes)
|
||||
{
|
||||
auto const vv = configData.getValue("header.port");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto a_ = vv.asBool(); }, ".*");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto a_ = vv.asString(); }, ".*");
|
||||
auto const vv = configData.getValueView("header.port");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv.asBool(); }, ".*");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv.asString(); }, ".*");
|
||||
|
||||
auto const cv = ConfigValue{ConfigType::Integer}.defaultValue(-5);
|
||||
auto const vv2 = ValueView(cv);
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto a_ = vv2.asIntType<uint32_t>(); }, ".*");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv2.asIntType<uint32_t>(); }, ".*");
|
||||
|
||||
auto const cv2 = ConfigValue{ConfigType::String}.defaultValue("asdf");
|
||||
auto const vv3 = ValueView(cv2);
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto a_ = vv3.asDouble(); }, ".*");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto a_ = vv3.asFloat(); }, ".*");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv3.asDouble(); }, ".*");
|
||||
EXPECT_DEATH({ [[maybe_unused]] auto unused = vv3.asFloat(); }, ".*");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user