Ejemplo n.º 1
0
 internal void InitialiseBuilder(
     IFixture fixture,
     SpecimenProvider sut)
 {
     // Act
     Assert.Same(fixture, sut.Builder);
 }
 internal void InitialiseBuilder(
     IFixture fixture,
     SpecimenProvider sut)
 {
     // Act
     Assert.Same(fixture, sut.Builder);
 }
Ejemplo n.º 3
0
        internal void ApplyParameterFilters(
            SpecimenProvider specimenProvider,
            RegexFilter filter)
        {
            // Arrange
            filter.IncludeMethod("Compare", typeof(Uri))
            .ExcludeParameter("uri1", typeof(Uri))
            .ExcludeParameter("uri2", typeof(Uri), "Compare")
            .Rules.Add(new RegexRule("exclude all", type: new Regex(".*"), method: new Regex(".*")));

            IArgumentNullExceptionFixture sut =
                new ArgumentNullExceptionFixture(typeof(Uri).GetTypeInfo().Assembly,
                                                 specimenProvider,
                                                 new List <IFilter> {
                filter
            },
                                                 new List <IMapping>());

            // Act
            List <MethodData> methodDatas = sut.GetData().ToList();

            // Assert
            Assert.Equal(3, methodDatas.Count);
            Assert.Equal(3, methodDatas.Select(m => m.ExecutionSetup).OfType <DefaultExecutionSetup>().Count());
        }
        internal void ReturnSingleNullObjectArrayForSingleParameter(SpecimenProvider sut)
        {
            // Act
            object[] actualParameters = ((ISpecimenProvider) sut).GetParameterSpecimens(new ParameterInfo[1], 0);

            Assert.NotNull(actualParameters);
            Assert.Equal(1, actualParameters.Length);
            Assert.Null(actualParameters[0]);
        }
Ejemplo n.º 5
0
        internal void ReturnSingleNullObjectArrayForSingleParameter(SpecimenProvider sut)
        {
            // Act
            object[] actualParameters = ((ISpecimenProvider)sut).GetParameterSpecimens(new ParameterInfo[1], 0);

            Assert.NotNull(actualParameters);
            Assert.Single(actualParameters);
            Assert.Null(actualParameters[0]);
        }
 internal void ThrowWhenNoParameters(SpecimenProvider sut)
 {
     // AAA
     string actualParamName =
         Assert.Throws<ArgumentException>(
             () => ((ISpecimenProvider) sut).GetParameterSpecimens(new ParameterInfo[] {}, 0))
               .ParamName;
     Assert.Equal("parameters", actualParamName);
 }
 internal void AddGlobalCustomizations(
     IFixture fixture,
     SpecimenProvider sut)
 {
     // Act/Assert
     Assert.Same(fixture, sut.Builder);
     Assert.Empty(fixture.Behaviors.OfType<ThrowingRecursionBehavior>());
     Assert.Equal(1, fixture.Behaviors.OfType<OmitOnRecursionBehavior>().Count());
     Assert.True(fixture.OmitAutoProperties);
 }
Ejemplo n.º 8
0
        internal void ThrowWhenNoParameters(SpecimenProvider sut)
        {
            // AAA
            string actualParamName =
                Assert.Throws <ArgumentException>(
                    () => ((ISpecimenProvider)sut).GetParameterSpecimens(new ParameterInfo[] {}, 0))
                .ParamName;

            Assert.Equal("parameters", actualParamName);
        }
Ejemplo n.º 9
0
 internal void AddGlobalCustomizations(
     IFixture fixture,
     SpecimenProvider sut)
 {
     // Act/Assert
     Assert.Same(fixture, sut.Builder);
     Assert.Empty(fixture.Behaviors.OfType <ThrowingRecursionBehavior>());
     Assert.Single(fixture.Behaviors.OfType <OmitOnRecursionBehavior>());
     Assert.True(fixture.OmitAutoProperties);
 }
Ejemplo n.º 10
0
        internal void CreateGuidTypeSpecimen(
            [Frozen] Guid expectedSpecimen,
            SpecimenProvider sut)
        {
            // Act
            object actualSpecimen = ((ISpecimenProvider)sut).CreateInstance(expectedSpecimen.GetType());

            // Assert
            Assert.Equal(expectedSpecimen, actualSpecimen);
        }
        internal void ThrowWhenNullIndexOutOfBounds(
            Mock<ParameterInfo>[] parameterMocks,
            SpecimenProvider sut)
        {
            // Arrange
            List<ParameterInfo> parameters = parameterMocks.Select(pm => pm.Object).ToList();

            // Act/Assert
            string actualParamName =
                Assert.Throws<ArgumentException>(
                    () => ((ISpecimenProvider)sut).GetParameterSpecimens(parameters, parameters.Count))
                      .ParamName;
            Assert.Equal("nullIndex", actualParamName);
        }
