public void CreateInstanceOptionsTest() { var actual = InstanceCreator.CreateInstanceOptions <TestModel>(); actual.Should() .NotBeNull(); }
public void InitTest() { var actual = InstanceCreator .CreateInstanceOptions <ModuleSetting>() .Complete() .CreateInstance(); actual.Should() .NotBeNull(); }
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'."); }
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(); }
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."); }
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."); }
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(); }
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."); }
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"); }
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(); }
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(); }
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(); }
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); }
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(); }