Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 1 | #include <gtest/gtest.h> |
| 2 | |
| 3 | #include <BaSyx/submodel/map_v2/common/ElementContainer.h> |
| 4 | #include <BaSyx/submodel/simple/common/ElementContainer.h> |
| 5 | #include <BaSyx/submodel/map_v2/submodelelement/SubmodelElement.h> |
| 6 | #include <BaSyx/submodel/map_v2/submodelelement/property/Property.h> |
| 7 | #include <BaSyx/submodel/simple/qualifier/Referable.h> |
| 8 | |
| 9 | using namespace basyx::submodel; |
| 10 | using namespace basyx::submodel::map; |
| 11 | using namespace basyx::submodel::api; |
| 12 | |
| 13 | using ImplTypes = ::testing::Types<map::ElementContainer<ISubmodelElement>, simple::ElementContainer<ISubmodelElement>>; |
| 14 | |
| 15 | template<class Impl> |
| 16 | class ElementContainerTest : public ::testing::Test { |
| 17 | protected: |
| 18 | using impl_t = Impl; |
| 19 | |
| 20 | protected: |
| 21 | std::unique_ptr<IElementContainer<ISubmodelElement>> elementContainer; |
| 22 | protected: |
| 23 | void SetUp() override |
| 24 | { |
| 25 | elementContainer = util::make_unique<Impl>(); |
| 26 | } |
| 27 | |
| 28 | void TearDown() override |
| 29 | { |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | TYPED_TEST_CASE(ElementContainerTest, ImplTypes); |
| 34 | |
| 35 | TYPED_TEST(ElementContainerTest, TestInit) |
| 36 | { |
| 37 | ASSERT_EQ(this->elementContainer->size(), 0); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | TYPED_TEST(ElementContainerTest, TestAdd) |
| 42 | { |
| 43 | auto prop = util::make_unique<Property<int>>( "testProperty" ); |
| 44 | prop->setValue(5); |
| 45 | |
| 46 | this->elementContainer->addElement(std::move(prop)); |
| 47 | ASSERT_EQ(this->elementContainer->size(), 1); |
| 48 | |
| 49 | auto submodelElement = this->elementContainer->getElement("testProperty"); |
| 50 | auto propB = dynamic_cast<map::Property<int>*>(submodelElement); |
| 51 | ASSERT_NE(propB, nullptr); |
| 52 | ASSERT_EQ(propB->getValue(), 5); |
| 53 | }; |
| 54 | |
| 55 | TYPED_TEST(ElementContainerTest, TestCreate) |
| 56 | { |
| 57 | auto prop = this->elementContainer->template createElement<Property<int>>("testProperty"); |
| 58 | ASSERT_EQ(this->elementContainer->size(), 1); |
| 59 | prop->setValue(5); |
| 60 | |
| 61 | auto submodelElement = this->elementContainer->getElement("testProperty"); |
| 62 | auto propB = dynamic_cast<map::Property<int>*>(submodelElement); |
| 63 | ASSERT_NE(propB, nullptr); |
| 64 | ASSERT_EQ(propB->getValue(), 5); |
| 65 | }; |
| 66 | |
| 67 | /********************************************************* |
| 68 | * Map implementation tests |
| 69 | *********************************************************/ |
| 70 | |
| 71 | class MapElementContainerTest : public ::testing::Test { |
| 72 | protected: |
| 73 | map::ElementContainer<ISubmodelElement> elementContainer; |
| 74 | protected: |
| 75 | void SetUp() override |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | void TearDown() override |
| 80 | { |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | TEST_F(MapElementContainerTest, TestMapImplementationCreateDeleteAfterwards) |
| 85 | { |
| 86 | auto prop = this->elementContainer.createElement<Property<int>>("testProperty"); |
| 87 | ASSERT_EQ(this->elementContainer.size(), 1); |
| 88 | prop->setValue(5); |
| 89 | |
| 90 | // Delete in map |
| 91 | auto & objectList = this->elementContainer.getMap().Get<basyx::object::object_list_t&>(); |
| 92 | objectList.clear(); |
| 93 | ASSERT_EQ(this->elementContainer.size(), 0); |
| 94 | auto prop_b = this->elementContainer.getElement("testProperty"); |
| 95 | ASSERT_EQ(prop_b, nullptr); |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | /********************************************************* |
| 100 | * Simple implementation tests |
| 101 | *********************************************************/ |
| 102 | class SimpleElementContainerTest : public ::testing::Test { |
| 103 | protected: |
| 104 | simple::ElementContainer<IReferable> elementContainer; |
| 105 | protected: |
| 106 | void SetUp() override |
| 107 | {} |
| 108 | |
| 109 | void TearDown() override |
| 110 | {} |
| 111 | }; |
| 112 | |
| 113 | TEST_F(SimpleElementContainerTest, TestSimpleImplementationCopy) |
| 114 | { |
| 115 | // std::unique_ptr<IReferable> ref = util::make_unique<simple::Referable>("testRef"); |
| 116 | // ref->setCategory("testCategory"); |
| 117 | // this->elementContainer.addElement(std::unique_ptr<IReferable>(ref.release())); |
| 118 | // ASSERT_EQ(this->elementContainer.size(), 1); |
| 119 | // |
| 120 | // simple::ElementContainer<IReferable> elementContainer2(std::move(elementContainer)); |
| 121 | // auto prop2 = dynamic_cast<simple::Referable*>(elementContainer2.getElement("testRef")); |
| 122 | // ASSERT_EQ(elementContainer2->size(), 1); |
| 123 | // ASSERT_EQ(prop2->getCategory(), std::string("testCategory")); |
| 124 | }; |
| 125 | |