Esempio n. 1
0
        public void CreateInstanceOptionsTest()
        {
            var actual = InstanceCreator.CreateInstanceOptions <TestModel>();

            actual.Should()
            .NotBeNull();
        }
Esempio n. 2
0
        public void InitTest()
        {
            var actual = InstanceCreator
                         .CreateInstanceOptions <ModuleSetting>()
                         .Complete()
                         .CreateInstance();

            actual.Should()
            .NotBeNull();
        }
Esempio n. 3
0
        public void ActivatorFailTest()
        {
            var options = InstanceCreator.CreateInstanceOptions <ModelNeedingFactory>();

            Action test = () => options.Complete()
                          // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                          .CreateInstance();

            test.ShouldThrow <CreateInstanceException>()
            .WithMessage("Failed to create instance due to missing or invalid factory for type 'Extend.Testing.InstanceCreatorTest+ModelWithCtor'.");
        }
Esempio n. 4
0
        public void ExcludeMemberTest()
        {
            var options = InstanceCreator.CreateInstanceOptions <TestModel>();

            options.Excluding(x => x.ByPath(y => y.MyIntList));

            var actual = options.Complete()
                         .CreateInstance();

            actual.MyIntList.Should()
            .BeNull();
        }
Esempio n. 5
0
        public void RootFactoryThrowExceptionTest()
        {
            var options = InstanceCreator.CreateInstanceOptions <TestModel>()
                          .WithFactory(x => throw new Exception("22"))
                          .For(x => x.IsTypeOf <TestModel>());

            Action test = () => options.Complete()
                          // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                          .CreateInstance();

            test.ShouldThrow <CreateInstanceException>()
            .WithMessage("Failed to create root object of type: TestModel.");
        }
Esempio n. 6
0
        public void FactoryThrowsExceptionTest()
        {
            var options = InstanceCreator.CreateInstanceOptions <TestModel>()
                          .WithFactory(x => throw new Exception("Factory Failed"))
                          .For(x => x.IsTypeOf <Double>());

            Action test = () => options.Complete()
                          // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                          .CreateInstance();

            test.ShouldThrow <CreateInstanceException>()
            .WithMessage("Factory has thrown exception.");
        }
Esempio n. 7
0
        public void SelectionRuleTest()
        {
            var actual = InstanceCreator
                         .CreateInstanceOptions <SimpleTestModel>()
                         .Excluding(x => x.ByPath(y => y.MyString))
                         .WithFactory(x => 666)
                         .For(x => x.IsTypeOf <Int32>())
                         .Complete()
                         .CreateInstance();

            actual.MyInt32.Should()
            .Be(666);
            actual.MyString.Should()
            .BeNull();
        }
Esempio n. 8
0
        public void MultipleMatchingFactoriesInOptionsTest()
        {
            var options = InstanceCreator.CreateInstanceOptions <TestModel>()
                          .WithFactory(x => 100)
                          .For(x => x.IsTypeOf <Double>())
                          .WithFactory(x => 200)
                          .For(x => x.IsTypeOf <Double>());

            Action test = () => options.Complete()
                          // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                          .CreateInstance();

            test.ShouldThrow <CreateInstanceException>()
            .WithMessage("Found multiple matching factories for member (in options). Type is 'System.Double'. Please make sure only one factory matches the member.");
        }
Esempio n. 9
0
        public void FactoryTest6()
        {
            var actual = InstanceCreator
                         .CreateInstanceOptions <SimpleTestModel>()
                         .WithFactory(x => 666)
                         .For(x => x.AllMembers())
                         .NotFor(x => x.ByPath(y => y.MyString))
                         .NotFor(x => x.IsTypeOf <SimpleTestModel>())
                         .WithFactory(x => "test")
                         .For(x => x.IsTypeOf <String>())
                         .Complete()
                         .CreateInstance();

            actual.MyInt32.Should()
            .Be(666);
            actual.MyString.Should()
            .Be("test");
        }
Esempio n. 10
0
        public void FactoryTest4()
        {
            var actual = InstanceCreator
                         .CreateInstanceOptions <TestModel>()
                         .WithFactory(x => 666)
                         .For(x => x.ByPath("MyInt32"))
                         .Complete()
                         .CreateInstance();

            actual.MyInt32.Should()
            .Be(666);
            actual.MyIntList.All(x => x == 666)
            .Should()
            .BeFalse();
            actual.MyIntArray.All(x => x == 666)
            .Should()
            .BeFalse();
        }
Esempio n. 11
0
        public void FactoryTest1()
        {
            var actual = InstanceCreator
                         .CreateInstanceOptions <TestModel>()
                         .WithFactory(x => 666)
                         .For(x => x.IsTypeOf <Int32>())
                         .Complete()
                         .CreateInstance();

            actual.MyInt32.Should()
            .Be(666);
            actual.MyIntList.All(x => x == 666)
            .Should()
            .BeTrue();
            actual.MyIntArray.All(x => x == 666)
            .Should()
            .BeTrue();
        }
Esempio n. 12
0
        public void MultipleMatchingFactoriesInGlobalConfigTest()
        {
            var options = InstanceCreator.CreateInstanceOptions <TestModel>();

            var factory = new ExpressionInstanceFactory(x => 100)
                          .AddSelectionRule(new TypeMemberSelectionRule(typeof(Double), MemberSelectionMode.Include, CompareMode.Is));

            InstanceCreator.DefaultFactories.Add(factory);

            Action test = () => options.Complete()
                          // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                          .CreateInstance();
            var exception = Assert.Throws <CreateInstanceException>(test);

            Assert.Equal("Found multiple matching factories for member (in global configuration). Type is 'System.Double'.  Please make sure only one factory matches the member.", exception.Message);

            InstanceCreator.DefaultFactories.Remove(factory);
            InstanceCreator.DefaultFactories.Contains(factory)
            .Should()
            .BeFalse();
        }
Esempio n. 13
0
        public void PropertiesTest4()
        {
            var actual = InstanceCreator
                         .CreateInstanceOptions <ModuleSetting>()
                         .WithFactory(x => 666)
                         .For(x => x.IsTypeOf <Int32>())
                         .WithFactory(x => "the devil is alive")
                         .For(x => x.IsTypeOf <String>())
                         .WithFactory(x => true)
                         .For(x => x.IsTypeOf <Boolean>())
                         .Complete()
                         .CreateInstance();

            actual.Should()
            .NotBeNull("is initialized");
            actual.Name.Should()
            .Be("the devil is alive");
            actual.Id.Should()
            .Be(666);
            actual.Visible.Should()
            .Be(true);
        }
Esempio n. 14
0
        public void DoNotPopulateCollectionTest()
        {
            var options = InstanceCreator.CreateInstanceOptions <TestModel>()
                          .PopulateCollectionMembers(false);

            var actual = options.Complete()
                         .CreateInstance();

            actual.MyIntList.Should()
            .BeEmpty();
            actual.MyStringList.Should()
            .BeEmpty();

            actual.MyIntArray.Should()
            .BeEmpty();
            actual.MyStringArray.Should()
            .BeEmpty();

            actual.MyInt32Enumerable.Should()
            .BeEmpty();
            actual.MyStringEnumerable.Should()
            .BeEmpty();
        }