Ejemplo n.º 12
0
        internal void ThrowWhenNullIndexOutOfBounds(
            Mock <ParameterInfo>[] parameterMocks,
            SpecimenProvider sut)
        {
            // Arrange
            List <ParameterInfo> parameters = parameterMocks.Select(pm => pm.Object).ToList();

            // Act/Assert
            string actualParamName =
                Assert.Throws <ArgumentException>(
                    () => ((ISpecimenProvider)sut).GetParameterSpecimens(parameters, parameters.Count))
                .ParamName;

            Assert.Equal("nullIndex", actualParamName);
        }
Ejemplo n.º 13
0
        internal void CreateParameterSpecimens(SpecimenProvider sut)
        {
            // Arrange
            Action <int, Guid, object, object, string> action = (i, g, o, n, s) => { };

            ParameterInfo[] parameters = action.GetMethodInfo().GetParameters();

            // Act
            object[] actualParameters = ((ISpecimenProvider)sut).GetParameterSpecimens(parameters, 3);

            // Act
            Assert.NotNull(actualParameters);
            Assert.Equal(5, actualParameters.Length);
            Assert.IsType(parameters[0].ParameterType, actualParameters[0]);
            Assert.IsType(parameters[1].ParameterType, actualParameters[1]);
            Assert.IsType(parameters[2].ParameterType, actualParameters[2]);
            Assert.Null(actualParameters[3]);
            Assert.IsType(parameters[4].ParameterType, actualParameters[4]);
        }
        internal void AddGlobalCustomizationsIsIdempotent(
            IFixture fixture)
        {
            // Arrange
            var throwingRecursionBehavior = fixture.Behaviors.OfType<ThrowingRecursionBehavior>().SingleOrDefault();
            if (throwingRecursionBehavior != null)
            {
                fixture.Behaviors.Remove(throwingRecursionBehavior);
                fixture.Behaviors.Add(new OmitOnRecursionBehavior());
            }
            Assert.False(fixture.OmitAutoProperties);

            // Act
            var sut = new SpecimenProvider(fixture);

            // Assert
            Assert.Same(fixture, sut.Builder);
            Assert.Empty(fixture.Behaviors.OfType<ThrowingRecursionBehavior>());
            Assert.Equal(1, fixture.Behaviors.OfType<OmitOnRecursionBehavior>().Count());
            Assert.True(fixture.OmitAutoProperties);
        }
        internal void CreateStaticMethodInvocationData(
            SpecimenProvider specimenProvider,
            RegexFilter filter)
        {
            // Arrange
            filter.IncludeMethod("Compare", typeof (Uri))
                  .Rules.Add(new RegexRule("exclude all", type: new Regex(".*"), method: new Regex(".*")));

            IArgumentNullExceptionFixture sut =
                new ArgumentNullExceptionFixture(typeof(Uri).Assembly,
                                                 specimenProvider,
                                                 new List<IFilter> { filter },
                                                 new List<IMapping>());

            // Act
            List<MethodData> methodDatas = sut.GetData().ToList();

            // Assert
            Assert.Equal(5, methodDatas.Count);
            Assert.Equal(5, methodDatas.Select(m => m.ExecutionSetup).OfType<DefaultExecutionSetup>().Count());
        }
Ejemplo n.º 16
0
        internal void AddGlobalCustomizationsIsIdempotent(
            IFixture fixture)
        {
            // Arrange
            ThrowingRecursionBehavior throwingRecursionBehavior = fixture.Behaviors.OfType <ThrowingRecursionBehavior>().SingleOrDefault();

            if (throwingRecursionBehavior != null)
            {
                fixture.Behaviors.Remove(throwingRecursionBehavior);
                fixture.Behaviors.Add(new OmitOnRecursionBehavior());
            }
            Assert.False(fixture.OmitAutoProperties);

            // Act
            var sut = new SpecimenProvider(fixture);

            // Assert
            Assert.Same(fixture, sut.Builder);
            Assert.Empty(fixture.Behaviors.OfType <ThrowingRecursionBehavior>());
            Assert.Single(fixture.Behaviors.OfType <OmitOnRecursionBehavior>());
            Assert.True(fixture.OmitAutoProperties);
        }
