public static async Task GetProtocolAndExecuteViaReflectionAsync_TResult___Should_execute_the_operation___When_called()
        {
            // Arrange
            var operation1 = new SiblingOperation3
            {
                Value = 4,
            };

            var operation2 = new SiblingOperation4
            {
                Value = 6,
            };

            var operation3 = new SiblingOperation3
            {
                Value = 1,
            };

            var protocolFactory = new ProtocolFactory();

            protocolFactory.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => new SiblingOperationProtocol());

            // Act
            var actual1 = await protocolFactory.GetProtocolAndExecuteViaReflectionAsync <int>(operation1);

            var actual2 = await protocolFactory.GetProtocolAndExecuteViaReflectionAsync <int>(operation2);

            var actual3 = await protocolFactory.GetProtocolAndExecuteViaReflectionAsync <int>(operation3);

            // Assert
            actual1.AsTest().Must().BeEqualTo(7);
            actual2.AsTest().Must().BeEqualTo(10);
            actual3.AsTest().Must().BeEqualTo(4);
        }
        public static void GetProtocolAndExecuteViaReflection_void___Should_execute_the_operation___When_called()
        {
            // Arrange
            var operation1Counter = 4;
            var operation2Counter = 10;

            var operation1 = new SiblingOperation1
            {
                ActionToRun = () => operation1Counter++,
            };

            var operation2 = new SiblingOperation2
            {
                ActionToRun = () => operation2Counter++,
            };

            var protocolFactory = new ProtocolFactory();

            protocolFactory.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => new SiblingOperationProtocol());

            // Act, Assert
            protocolFactory.GetProtocolAndExecuteViaReflection(operation1);
            operation1Counter.AsTest().Must().BeEqualTo(5);

            protocolFactory.GetProtocolAndExecuteViaReflection(operation2);
            operation2Counter.AsTest().Must().BeEqualTo(11);

            protocolFactory.GetProtocolAndExecuteViaReflection(operation1);
            operation1Counter.AsTest().Must().BeEqualTo(6);
        }
Пример #3
0
        public static void ShallowClone___Should_return_a_shallow_clone___When_called()
        {
            // Arrange
            var systemUnderTest1 = new ProtocolFactory();
            var systemUnderTest2 = new ProtocolFactory();

            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();

            systemUnderTest1.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => protocol1);
            systemUnderTest2.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => protocol1);

            // Act
            var actual1 = systemUnderTest1.ShallowClone();
            var actual2 = systemUnderTest2.ShallowClone();

            actual1.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => protocol2);
            systemUnderTest2.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => protocol2);

            // Assert
            var operation1            = new GetProtocolOp(new SharedOperation());
            var protocolFromOriginal1 = systemUnderTest1.Execute(operation1);
            var protocolFromOriginal2 = systemUnderTest2.Execute(operation1);
            var protocolFromClone1    = actual1.Execute(operation1);
            var protocolFromClone2    = actual2.Execute(operation1);

            protocolFromOriginal1.AsTest().Must().BeSameReferenceAs(protocol1);
            protocolFromOriginal2.AsTest().Must().BeSameReferenceAs(protocol1);
            protocolFromClone1.AsTest().Must().BeSameReferenceAs(protocol1);
            protocolFromClone2.AsTest().Must().BeSameReferenceAs(protocol1);

            var operation2 = new GetProtocolOp(new SiblingOperation1(), MissingProtocolStrategy.ReturnNull);

            protocolFromOriginal1 = systemUnderTest1.Execute(operation2);
            protocolFromOriginal2 = systemUnderTest2.Execute(operation2);
            protocolFromClone1    = actual1.Execute(operation2);
            protocolFromClone2    = actual2.Execute(operation2);
            protocolFromOriginal1.AsTest().Must().BeNull();
            protocolFromOriginal2.AsTest().Must().BeSameReferenceAs(protocol2);
            protocolFromClone1.AsTest().Must().BeSameReferenceAs(protocol2);
            protocolFromClone2.AsTest().Must().BeNull();
        }
