Exemple #1
0
        public void RemoveComponent()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            container.SetComponent(new ComponentA());
            container.SetComponent(new ComponentB());
            container.SetComponent(new ComplexComponent());

            Assert.That(container.HasComponent <ComponentA>(), Is.True);
            Assert.That(container.HasComponent <ComponentB>(), Is.True);
            Assert.That(container.HasComponent <ComplexComponent>(), Is.True);
            Assert.That(container.Components.Count, Is.EqualTo(3));

            Assert.That(container.RemoveComponent <ComplexComponent>(), Is.True);
            Assert.That(container.Components.Count, Is.EqualTo(2));

            Assert.That(container.RemoveComponent <DerivedClass>(), Is.False);
            Assert.That(container.Components.Count, Is.EqualTo(2));

            Assert.That(container.RemoveComponent <ITestInterface>(), Is.True);
            Assert.That(container.Components.Count, Is.EqualTo(0));

            Assert.Throws <ArgumentNullException>(() => container.RemoveComponent(null));
            Assert.Throws <InvalidOperationException>(() => container.RemoveComponent(typeof(object)));
            Assert.Throws <InvalidOperationException>(() => container.RemoveComponent(typeof(InvalidComponent)));
        }
Exemple #2
0
        public void DeserializeInvalidJson_DoesNotThrow()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            LogAssert.Expect(LogType.Exception, new Regex("InvalidJsonException: Input json was invalid.*"));
            TestHierarchicalComponentContainer.DeserializeFromJson(container, "{\"Dependencies\": [], \"Components\": [{\"$type\": }, {\"$type\": }]}");
        }
Exemple #3
0
        public void AddDependency_CannotAddSelfDependency()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            Assert.That(container.AddDependency(container), Is.False);
            Assert.That(container.Dependencies.Count, Is.Zero);
        }
Exemple #4
0
        public void CopyVisitor_DoesNotCopyCollectionReferences()
        {
            var list = new List <int> {
                1, 2, 3
            };
            var component = new ComplexComponent {
                ListInteger = list
            };
            var container = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(component));
            var value     = container.GetComponent <ComplexComponent>();

            Assert.That(value, Is.Not.SameAs(component));
            Assert.That(value, Is.EqualTo(component));
            Assert.That(value.ListInteger, Is.Not.SameAs(list));
            Assert.That(value.ListInteger, Is.EqualTo(list));

            list.AddRange(new int[] { 4, 5, 6 });
            Assert.That(value.ListInteger, Is.Not.SameAs(list));
            Assert.That(value.ListInteger, Is.Not.EqualTo(list));

            value = container.GetComponent <ComplexComponent>();
            Assert.That(value.ListInteger, Is.Not.SameAs(list));
            Assert.That(value.ListInteger, Is.Not.EqualTo(list));

            container.SetComponent(component);
            value = container.GetComponent <ComplexComponent>();
            Assert.That(value.ListInteger, Is.Not.SameAs(list));
            Assert.That(value.ListInteger, Is.EqualTo(list));
        }
        public void DeserializeInvalidJson_DoesNotThrow()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            LogAssert.Expect(LogType.Error, new Regex("Failed to deserialize memory container.*"));
            TestHierarchicalComponentContainer.DeserializeFromJson(container, "{\"Dependencies\": [], \"Components\": [{\"$type\": }, {\"$type\": }]}");
        }
Exemple #6
0
        public void DeserializeInvalidDependencies_ComponentsArePreserved()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [123, \"abc\"], \"Components\": [{{\"$type\": {typeof(ComponentA).GetFullyQualifedAssemblyTypeName().DoubleQuotes()}}}]}}");
            Assert.That(container.HasComponent <ComponentA>(), Is.True);
        }
        public void DeserializeInvalidComponents_OtherComponentsArePreserved()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [], \"Components\": [{{\"$type\": {typeof(ComponentA).GetQualifedAssemblyTypeName().DoubleQuotes()}}}, {{\"$type\": \"Some.InvalidComponent.Name, Unknown.Assembly\"}}]}}");
            Assert.That(container.HasComponent <ComponentA>(), Is.True);
        }
Exemple #8
0
        public void DeserializeNullDependencies_DependenciesArePreserved()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [null, \"GlobalObjectId_V1-0-00000000000000000000000000000000-0-0\"], \"Components\": []}}");
            Assert.That(container.Dependencies, Is.EqualTo(new ITestComponent[] { null, null }));
        }
