/* * Author: Patrick-Christopher Mattulat * Company: Lynar Studios * E-Mail: webmaster@lynarstudios.com * Created: 2023-02-12 * Changed: 2023-02-15 * * */ #include #include #include #include using namespace ls::std::core; using namespace ls::std::core::type; using namespace ls::std::io; using namespace ::std; namespace { class SerializableSectionPairRowTest : public ::testing::Test { protected: SerializableSectionPairRowTest() = default; ~SerializableSectionPairRowTest() override = default; void SetUp() override {} void TearDown() override {} }; TEST_F(SerializableSectionPairRowTest, constructor_no_reference) { EXPECT_THROW( { try { SerializableSectionPairRow serializable(nullptr); } catch (const IllegalArgumentException &_exception) { throw; } }, IllegalArgumentException); } TEST_F(SerializableSectionPairRowTest, getValue) { SerializableSectionPairRow serializable{make_shared("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE)}; ASSERT_TRUE(serializable.getValue() != nullptr); } TEST_F(SerializableSectionPairRowTest, marshal_single_value) { shared_ptr row = make_shared("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE); shared_ptr singleValue = dynamic_pointer_cast(row->getValue()); singleValue->set("blue"); SerializableSectionPairRow serializable{row}; byte_field expected = "favourite-color=blue" + NewLine::get(); byte_field actual = serializable.marshal(); ASSERT_STREQ(expected.c_str(), actual.c_str()); } TEST_F(SerializableSectionPairRowTest, marshal_list_value) { shared_ptr row = make_shared("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE); shared_ptr listValue = dynamic_pointer_cast(row->getValue()); listValue->add("blue"); listValue->add("red"); listValue->add("purple"); SerializableSectionPairRow serializable{row}; string expected = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get(); ASSERT_STREQ(expected.c_str(), serializable.marshal().c_str()); } TEST_F(SerializableSectionPairRowTest, unmarshal_single_value) { shared_ptr row = make_shared("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE); shared_ptr singleValue = dynamic_pointer_cast(row->getValue()); SerializableSectionPairRow serializable{row}; serializable.unmarshal("favourite-color=blue"); ASSERT_STREQ("favourite-color", row->getKey().c_str()); ASSERT_STREQ("blue", singleValue->get().c_str()); } TEST_F(SerializableSectionPairRowTest, unmarshal_list_value) { shared_ptr row = make_shared("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE); shared_ptr listValue = dynamic_pointer_cast(row->getValue()); SerializableSectionPairRow serializable{row}; string data = "favourite-colors:" + NewLine::get() + " blue" + NewLine::get() + " red" + NewLine::get() + " purple" + NewLine::get(); serializable.unmarshal(data); ASSERT_STREQ("favourite-colors", row->getKey().c_str()); ASSERT_EQ(3, listValue->getSize()); ASSERT_STREQ("blue", listValue->get(0).c_str()); ASSERT_STREQ("red", listValue->get(1).c_str()); ASSERT_STREQ("purple", listValue->get(2).c_str()); } }