Esempio n. 1
0
    public object?Dummy(GenerationRequest request, Type type)
    {
        var smartType = SmartType.For(type);

        if (type.IsPrimitive)
        {
            return(_unconstrainedChain.Resolve(this, request, type));
        }

        if (type == typeof(string))
        {
            return(_unconstrainedChain.Resolve(this, request, type));
        }

        var emptyCollectionInstantiation = new EmptyCollectionInstantiation();

        if (smartType.IsImplementationOfOpenGeneric(typeof(IEnumerable <>)))
        {
            return(emptyCollectionInstantiation.CreateCollectionPassedAsGenericType(type));
        }

        if (smartType.IsOpenGeneric(typeof(IEnumerable <>)))
        {
            return(emptyCollectionInstantiation.EmptyEnumerableOf(type.GetCollectionItemType()));
        }

        if (type.IsAbstract)
        {
            return(default);
        public void ShouldReportWhetherTypeHasAtMostGivenConstructorCount(
            int maxCount, bool expectedResult)
        {
            var type = SmartType.For(typeof(string));

            XAssert.Equal(expectedResult, type.HasPublicConstructorCountOfAtMost(maxCount));
        }
Esempio n. 3
0
        public static void NoStaticFields(Type type)
        {
            var staticFields = new List <IFieldWrapper>(SmartType.For(type).GetAllStaticFields());

            staticFields.Should()
            .BeEmpty("SmartType " + type + " should not contain static fields, but: " + Environment.NewLine +
                     StringFrom(staticFields));
        }
Esempio n. 4
0
 private GenericFakeChainFactory <T> CreateGenericFakeChainFactory <T>()
 {
     return(new GenericFakeChainFactory <T>(CreateSpecialCasesOfResolutions <T>(), new FallbackTypeGenerator <T>(new FallbackTypeGenerator(
                                                                                                                     new IFallbackGeneratedObjectCustomization[]
     {
         new FillPropertiesCustomization(),
         new FillFieldsCustomization()
     }, SmartType.For(typeof(T))))));
 }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var properties = SmartType.For(_type).GetAllPublicInstanceProperties();

            foreach (var item in properties.Where(item => item.HasPublicSetter()))
            {
                violations.Add(item.ShouldNotBeMutableButIs());
            }
        }
        public static AndConstraint <TypeAssertions> NotHaveStaticFields(this TypeAssertions assertions)
        {
            Type type         = assertions.Subject;
            var  staticFields = new List <IAmField>(SmartType.For(type).GetAllStaticFields());

            staticFields.Should()
            .BeEmpty("SmartType " + type + " should not contain static fields, but: " + Environment.NewLine +
                     ReflectionElementsList.Format(staticFields));
            return(new AndConstraint <TypeAssertions>(assertions));
        }
Esempio n. 7
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var constructors          = SmartType.For(_type).GetAllPublicConstructors();
            var fallbackTypeGenerator = new FallbackTypeGenerator(_type);

            foreach (var constructor in constructors)
            {
                AssertNullCheckForEveryPossibleArgumentOf(violations, constructor, fallbackTypeGenerator);
            }
        }
Esempio n. 8
0
 public static void AssertEqualityOperatorIsDefinedFor(Type type)
 {
     try
     {
         SmartType.For(type).EqualityOperator();
     }
     catch (Exception e)
     {
         throw new InvalidOperationException("Expected no exception when retrieving equality operator, but got " + e);
     }
 }
        public static AndConstraint <TypeAssertions> HaveEventWithShortName(this TypeAssertions assertions,
                                                                            string eventName)
        {
            var allEvents = SmartType.For(assertions.Subject).GetAllEvents();

            allEvents.Should().Match(
                events => events.Any(ev => ev.HasName(eventName)),
                "Type " + assertions.Subject + " should define expected event " + eventName);

            return(new AndConstraint <TypeAssertions>(assertions));
        }
Esempio n. 10
0
        private void CheckImmutability(ConstraintsViolations violations, Type type)
        {
            var fields        = SmartType.For(type).GetAllInstanceFields().ToList();
            var fieldWrappers = fields
                                .Where(item => item.IsNotDeveloperDefinedReadOnlyField());

            foreach (var item in fieldWrappers)
            {
                violations.Add(item.ShouldNotBeMutableButIs());
            }
        }
