Пример #1
0
        /// <summary>
        /// Creates a factory method for the given type.
        /// </summary>
        /// <param name="setup">The setup which is used for the type.</param>
        /// <returns>A function with which the given type can be instantiated.</returns>
        internal static Func <T> CreateFactoryMethod(FillerSetup setup)
        {
            Type targetType = typeof(T);

            if (!Setup.TypeToRandomFunc.ContainsKey(targetType))
            {
                if (targetType.IsClass())
                {
                    var fillerType   = typeof(Filler <>).MakeGenericType(typeof(T));
                    var objectFiller = Activator.CreateInstance(fillerType);

                    if (setup != null)
                    {
                        var setupMethod = objectFiller.GetType().GetMethods()
                                          .Where(x => x.Name == "Setup" && x.GetParameters().Length == 1)
                                          .Single(x => x.GetParameters().First().ParameterType == typeof(FillerSetup));

                        setupMethod.Invoke(objectFiller, new[] { setup });
                    }

                    var createMethod = objectFiller.GetType().GetMethods().Single(x => !x.GetParameters().Any() && x.Name == "Create");
                    return(() => (T)createMethod.Invoke(objectFiller, null));
                }

                return(() => default(T));
            }

            return(() => (T)Setup.TypeToRandomFunc[typeof(T)]());
        }
Пример #2
0
        /// <summary>
        /// Creates a setup based uppon another setup
        /// </summary>
        /// <typeparam name="TTarget">The type which is configured.</typeparam>
        /// <param name="baseSetup">The setup which is used as basis of the new one.</param>
        /// <returns>Setup you can use for Filler and Randomizer.</returns>
        public static FluentFillerApi <TTarget> Create <TTarget>(FillerSetup baseSetup) where TTarget : class
        {
            var setupManager = new SetupManager();

            setupManager.FillerSetup = baseSetup ?? new FillerSetup();

            return(new FluentFillerApi <TTarget>(setupManager));
        }
Пример #3
0
        /// <summary>
        /// With this method you can use a previously defined setup for specific types.
        /// </summary>
        /// <param name="setup">The setup for the type.</param>
        /// <returns>Main FluentFiller API</returns>
        public FluentFillerApi <TTargetObject> Use(FillerSetup setup)
        {
            var factoryMethod = Randomizer <TTargetType> .CreateFactoryMethod(setup);

            this.setupManager.GetFor <TTargetObject>().TypeToRandomFunc[typeof(TTargetType)] = () => factoryMethod();

            return(this.callback);
        }
Пример #4
0
        /// <summary>
        /// Call this to start the setup for the <see cref="Filler{T}"/> and use a setup which you created
        /// before with the <see cref="IFluentApi{TTargetObject,TTargetType}"/>
        /// </summary>
        /// <param name="fillerSetupToUse">
        /// FillerSetup to use
        /// </param>
        /// <returns>
        /// Fluent API Setup
        /// </returns>
        public FluentFillerApi <T> Setup(FillerSetup fillerSetupToUse)
        {
            if (fillerSetupToUse != null)
            {
                this.setupManager.FillerSetup = fillerSetupToUse;
            }

            return(new FluentFillerApi <T>(this.setupManager));
        }
Пример #5
0
        /// <summary>
        /// Call this to start the setup for the <see cref="Filler{T}"/> and use a setup which you created
        /// before with the <see cref="IFluentApi{TTargetObject,TTargetType}"/>
        /// </summary>
        /// <param name="fillerSetupToUse">
        /// FillerSetup to use
        /// </param>
        /// <param name="explicitSetup">True if just properties shall get filled which configured in filler setup.</param>
        /// <returns>
        /// Fluent API Setup
        /// </returns>
        public FluentFillerApi <T> Setup(FillerSetup fillerSetupToUse, bool explicitSetup)
        {
            if (fillerSetupToUse != null)
            {
                this.setupManager.FillerSetup = fillerSetupToUse;
            }

            this.justConfiguredProperties = explicitSetup;
            return(new FluentFillerApi <T>(this.setupManager));
        }
Пример #6
0
        /// <summary>
        /// Defines which <see cref="FillerSetup"/> is used to generate a value for the given <see cref="TTargetType"/>
        /// </summary>
        /// <param name="setup">The setup which is used for configuration.</param>
        public FluentFillerApi <TTargetObject> Use(FillerSetup setup)
        {
            foreach (PropertyInfo propertyInfo in this.affectedProperties)
            {
                var factoryMethod = Randomizer <TTargetType> .CreateFactoryMethod(setup);

                this.setupManager.GetFor <TTargetObject>().PropertyToRandomFunc[propertyInfo] = () => factoryMethod();
            }

            return(this.callback);
        }
        public void GetFillerSetup()
        {

            Filler<Person> filler = new Filler<Person>();
            _fillerSetup = filler.Setup()
                 .OnType<IAddress>().CreateInstanceOf<Address>()
                 .OnProperty(x => x.Age).Use(new IntRange(18, 35))
                 .OnProperty(x => x.FirstName).Use(new RealNames(NameStyle.FirstName))
                 .OnProperty(x => x.LastName).Use(new RealNames(NameStyle.LastName))
                 .SetupFor<Address>()
                 .OnProperty(x => x.HouseNumber).Use(new IntRange(1, 100))
                 .Result;

        }
Пример #8
0
        /// <summary>
        /// Creates a value base on a filler setup
        /// </summary>
        /// <param name="setup">Setup for the objectfiller</param>
        /// <returns>Created value</returns>
        public static T Create(FillerSetup setup)
        {
            var creationMethod = CreateFactoryMethod(setup);

            T result;

            try
            {
                result = creationMethod();
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                          "The type " + typeof(T).FullName + " needs additional information to get created. "
                          + "Please use the Filler class and call \"Setup\" to create a setup for that type. See Innerexception for more details.",
                          ex);
            }

            return(result);
        }
Пример #9
0
 /// <summary>
 /// Creates a set of random items of the given type and will use a <see cref="FillerSetup"/> for that.
 /// </summary>
 /// <param name="setup">Setup to use.</param>
 /// <param name="amount">Amount of items created.</param>
 /// <returns>Set of random items of the given type.</returns>
 public static IEnumerable <T> Create(FillerSetup setup, int amount)
 {
     return(Create(amount, () => Create(setup)));
 }
Пример #10
0
 /// <summary>
 /// Call this to start the setup for the <see cref="Filler{T}"/> and use a setup which you created
 /// before with the <see cref="IFluentApi{TTargetObject,TTargetType}"/>
 /// </summary>
 /// <param name="fillerSetupToUse">
 /// FillerSetup to use
 /// </param>
 /// <returns>
 /// Fluent API Setup
 /// </returns>
 public FluentFillerApi <T> Setup(FillerSetup fillerSetupToUse)
 {
     return(this.Setup(fillerSetupToUse, false));
 }
Пример #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SetupManager"/> class.
 /// </summary>
 internal SetupManager()
 {
     FillerSetup = new FillerSetup();
 }