Exemple #9
0
        public void DeserializeInvalidJson_DoesNotThrow()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            LogAssert.Expect(LogType.Error, $"Failed to deserialize memory container of type '{typeof(TestHierarchicalComponentContainer).FullName}':\nInput json was invalid. ExpectedType=[Value] ActualType=[EndObject] ActualChar=['}}'] at Line=[1] at Character=[47]");
            TestHierarchicalComponentContainer.DeserializeFromJson(container, "{\"Dependencies\": [], \"Components\": [{\"$type\": }, {\"$type\": }]}");
        }
Exemple #10
0
        public void ComponentOverrides_FromMultipleDependencies()
        {
            var containerA = TestHierarchicalComponentContainer.CreateInstance();

            containerA.SetComponent(new ComponentA {
                Integer = 1
            });

            var containerB = TestHierarchicalComponentContainer.CreateInstance();

            containerB.AddDependency(containerA);

            var componentA = containerB.GetComponent <ComponentA>();

            componentA.Float = 123.456f;
            containerB.SetComponent(componentA);

            var containerC = TestHierarchicalComponentContainer.CreateInstance();

            containerC.AddDependency(containerB);

            componentA        = containerC.GetComponent <ComponentA>();
            componentA.String = "test";
            containerC.SetComponent(componentA);

            var containerD = TestHierarchicalComponentContainer.CreateInstance();

            containerD.AddDependency(containerC);

            var value = containerD.GetComponent <ComponentA>();

            Assert.That(value.Integer, Is.EqualTo(1));
            Assert.That(value.Float, Is.EqualTo(123.456f));
            Assert.That(value.String, Is.EqualTo("test"));
        }
Exemple #11
0
        public void ComponentValues_AreValid()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();
            var component = new ComplexComponent
            {
                Integer = 1,
                Float   = 123.456f,
                String  = "test",
                Nested  = new ComponentA
                {
                    Integer = 42
                },
                ListInteger = new List <int> {
                    1, 1, 2, 3, 5, 8, 13
                }
            };

            container.SetComponent(component);

            var value = container.GetComponent <ComplexComponent>();

            Assert.That(value.Integer, Is.EqualTo(1));
            Assert.That(value.Float, Is.EqualTo(123.456f));
            Assert.That(value.String, Is.EqualTo("test"));
            Assert.That(value.Nested.Integer, Is.EqualTo(42));
            Assert.That(value.ListInteger, Is.EquivalentTo(new List <int> {
                1, 1, 2, 3, 5, 8, 13
            }));
        }
Exemple #12
0
        public void GetComponents_WithType()
        {
            var containerA       = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentA()));
            var containerB       = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentB()));
            var complexContainer = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComplexComponent()));

            containerA.AddDependency(containerB);
            containerB.AddDependency(complexContainer);

            Assert.That(containerA.GetComponents <ComponentA>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentA) }));
            Assert.That(containerA.GetComponents <ComponentB>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentB) }));
            Assert.That(containerA.GetComponents <ComplexComponent>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComplexComponent) }));

            Assert.That(containerB.GetComponents <ComponentA>(), Is.Empty);
            Assert.That(containerB.GetComponents <ComponentB>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentB) }));
            Assert.That(containerB.GetComponents <ComplexComponent>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComplexComponent) }));

            Assert.That(complexContainer.GetComponents <ComponentA>(), Is.Empty);
            Assert.That(complexContainer.GetComponents <ComponentB>(), Is.Empty);
            Assert.That(complexContainer.GetComponents <ComplexComponent>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComplexComponent) }));

            Assert.That(containerA.GetComponents <ITestInterface>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentA), typeof(ComponentB) }));
            Assert.That(containerB.GetComponents <ITestInterface>().Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentB) }));
            Assert.That(complexContainer.GetComponents <ITestInterface>(), Is.Empty);
        }
Exemple #13
0
        public void AddingDestoyedDependency_Throws()
        {
            var container         = TestHierarchicalComponentContainer.CreateInstance();
            var missingDependency = TestHierarchicalComponentContainer.CreateInstance();

            UnityEngine.Object.DestroyImmediate(missingDependency);
            Assert.Throws <ArgumentNullException>(() => container.AddDependency(missingDependency));
        }
