[Test] public void CyclicRequirements() { ComponentRequirementMap map = new ComponentRequirementMap(); // In a cyclic dependency situation, behavior is undefined and there is no "right" solution. // For this test, just expect the system to not crash. Check some select results that we // can safely expect. TestingLogOutput logWatcher = new TestingLogOutput(); Logs.AddGlobalOutput(logWatcher); map.GetRequirements(typeof(TestComponentC1)); map.GetRequirements(typeof(TestComponentC2)); map.GetRequirements(typeof(TestComponentC3)); logWatcher.AssertNoErrors(); logWatcher.AssertWarning(); map.GetRequirementsToCreate(new GameObject(), typeof(TestComponentC1)); map.GetRequirementsToCreate(new GameObject(), typeof(TestComponentC2)); map.GetRequirementsToCreate(new GameObject(), typeof(TestComponentC3)); logWatcher.AssertNoErrors(); logWatcher.AssertWarning(); Assert.IsFalse(map.IsRequired(typeof(TestComponentC1), typeof(TestComponentC1))); Assert.IsFalse(map.IsRequired(typeof(TestComponentC2), typeof(TestComponentC2))); Assert.IsFalse(map.IsRequired(typeof(TestComponentC3), typeof(TestComponentC3))); map.IsRequired(typeof(TestComponentC1), typeof(TestComponentC2)); map.IsRequired(typeof(TestComponentC1), typeof(TestComponentC3)); map.IsRequired(typeof(TestComponentC2), typeof(TestComponentC1)); map.IsRequired(typeof(TestComponentC2), typeof(TestComponentC3)); map.IsRequired(typeof(TestComponentC3), typeof(TestComponentC1)); map.IsRequired(typeof(TestComponentC3), typeof(TestComponentC2)); map.IsRequirementMet(new GameObject(), typeof(TestComponentC1)); map.IsRequirementMet(new GameObject(), typeof(TestComponentC2)); map.IsRequirementMet(new GameObject(), typeof(TestComponentC3)); map.IsRequirementMet(new GameObject(), typeof(TestComponentC1), new[] { typeof(TestComponentC2) }); map.IsRequirementMet(new GameObject(), typeof(TestComponentC2), new[] { typeof(TestComponentC3) }); map.IsRequirementMet(new GameObject(), typeof(TestComponentC3), new[] { typeof(TestComponentC1) }); map.IsRequirementMet(new GameObject(), typeof(TestComponentC1), new[] { typeof(TestComponentC2), typeof(TestComponentC3) }); map.IsRequirementMet(new GameObject(), typeof(TestComponentC2), new[] { typeof(TestComponentC3), typeof(TestComponentC1) }); map.IsRequirementMet(new GameObject(), typeof(TestComponentC3), new[] { typeof(TestComponentC1), typeof(TestComponentC2) }); Logs.RemoveGlobalOutput(logWatcher); }
[Test] public void InvalidRequirements() { ComponentRequirementMap map = new ComponentRequirementMap(); // Expect that all invalid requirements have been ignored CollectionAssert.AreEquivalent(new Type[0], map.GetRequirements(typeof(TestComponentE1))); CollectionAssert.AreEquivalent(new Type[0], map.GetRequirements(typeof(TestComponentE2))); CollectionAssert.AreEqual(new Type[0], map.GetRequirementsToCreate(new GameObject(), typeof(TestComponentE1))); CollectionAssert.AreEqual(new Type[0], map.GetRequirementsToCreate(new GameObject(), typeof(TestComponentE2))); Assert.IsFalse(map.IsRequired(typeof(TestComponentE1), typeof(Pixmap))); Assert.IsFalse(map.IsRequired(typeof(TestComponentE2), typeof(NonPublicComponent))); Assert.IsTrue(map.IsRequirementMet(new GameObject(), typeof(TestComponentE1))); Assert.IsTrue(map.IsRequirementMet(new GameObject(), typeof(TestComponentE2))); }
[Test] public void SelfRequirements() { ComponentRequirementMap map = new ComponentRequirementMap(); CollectionAssert.AreEquivalent(new Type[0], map.GetRequirements(typeof(TestComponentB1))); CollectionAssert.AreEqual(new Type[0], map.GetRequirementsToCreate(new GameObject(), typeof(TestComponentB1))); Assert.IsFalse(map.IsRequired(typeof(TestComponentB1), typeof(TestComponentB1))); Assert.IsTrue(map.IsRequirementMet(new GameObject(), typeof(TestComponentB1))); }
[Test] public void DirectRequirements() { ComponentRequirementMap map = new ComponentRequirementMap(); CollectionAssert.AreEquivalent(new[] { typeof(TestComponentA3) }, map.GetRequirements(typeof(TestComponentA2))); CollectionAssert.AreEqual(new[] { typeof(TestComponentA3) }, map.GetRequirementsToCreate(new GameObject(), typeof(TestComponentA2))); Assert.IsTrue(map.IsRequired(typeof(TestComponentA2), typeof(TestComponentA3))); Assert.IsFalse(map.IsRequired(typeof(TestComponentA2), typeof(TestComponentA2))); Assert.IsFalse(map.IsRequired(typeof(TestComponentA2), typeof(TestComponentA1))); Assert.IsFalse(map.IsRequirementMet(new GameObject(), typeof(TestComponentA2))); Assert.IsTrue(map.IsRequirementMet(new GameObject(), typeof(TestComponentA2), new[] { typeof(TestComponentA3) })); Assert.IsFalse(map.IsRequirementMet(new GameObject(), typeof(TestComponentA2), new[] { typeof(TestComponentA1) })); Assert.IsTrue(map.IsRequirementMet(new GameObject(), typeof(TestComponentA2), new[] { typeof(TestComponentA3), typeof(TestComponentA1) })); Assert.IsTrue(map.IsRequirementMet(new GameObject(), typeof(TestComponentA2), new[] { typeof(TestComponentA1), typeof(TestComponentA3) })); }
[Test] public void AbstractRequirements() { ComponentRequirementMap map = new ComponentRequirementMap(); // Interface requirements are stated as-is without further exploration CollectionAssert.AreEquivalent(new[] { typeof(ITestComponentDX) }, map.GetRequirements(typeof(TestComponentD1))); CollectionAssert.AreEquivalent(new[] { typeof(ITestComponentDY) }, map.GetRequirements(typeof(TestComponentD2))); CollectionAssert.AreEquivalent(new[] { typeof(ITestComponentDY), typeof(TestComponentD6) }, map.GetRequirements(typeof(TestComponentD3))); CollectionAssert.AreEquivalent(new[] { typeof(TestComponentD6) }, map.GetRequirements(typeof(TestComponentD4))); CollectionAssert.AreEquivalent(new Type[0], map.GetRequirements(typeof(TestComponentD5))); CollectionAssert.AreEquivalent(new Type[0], map.GetRequirements(typeof(TestComponentD6))); // Creation requirements are conditional depending on what's already there. // Most of these checks are of a validation nature where we don't expect anything // to be created, given that a specific set of Components is already existing. AssertCreationChain(map, typeof(TestComponentD1), null, new [] { typeof(TestComponentD6), typeof(TestComponentD4), typeof(TestComponentD2) }); AssertCreationChain(map, typeof(TestComponentD1), new [] { typeof(TestComponentD6), typeof(TestComponentD4), typeof(TestComponentD2) }, null); AssertCreationChain(map, typeof(TestComponentD1), new [] { typeof(TestComponentD5), typeof(TestComponentD2) }, null); AssertCreationChain(map, typeof(TestComponentD1), new [] { typeof(TestComponentD6), typeof(TestComponentD5), typeof(TestComponentD3) }, null); AssertCreationChain(map, typeof(TestComponentD2), null, new [] { typeof(TestComponentD6), typeof(TestComponentD4) }); AssertCreationChain(map, typeof(TestComponentD2), new [] { typeof(TestComponentD6), typeof(TestComponentD4) }, null); AssertCreationChain(map, typeof(TestComponentD2), new [] { typeof(TestComponentD5) }, null); AssertCreationChainUnsorted(map, typeof(TestComponentD3), null, new [] { typeof(TestComponentD6), typeof(TestComponentD5) }); AssertCreationChain(map, typeof(TestComponentD3), new [] { typeof(TestComponentD6), typeof(TestComponentD5) }, null); AssertCreationChain(map, typeof(TestComponentD3), new [] { typeof(TestComponentD6), typeof(TestComponentD4) }, null); AssertCreationChain(map, typeof(TestComponentD4), null, new [] { typeof(TestComponentD6) }); AssertCreationChain(map, typeof(TestComponentD5)); AssertCreationChain(map, typeof(TestComponentD6)); // Check partial dependency creation chains where only one part of the requirement list // isn't satisfied yet. AssertCreationChain(map, typeof(TestComponentD3), new [] { typeof(TestComponentD5) }, new [] { typeof(TestComponentD6) }); AssertCreationChain(map, typeof(TestComponentD3), new [] { typeof(TestComponentD6) }, new [] { typeof(TestComponentD5) }); }