Exemplo n.º 1
0
        public void CreateFromTypeWhenParentCanGenerateBothParametersWillReturnCorrectResult()
        {
            // Fixture setup
            var expectedParameterValues = new object[] { 1, 2m };
            var parameterQueue = new Queue<object>(expectedParameterValues);

            var requestedType = typeof(DoubleParameterType<int, decimal>);
            var parameters = requestedType.GetConstructors().Single().GetParameters();

            var container = new DelegatingSpecimenContext();
            container.OnResolve = r =>
            {
                if (parameters.Any(r.Equals))
                {
                    return parameterQueue.Dequeue();
                }
                return null;
            };

            var sut = new MethodInvoker(new ModestConstructorQuery());
            // Exercise system
            var result = sut.Create(requestedType, container);
            // Verify outcome
            var actual = (DoubleParameterType<int, decimal>)result;
            Assert.Equal(expectedParameterValues[0], actual.Parameter1);
            Assert.Equal(expectedParameterValues[1], actual.Parameter2);
            // Teardown
        }
Exemplo n.º 2
0
 public void SutIsSpecimenBuilder()
 {
     // Fixture setup
     // Exercise system
     var sut = new MethodInvoker(new ModestConstructorQuery());
     // Verify outcome
     Assert.IsAssignableFrom<ISpecimenBuilder>(sut);
     // Teardown
 }
Exemplo n.º 3
0
 public void CreateWithNullContainerWillThrow()
 {
     // Fixture setup
     var sut = new MethodInvoker(new ModestConstructorQuery());
     var dummyRequest = new object();
     // Exercise system and verify outcome
     Assert.Throws<ArgumentNullException>(() =>
         sut.Create(dummyRequest, null));
     // Teardown
 }
Exemplo n.º 4
0
 public void QueryIsCorrect()
 {
     // Fixture setup
     var expectedPicker = new DelegatingMethodQuery();
     var sut = new MethodInvoker(expectedPicker);
     // Exercise system
     IMethodQuery result = sut.Query;
     // Verify outcome
     Assert.Equal(expectedPicker, result);
     // Teardown
 }
Exemplo n.º 5
0
 public void CreateWithNullRequestWillReturnNull()
 {
     // Fixture setup
     var sut = new MethodInvoker(new ModestConstructorQuery());
     // Exercise system
     var dummyContainer = new DelegatingSpecimenContext();
     var result = sut.Create(null, dummyContainer);
     // Verify outcome
     var expectedResult = new NoSpecimen();
     Assert.Equal(expectedResult, result);
     // Teardown
 }
        /// <summary>
        /// Customizes the specified fixture by modifying <see cref="TargetType"/> to use
        /// <see cref="Query"/> as the strategy for creating new specimens.
        /// </summary>
        /// <param name="fixture">The fixture to customize.</param>
        public void Customize(IFixture fixture)
        {
            if (fixture == null)
            {
                throw new ArgumentNullException("fixture");
            }

            var factory = new MethodInvoker(this.Query);
            var composer = new TypedBuilderComposer(this.TargetType, factory);

            fixture.Customizations.Insert(0, composer.Compose());
        }
        /// <summary>
        /// Customizes the specified fixture by modifying <see cref="TargetType"/> to use
        /// <see cref="Query"/> as the strategy for creating new specimens.
        /// </summary>
        /// <param name="fixture">The fixture to customize.</param>
        public void Customize(IFixture fixture)
        {
            if (fixture == null)
            {
                throw new ArgumentNullException("fixture");
            }

            var factory = new MethodInvoker(this.Query);
            var builder = SpecimenBuilderNodeFactory.CreateTypedNode(
                this.targetType,
                factory);

            fixture.Customizations.Insert(0, builder);
        }