Exemple #14
0
        public void DeserializeInvalidComponents_OtherComponentsArePreserved()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            LogAssert.Expect(LogType.Error, $"Failed to deserialize memory container of type '{typeof(TestHierarchicalComponentContainer).FullName}':\nSystem.InvalidOperationException: PropertyContainer.Construct failed to construct DstType=[{typeof(ITestComponent).FullName}]. Could not resolve type from TypeName=[Some.InvalidComponent.Name, Unknown.Assembly].");
            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [], \"Components\": [{{\"$type\": {typeof(ComponentA).GetFullyQualifedAssemblyTypeName().DoubleQuotes()}}}, {{\"$type\": \"Some.InvalidComponent.Name, Unknown.Assembly\"}}]}}");
            Assert.That(container.HasComponent <ComponentA>(), Is.True);
        }
Exemple #15
0
        public void DeserializeInvalidDependencies_ComponentsArePreserved()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            LogAssert.Expect(LogType.Error, new Regex("While deserializing memory container of type.*"));
            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [123, \"abc\"], \"Components\": [{{\"$type\": {typeof(ComponentA).GetAssemblyQualifiedTypeName().DoubleQuotes()}}}]}}");
            Assert.That(container.HasComponent <ComponentA>(), Is.True);
        }
Exemple #16
0
        public void CannotSet_InterfaceType()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            Assert.Throws <InvalidOperationException>(() =>
            {
                container.SetComponent(typeof(ITestInterface), new ComponentA());
            });
        }
Exemple #17
0
        public void CannotSet_AbstractType()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            Assert.Throws <InvalidOperationException>(() =>
            {
                container.SetComponent(typeof(AbstractClass), new DerivedClass());
            });
        }
Exemple #18
0
        public void CannotSet_NullType()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            Assert.Throws <ArgumentNullException>(() =>
            {
                container.SetComponent(null, new ComponentA());
            });
        }
        public void DeserializeInvalidDependencies_ComponentsArePreserved()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

#if UNITY_2020_1_OR_NEWER
            LogAssert.Expect(LogType.Error, new Regex("Failed to deserialize memory container.*"));
#endif
            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [123, \"abc\"], \"Components\": [{{\"$type\": {typeof(ComponentA).GetQualifedAssemblyTypeName().DoubleQuotes()}}}]}}");
            Assert.That(container.HasComponent <ComponentA>(), Is.True);
        }
Exemple #20
0
        public void TryGetComponent()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentA()));

            Assert.That(container.TryGetComponent <ComponentA>(out var _), Is.True);
            Assert.That(container.TryGetComponent <ComponentB>(out var _), Is.False);
            Assert.That(container.TryGetComponent(null, out var _), Is.False);
            Assert.That(container.TryGetComponent(typeof(object), out var _), Is.False);
            Assert.That(container.TryGetComponent(typeof(InvalidComponent), out var _), Is.False);
        }
Exemple #21
0
        public void AddDependency()
        {
            var containerA = TestHierarchicalComponentContainer.CreateInstance();
            var containerB = TestHierarchicalComponentContainer.CreateInstance();

            Assert.That(containerA.AddDependency(containerB), Is.True);
            Assert.That(containerA.AddDependency(containerB), Is.False);
            Assert.That(containerA.Dependencies, Is.EqualTo(new[] { containerB }));
            Assert.Throws <ArgumentNullException>(() => containerA.AddDependency(null));
        }
Exemple #22
0
        public void SetComponent()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentA()));

            Assert.That(container.Components.Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentA) }));
            Assert.Throws <ArgumentNullException>(() => container.SetComponent(null, default));
            Assert.Throws <InvalidOperationException>(() => container.SetComponent(typeof(object), default));
            Assert.Throws <InvalidOperationException>(() => container.SetComponent(typeof(InvalidComponent), default));
            Assert.Throws <InvalidOperationException>(() => container.SetComponent(typeof(ITestInterface), default));
        }
Exemple #23
0
        public void AsReadOnly_ReflectChanges()
        {
            var container   = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent <ComponentA>());
            var containerRO = container.AsReadOnly();

            Assert.That(containerRO.GetComponentTypes(), Is.EquivalentTo(new[] { typeof(ComponentA) }));

            container.SetComponent <ComponentB>();
            Assert.That(containerRO.GetComponentTypes(), Is.EquivalentTo(new[] { typeof(ComponentA), typeof(ComponentB) }));
        }
