Пример #1
0
        public ObjectBuildRecord CreateRecord(Type type, IBuilder builder, BuildSession session)
        {
            var listType        = type.GetGenericArguments()[0];
            var constructorType = typeof(List <>).MakeGenericType(listType);
            var constructor     = constructorType.GetConstructor(Type.EmptyTypes);

            if (constructor == null)
            {
                throw new BuilderException(string.Format("Unable to construct {0} as no default constructor was found", type));
            }

            var list = (IList)constructor.Invoke(new object[0]);

            if (session.AvailableBuildDepth >= 1)
            {
                var range    = session.GetCollectionSize(listType);
                var listSize = session.Random.NextInRange(range);
                for (int i = 0; i < listSize; i++)
                {
                    list.Add(builder.Build(listType));
                }
            }

            return(new ObjectBuildRecord(type, list, false));
        }
Пример #2
0
        public void GetCollectionSize_ReturnsDefaultIfNoneDefined()
        {
            var defRange = new Range(10, 1000);

            _session = new BuildSession(new TestBuilder {
                CollectionSizes = _registry, DefaultCollectionSize = defRange
            }, null, _random);
            Assert.AreSame(defRange, _session.GetCollectionSize(typeof(string)));
        }
Пример #3
0
        public void GetCollectionSize_ReturnsDefinedRange()
        {
            var stringRange = new Range(-10, 1);
            var defRange    = new Range(10, 1000);

            _registry.SetForType <string>(stringRange);
            _session = new BuildSession(new TestBuilder {
                CollectionSizes = _registry, DefaultCollectionSize = defRange
            }, null, _random);

            Assert.AreSame(stringRange, _session.GetCollectionSize(typeof(string)));
        }
Пример #4
0
        public ObjectBuildRecord CreateRecord(Type type, IBuilder builder, BuildSession session)
        {
            Array instance;

            if (session.AvailableBuildDepth >= 2)
            {
                var arrayType = type.GetElementType();
                var range     = session.GetCollectionSize(arrayType);
                instance = Array.CreateInstance(arrayType, session.Random.NextInRange(range));
                for (int i = 0; i < instance.Length; i++)
                {
                    instance.SetValue(builder.Build(arrayType), i);
                }
            }
            else
            {
                instance = Array.CreateInstance(type.GetElementType(), 0);
            }
            return(new ObjectBuildRecord(type, instance, false));
        }