/Src/AutoFixtureUnitTest/Kernel/ConstructorInvokerTest.cs
C# | 256 lines | 185 code | 21 blank | 50 comment | 12 complexity | 917cb755b11d3875a82c2d109662031a MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using Ploeh.AutoFixture.Kernel;
- using Ploeh.TestTypeFoundation;
- using Xunit;
-
- namespace Ploeh.AutoFixtureUnitTest.Kernel
- {
- #pragma warning disable 618
- public class ConstructorInvokerTest
- {
- [Fact]
- public void SutIsSpecimenBuilder()
- {
- // Fixture setup
- // Exercise system
- var sut = new ConstructorInvoker(new ModestConstructorQuery());
- // Verify outcome
- Assert.IsAssignableFrom<ISpecimenBuilder>(sut);
- // Teardown
- }
-
- [Fact]
- public void InitializeWithNullPickerThrows()
- {
- // Fixture setup
- // Exercise system and verify outcome
- Assert.Throws<ArgumentNullException>(() =>
- new ConstructorInvoker(null));
- // Teardown
- }
-
- [Fact]
- public void QueryIsCorrect()
- {
- // Fixture setup
- var expectedPicker = new DelegatingConstructorQuery();
- var sut = new ConstructorInvoker(expectedPicker);
- // Exercise system
- IConstructorQuery result = sut.Query;
- // Verify outcome
- Assert.Equal(expectedPicker, result);
- // Teardown
- }
-
- [Fact]
- public void CreateWithNullRequestWillReturnNull()
- {
- // Fixture setup
- var sut = new ConstructorInvoker(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
- }
-
- [Fact]
- public void CreateWithNullContainerWillThrow()
- {
- // Fixture setup
- var sut = new ConstructorInvoker(new ModestConstructorQuery());
- var dummyRequest = new object();
- // Exercise system and verify outcome
- Assert.Throws<ArgumentNullException>(() =>
- sut.Create(dummyRequest, null));
- // Teardown
- }
-
- [Fact]
- public void CreateFromNonTypeRequestWillReturnCorrectResult()
- {
- // Fixture setup
- var nonTypeRequest = new object();
- var dummyContainer = new DelegatingSpecimenContext();
- var sut = new ConstructorInvoker(new ModestConstructorQuery());
- // Exercise system
- var result = sut.Create(nonTypeRequest, dummyContainer);
- // Verify outcome
- var expectedResult = new NoSpecimen(nonTypeRequest);
- Assert.Equal(expectedResult, result);
- // Teardown
- }
-
- [Fact]
- public void CreateReturnsCorrectResultWhenQueryReturnsEmptyConstructors()
- {
- // Fixture setup
- var dummyRequest = typeof(object);
- var query = new DelegatingConstructorQuery { OnSelectConstructors = t => Enumerable.Empty<IMethod>() };
- var sut = new ConstructorInvoker(query);
- // Exercise system
- var dummyContext = new DelegatingSpecimenContext();
- var result = sut.Create(dummyRequest, dummyContext);
- // Verify outcome
- var expectedResult = new NoSpecimen(dummyRequest);
- Assert.Equal(expectedResult, result);
- // Teardown
- }
-
- [Fact]
- public void CreateFromTypeRequestWhenContainerCannotSatisfyParameterRequestWillReturnCorrectResult()
- {
- // Fixture setup
- var type = typeof(string);
- var container = new DelegatingSpecimenContext { OnResolve = r => new NoSpecimen(type) };
- var sut = new ConstructorInvoker(new ModestConstructorQuery());
- // Exercise system
- var result = sut.Create(type, container);
- // Verify outcome
- var expectedResult = new NoSpecimen(type);
- Assert.Equal(expectedResult, result);
- // Teardown
- }
-
- [Fact]
- public void CreateFromTypeWithNoPublicConstructorWhenContainerCanSatisfyRequestReturnsCorrectResult()
- {
- // Fixture setup
- var container = new DelegatingSpecimenContext { OnResolve = r => new object() };
- var sut = new ConstructorInvoker(new ModestConstructorQuery());
- // Exercise system
- var result = sut.Create(typeof(AbstractType), container);
- // Verify outcome
- var expectedResult = new NoSpecimen(typeof(AbstractType));
- Assert.Equal(expectedResult, result);
- // Teardown
- }
-
- [Fact]
- public void CreateFromTypeWhenParentCanGenerateOneParameterButNotTheOtherWillReturnCorrectNull()
- {
- // Fixture setup
- var requestedType = typeof(DoubleParameterType<string, int>);
- var parameters = requestedType.GetConstructors().Single().GetParameters();
- var container = new DelegatingSpecimenContext { OnResolve = r => parameters[0] == r ? new object() : new NoSpecimen(r) };
- var sut = new ConstructorInvoker(new ModestConstructorQuery());
- // Exercise system
- var result = sut.Create(requestedType, container);
- // Verify outcome
- Assert.IsAssignableFrom<NoSpecimen>(result);
- // Teardown
- }
-
- [Fact]
- 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 ConstructorInvoker(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
- }
-
- [Fact]
- 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.", "r");
- };
-
- var sut = new ConstructorInvoker(new ModestConstructorQuery());
- // Exercise system
- sut.Create(requestedType, contextMock);
- // Verify outcome
- Assert.True(mockVerified, "Mock verification");
- // Teardown
- }
-
- [Fact]
- 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 DelegatingConstructorQuery { OnSelectConstructors = t => new IMethod[] { new ConstructorMethod(ctor1), new ConstructorMethod(ctor2) } };
- var sut = new ConstructorInvoker(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;
- }
- }
- return new NoSpecimen(r);
- };
- // 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
- }
- }
- #pragma warning restore 618
- }