public static void Execute___Should_return_registered_protocol___When_called() { // Arrange var systemUnderTest = new ChainOfResponsibilityProtocolFactory(); IProtocol protocol1 = new SharedOperationProtocol1(); IProtocol protocol2 = new SiblingOperationProtocol(); IProtocol protocol3 = new SharedOperationProtocol2(); var protocolFactory1 = protocol1.ToProtocolFactory(); var protocolFactory2 = protocol2.ToProtocolFactory(); var protocolFactory3 = protocol3.ToProtocolFactory(); systemUnderTest.AddToEndOfChain(protocolFactory1); systemUnderTest.AddToEndOfChain(protocolFactory2); systemUnderTest.AddToEndOfChain(protocolFactory3); var operation1 = new GetProtocolOp(new SharedOperation()); var operation2 = new GetProtocolOp(new SiblingOperation1()); var operation3 = new GetProtocolOp(new SiblingOperation2()); // Act var actual1 = systemUnderTest.Execute(operation1); var actual2 = systemUnderTest.Execute(operation2); var actual3 = systemUnderTest.Execute(operation3); // Assert actual1.AsTest().Must().BeSameReferenceAs(protocol1); actual2.AsTest().Must().BeSameReferenceAs(protocol2); actual3.AsTest().Must().BeSameReferenceAs(protocol2); }
private static ChainOfResponsibilityProtocolFactory BuildProtocolFactoryToExecuteAllOperations( this ReportCache reportCache, DateTime timestampUtc, IReadOnlyCollection <Func <IProtocolFactory, IProtocolFactory> > protocolFactoryFuncs, IReadOnlyCollection <Type> additionalTypesForCoreCellOps, Func <RecalcPhase> getRecalcPhaseFunc) { protocolFactoryFuncs = protocolFactoryFuncs ?? new List <Func <IProtocolFactory, IProtocolFactory> >(); if (protocolFactoryFuncs.Any(_ => _ == null)) { throw new ArgumentException(Invariant($"{nameof(protocolFactoryFuncs)} contains a null element.")); } additionalTypesForCoreCellOps = additionalTypesForCoreCellOps ?? new List <Type>(); if (additionalTypesForCoreCellOps.Any(_ => _ == null)) { throw new ArgumentException(Invariant($"{nameof(additionalTypesForCoreCellOps)} contains a null element.")); } var result = new ChainOfResponsibilityProtocolFactory(); // Add caller's protocols to the chain of responsibility. foreach (var protocolFactoryFunc in protocolFactoryFuncs) { result.AddToEndOfChain(protocolFactoryFunc(result)); } // Add DataStructureCellProtocols{TValue} and DataStructureConvenienceProtocols{TResult} to chain of responsibility. var typesForCoreCellOps = DefaultTypesSupportedForCoreCellOps .Concat(additionalTypesForCoreCellOps) .ToList(); var coreProtocolsFactory = new ProtocolFactory(); ConstructorInfo GetCellProtocolsFunc(Type type) => typeof(DataStructureCellProtocols <>).MakeGenericType(type).GetConstructors().Single(); ConstructorInfo GetConvenienceProtocolsFunc(Type type) => typeof(DataStructureConvenienceProtocols <>).MakeGenericType(type).GetConstructors().Single(); var cellProtocolsConstructorInfoParams = new object[] { reportCache, result, timestampUtc, getRecalcPhaseFunc }; var convenienceProtocolsConstructorInfoParams = new object[] { result }; foreach (var typeForCoreCellOps in typesForCoreCellOps) { RegisterProtocols(typeForCoreCellOps, CachedTypeToCellProtocolsConstructorInfoMap, coreProtocolsFactory, GetCellProtocolsFunc, cellProtocolsConstructorInfoParams); RegisterProtocols(typeForCoreCellOps, CachedTypeToConvenienceProtocolsConstructorInfoMap, coreProtocolsFactory, GetConvenienceProtocolsFunc, convenienceProtocolsConstructorInfoParams); } result.AddToEndOfChain(coreProtocolsFactory); return(result); }
public static void AddToEndOfChain___Should_throw_ArgumentNullException___When_parameter_protocolFactory_is_null() { // Arrange var systemUnderTest = new ChainOfResponsibilityProtocolFactory(); // Act var actual = Record.Exception(() => systemUnderTest.AddToEndOfChain(null)); // Assert actual.AsTest().Must().BeOfType <ArgumentNullException>(); actual.Message.AsTest().Must().ContainString("protocolFactory"); }