Exemplo n.º 1
0
        public void DoCanConverTest(Type fromType, Type toType, bool expected)
        {
            var target = new ValueConverter();

            bool result = target.CanConvert(fromType, toType);

            result.ShouldBe(expected);
        }
        /// <summary>
        /// Creates a runnable with the specified name and actual arguments
        /// </summary>
        public virtual IRunnable Create(String runnableName, IEnumerable <KeyValuePair <String, Object> > actualArguments)
        {
            Assume.NotNull(runnableName, nameof(runnableName));
            Assume.NotNull(actualArguments, nameof(actualArguments));

            // instantiate runnable
            var runnableType = AvailableRunnables.FirstOrDefault(type => RunnableNameMatcher.Match(type.Name, runnableName));

            if (runnableType == null)
            {
                throw new Exception($"Runnable ({runnableName}) is not available!");
            }

            IRunnable runnable = null;

            // get its default constructor
            var constructors       = runnableType.GetConstructors();
            var defaultConstructor = constructors.FirstOrDefault(constructor => constructor.GetParameters().Length == 0);

            if (defaultConstructor != null)
            {
                runnable = defaultConstructor.Invoke(Enumerable.Empty <Object>().ToArray()) as IRunnable;
            }
            if (ServiceFactory != null)
            {
                // instantiate with service factory
                var constructorWithService = constructors.FirstOrDefault(constructor => {
                    var parameters = constructor.GetParameters();
                    if (parameters.Length != 1)
                    {
                        return(false);
                    }

                    var isServiceFactory = typeof(IServiceFactory).IsAssignableFrom(parameters[0].ParameterType);

                    return(isServiceFactory);
                });

                if (constructorWithService != null)
                {
                    runnable = constructorWithService.Invoke(new[] { ServiceFactory }) as IRunnable;
                }
            }

            if (runnable == null)
            {
                throw new Exception("Cannot find proper constructor of Runnable instance!");
            }

            // update properties via [Parameter] attribute
            var properties = runnableType.GetProperties()
                             .Select(property => new
            {
                Property = property,
                BindTo   = property.GetCustomAttributes(typeof(ParameterAttribute), true)
                           .SingleOrDefault() as ParameterAttribute
            })
                             .Where(prop => prop.BindTo != null);

            foreach (var property in properties)
            {
                var argument = actualArguments.FirstOrDefault(arg => ParameterNameMatcher.Match(arg.Key, property.BindTo.PropertyName));

                var value = argument.Value;
                if (ValueConverter.CanConvert(value, property.Property.PropertyType))
                {
                    value = ValueConverter.Convert(value);
                }
                property.Property.SetValue(runnable, value);
            }

            return(runnable);
        }