//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2024, the clio developers. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include "util/newconfig/ConfigDefinition.hpp" #include "util/newconfig/ConfigValue.hpp" #include "util/newconfig/FakeConfigData.hpp" #include "util/newconfig/Types.hpp" #include "util/newconfig/ValueView.hpp" #include #include using namespace util::config; struct ValueViewTest : testing::Test { ClioConfigDefinition const configData = generateConfig(); }; TEST_F(ValueViewTest, ValueView) { ConfigValue const cv = ConfigValue{ConfigType::String}.defaultValue("value"); ValueView const vv = ValueView(cv); EXPECT_EQ("value", vv.asString()); EXPECT_EQ(ConfigType::String, vv.type()); EXPECT_EQ(true, vv.hasValue()); EXPECT_EQ(false, vv.isOptional()); } TEST_F(ValueViewTest, DifferentIntegerTest) { auto const vv = configData.getValue("header.port"); auto const uint32 = vv.asIntType(); auto const uint64 = vv.asIntType(); auto const int32 = vv.asIntType(); auto const int64 = vv.asIntType(); EXPECT_EQ(vv.asIntType(), uint32); EXPECT_EQ(vv.asIntType(), uint64); EXPECT_EQ(vv.asIntType(), int32); EXPECT_EQ(vv.asIntType(), int64); auto const doubleVal = vv.asIntType(); auto const floatVal = vv.asIntType(); auto const sameDouble = vv.asDouble(); auto const sameFloat = vv.asFloat(); auto const precision = 1e-9; EXPECT_NEAR(doubleVal, sameDouble, precision); EXPECT_NEAR(floatVal, sameFloat, precision); auto const ipVal = configData.getValue("ip"); auto const ipDouble = ipVal.asDouble(); auto const ipFloat = ipVal.asFloat(); EXPECT_NEAR(ipDouble, 444.22, precision); EXPECT_NEAR(ipFloat, 444.22f, precision); } 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 cv = ConfigValue{ConfigType::Integer}.defaultValue(-5); auto const vv2 = ValueView(cv); EXPECT_DEATH({ [[maybe_unused]] auto a_ = vv2.asIntType(); }, ".*"); 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(); }, ".*"); }