public void VerifyConstructorInvokesBehaviorExpectationWithCorrectMethod(MemberRef <ConstructorInfo> ctorRef)
        {
            // Arrange
            var ctor       = ctorRef.Member;
            var parameters = ctor.GetParameters();

            var expectation = new DelegatingBehaviorExpectation
            {
                OnVerify = c =>
                {
                    var unwrapper = Assert.IsAssignableFrom <ReflectionExceptionUnwrappingCommand>(c);
                    var methodCmd = Assert.IsAssignableFrom <MethodInvokeCommand>(unwrapper.Command);

                    var ctorMethod = Assert.IsAssignableFrom <ConstructorMethod>(methodCmd.Method);
                    Assert.Equal(ctor, ctorMethod.Constructor);
                    Assert.True(parameters.SequenceEqual(ctorMethod.Parameters));
                }
            };

            var sut = new GuardClauseAssertion(new Fixture(), expectation);

            // Act
            sut.Verify(ctor);
            // Assert (done by mock)
        }
        public void VerifyConstructorInvokesBehaviorExpectationWithCorrectParameterInfo(MemberRef <ConstructorInfo> ctorRef)
        {
            // Arrange
            var ctor       = ctorRef.Member;
            var parameters = ctor.GetParameters();

            var observedParameters = new List <ParameterInfo>();
            var expectation        = new DelegatingBehaviorExpectation
            {
                OnVerify = c =>
                {
                    var unwrapper = Assert.IsAssignableFrom <ReflectionExceptionUnwrappingCommand>(c);
                    var methodCmd = Assert.IsAssignableFrom <MethodInvokeCommand>(unwrapper.Command);

                    observedParameters.Add(methodCmd.ParameterInfo);
                }
            };

            var sut = new GuardClauseAssertion(new Fixture(), expectation);

            // Act
            sut.Verify(ctor);
            // Assert
            Assert.Equal(parameters, observedParameters);
        }
        public void VerifyConstructorInvokesBehaviorExpectationWithCorrectReplacementIndices(MemberRef <ConstructorInfo> ctorRef)
        {
            // Arrange
            var ctor       = ctorRef.Member;
            var parameters = ctor.GetParameters();

            var observedIndices = new List <int>();
            var expectation     = new DelegatingBehaviorExpectation
            {
                OnVerify = c =>
                {
                    var unwrapper = Assert.IsAssignableFrom <ReflectionExceptionUnwrappingCommand>(c);
                    var methodCmd = Assert.IsAssignableFrom <MethodInvokeCommand>(unwrapper.Command);

                    var replacement = Assert.IsAssignableFrom <IndexedReplacement <object> >(methodCmd.Expansion);
                    observedIndices.Add(replacement.ReplacementIndex);
                }
            };

            var sut = new GuardClauseAssertion(new Fixture(), expectation);

            // Act
            sut.Verify(ctor);
            // Assert
            var expectedIndices = Enumerable.Range(0, parameters.Length).ToArray();

            Assert.Equal(expectedIndices, observedIndices);
        }
        public void VerifyMethodInvokesBehaviorExpectationWithCorrectMethod(MemberRef <MethodInfo> methodRef)
        {
            // Arrange
            var method     = methodRef.Member;
            var parameters = method.GetParameters();

            var expectation = new DelegatingBehaviorExpectation
            {
                OnVerify = c =>
                {
                    var unwrapper = Assert.IsAssignableFrom <ReflectionExceptionUnwrappingCommand>(c);
                    var methodCmd = Assert.IsAssignableFrom <MethodInvokeCommand>(unwrapper.Command);

                    var instanceMethod = Assert.IsAssignableFrom <InstanceMethod>(methodCmd.Method);
                    Assert.Equal(method, instanceMethod.Method);
                    Assert.IsAssignableFrom(method.DeclaringType, instanceMethod.Owner);
                    Assert.True(parameters.SequenceEqual(instanceMethod.Parameters));
                }
            };

            var sut = new GuardClauseAssertion(new Fixture(), expectation);

            // Act
            sut.Verify(method);
            // Assert (done by mock)
        }
        public void VerifyPropertyCorrectlyInvokesBehaviorExpectation()
        {
            // Arrange
            var fixture = new Fixture();
            var owner   = fixture.Freeze <PropertyHolder <Version> >(c => c.OmitAutoProperties());

            fixture.Freeze <Version>();

            var property = owner.GetType().GetProperty("Property");

            var mockVerified = false;
            var expectation  = new DelegatingBehaviorExpectation
            {
                OnVerify = c =>
                {
                    var unwrapper = Assert.IsAssignableFrom <ReflectionExceptionUnwrappingCommand>(c);
                    var setterCmd = Assert.IsAssignableFrom <PropertySetCommand>(unwrapper.Command);
                    mockVerified = setterCmd.PropertyInfo.Equals(property) &&
                                   setterCmd.Owner.Equals(owner);
                }
            };
            var sut = new GuardClauseAssertion(fixture, expectation);

            // Act
            sut.Verify(property);
            // Assert
            Assert.True(mockVerified, "Mock verified.");
        }
        public void DynamicDummyTypeIfReturnMethodIsCalledReturnsAnonymousValue()
        {
            // Arrange
            Fixture fixture     = new Fixture();
            var     objectValue = fixture.Freeze <object>();
            var     intValue    = fixture.Freeze <int>();

            bool mockVerification    = false;
            var  behaviorExpectation = new DelegatingBehaviorExpectation()
            {
                OnVerify = c =>
                {
                    var dynamicInstance = (IDynamicInstanceTestType)GetParameters(c).ElementAt(0);
                    Assert.Equal(objectValue, dynamicInstance.Property);
                    Assert.Equal(intValue, dynamicInstance.ReturnMethod(null, 123));
                    mockVerification = true;
                }
            };

            var sut        = new GuardClauseAssertion(fixture, behaviorExpectation);
            var methodInfo = typeof(DynamicInstanceTestConstraint <>).GetMethod("Method");

            // Act
            sut.Verify(methodInfo);
            // Assert
            Assert.True(mockVerification, "mock verification.");
        }
        public void BehaviorExpectationIsCorrectFromGreedyConstructor()
        {
            // Arrange
            var dummyComposer             = new Fixture();
            IBehaviorExpectation expected = new DelegatingBehaviorExpectation();
            var sut = new GuardClauseAssertion(dummyComposer, expected);
            // Act
            IBehaviorExpectation result = sut.BehaviorExpectation;

            // Assert
            Assert.Equal(expected, result);
        }
        public void ComposerIsCorrectFromGreedyConstructor()
        {
            // Arrange
            ISpecimenBuilder expectedBuilder = new Fixture();
            var dummyExpectation             = new DelegatingBehaviorExpectation();
            var sut = new GuardClauseAssertion(expectedBuilder, dummyExpectation);
            // Act
            ISpecimenBuilder result = sut.Builder;

            // Assert
            Assert.Equal(expectedBuilder, result);
        }
        public void VerifyMethodIgnoresEquals(Type type, Type argumentType)
        {
            // Arrange
            var method = type.GetMethod("Equals", new[] { argumentType });

            var invoked     = false;
            var expectation = new DelegatingBehaviorExpectation {
                OnVerify = c => invoked = true
            };

            var sut = new GuardClauseAssertion(new Fixture(), expectation);

            // Act
            sut.Verify(method);
            // Assert
            Assert.False(invoked);
        }
        public void DynamicDummyTypeIfVoidMethodIsCalledDoesNotThrows()
        {
            // Arrange
            bool mockVerification    = false;
            var  behaviorExpectation = new DelegatingBehaviorExpectation
            {
                OnVerify = c =>
                {
                    var dynamicInstance = (IDynamicInstanceTestType)GetParameters(c).ElementAt(0);
                    Assert.Null(Record.Exception(() => dynamicInstance.VoidMethod(null, 123)));
                    Assert.Null(Record.Exception(() => { dynamicInstance.Property = new object(); }));
                    mockVerification = true;
                }
            };
            var sut        = new GuardClauseAssertion(new Fixture(), behaviorExpectation);
            var methodInfo = typeof(DynamicInstanceTestConstraint <>).GetMethod("Method");

            // Act
            sut.Verify(methodInfo);
            // Assert
            Assert.True(mockVerification, "mock verification.");
        }
        public void VerifyConstructorInvokesBehaviorExpectationWithCorrectParametersForReplacement(MemberRef <ConstructorInfo> ctorRef)
        {
            // Arrange
            var ctor       = ctorRef.Member;
            var parameters = ctor.GetParameters();

            var expectation = new DelegatingBehaviorExpectation
            {
                OnVerify = c =>
                {
                    var unwrapper = Assert.IsAssignableFrom <ReflectionExceptionUnwrappingCommand>(c);
                    var methodCmd = Assert.IsAssignableFrom <MethodInvokeCommand>(unwrapper.Command);

                    var replacement = Assert.IsAssignableFrom <IndexedReplacement <object> >(methodCmd.Expansion);
                    Assert.True(replacement.Source.Select(x => x.GetType()).SequenceEqual(parameters.Select(p => p.ParameterType)));
                }
            };

            var sut = new GuardClauseAssertion(new Fixture(), expectation);

            // Act
            sut.Verify(ctor);
            // Assert (done by mock)
        }