Exemple #24
0
        public void GetComponent()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentA()));

            Assert.That(container.GetComponent <ComponentA>(), Is.Not.Null);
            Assert.Throws <InvalidOperationException>(() => container.GetComponent <ComponentB>());
            Assert.Throws <ArgumentNullException>(() => container.GetComponent(null));
            Assert.Throws <InvalidOperationException>(() => container.GetComponent(typeof(object)));
            Assert.Throws <InvalidOperationException>(() => container.GetComponent(typeof(InvalidComponent)));
        }
        public void DeserializeNullDependencies_DependenciesArePreserved()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [null, \"GlobalObjectId_V1-0-00000000000000000000000000000000-0-0\"], \"Components\": []}}");
#if UNITY_2020_1_OR_NEWER
            Assert.That(container.Dependencies.Select(d => d.asset), Is.EqualTo(new TestHierarchicalComponentContainer[] { null, null }));
#else
            Assert.That(container.Dependencies, Is.EqualTo(new TestHierarchicalComponentContainer[] { null, null }));
#endif
        }
Exemple #26
0
        public void RemoveDependency()
        {
            var containerA = TestHierarchicalComponentContainer.CreateInstance();
            var containerB = TestHierarchicalComponentContainer.CreateInstance();

            containerA.AddDependency(containerB);

            Assert.That(containerA.RemoveDependency(containerB), Is.True);
            Assert.That(containerA.RemoveDependency(containerB), Is.False);
            Assert.That(containerB.RemoveDependency(containerA), Is.False);
            Assert.Throws <ArgumentNullException>(() => containerA.RemoveDependency(null));
        }
Exemple #27
0
        public void GetDependencies()
        {
            var containerA = TestHierarchicalComponentContainer.CreateInstance();
            var containerB = TestHierarchicalComponentContainer.CreateInstance();
            var containerC = TestHierarchicalComponentContainer.CreateInstance();

            containerA.AddDependency(containerB);
            containerB.AddDependency(containerC);

            Assert.That(containerA.GetDependencies(), Is.EqualTo(new[] { containerB, containerC }));
            Assert.That(containerB.GetDependencies(), Is.EqualTo(new[] { containerC }));
            Assert.That(containerC.GetDependencies(), Is.Empty);
        }
Exemple #28
0
        public void GetComponentTypes()
        {
            var containerA       = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentA()));
            var containerB       = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentB()));
            var complexContainer = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComplexComponent()));

            containerA.AddDependency(containerB);
            containerB.AddDependency(complexContainer);

            Assert.That(containerA.GetComponentTypes(), Is.EquivalentTo(new[] { typeof(ComponentA), typeof(ComponentB), typeof(ComplexComponent) }));
            Assert.That(containerB.GetComponentTypes(), Is.EquivalentTo(new[] { typeof(ComponentB), typeof(ComplexComponent) }));
            Assert.That(complexContainer.GetComponentTypes(), Is.EquivalentTo(new[] { typeof(ComplexComponent) }));
        }
Exemple #29
0
        public void DeserializeMultipleTimes_ShouldNotAppendData()
        {
            var container = TestHierarchicalComponentContainer.CreateInstance();

            Assert.That(container.HasComponent <ComponentA>(), Is.False);
            Assert.That(container.Components.Count, Is.Zero);
            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [], \"Components\": [{{\"$type\": {typeof(ComponentA).GetFullyQualifedAssemblyTypeName().DoubleQuotes()}}}]}}");
            Assert.That(container.HasComponent <ComponentA>(), Is.True);
            Assert.That(container.Components.Count, Is.EqualTo(1));
            TestHierarchicalComponentContainer.DeserializeFromJson(container, $"{{\"Dependencies\": [], \"Components\": [{{\"$type\": {typeof(ComponentA).GetFullyQualifedAssemblyTypeName().DoubleQuotes()}}}]}}");
            Assert.That(container.HasComponent <ComponentA>(), Is.True);
            Assert.That(container.Components.Count, Is.EqualTo(1));
        }
Exemple #30
0
        public void IsComponentOverridden()
        {
            var containerA = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentA()));
            var containerB = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent(new ComponentA()));

            containerA.AddDependency(containerB);

            Assert.That(containerA.IsComponentOverridden <ComponentA>(), Is.True);
            Assert.That(containerB.IsComponentOverridden <ComponentA>(), Is.False);

            Assert.Throws <ArgumentNullException>(() => containerA.IsComponentOverridden(null));
            Assert.Throws <InvalidOperationException>(() => containerA.IsComponentOverridden(typeof(object)));
            Assert.Throws <InvalidOperationException>(() => containerA.IsComponentOverridden(typeof(InvalidComponent)));
        }