Esempio n. 11
0
        public static void HasNullProtectedConstructors <T>()
        {
            var type = SmartType.For(typeof(T));

            if (!type.HasConstructorWithParameters())
            {
                var constraints = new List <IConstraint> {
                    new ConstructorsMustBeNullProtected(type.ToClrType())
                };
                TypeAdheresTo(constraints);
            }
        }
Esempio n. 12
0
        public T SubstituteOf <T>() where T : class
        {
            var type = typeof(T);
            var sub  = Substitute.For <T>();

            var methods = SmartType.For(type).GetAllPublicInstanceMethodsWithReturnValue();

            foreach (var method in methods)
            {
                method.InvokeWithAnyArgsOn(sub, Instance).ReturnsForAnyArgs(method.GenerateAnyReturnValue(type1 => Instance(type1)));
            }
            return(sub);
        }
Esempio n. 13
0
        public static void NoStaticFields(Assembly assembly)
        {
            var staticFields = new List <IFieldWrapper>();

            foreach (var type in assembly.GetTypes())
            {
                staticFields.AddRange(SmartType.For(type).GetAllStaticFields());
            }

            staticFields.Should()
            .BeEmpty("assembly " + assembly + " should not contain static fields, but: " + Environment.NewLine +
                     StringFrom(staticFields));
        }
Esempio n. 14
0
        public static void NoNonPublicEvents(Assembly assembly)
        {
            var nonPublicEvents = new List <IEventWrapper>();

            foreach (var type in assembly.GetTypes())
            {
                nonPublicEvents.AddRange(SmartType.For(type).GetAllNonPublicEventsWithoutExplicitlyImplemented());
            }

            nonPublicEvents.Should()
            .BeEmpty("assembly " + assembly + " should not contain non-public events, but: " + Environment.NewLine +
                     StringFrom(nonPublicEvents));
        }
Esempio n. 15
0
        public static void HasUniqueConstants <T>()
        {
            string errors    = "";
            var    constants = SmartType.For(typeof(T)).GetAllConstants();

            foreach (var constant in constants)
            {
                foreach (var otherConstant in constants)
                {
                    constant.AssertNotDuplicateOf(otherConstant);
                }
            }
        }
        public static AndConstraint <TypeAssertions> HaveUniqueConstants(this TypeAssertions assertions)
        {
            var constants = SmartType.For(assertions.Subject).GetAllConstants();

            foreach (var constant in constants)
            {
                foreach (var otherConstant in constants)
                {
                    constant.AssertNotDuplicateOf(otherConstant);
                }
            }

            return(new AndConstraint <TypeAssertions>(assertions));
        }
Esempio n. 17
0
        public static AndConstraint <AssemblyAssertions> NotHaveStaticFields(this AssemblyAssertions assertions)
        {
            Assembly assembly     = assertions.Subject;
            var      staticFields = new List <IAmField>();

            foreach (var type in assembly.GetTypes())
            {
                staticFields.AddRange(SmartType.For(type).GetAllStaticFields());
            }

            staticFields.Should()
            .BeEmpty(
                "assembly " + assembly + " should not contain static fields, but: " + Environment.NewLine + ReflectionElementsList.Format(staticFields));
            return(new AndConstraint <AssemblyAssertions>(assertions));
        }
Esempio n. 18
0
        //todo move substitute generator to a separate nuget project
        public T GenerateInstance(InstanceGenerator instanceGenerator)
        {
            var type = typeof(T);
            var sub  = Substitute.For <T>();

            var methods = SmartType.For(type).GetAllPublicInstanceMethodsWithReturnValue();

            foreach (var method in methods)
            {
                method.InvokeWithAnyArgsOn(sub, instanceGenerator.Instance)
                .ReturnsForAnyArgs(method.GenerateAnyReturnValue(instanceGenerator.Instance));
            }

            return(sub);
        }
Esempio n. 19
0
    public void CustomizeCreatedValue(
        object result,
        InstanceGenerator instanceGenerator,
        GenerationRequest request,
        Type type)
    {
        var smartType = SmartType.For(type);

        foreach (var customization in _customizations)
        {
            customization.ApplyTo(
                smartType,
                result,
                instanceGenerator, request);
        }
    }