Пример #4
0
        public static void RegisterProtocolForSupportedOperations___Should_throw_ArgumentException___When_protocol_does_not_execute_any_operations()
        {
            // Arrange
            var systemUnderTest = new ProtocolFactory();

            // Act
            var actual = Record.Exception(() => systemUnderTest.RegisterProtocolForSupportedOperations(typeof(ProtocolWithNoOperations), A.Dummy <IProtocol>));

            // Assert
            actual.AsTest().Must().BeOfType <ArgumentException>();
            actual.Message.AsTest().Must().ContainString(Invariant($"Protocol '{nameof(ProtocolFactoryTest)}.{nameof(ProtocolWithNoOperations)}' does not execute any operations."));
        }
Пример #5
0
        public static void RegisterProtocolForSupportedOperations___Should_throw_ArgumentNullException___When_parameter_getProtocolFunc_is_null()
        {
            // Arrange
            var systemUnderTest = new ProtocolFactory();

            // Act
            var actual = Record.Exception(() => systemUnderTest.RegisterProtocolForSupportedOperations(A.Dummy <IProtocol>().GetType(), null));

            // Assert
            actual.AsTest().Must().BeOfType <ArgumentNullException>();
            actual.Message.AsTest().Must().ContainString("getProtocolFunc");
        }
Пример #6
0
        public static void RegisterProtocolForSupportedOperations___Should_throw_ArgumentException___When_parameter_protocolType_is_not_assignable_to_IProtocol()
        {
            // Arrange
            var systemUnderTest = new ProtocolFactory();

            // Act
            var actual = Record.Exception(() => systemUnderTest.RegisterProtocolForSupportedOperations(typeof(object), A.Dummy <IProtocol>));

            // Assert
            actual.AsTest().Must().BeOfType <ArgumentException>();
            actual.Message.AsTest().Must().ContainString("protocolType 'object' is not assignable to IProtocol");
        }
Пример #7
0
        public static void RegisterProtocolForSupportedOperations___Should_throw_ArgumentOutOfRangeException___When_parameter_protocolAlreadyRegisteredForOperationStrategy_is_Unknown()
        {
            // Arrange
            var systemUnderTest = new ProtocolFactory();

            // Act
            var actual = Record.Exception(() => systemUnderTest.RegisterProtocolForSupportedOperations(A.Dummy <IProtocol>().GetType(), () => A.Dummy <IProtocol>(), ProtocolAlreadyRegisteredForOperationStrategy.Unknown));

            // Assert
            actual.AsTest().Must().BeOfType <ArgumentOutOfRangeException>();
            actual.Message.AsTest().Must().ContainString("protocolAlreadyRegisteredForOperationStrategy");
            actual.Message.AsTest().Must().ContainString("Unknown");
        }
Пример #8
0
        public static void Execute___Should_throw_InvalidOperationException___When_getProtocolFunc_returns_null()
        {
            // Arrange
            var systemUnderTest = new ProtocolFactory();

            systemUnderTest.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => null);

            var operation = new GetProtocolOp(new SharedOperation());

            // Act
            var actual = Record.Exception(() => systemUnderTest.Execute(operation));

            // Assert
            actual.AsTest().Must().BeOfType <InvalidOperationException>();
            actual.Message.AsTest().Must().ContainString(Invariant($"The func to get the protocol for the following specified operation returned null: '{nameof(ProtocolFactoryTest)}.{nameof(SharedOperation)}'"));
        }
Пример #9
0
        private static void RegisterProtocols(
            Type typeForCoreCellOps,
            ConcurrentDictionary <Type, ConstructorInfo> typeToConstructorInfoCache,
            ProtocolFactory protocolFactory,
            Func <Type, ConstructorInfo> getConstructorInfoFunc,
            object[] constructorInfoParamsToInvoke)
        {
            if (!typeToConstructorInfoCache.TryGetValue(typeForCoreCellOps, out var constructorInfo))
            {
                constructorInfo = getConstructorInfoFunc(typeForCoreCellOps);

                typeToConstructorInfoCache.TryAdd(typeForCoreCellOps, constructorInfo);
            }

            var protocol = (IProtocol)constructorInfo.Invoke(constructorInfoParamsToInvoke);

            protocolFactory.RegisterProtocolForSupportedOperations(protocol.GetType(), () => protocol, ProtocolAlreadyRegisteredForOperationStrategy.Skip);
        }