Exemplo n.º 8
0
 public void CreateFromNonTypeRequestWillReturnCorrectResult()
 {
     // Fixture setup
     var nonTypeRequest = new object();
     var dummyContainer = new DelegatingSpecimenContext();
     var sut = new MethodInvoker(new ModestConstructorQuery());
     // Exercise system
     var result = sut.Create(nonTypeRequest, dummyContainer);
     // Verify outcome
     #pragma warning disable 618
     var expectedResult = new NoSpecimen(nonTypeRequest);
     #pragma warning restore 618
     Assert.Equal(expectedResult, result);
     // Teardown
 }
        /// <summary>
        /// Customizes the fixture by creating a <see cref="targetType"/> that has no auto populated properties.
        /// </summary>
        /// <param name="fixture">The fixture to customize.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="fixture"/> is null.
        /// </exception>
        public void Customize(IFixture fixture)
        {
            if (fixture == null)
            {
                throw new ArgumentNullException(nameof(fixture));
            }

            var constructor = new MethodInvoker(new ModestConstructorQuery());

            var builder = SpecimenBuilderNodeFactory.CreateTypedNode(
                this.targetType, constructor);

            fixture.Customizations.Insert(0, builder);

        }
Exemplo n.º 10
0
 public void CreateFromTypeRequestWhenContainerCannotSatisfyParameterRequestWillReturnCorrectResult()
 {
     // Fixture setup
     var type = typeof(string);
     #pragma warning disable 618
     var container = new DelegatingSpecimenContext { OnResolve = r => new NoSpecimen(type) };
     #pragma warning restore 618
     var sut = new MethodInvoker(new ModestConstructorQuery());
     // Exercise system
     var result = sut.Create(type, container);
     // Verify outcome
     #pragma warning disable 618
     var expectedResult = new NoSpecimen(type);
     #pragma warning restore 618
     Assert.Equal(expectedResult, result);
     // Teardown
 }
Exemplo n.º 11
0
 public void CreateFromTypeWhenParentCanGenerateOneParameterButNotTheOtherWillReturnCorrectNull()
 {
     // Fixture setup
     var requestedType = typeof(DoubleParameterType<string, int>);
     var parameters = requestedType.GetConstructors().Single().GetParameters();
     #pragma warning disable 618
     var container = new DelegatingSpecimenContext { OnResolve = r => parameters[0] == r ? new object() : new NoSpecimen(r) };
     #pragma warning restore 618
     var sut = new MethodInvoker(new ModestConstructorQuery());
     // Exercise system
     var result = sut.Create(requestedType, container);
     // Verify outcome
     Assert.IsAssignableFrom<NoSpecimen>(result);
     // Teardown
 }
Exemplo n.º 12
0
 public void CreateReturnsCorrectResultWhenQueryReturnsEmptyConstructors()
 {
     // Fixture setup
     var dummyRequest = typeof(object);
     var query = new DelegatingMethodQuery { OnSelectMethods = t => Enumerable.Empty<IMethod>() };
     var sut = new MethodInvoker(query);
     // Exercise system
     var dummyContext = new DelegatingSpecimenContext();
     var result = sut.Create(dummyRequest, dummyContext);
     // Verify outcome
     #pragma warning disable 618
     var expectedResult = new NoSpecimen(dummyRequest);
     #pragma warning restore 618
     Assert.Equal(expectedResult, result);
     // Teardown
 }
Exemplo n.º 13
0
 public void CreateFromTypeWithNoPublicConstructorWhenContainerCanSatisfyRequestReturnsCorrectResult()
 {
     // Fixture setup
     var container = new DelegatingSpecimenContext { OnResolve = r => new object() };
     var sut = new MethodInvoker(new ModestConstructorQuery());
     // Exercise system
     var result = sut.Create(typeof(AbstractType), container);
     // Verify outcome
     #pragma warning disable 618
     var expectedResult = new NoSpecimen(typeof(AbstractType));
     #pragma warning restore 618
     Assert.Equal(expectedResult, result);
     // Teardown
 }
