public void MissingDependencies_DoesNotThrow() { var container = TestHierarchicalComponentContainer.CreateInstance(); container.SetComponent(new ComplexComponent()); // We cannot directly add `null` using AddDependency, so we add it to the underlying list to simulate a // missing dependency (either added through UI or missing asset). container.Dependencies.Add(null); var missingDependency = TestHierarchicalComponentContainer.CreateInstance(); missingDependency.SetComponent(new ComplexComponent()); container.AddDependency(missingDependency); UnityEngine.Object.DestroyImmediate(missingDependency); Assert.DoesNotThrow(() => container.TryGetComponent <ComplexComponent>(out _)); Assert.DoesNotThrow(() => container.GetComponent <ComplexComponent>()); Assert.DoesNotThrow(() => container.GetDependencies()); Assert.DoesNotThrow(() => container.HasComponent <ComplexComponent>()); }
public void ComponentSerialization() { var container = TestHierarchicalComponentContainer.CreateInstance(); container.SetComponent(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 } }); var json = container.SerializeToJson(); Assert.That(json.Length, Is.GreaterThan(3)); var deserializedContainer = TestHierarchicalComponentContainer.CreateInstance(); TestHierarchicalComponentContainer.DeserializeFromJson(deserializedContainer, json); var component = deserializedContainer.GetComponent <ComplexComponent>(); Assert.That(component.Integer, Is.EqualTo(1)); Assert.That(component.Float, Is.EqualTo(123.456f)); Assert.That(component.String, Is.EqualTo("test")); Assert.That(component.Nested.Integer, Is.EqualTo(42)); Assert.That(component.ListInteger, Is.EquivalentTo(new List <int> { 1, 1, 2, 3, 5, 8, 13 })); var reserializedJson = deserializedContainer.SerializeToJson(); Assert.That(reserializedJson, Is.EqualTo(json)); }
public void AsReadOnly() { var container = TestHierarchicalComponentContainer.CreateInstance(c => c.SetComponent <ComponentA>()); var containerRO = container.AsReadOnly(); Assert.That(container.HasComponent <ComponentA>(), Is.EqualTo(containerRO.HasComponent <ComponentA>())); Assert.That(container.HasComponent <ComponentB>(), Is.EqualTo(containerRO.HasComponent <ComponentB>())); Assert.That(container.IsComponentInherited <ComponentA>(), Is.EqualTo(containerRO.IsComponentInherited <ComponentA>())); Assert.That(container.IsComponentInherited <ComponentB>(), Is.EqualTo(containerRO.IsComponentInherited <ComponentB>())); Assert.That(container.IsComponentOverriding <ComponentA>(), Is.EqualTo(containerRO.IsComponentOverriding <ComponentA>())); Assert.That(container.IsComponentOverriding <ComponentB>(), Is.EqualTo(containerRO.IsComponentOverriding <ComponentB>())); Assert.That(container.GetComponent <ComponentA>(), Is.EqualTo(containerRO.GetComponent <ComponentA>())); Assert.Throws <InvalidOperationException>(() => containerRO.GetComponent <ComponentB>()); Assert.That(container.TryGetComponent <ComponentA>(out _), Is.EqualTo(containerRO.TryGetComponent <ComponentA>(out _))); Assert.That(container.TryGetComponent <ComponentB>(out _), Is.EqualTo(containerRO.TryGetComponent <ComponentB>(out _))); Assert.That(container.GetComponentOrDefault <ComponentA>(), Is.EqualTo(containerRO.GetComponentOrDefault <ComponentA>())); Assert.That(container.GetComponentOrDefault <ComponentB>(), Is.EqualTo(containerRO.GetComponentOrDefault <ComponentB>())); Assert.That(container.GetComponents(), Is.EquivalentTo(containerRO.GetComponents())); Assert.That(container.GetComponents <ComponentA>(), Is.EquivalentTo(containerRO.GetComponents <ComponentA>())); Assert.That(container.GetComponents <ComponentB>(), Is.EquivalentTo(containerRO.GetComponents <ComponentB>())); Assert.That(container.GetComponentTypes(), Is.EquivalentTo(containerRO.GetComponentTypes())); }
public void HasDependency() { var containerA = TestHierarchicalComponentContainer.CreateInstance(); var containerB = TestHierarchicalComponentContainer.CreateInstance(); var containerC = TestHierarchicalComponentContainer.CreateInstance(); containerA.AddDependency(containerB); containerB.AddDependency(containerC); Assert.That(containerA.HasDependency(containerA), Is.False); Assert.That(containerA.HasDependency(containerB), Is.True); Assert.That(containerA.HasDependency(containerC), Is.True); Assert.That(containerB.HasDependency(containerA), Is.False); Assert.That(containerB.HasDependency(containerB), Is.False); Assert.That(containerB.HasDependency(containerC), Is.True); Assert.That(containerC.HasDependency(containerA), Is.False); Assert.That(containerC.HasDependency(containerB), Is.False); Assert.That(containerC.HasDependency(containerC), Is.False); Assert.That(containerA.HasDependency(null), Is.False); }
public void ComponentInheritance() { var containerA = TestHierarchicalComponentContainer.CreateInstance(); containerA.SetComponent(new ComponentA { Integer = 1, Float = 123.456f, String = "test" }); var containerB = TestHierarchicalComponentContainer.CreateInstance(); containerB.AddDependency(containerA); containerB.SetComponent(new ComponentB { Byte = 255, Double = 3.14159265358979323846, Short = 32767 }); Assert.That(containerB.IsComponentInherited <ComponentA>(), Is.True); Assert.That(containerB.GetComponent <ComponentA>(), Is.EqualTo(new ComponentA { Integer = 1, Float = 123.456f, String = "test" })); Assert.That(containerB.IsComponentInherited <ComponentB>(), Is.False); Assert.That(containerB.GetComponent <ComponentB>(), Is.EqualTo(new ComponentB { Byte = 255, Double = 3.14159265358979323846, Short = 32767 })); }
public void GetComponents() { 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); var containerAComponents = containerA.GetComponents(); Assert.That(containerAComponents.Count, Is.EqualTo(3)); Assert.That(containerAComponents.Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentA), typeof(ComponentB), typeof(ComplexComponent) })); var containerBComponents = containerB.GetComponents(); Assert.That(containerBComponents.Count, Is.EqualTo(2)); Assert.That(containerBComponents.Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComponentB), typeof(ComplexComponent) })); var complexContainerComponents = complexContainer.GetComponents(); Assert.That(complexContainerComponents.Count, Is.EqualTo(1)); Assert.That(complexContainerComponents.Select(c => c.GetType()), Is.EquivalentTo(new[] { typeof(ComplexComponent) })); }
public void ComponentInheritance_FromMultipleDependencies() { var containerA = TestHierarchicalComponentContainer.CreateInstance(); containerA.SetComponent(new ComponentA { Integer = 1, Float = 123.456f, String = "test" }); var containerB = TestHierarchicalComponentContainer.CreateInstance(); containerB.AddDependency(containerA); containerB.SetComponent(new ComponentB { Byte = 255, Double = 3.14159265358979323846, Short = 32767 }); var containerC = TestHierarchicalComponentContainer.CreateInstance(); containerC.SetComponent(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 } }); var containerD = TestHierarchicalComponentContainer.CreateInstance(); containerD.AddDependency(containerB); containerD.AddDependency(containerC); Assert.That(containerD.IsComponentInherited <ComponentA>(), Is.True); Assert.That(containerD.GetComponent <ComponentA>(), Is.EqualTo(new ComponentA { Integer = 1, Float = 123.456f, String = "test" })); Assert.That(containerD.IsComponentInherited <ComponentB>(), Is.True); Assert.That(containerD.GetComponent <ComponentB>(), Is.EqualTo(new ComponentB { Byte = 255, Double = 3.14159265358979323846, Short = 32767 })); Assert.That(containerD.IsComponentInherited <ComplexComponent>(), Is.True); var complexComponent = containerD.GetComponent <ComplexComponent>(); Assert.That(complexComponent.Integer, Is.EqualTo(1)); Assert.That(complexComponent.Float, Is.EqualTo(123.456f)); Assert.That(complexComponent.String, Is.EqualTo("test")); Assert.That(complexComponent.Nested.Integer, Is.EqualTo(42)); Assert.That(complexComponent.ListInteger, Is.EquivalentTo(new List <int> { 1, 1, 2, 3, 5, 8, 13 })); }
public void GetComponentSource() { var containerA = TestHierarchicalComponentContainer.CreateInstance(c => { c.SetComponent <ComponentA>(); }); var containerB = TestHierarchicalComponentContainer.CreateInstance(c => { c.SetComponent <ComponentA>(); c.SetComponent <ComponentB>(); }); var containerC = TestHierarchicalComponentContainer.CreateInstance(c => { c.SetComponent <ComponentA>(); c.SetComponent <ComponentB>(); c.SetComponent <ComponentC>(); }); var containerD = TestHierarchicalComponentContainer.CreateInstance(c => { c.SetComponent <ComponentA>(); c.SetComponent <ComponentB>(); c.SetComponent <ComponentC>(); c.SetComponent <ComponentD>(); }); containerA.AddDependency(containerB); containerA.AddDependency(containerC); containerC.AddDependency(containerD); Assert.That(containerA.GetComponentSource <ComponentA>(false), Is.EqualTo(containerA)); Assert.That(containerB.GetComponentSource <ComponentA>(false), Is.EqualTo(containerB)); Assert.That(containerC.GetComponentSource <ComponentA>(false), Is.EqualTo(containerC)); Assert.That(containerD.GetComponentSource <ComponentA>(false), Is.EqualTo(containerD)); Assert.That(containerA.GetComponentSource <ComponentA>(true), Is.EqualTo(containerC)); Assert.That(containerB.GetComponentSource <ComponentA>(true), Is.Null); Assert.That(containerC.GetComponentSource <ComponentA>(true), Is.EqualTo(containerD)); Assert.That(containerD.GetComponentSource <ComponentA>(true), Is.Null); Assert.That(containerA.GetComponentSource <ComponentB>(false), Is.EqualTo(containerC)); Assert.That(containerB.GetComponentSource <ComponentB>(false), Is.EqualTo(containerB)); Assert.That(containerC.GetComponentSource <ComponentB>(false), Is.EqualTo(containerC)); Assert.That(containerD.GetComponentSource <ComponentB>(false), Is.EqualTo(containerD)); Assert.That(containerA.GetComponentSource <ComponentB>(true), Is.EqualTo(containerC)); Assert.That(containerB.GetComponentSource <ComponentB>(true), Is.Null); Assert.That(containerC.GetComponentSource <ComponentB>(true), Is.EqualTo(containerD)); Assert.That(containerD.GetComponentSource <ComponentB>(true), Is.Null); Assert.That(containerA.GetComponentSource <ComponentC>(false), Is.EqualTo(containerC)); Assert.That(containerB.GetComponentSource <ComponentC>(false), Is.Null); Assert.That(containerC.GetComponentSource <ComponentC>(false), Is.EqualTo(containerC)); Assert.That(containerD.GetComponentSource <ComponentC>(false), Is.EqualTo(containerD)); Assert.That(containerA.GetComponentSource <ComponentC>(true), Is.EqualTo(containerC)); Assert.That(containerB.GetComponentSource <ComponentC>(true), Is.Null); Assert.That(containerC.GetComponentSource <ComponentC>(true), Is.EqualTo(containerD)); Assert.That(containerD.GetComponentSource <ComponentC>(true), Is.Null); Assert.That(containerA.GetComponentSource <ComponentD>(false), Is.EqualTo(containerD)); Assert.That(containerB.GetComponentSource <ComponentD>(false), Is.Null); Assert.That(containerC.GetComponentSource <ComponentD>(false), Is.EqualTo(containerD)); Assert.That(containerD.GetComponentSource <ComponentD>(false), Is.EqualTo(containerD)); Assert.That(containerA.GetComponentSource <ComponentD>(true), Is.EqualTo(containerD)); Assert.That(containerB.GetComponentSource <ComponentD>(true), Is.Null); Assert.That(containerC.GetComponentSource <ComponentD>(true), Is.EqualTo(containerD)); Assert.That(containerD.GetComponentSource <ComponentD>(true), Is.Null); Assert.That(containerA.GetComponentSource <ComplexComponent>(false), Is.Null); Assert.That(containerB.GetComponentSource <ComplexComponent>(false), Is.Null); Assert.That(containerC.GetComponentSource <ComplexComponent>(false), Is.Null); Assert.That(containerD.GetComponentSource <ComplexComponent>(false), Is.Null); Assert.That(containerA.GetComponentSource <ComplexComponent>(true), Is.Null); Assert.That(containerB.GetComponentSource <ComplexComponent>(true), Is.Null); Assert.That(containerC.GetComponentSource <ComplexComponent>(true), Is.Null); Assert.That(containerD.GetComponentSource <ComplexComponent>(true), Is.Null); Assert.Throws <ArgumentNullException>(() => containerA.GetComponentSource(null, false)); Assert.Throws <InvalidOperationException>(() => containerA.GetComponentSource(typeof(object), false)); Assert.Throws <InvalidOperationException>(() => containerA.GetComponentSource(typeof(InvalidComponent), false)); Assert.Throws <ArgumentNullException>(() => containerA.GetComponentSource(null, true)); Assert.Throws <InvalidOperationException>(() => containerA.GetComponentSource(typeof(object), true)); Assert.Throws <InvalidOperationException>(() => containerA.GetComponentSource(typeof(InvalidComponent), true)); }