Esempio n. 20
0
    public bool ConstructorIsInternalOrHasAtLeastOneNonConcreteArgumentType(Type type)
    {
        var smartType   = SmartType.For(type);
        var constructor = smartType.PickConstructorWithLeastNonPointersParameters();

        if (constructor.HasValue)
        {
            return(constructor.Value()
                   .HasAbstractOrInterfaceArguments() ||
                   constructor.Value().IsInternal());
        }
        else
        {
            throw new ConstructorNotFoundException(smartType.ToString());
        }
    }
Esempio n. 21
0
    public List <object> GenerateConstructorParameters(Func <Type, GenerationRequest, object> parameterFactory,
                                                       GenerationRequest request, Type type)
    {
        var smartType   = SmartType.For(type);
        var constructor = smartType.PickConstructorWithLeastNonPointersParameters();

        if (constructor.HasValue)
        {
            return(constructor.Value()
                   .GenerateAnyParameterValues(parameterFactory, request));
        }
        else
        {
            throw new ConstructorNotFoundException(smartType.ToString());
        }
    }
Esempio n. 22
0
    public object GenerateInstance(InstanceGenerator instanceGenerator, GenerationRequest request, Type type)
    {
        var smartType        = SmartType.For(type);
        var maybeConstructor = smartType.PickConstructorWithLeastNonPointersParameters();

        if (maybeConstructor.HasValue)
        {
            maybeConstructor.Value().DumpInto(request);
            var instance = maybeConstructor.Value()
                           .InvokeWithParametersCreatedBy(instanceGenerator.Instance, request);
            smartType.AssertMatchesTypeOf(instance);
            return(instance);
        }
        else
        {
            throw new ConstructorNotFoundException(smartType.ToString());
        }
    }
Esempio n. 23
0
        public static void SingleConstructor(Assembly assembly)
        {
            var constructorLimitsExceeded = new List <Tuple <Type, int> >();

            foreach (var type in assembly.GetTypes())
            {
                var constructorCount = SmartType.For(type).GetAllPublicConstructors().Count();
                if (constructorCount > 1)
                {
                    constructorLimitsExceeded.Add(Tuple.Create(type, constructorCount));
                }
            }

            constructorLimitsExceeded.Any().Should()
            .BeFalse("assembly " + assembly +
                     " should not contain types with more than one constructor, but: " +
                     Environment.NewLine +
                     StringFrom(constructorLimitsExceeded));
        }
Esempio n. 24
0
        public static AndConstraint <AssemblyAssertions> HaveOnlyTypesWithSingleConstructor(this AssemblyAssertions assertions)
        {
            Assembly assembly = assertions.Subject;
            var      constructorLimitsExceeded = new List <Tuple <Type, int> >();

            foreach (var type in assembly.GetTypes())
            {
                var constructorCount = SmartType.For(type).GetAllPublicConstructors().Count();
                if (constructorCount > 1)
                {
                    constructorLimitsExceeded.Add(Tuple.Create(type, constructorCount));
                }
            }

            constructorLimitsExceeded.Any().Should()
            .BeFalse("assembly " + assembly +
                     " should not contain types with more than one constructor, but: " +
                     Environment.NewLine + ReflectionElementsList.Format(constructorLimitsExceeded));
            return(new AndConstraint <AssemblyAssertions>(assertions));
        }
Esempio n. 25
0
 public static bool EqualInTermsOfEqualityOperator(Type type, object?instance1, object?instance2)
 {
     return((bool)SmartType.For(type).EqualityOperator().Evaluate(instance1, instance2));
 }
Esempio n. 26
0
 public static bool NotEqualInTermsOfInEqualityOperator(Type type, object instance1, object instance2)
 {
     return((bool)SmartType.For(type).Inequality().Evaluate(instance1, instance2));
 }
 public void ShouldReportWhenItIsNotDerivedFromException()
 {
     Assert.False(SmartType.For(typeof(string)).IsException());
 }
 public void ShouldReportWhenItIsDerivedFromException(Type exceptionType)
 {
     Assert.True(SmartType.For(exceptionType).IsException());
 }
Esempio n. 29
0
        public static IEnumerable <IMethod> FactoryMethods(Type type)
        {
            var factoryMethods = SmartType.For(type).FactoryMethods().Select(cw => new ConstructorWrapperToIMethod(cw));

            return(factoryMethods);
        }
Esempio n. 30
0
 public bool AppliesTo(Type type)
 {
     return(SmartType.For(type).Is(typeof(IEnumerator)));
 }