#include #include #include namespace sdbusplus { struct ThrowingUnpack { template bool operator()(Args&&... args) const { unpackProperties(std::forward(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 std::optional operator()(Args&&... args) const { std::optional error; unpackPropertiesNoThrow( [&error](sdbusplus::UnpackErrorReason reason, const std::string& property) { error.emplace(reason, property); }, std::forward(args)...); return error; } }; template struct TestingTypes { using SystemUnderTest = A; using Container = B; }; using VariantType = std::variant; using ContainerTypes = testing::Types< TestingTypes>>, TestingTypes>>>; template std::optional captureException(F&& code) { try { code(); } catch (const Exception& e) { return e; } return std::nullopt; } template 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 val1; std::optional 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 struct UnpackPropertiesThrowingTest : public UnpackPropertiesTest {}; using ContainerTypesThrowing = testing::Types>>>; 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([&] { 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([&] { 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 val1; std::optional val2; auto error = captureException([&] { 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 struct UnpackPropertiesNonThrowingTest : public UnpackPropertiesTest {}; using ContainerTypesNonThrowing = testing::Types>>>; 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 val1; std::optional 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 struct UnpackPropertiesTest_ForVector : public UnpackPropertiesTest {}; using ContainerTypesVector = testing::Types< TestingTypes>>, TestingTypes>>>; 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