123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <string>
- #include <gmock/gmock.h>
- #include <gtest/gtest.h>
- #include <AzCore/RTTI/TypeInfo.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <ScenePopulation/ScenePopulation/Containers/DataObject.h>
- namespace Containers = AZ::ScenePopulation::Containers;
- class ComplexObject
- {
- public:
- AZ_RTTI(ComplexObject, "{1969D4A3-3714-4E1C-816E-E066538A2A0B}")
- ComplexObject()
- : m_value0(42)
- , m_value1("test")
- {}
- explicit ComplexObject(int value0)
- : m_value0(value0)
- , m_value1("test")
- {}
- ComplexObject(int value0, const std::string& value1)
- : m_value0(value0)
- , m_value1(value1)
- {}
- bool operator==(const ComplexObject& rhs) const
- {
- return m_value0 == rhs.m_value0 && m_value1 == rhs.m_value1;
- }
- int m_value0;
- std::string m_value1;
- };
- class DestructionClass
- {
- public:
- AZ_RTTI(DestructionClass, "{6EFE6C0C-80E8-4D62-9705-A7315049DFBF}")
- ~DestructionClass()
- {
- Destructor();
- }
- MOCK_METHOD0(Destructor, void());
- };
- class BaseInterface
- {
- public:
- AZ_RTTI(BaseInterface, "{723F701D-B718-432B-AE67-78F999A64883}")
- };
- class SecondInterface
- {
- public:
- AZ_RTTI(SecondInterface, "{8D0420F1-C102-4615-94F3-9D39E0D3A272}")
- };
- class SingleInheritance
- : public BaseInterface
- {
- public:
- AZ_RTTI(SingleInheritance, "{2D5EA157-8137-4FD4-A08D-58941FB149B2}", BaseInterface);
- };
- class MultipleInheritance
- : public BaseInterface
- , public SecondInterface
- {
- public:
- AZ_RTTI(MultipleInheritance, "{277E1CAA-EB89-4820-B37E-B49922FD0DF9}", BaseInterface, SecondInterface);
- };
- class ReflectionClass
- {
- public:
- AZ_RTTI(ReflectionClass, "{81E5CB64-B879-440F-9659-9D45AA544144}")
- uint32_t testVar;
- MOCK_METHOD0(Reflect, void());
- static ReflectionClass* currentTestClass;
- static void Reflect(AZ::ReflectContext* context)
- {
- AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
- if (serializeContext)
- {
- serializeContext->
- Class<ReflectionClass>()->
- Version(1)->
- Field("testVar", &ReflectionClass::testVar);
- }
- currentTestClass->Reflect();
- }
- };
- ReflectionClass* ReflectionClass::currentTestClass = nullptr;
- class DataObjectTest
- : public ::testing::Test
- {
- public:
- DataObjectTest()
- {
- }
- };
- namespace TestNamespace
- {
- class TestClassInNamespace
- {
- public:
- AZ_RTTI(TestClassInNamespace, "{F6CF64ED-6255-4E93-A34D-8BE58546C12E}")
- };
- }
- // Create
- TEST_F(DataObjectTest, Create_CreateUninitializedPOD_ValidDataObject)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
- EXPECT_NE(nullptr, result);
- }
- TEST_F(DataObjectTest, Create_CreateInitializedPOD_ValidDataObject)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>(42);
- EXPECT_NE(nullptr, result);
- }
- TEST_F(DataObjectTest, Create_CreateUninitializedComplexObject_ValidDataObject)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>();
- EXPECT_NE(nullptr, result);
- }
- TEST_F(DataObjectTest, Create_CreateComplexObjectWithSingleParamter_ValidDataObject)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>(42);
- EXPECT_NE(nullptr, result);
- }
- TEST_F(DataObjectTest, Create_CreateComplexObjectWithMultipileParamters_ValidDataObject)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>(42, "Test string");
- EXPECT_NE(nullptr, result);
- }
- // IsType
- TEST_F(DataObjectTest, IsType_GetPODType_PODIsRecognizedAsType)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
- ASSERT_NE(nullptr, result);
- EXPECT_TRUE(result->IsType<int>());
- }
- TEST_F(DataObjectTest, IsType_GetComplexClassType_ComplexObjectIsRecognizedAsType)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>();
- ASSERT_NE(nullptr, result);
- EXPECT_TRUE(result->IsType<ComplexObject>());
- }
- TEST_F(DataObjectTest, IsType_SingleInheritance_BaseInterfaceFoundFromDerivedClass)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<SingleInheritance>();
- ASSERT_NE(nullptr, result);
- EXPECT_TRUE(result->IsType<BaseInterface>());
- }
- TEST_F(DataObjectTest, IsType_MultipleInheritance_BothInterfacesFoundFromDerivedClass)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<MultipleInheritance>();
- ASSERT_NE(nullptr, result);
- EXPECT_TRUE(result->IsType<BaseInterface>());
- EXPECT_TRUE(result->IsType<SecondInterface>());
- }
- // GetTypeName
- TEST_F(DataObjectTest, GetTypeName_GetNameOfPOD_NameOfPODIsInt)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
- ASSERT_NE(nullptr, result);
- EXPECT_STRCASEEQ("int", result->GetTypeName());
- }
- TEST_F(DataObjectTest, GetTypeName_GetNameOfComplexClass_NameIsComplexObject)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>();
- ASSERT_NE(nullptr, result);
- EXPECT_STRCASEEQ("ComplexObject", result->GetTypeName());
- }
- TEST_F(DataObjectTest, GetTypeName_GetNameOfComplexClassInNamespace_TypeNameOfClassWithoutNamespace)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<TestNamespace::TestClassInNamespace>();
- ASSERT_NE(nullptr, result);
- EXPECT_STRCASEEQ("TestClassInNamespace", result->GetTypeName());
- }
- TEST_F(DataObjectTest, GetTypeName_GetNameOfTypedefedType_NameOfOriginalType)
- {
- using U32 = int;
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<U32>();
- ASSERT_NE(nullptr, result);
- EXPECT_STRCASEEQ("int", result->GetTypeName());
- }
- // DynamicCast
- TEST_F(DataObjectTest, DynamicCast_CastToGivenPOD_ValidPointer)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
- ASSERT_NE(nullptr, result);
- EXPECT_NE(nullptr, result->DynamicCast<int*>());
- }
- TEST_F(DataObjectTest, DynamicCast_CastToInvalidType_Nullptr)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
- ASSERT_NE(nullptr, result);
- EXPECT_EQ(nullptr, result->DynamicCast<float*>());
- }
- TEST_F(DataObjectTest, DynamicCast_GivenPODDataIsAccesible_SameValueAsStored)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>(42);
- ASSERT_NE(nullptr, result);
- int* value = result->DynamicCast<int*>();
- EXPECT_EQ(42, *value);
- }
- TEST_F(DataObjectTest, DynamicCast_GivenComplextDataIsAccesible_SameValueAsStored)
- {
- ComplexObject comparison = ComplexObject(42, "test");
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>(42, "test");
- ASSERT_NE(nullptr, result);
- ComplexObject* value = result->DynamicCast<ComplexObject*>();
- EXPECT_EQ(comparison, *value);
- }
- // Destruction
- TEST_F(DataObjectTest, Destruction_DestructorCalledOnConstructedObject_DestructorCalled)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<DestructionClass>();
- ASSERT_NE(nullptr, result);
- DestructionClass* value = result->DynamicCast<DestructionClass*>();
- ASSERT_NE(nullptr, value);
- EXPECT_CALL(*value, Destructor()).Times(1);
- }
- // Reflect
- TEST_F(DataObjectTest, Reflect_ReflectIsCalledOnStoredObject_ReflectCalled)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ReflectionClass>();
- ReflectionClass* value = result->DynamicCast<ReflectionClass*>();
- ASSERT_NE(nullptr, value);
- ReflectionClass::currentTestClass = value;
- EXPECT_CALL(*value, Reflect()).Times(1);
- AZ::SerializeContext context;
- result->ReflectData(&context);
- }
- TEST_F(DataObjectTest, Reflect_ReflectIsCalledMultipleTimesOnSameStoredObject_ReflectCalledOnceAndNowAssertsFromSerializeContext)
- {
- std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ReflectionClass>();
- ReflectionClass* value = result->DynamicCast<ReflectionClass*>();
- ASSERT_NE(nullptr, value);
- ReflectionClass::currentTestClass = value;
- EXPECT_CALL(*value, Reflect()).Times(1);
- AZ::SerializeContext context;
- result->ReflectData(&context);
- result->ReflectData(&context);
- }
|