public void PopulateAddsItemsToCollectionFromExecuteStrategy()
        {
            var expected   = new Collection <Guid>();
            var buildChain = new BuildHistory();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            executeStrategy.BuildChain.Returns(buildChain);
            executeStrategy.Configuration.Returns(configuration);

            executeStrategy.Create(typeof(Guid)).Returns(Guid.NewGuid());

            var sut = new EnumerableTypeCreator
            {
                MinCount = 5,
                MaxCount = 15
            };

            var actual = sut.Populate(executeStrategy, expected);

            actual.Should().BeSameAs(expected);

            var set = (Collection <Guid>)actual;

            set.Count.Should().BeGreaterOrEqualTo(sut.MinCount);
            set.Count.Should().BeLessOrEqualTo(sut.MaxCount);
            set.All(x => x != Guid.Empty).Should().BeTrue();
        }
        public void PriorityReturnsHigherThanDefaultTypeCreator()
        {
            var sut   = new EnumerableTypeCreator();
            var other = new DefaultTypeCreator();

            sut.Priority.Should().BeGreaterThan(other.Priority);
        }
        public void CanPopulateThrowsExceptionWithNullConfiguration()
        {
            var sut = new EnumerableTypeCreator();

            Action action = () => sut.CanPopulate(null !, null !, typeof(string));

            action.Should().Throw <ArgumentNullException>();
        }
        public void CanCreateThrowsExceptionWithNullTypeTest()
        {
            var target = new EnumerableTypeCreator();

            Action action = () => target.CanCreate(null, null, null);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void CanCreateReturnsWhetherTypeIsSupportedTest(Type type, bool supported)
        {
            var target = new EnumerableTypeCreator();

            var actual = target.CanCreate(type, null, null);

            actual.Should().Be(supported);
        }
        public void CanPopulateThrowsExceptionWithNullType()
        {
            var sut           = new EnumerableTypeCreator();
            var configuration = Substitute.For <IBuildConfiguration>();

            Action action = () => sut.CanPopulate(configuration, null !, (Type)null !);

            action.Should().Throw <ArgumentNullException>();
        }
        public void CanPopulateReturnsWhetherTypeIsSupportedTest(Type type, bool supported)
        {
            var configuration = Substitute.For <IBuildConfiguration>();

            var sut = new EnumerableTypeCreator();

            var actual = sut.CanPopulate(configuration, null !, type);

            actual.Should().Be(supported);
        }
        public void CanCreateThrowsExceptionWithNullProperty()
        {
            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            executeStrategy.Configuration.Returns(configuration);

            var sut = new EnumerableTypeCreator();

            Action action = () => sut.CanCreate(configuration, null !, (PropertyInfo)null !);

            action.Should().Throw <ArgumentNullException>();
        }
        public void CreateThrowExceptionWhenTypeNotEnumerable()
        {
            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            executeStrategy.Configuration.Returns(configuration);

            var sut = new EnumerableTypeCreator();

            Action action = () => sut.Create(executeStrategy, typeof(ICantCreate));

            action.Should().Throw <BuildException>();
        }
        public void PopulateThrowsExceptionWithUnsupportedType()
        {
            var instance = new Lazy <bool>(() => true);

            var buildChain      = new BuildHistory();
            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            executeStrategy.Configuration.Returns(configuration);
            executeStrategy.BuildChain.Returns(buildChain);

            var sut = new EnumerableTypeCreator();

            Action action = () => sut.Populate(executeStrategy, instance);

            action.Should().Throw <NotSupportedException>();
        }
        public void PopulateAddsItemsToInstancesTest(Type type)
        {
            var configuration = Model.UsingDefaultConfiguration();

            var sut = new EnumerableTypeCreator();

            var executeStrategy = new DefaultExecuteStrategy();

            executeStrategy.Initialize(configuration);

            var actual = sut.Create(executeStrategy, type) !;

            sut.Populate(executeStrategy, actual);

            var converted = (IEnumerable)actual;

            converted.Should().NotBeEmpty();
        }
        public void CreateReturnsInstanceOfMostAppropriateTypeTest(Type requestedType, Type expectedType)
        {
            var buildChain = new BuildHistory();

            var executeStrategy = Substitute.For <IExecuteStrategy>();
            var configuration   = Substitute.For <IBuildConfiguration>();

            executeStrategy.BuildChain.Returns(buildChain);
            executeStrategy.Configuration.Returns(configuration);

            var sut = new EnumerableTypeCreator();

            var actual = sut.Create(executeStrategy, requestedType);

            actual.Should().NotBeNull();
            actual.Should().BeOfType(expectedType);
            actual.Should().BeAssignableTo(requestedType);
            actual.As <IEnumerable>().Should().BeEmpty();
        }
        public void CreateValidatesWhetherTypeIsSupportedTest(Type type, bool supported)
        {
            var target = new EnumerableTypeCreator();

            Action action = () => target.Create(type, null, null);

            if (supported)
            {
                action.ShouldNotThrow();
            }
            else
            {
                action.ShouldThrow<NotSupportedException>();
            }
        }
        public void PopulateThrowsExceptionWithUnsupportedTypeTest()
        {
            var instance = new Lazy<bool>(() => true);

            var strategy = Substitute.For<IExecuteStrategy>();

            var target = new EnumerableTypeCreator();

            Action action = () => target.Populate(instance, strategy);

            action.ShouldThrow<NotSupportedException>();
        }
        public void AutoPopulateReturnsFalseTest()
        {
            var target = new EnumerableTypeCreator();

            target.AutoPopulate.Should().BeFalse();
        }
        public void PopulateAddsItemsToListFromExecuteStrategyTest()
        {
            var expected = new List<Guid>();

            var strategy = Substitute.For<IExecuteStrategy>();

            strategy.CreateWith(typeof(Guid)).Returns(Guid.NewGuid());

            var target = new EnumerableTypeCreator
            {
                AutoPopulateCount = 15
            };

            var actual = target.Populate(expected, strategy);

            actual.Should().BeSameAs(expected);

            var set = (List<Guid>) actual;

            set.Should().HaveCount(target.AutoPopulateCount);
            set.All(x => x != Guid.Empty).Should().BeTrue();
        }
        public void PopulateAddsItemsToInstancesTest(Type type)
        {
            var configuration = Model.BuildStrategy;
            var buildLog = configuration.GetBuildLog();

            var target = new EnumerableTypeCreator();
            var executeStrategy = new DefaultExecuteStrategy();

            executeStrategy.Initialize(configuration, buildLog);

            var actual = target.Create(type, null, null);

            target.Populate(actual, executeStrategy);

            var converted = (IEnumerable) actual;

            converted.Should().NotBeEmpty();
        }
        public void AutoDetectConstructorReturnsFalseTest()
        {
            var target = new EnumerableTypeCreator();

            target.AutoDetectConstructor.Should().BeFalse();
        }
        public void PopulateThrowsExceptionWithNullInstanceTest()
        {
            var strategy = Substitute.For<IExecuteStrategy>();

            var target = new EnumerableTypeCreator();

            Action action = () => target.Populate(null, strategy);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void SettingDefaultAutoPopulateCountOnlyAffectsNewInstancesTest()
        {
            var expected = EnumerableTypeCreator.DefaultAutoPopulateCount;

            try
            {
                var first = new EnumerableTypeCreator();

                EnumerableTypeCreator.DefaultAutoPopulateCount = 11;

                var second = new EnumerableTypeCreator();

                first.AutoPopulateCount.Should().Be(expected);
                second.AutoPopulateCount.Should().Be(11);
            }
            finally
            {
                EnumerableTypeCreator.DefaultAutoPopulateCount = expected;
            }
        }
        public void SettingAutoPopulateCountShouldNotChangeDefaultAutoPopulateCountTest()
        {
            var target = new EnumerableTypeCreator
            {
                AutoPopulateCount = Environment.TickCount
            };

            EnumerableTypeCreator.DefaultAutoPopulateCount.Should().NotBe(target.AutoPopulateCount);
        }
        public void ProrityReturnsHigherThanDefaultTypeCreatorTest()
        {
            var target = new EnumerableTypeCreator();
            var other = new DefaultTypeCreator();

            target.Priority.Should().BeGreaterThan(other.Priority);
        }
        public void CreateReturnsNewListOfSpecfiedTypeTest(Type targetType)
        {
            var target = new EnumerableTypeCreator();

            var actual = target.Create(targetType, null, null);

            actual.Should().BeOfType<List<int>>();
            actual.As<List<int>>().Should().BeEmpty();
        }
        public void AutoPopulateReturnsFalse()
        {
            var sut = new EnumerableTypeCreator();

            sut.AutoPopulate.Should().BeFalse();
        }
        public void CreateReturnsInstanceTest(Type type)
        {
            var target = new EnumerableTypeCreator();

            var actual = target.Create(type, null, null);

            actual.Should().NotBeNull();
        }
        public void PopulateThrowsExceptionWithNullStrategyTest()
        {
            var instance = new List<string>();

            var target = new EnumerableTypeCreator();

            Action action = () => target.Populate(instance, null);

            action.ShouldThrow<ArgumentNullException>();
        }