Exemplo n.º 14
0
        public void CreateFromTypeWillInvokeContainerCorrectly()
        {
            // Fixture setup
            var requestedType = typeof(DoubleParameterType<long, short>);
            var parameters = requestedType.GetConstructors().Single().GetParameters();

            var mockVerified = false;
            var contextMock = new DelegatingSpecimenContext();
            contextMock.OnResolve = r =>
            {
                if (parameters.Any(r.Equals))
                {
                    mockVerified = true;
                    var pType = ((ParameterInfo)r).ParameterType;
                    if (typeof(long) == pType)
                    {
                        return new long();
                    }
                    if (typeof(short) == pType)
                    {
                        return new short();
                    }
                }
                throw new ArgumentException("Unexpected container request.", nameof(r));
            };

            var sut = new MethodInvoker(new ModestConstructorQuery());
            // Exercise system
            sut.Create(requestedType, contextMock);
            // Verify outcome
            Assert.True(mockVerified, "Mock verification");
            // Teardown
        }
Exemplo n.º 15
0
 public void SutIsCorrectInitialGraph()
 {
     // Fixture setup
     var sut = SpecimenBuilderNodeFactory.CreateComposer<int>();
     // Exercise system
     // Verify outcome
     var factory = new MethodInvoker(new ModestConstructorQuery());
     var expected = new NodeComposer<int>(
         SpecimenBuilderNodeFactory.CreateTypedNode(typeof(int), factory));
     Assert.True(expected.GraphEquals(sut, new NodeComparer()));
     // Teardown
 }
Exemplo n.º 16
0
        public void CreateAndAddProperyValues()
        {
            // Fixture setup
            var ctorInvoker = new MethodInvoker(new ModestConstructorQuery());
            var strCmd = new BindingCommand<DoublePropertyHolder<string, int>, string>(ph => ph.Property1);
            var intCmd = new BindingCommand<DoublePropertyHolder<string, int>, int>(ph => ph.Property2);
            var strPostprocessor = new Postprocessor<DoublePropertyHolder<string, int>>(ctorInvoker, strCmd);
            var intPostprocessor = new Postprocessor<DoublePropertyHolder<string, int>>(strPostprocessor, intCmd);

            var builder = new CompositeSpecimenBuilder(
                new FilteringSpecimenBuilder(intPostprocessor, new ExactTypeSpecification(typeof(DoublePropertyHolder<string, int>))),
                Scenario.CreateAutoPropertyBuilder());
            var container = new SpecimenContext(builder);
            // Exercise system
            var result = container.Resolve(typeof(DoublePropertyHolder<string, int>));
            // Verify outcome
            var actual = Assert.IsAssignableFrom<DoublePropertyHolder<string, int>>(result);
            Assert.False(string.IsNullOrEmpty(actual.Property1), "Property1");
            Assert.Equal(1, actual.Property2);
            // Teardown
        }
Exemplo n.º 17
0
        public void CreateFromTypeWillUseFirstConstructorItCanSatisfy()
        {
            // Fixture setup
            var requestedType = typeof(MultiUnorderedConstructorType);
            var ctor1 = requestedType.GetConstructor(new[] { typeof(MultiUnorderedConstructorType.ParameterObject) });
            var ctor2 = requestedType.GetConstructor(new[] { typeof(string), typeof(int) });

            var picker = new DelegatingMethodQuery { OnSelectMethods = t => new IMethod[] { new ConstructorMethod(ctor1), new ConstructorMethod(ctor2) } };
            var sut = new MethodInvoker(picker);

            var ctor2Params = ctor2.GetParameters();
            var expectedText = "Anonymous text";
            var expectedNumber = 14;

            var context = new DelegatingSpecimenContext();
            context.OnResolve = r =>
            {
                if (ctor2Params.Any(r.Equals))
                {
                    var pType = ((ParameterInfo)r).ParameterType;
                    if (typeof(string) == pType)
                    {
                        return expectedText;
                    }
                    if (typeof(int) == pType)
                    {
                        return expectedNumber;
                    }
                }
            #pragma warning disable 618
                return new NoSpecimen(r);
            #pragma warning restore 618
            };
            // Exercise system
            var result = sut.Create(requestedType, context);
            // Verify outcome
            var actual = Assert.IsAssignableFrom<MultiUnorderedConstructorType>(result);
            Assert.Equal(expectedText, actual.Text);
            Assert.Equal(expectedNumber, actual.Number);
            // Teardown
        }