static (BaseTestChainedExtension[], ExtensionChain) CreateTestExtensionChainAndArray(int count) { var arr = new BaseTestChainedExtension [count]; for (int i = 0; i < count; ++i) { arr [i] = new BaseTestChainedExtension(); } return(arr, ExtensionChain.Create(arr)); }
public void ExtensionChainQuerying() { var exts = new BaseTestChainedExtension [] { new SquareChainedExtension(), new RectangleChainedExtension(), new ShapeChainedExtension(), new GreenChainedExtension(), new ColorChainedExtension(), new BaseTestChainedExtension(), }; var initialSquare = exts [0]; var initialGreen = exts [3]; var initialBase = exts [5]; var chain = ExtensionChain.Create(exts); // Look for shape extensions var square = chain.GetExtension <SquareChainedExtension> (); var rectangle = chain.GetExtension <RectangleChainedExtension> (); var shape = chain.GetExtension <ShapeChainedExtension> (); Assert.IsNull(square.Next); Assert.AreSame(initialSquare, rectangle.Next); Assert.AreSame(initialSquare, shape.Next); // Look for color extensions var green = chain.GetExtension <GreenChainedExtension> (); var color = chain.GetExtension <ColorChainedExtension> (); Assert.IsNull(green.Next); Assert.AreSame(initialGreen, color.Next); var chainedExtension = chain.GetExtension <BaseTestChainedExtension> (); Assert.IsNull(chainedExtension.Next); chain.Dispose(); // Check that the extensions we queried get disposed Assert.IsTrue(square.IsDisposed); Assert.IsTrue(rectangle.IsDisposed); Assert.IsTrue(shape.IsDisposed); Assert.IsTrue(green.IsDisposed); Assert.IsTrue(color.IsDisposed); Assert.IsTrue(chainedExtension.IsDisposed); }
public void ExtensionChainModification() { var chain = CreateTestExtensionChain(2); // Assert default insertion position is at the end. var ext = new BaseTestChainedExtension(); chain.AddExtension(ext); var currentExts = GetAllExtensions(chain); Assert.AreEqual(3, currentExts.Length); Assert.AreSame(ext, currentExts [currentExts.Length - 1]); // Make it insert by default the beginning var defaultInsertionPosition = currentExts [0]; chain.SetDefaultInsertionPosition(defaultInsertionPosition); // Assert insert in default position var defaultInserted = new BaseTestChainedExtension(); chain.AddExtension(defaultInserted); currentExts = GetAllExtensions(chain); Assert.AreEqual(4, currentExts.Length); Assert.AreSame(defaultInserted, currentExts [0]); // Assert insert before a given extension var beforeExt = new BaseTestChainedExtension(); chain.AddExtension(beforeExt, insertBefore: ext); currentExts = GetAllExtensions(chain); Assert.AreEqual(5, currentExts.Length); Assert.AreSame(beforeExt, currentExts[currentExts.Length - 2]); Assert.AreSame(ext, currentExts [currentExts.Length - 1]); // Assert insert after a given extension var afterExt = new BaseTestChainedExtension(); chain.AddExtension(afterExt, insertAfter: ext); currentExts = GetAllExtensions(chain); Assert.AreEqual(6, currentExts.Length); Assert.AreSame(beforeExt, currentExts [currentExts.Length - 3]); Assert.AreSame(ext, currentExts [currentExts.Length - 2]); Assert.AreSame(afterExt, currentExts [currentExts.Length - 1]); // Remove the default one and probe it doesn't exist chain.RemoveExtension(defaultInsertionPosition); currentExts = GetAllExtensions(chain);; Assert.AreEqual(5, currentExts.Length); Assert.That(currentExts, Is.Not.Contains(defaultInsertionPosition)); // Validate that we insert at the end now var lastExt = new BaseTestChainedExtension(); chain.AddExtension(lastExt); currentExts = GetAllExtensions(chain); Assert.AreEqual(6, currentExts.Length); Assert.AreSame(lastExt, currentExts [currentExts.Length - 1]); }
public void ExtensionChainQueryingCachingBatch() { var exts = new BaseTestChainedExtension [] { new SquareChainedExtension(), new RectangleChainedExtension(), new ShapeChainedExtension(), new GreenChainedExtension(), new ColorChainedExtension(), new BaseTestChainedExtension(), }; var arrSquare = exts.Single(x => x.GetType() == typeof(SquareChainedExtension)); var arrRect = exts.Single(x => x.GetType() == typeof(RectangleChainedExtension)); var arrShape = exts.Single(x => x.GetType() == typeof(ShapeChainedExtension)); var arrGreen = exts.Single(x => x.GetType() == typeof(GreenChainedExtension)); var arrColor = exts.Single(x => x.GetType() == typeof(ColorChainedExtension)); var arrBase = exts.Single(x => x.GetType() == typeof(BaseTestChainedExtension)); var chain = ExtensionChain.Create(exts); // Look for shape extensions var square = chain.GetExtension <SquareChainedExtension> (); var rectangle = chain.GetExtension <RectangleChainedExtension> (); var shape = chain.GetExtension <ShapeChainedExtension> (); var green = chain.GetExtension <GreenChainedExtension> (); var color = chain.GetExtension <ColorChainedExtension> (); var chainedExtension = chain.GetExtension <BaseTestChainedExtension> (); int shapeInitializedCount = 1; int colorInitializedCount = 1; int baseInitializedCount = 1; int noNextInitializedCount = 1; AssertInitializedCount(); BaseTestChainedExtension toAdd = null; using (chain.BatchModify()) { // Add a BaseTestChainedExtension before the last item // This should not trigger any init, as it's not a candidate for anything. toAdd = new BaseTestChainedExtension(); chain.AddExtension(toAdd, insertBefore: arrBase); AssertInitializedCount(); // Remove the same one we added. chain.RemoveExtension(toAdd); AssertInitializedCount(); // This won't have any effect, all of the colors go to green which is to the left of the node we insert after toAdd = new ColorChainedExtension(); chain.AddExtension(toAdd, insertAfter: arrColor); AssertInitializedCount(); } // Final result is only no next ones being chained. noNextInitializedCount++; AssertInitializedCount(); using (chain.BatchModify()) { // This won't have any effect, all of the colors go to green, before color toAdd = new ColorChainedExtension(); chain.AddExtension(toAdd, insertBefore: arrColor); AssertInitializedCount(); // Removing green would cause colors and the base extensions to be rechained. chain.RemoveExtension(arrGreen); AssertInitializedCount(); // Adding a new square after the first one should rechain everything but shapes toAdd = new SquareChainedExtension(); chain.AddExtension(toAdd, insertAfter: arrSquare); AssertInitializedCount(); } // Final result is everything except shapes being rechained noNextInitializedCount++; baseInitializedCount++; colorInitializedCount++; AssertInitializedCount(); using (chain.BatchModify()) { // Removing the square rechain everything but shapes chain.RemoveExtension(toAdd); AssertInitializedCount(); // Adding a square at the beginning should rechain everything chain.AddExtension(toAdd, insertBefore: arrSquare); AssertInitializedCount(); // Then removing it should rechain everything. chain.RemoveExtension(toAdd); AssertInitializedCount(); } // Final result is everything rechained noNextInitializedCount++; baseInitializedCount++; colorInitializedCount++; shapeInitializedCount++; AssertInitializedCount(); void AssertInitializedCount() { Assert.AreEqual(noNextInitializedCount, square.InitializedCount); Assert.AreEqual(shapeInitializedCount, rectangle.InitializedCount); Assert.AreEqual(shapeInitializedCount, shape.InitializedCount); Assert.AreEqual(noNextInitializedCount, green.InitializedCount); Assert.AreEqual(colorInitializedCount, color.InitializedCount); Assert.AreEqual(noNextInitializedCount, chainedExtension.InitializedCount); } }