public void build_coercion_strategy_for_an_array() { EnumerableInstance.DetermineCoercion(typeof(IWidget[])).ShouldBeOfType <ArrayCoercion <IWidget> >(); }
public static IDependencySource SourceFor(string ctorOrSetter, string name, Type dependencyType, object value) { if (value == null) { if (dependencyType.IsSimple()) { return(new DependencyProblem { Message = MissingPrimitiveWarning, Type = ctorOrSetter, Name = name, ReturnedType = dependencyType }); } if (EnumerableInstance.IsEnumerable(dependencyType)) { return(new AllPossibleValuesDependencySource(dependencyType)); } return(new DefaultDependencySource(dependencyType)); } if (value is IDependencySource) { return(value as IDependencySource); } if (value is Instance) { return(value.As <Instance>().ToDependencySource(dependencyType)); } if (value.GetType().CanBeCastTo(dependencyType)) { return(new Constant(dependencyType, value)); } if (dependencyType.IsSimple()) { try { return(new Constant(dependencyType, ConvertType(value, dependencyType))); } catch (Exception) { return(new DependencyProblem { Type = ctorOrSetter, Name = name, ReturnedType = dependencyType, Message = CastingError.ToFormat(value, value.GetType().GetFullName(), dependencyType.GetFullName()) }); } } if (EnumerableInstance.IsEnumerable(dependencyType)) { var coercion = EnumerableInstance.DetermineCoercion(dependencyType); var coercedValue = coercion.Convert(value.As <IEnumerable <object> >()); return(new Constant(dependencyType, coercedValue)); } return(new DependencyProblem { Type = ctorOrSetter, Name = name, ReturnedType = dependencyType, Message = UnableToDetermineDependency.ToFormat(dependencyType.GetFullName(), value) }); }
public void build_coercion_strategy_for_a_plain_enumerable() { EnumerableInstance.DetermineCoercion(typeof(IEnumerable <IWidget>)).ShouldBeOfType <ListCoercion <IWidget> >(); }