Ejemplo n.º 17
0
        internal void CreateInstanceMethodInvocationData(
            SpecimenProvider specimenProvider,
            RegexFilter filter)
        {
            // Arrange
            filter.IncludeMethod("GetComponents", typeof(Uri))
            .Rules.Add(new RegexRule("exclude all", type: new Regex(".*"), method: new Regex(".*")));

            IArgumentNullExceptionFixture sut =
                new ArgumentNullExceptionFixture(typeof(Uri).GetTypeInfo().Assembly,
                                                 specimenProvider,
                                                 new List <IFilter> {
                filter
            },
                                                 new List <IMapping>());

            // Act
            List <MethodData> methodDatas = sut.GetData().ToList();

            // Assert
            Assert.Equal(2, methodDatas.Count);
            Assert.Equal(2, methodDatas.Select(m => m.ExecutionSetup).OfType <DefaultExecutionSetup>().Count());
        }
        internal void CreateGuidTypeSpecimen(
            [Frozen] Guid expectedSpecimen,
            SpecimenProvider sut)
        {
            // Act
            object actualSpecimen = ((ISpecimenProvider)sut).CreateInstance(expectedSpecimen.GetType());

            // Assert
            Assert.Equal(expectedSpecimen, actualSpecimen);
        }
        internal void CreateParameterSpecimens(SpecimenProvider sut)
        {
            // Arrange
            Action<int, Guid, object, object, string> action = (i, g, o, n, s) => { };
            ParameterInfo[] parameters = action.Method.GetParameters();

            // Act
            object[] actualParameters = ((ISpecimenProvider)sut).GetParameterSpecimens(parameters, 3);

            // Act
            Assert.NotNull(actualParameters);
            Assert.Equal(5, actualParameters.Length);
            Assert.IsType(parameters[0].ParameterType, actualParameters[0]);
            Assert.IsType(parameters[1].ParameterType, actualParameters[1]);
            Assert.IsType(parameters[2].ParameterType, actualParameters[2]);
            Assert.Null(actualParameters[3]);
            Assert.IsType(parameters[4].ParameterType, actualParameters[4]);
        }
        /// <summary>
        /// Sets up the parameter data for the <paramref name="method"/> on the <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> the method belongs to.</param>
        /// <param name="method">The method.</param>
        /// <returns>The parameter data for the <paramref name="method"/> on the <paramref name="type"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="type"/> or <paramref name="method"/> parameters
        /// are <see langword="null"/>.</exception>
        private IEnumerable <MethodData> SetupParameterData(Type type, MethodBase method)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            ParameterInfo[] parameterInfos = method.GetParameters();
            var             data           = new List <MethodData>(parameterInfos.Length);

            for (int parameterIndex = 0; parameterIndex < parameterInfos.Length; ++parameterIndex)
            {
                ParameterInfo parameterInfo = parameterInfos[parameterIndex];

                // Apply the filters against the parameter.
                if (ParameterFilters.Any(filter => filter.ApplyFilter(type, method, parameterInfo)))
                {
                    continue;
                }

                try
                {
                    object[] parameters = SpecimenProvider.GetParameterSpecimens(parameterInfos, parameterIndex);

                    object instanceUnderTest = null;
                    if (!method.IsStatic)
                    {
                        instanceUnderTest = SpecimenProvider.CreateInstance(type);
                    }

                    data.Add(
                        new MethodData(
                            classUnderTest: type,
                            instanceUnderTest: instanceUnderTest,
                            methodUnderTest: method,
                            parameters: parameters,
                            nullParameter: parameterInfo.Name,
                            nullIndex: parameterIndex,
                            executionSetup: new DefaultExecutionSetup()));
                }
                catch (Exception ex)
                {
                    var compositionEx = new CompositionException(type, method, parameterInfo.Name, ex);
                    data.Add(
                        new MethodData(
                            classUnderTest: type,
                            instanceUnderTest: null,
                            methodUnderTest: method,
                            parameters: new object[] { },
                            nullParameter: parameterInfo.Name,
                            nullIndex: parameterIndex,
                            executionSetup: new ErroredExecutionSetup(compositionEx)));
                }
            }

            return(data);
        }