blob: 1a74c7964709f061875818c0ce65f667c4f4c2c3 [file] [log] [blame]
#include <boost/container/flat_map.hpp>
#include <sdbusplus/unpack_properties.hpp>
#include <gmock/gmock.h>
namespace sdbusplus
{
struct ThrowingUnpack
{
template <typename... Args>
bool operator()(Args&&... args) const
{
unpackProperties(std::forward<Args>(args)...);
return false;
}
};
struct NonThrowingUnpack
{
struct UnpackError
{
UnpackError(sdbusplus::UnpackErrorReason r, const std::string& p) :
reason(r), property(p){};
sdbusplus::UnpackErrorReason reason;
std::string property;
};
template <typename... Args>
std::optional<UnpackError> operator()(Args&&... args) const
{
std::optional<UnpackError> error;
unpackPropertiesNoThrow(
[&error](sdbusplus::UnpackErrorReason reason,
const std::string& property) {
error.emplace(reason, property);
},
std::forward<Args>(args)...);
return error;
}
};
template <typename A, typename B>
struct TestingTypes
{
using SystemUnderTest = A;
using Container = B;
};
using VariantType = std::variant<std::string, uint32_t, float, double>;
using ContainerTypes = testing::Types<
TestingTypes<NonThrowingUnpack,
std::vector<std::pair<std::string, VariantType>>>,
TestingTypes<ThrowingUnpack,
std::vector<std::pair<std::string, VariantType>>>>;
template <typename Exception, typename F>
std::optional<Exception> captureException(F&& code)
{
try
{
code();
}
catch (const Exception& e)
{
return e;
}
return std::nullopt;
}
template <typename Params>
struct UnpackPropertiesTest : public testing::Test
{
void SetUp() override
{
using namespace std::string_literals;
data.insert(data.end(),
std::make_pair("Key-1"s, VariantType("string"s)));
data.insert(data.end(), std::make_pair("Key-2"s, VariantType(42.f)));
data.insert(data.end(), std::make_pair("Key-3"s, VariantType(15.)));
}
typename Params::Container data;
typename Params::SystemUnderTest unpackPropertiesCall;
};
TYPED_TEST_SUITE(UnpackPropertiesTest, ContainerTypes);
TYPED_TEST(UnpackPropertiesTest, returnsValueWhenKeyIsPresentAndTypeMatches)
{
using namespace testing;
std::string val1;
float val2 = 0.f;
double val3 = 0.;
EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2",
val2, "Key-3", val3));
ASSERT_THAT(val1, Eq("string"));
ASSERT_THAT(val2, FloatEq(42.f));
ASSERT_THAT(val3, DoubleEq(15.));
}
TYPED_TEST(UnpackPropertiesTest,
unpackDoesntChangeOriginalDataWhenPassedAsNonConstReference)
{
using namespace testing;
std::string val1, val2;
EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1));
EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val2));
ASSERT_THAT(val1, Eq("string"));
ASSERT_THAT(val2, Eq("string"));
}
TYPED_TEST(UnpackPropertiesTest, doesntReportMissingPropertyForOptional)
{
using namespace testing;
using namespace std::string_literals;
std::optional<std::string> val1;
std::optional<std::string> val4;
EXPECT_FALSE(
this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-4", val4));
ASSERT_THAT(val1, Eq("string"));
ASSERT_THAT(val4, Eq(std::nullopt));
}
TYPED_TEST(UnpackPropertiesTest, setPresentPointersOnSuccess)
{
using namespace testing;
using namespace std::string_literals;
const std::string* val1 = nullptr;
const float* val2 = nullptr;
const double* val3 = nullptr;
const std::string* val4 = nullptr;
EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2",
val2, "Key-3", val3, "Key-4",
val4));
ASSERT_TRUE(val1 && val2 && val3);
ASSERT_TRUE(!val4);
ASSERT_THAT(*val1, Eq("string"));
ASSERT_THAT(*val2, FloatEq(42.f));
ASSERT_THAT(*val3, DoubleEq(15.));
}
template <typename Params>
struct UnpackPropertiesThrowingTest : public UnpackPropertiesTest<Params>
{};
using ContainerTypesThrowing = testing::Types<TestingTypes<
ThrowingUnpack, std::vector<std::pair<std::string, VariantType>>>>;
TYPED_TEST_SUITE(UnpackPropertiesThrowingTest, ContainerTypesThrowing);
TYPED_TEST(UnpackPropertiesThrowingTest, throwsErrorWhenKeyIsMissing)
{
using namespace testing;
std::string val1;
float val2 = 0.f;
double val3 = 0.;
auto error = captureException<exception::UnpackPropertyError>([&] {
this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-4", val2,
"Key-3", val3);
});
ASSERT_TRUE(error);
ASSERT_THAT(error->reason, Eq(UnpackErrorReason::missingProperty));
ASSERT_THAT(error->propertyName, Eq("Key-4"));
}
TYPED_TEST(UnpackPropertiesThrowingTest, throwsErrorWhenTypeDoesntMatch)
{
using namespace testing;
std::string val1;
std::string val2;
double val3 = 0.;
auto error = captureException<exception::UnpackPropertyError>([&] {
this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2", val2,
"Key-3", val3);
});
ASSERT_TRUE(error);
ASSERT_THAT(error->reason, Eq(UnpackErrorReason::wrongType));
ASSERT_THAT(error->propertyName, Eq("Key-2"));
}
TYPED_TEST(UnpackPropertiesThrowingTest, throwsErrorWhenOptionalTypeDoesntMatch)
{
using namespace testing;
std::optional<std::string> val1;
std::optional<std::string> val2;
auto error = captureException<exception::UnpackPropertyError>([&] {
this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2", val2);
});
ASSERT_TRUE(error);
ASSERT_THAT(error->reason, Eq(UnpackErrorReason::wrongType));
ASSERT_THAT(error->propertyName, Eq("Key-2"));
}
template <typename Params>
struct UnpackPropertiesNonThrowingTest : public UnpackPropertiesTest<Params>
{};
using ContainerTypesNonThrowing = testing::Types<TestingTypes<
NonThrowingUnpack, std::vector<std::pair<std::string, VariantType>>>>;
TYPED_TEST_SUITE(UnpackPropertiesNonThrowingTest, ContainerTypesNonThrowing);
TYPED_TEST(UnpackPropertiesNonThrowingTest, ErrorWhenKeyIsMissing)
{
using namespace testing;
std::string val1;
float val2 = 0.f;
double val3 = 0.;
auto badProperty = this->unpackPropertiesCall(this->data, "Key-1", val1,
"Key-4", val2, "Key-3", val3);
ASSERT_TRUE(badProperty);
EXPECT_THAT(badProperty->reason, Eq(UnpackErrorReason::missingProperty));
EXPECT_THAT(badProperty->property, Eq("Key-4"));
}
TYPED_TEST(UnpackPropertiesNonThrowingTest, ErrorWhenTypeDoesntMatch)
{
using namespace testing;
std::string val1;
std::string val2;
double val3 = 0.;
auto badProperty = this->unpackPropertiesCall(this->data, "Key-1", val1,
"Key-2", val2, "Key-3", val3);
ASSERT_TRUE(badProperty);
EXPECT_THAT(badProperty->reason, Eq(UnpackErrorReason::wrongType));
EXPECT_THAT(badProperty->property, Eq("Key-2"));
}
TYPED_TEST(UnpackPropertiesNonThrowingTest, ErrorWhenOptionalTypeDoesntMatch)
{
using namespace testing;
std::optional<std::string> val1;
std::optional<std::string> val2;
auto badProperty = this->unpackPropertiesCall(this->data, "Key-1", val1,
"Key-2", val2);
ASSERT_TRUE(badProperty);
EXPECT_THAT(badProperty->reason, Eq(UnpackErrorReason::wrongType));
EXPECT_THAT(badProperty->property, Eq("Key-2"));
}
template <typename Params>
struct UnpackPropertiesTest_ForVector : public UnpackPropertiesTest<Params>
{};
using ContainerTypesVector = testing::Types<
TestingTypes<NonThrowingUnpack,
std::vector<std::pair<std::string, VariantType>>>,
TestingTypes<ThrowingUnpack,
std::vector<std::pair<std::string, VariantType>>>>;
TYPED_TEST_SUITE(UnpackPropertiesTest_ForVector, ContainerTypesVector);
TYPED_TEST(UnpackPropertiesTest_ForVector, silentlyDiscardsDuplicatedKeyInData)
{
using namespace testing;
using namespace std::string_literals;
std::string val1;
float val2 = 0.f;
double val3 = 0.;
this->data.insert(this->data.end(),
std::make_pair("Key-1"s, VariantType("string2"s)));
EXPECT_FALSE(this->unpackPropertiesCall(this->data, "Key-1", val1, "Key-2",
val2, "Key-3", val3));
ASSERT_THAT(val1, Eq("string"));
ASSERT_THAT(val2, FloatEq(42.f));
ASSERT_THAT(val3, DoubleEq(15.));
}
} // namespace sdbusplus