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)); }
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)); }
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)); }
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); } }
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)); }
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()); } }
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); } }
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); }
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)); }
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)); }
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)); }
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)); }
//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); }
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); } }
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()); } }
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()); } }
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()); } }
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)); }
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)); }
public static bool EqualInTermsOfEqualityOperator(Type type, object?instance1, object?instance2) { return((bool)SmartType.For(type).EqualityOperator().Evaluate(instance1, instance2)); }
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()); }
public static IEnumerable <IMethod> FactoryMethods(Type type) { var factoryMethods = SmartType.For(type).FactoryMethods().Select(cw => new ConstructorWrapperToIMethod(cw)); return(factoryMethods); }
public bool AppliesTo(Type type) { return(SmartType.For(type).Is(typeof(IEnumerator))); }