public void TestCustomFieldsContainerCopy() { var dataConfiguratorCollectionFactory = new TestDataConfiguratorCollectionFactory(); var dataConfiguratorCollection = new TestDataConfiguratorCollection <WebData>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { configurator.Target(x => x.CustomFieldsCopy.Value.Each().Key).Set(x => x.CustomFields.Value.Current().Key); configurator.Target(x => x.CustomFieldsCopy.Value.Each().Value.Value).Set(x => x.CustomFields.Value.Current().Value.Value); configurator.Target(x => x.CustomFieldsCopy.Value.Each().Value.TypeCode).Set(x => x.CustomFields.Value.Current().Value.TypeCode); configurator.Target(x => x.CustomFieldsCopy.Value.Each().Value.Title).Set(x => x.CustomFields.Value.Current().Value.Title); }); var mutator = dataConfiguratorCollection.GetMutatorsTree(MutatorsContext.Empty).GetTreeMutator(); var data = new WebData { CustomFields = new Lazy <Dictionary <string, CustomFieldValue> >(() => new Dictionary <string, CustomFieldValue> { { "X", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 0 } }, { "Y", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 1 } }, { "Z", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 2 } } }), }; mutator(data); Assert.AreEqual(0, data.CustomFieldsCopy.Value["X"].Value); Assert.AreEqual(1, data.CustomFieldsCopy.Value["Y"].Value); Assert.AreEqual(2, data.CustomFieldsCopy.Value["Z"].Value); }
public void TestModelDataValidator() { var dataConfiguratorCollectionFactory = new TestDataConfiguratorCollectionFactory(); var dataConfiguratorCollection = new TestDataConfiguratorCollection <Data>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { configurator.Target(data => data.S).Required(); configurator.Target(data => data.StrArr.Each()).InvalidIf(data => data.StrArr.Current() == "zzz", data => null); configurator.Target(data => data.Items.Each().S).Required(); } ); var modelDataConfiguratorCollection = new TestDataConfiguratorCollection <ModelData>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { }); dataConfiguratorCollectionFactory.Register(dataConfiguratorCollection); dataConfiguratorCollectionFactory.Register(new TestDataConfiguratorCollection <WebData>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { })); dataConfiguratorCollectionFactory.Register(modelDataConfiguratorCollection); var modelValidator = modelDataConfiguratorCollection.GetMutatorsTree(new[] { typeof(Data), typeof(WebData) }, new[] { MutatorsContext.Empty, MutatorsContext.Empty, MutatorsContext.Empty, }, new[] { MutatorsContext.Empty, MutatorsContext.Empty, }).GetValidator(); var validationResultTreeNode = modelValidator(new ModelData { CustomFields = new Dictionary <string, CustomFieldValue> { { "S", new CustomFieldValue { TypeCode = TypeCode.String } }, { "StrArr", new CustomFieldValue { TypeCode = TypeCode.String, IsArray = true, Value = new[] { "qxx", "zzz" } } } }, Items = new[] { new ModelDataItem { CustomFields = new Dictionary <string, CustomFieldValue> { { "S", new CustomFieldValue { TypeCode = TypeCode.String } } }, }, } }); validationResultTreeNode.AssertEquivalent( new ValidationResultTreeNode <ModelData> { { "CustomFields.S.Value", FormattedValidationResult.Error(new ValueRequiredText(), null, new SimplePathFormatterText { Paths = new[] { "CustomFields.S.Value" } }, 0) }, { "CustomFields.StrArr.Value.1", FormattedValidationResult.Error(null, "zzz", new SimplePathFormatterText { Paths = new[] { "CustomFields.StrArr.Value[1]" } }, 0) }, { "Items.0.CustomFields.S.Value", FormattedValidationResult.Error(new ValueRequiredText(), null, new SimplePathFormatterText { Paths = new[] { "Items[0].CustomFields.S.Value" } }, 0) }, } ); }
public void TestModelDataMutator() { var dataConfiguratorCollectionFactory = new TestDataConfiguratorCollectionFactory(); var dataConfiguratorCollection = new TestDataConfiguratorCollection <Data>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { configurator.Target(data => data.X).Set(data => data.Y + data.Z); configurator.Target(data => data.Items.Each().X).Set(data => data.Items.Current().Y + data.Items.Current().Z); } ); var modelDataConfiguratorCollection = new TestDataConfiguratorCollection <ModelData>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { }); dataConfiguratorCollectionFactory.Register(dataConfiguratorCollection); dataConfiguratorCollectionFactory.Register(new TestDataConfiguratorCollection <WebData>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { })); dataConfiguratorCollectionFactory.Register(modelDataConfiguratorCollection); var modelMutator = modelDataConfiguratorCollection.GetMutatorsTree(new[] { typeof(Data), typeof(WebData) }, new[] { MutatorsContext.Empty, MutatorsContext.Empty, MutatorsContext.Empty, }, new[] { MutatorsContext.Empty, MutatorsContext.Empty, }).GetTreeMutator(); var modelData = new ModelData { CustomFields = new Dictionary <string, CustomFieldValue> { { "X", new CustomFieldValue { Value = 0, TypeCode = TypeCode.Int32 } }, { "Y", new CustomFieldValue { Value = 1, TypeCode = TypeCode.Int32 } }, { "Z", new CustomFieldValue { Value = 2, TypeCode = TypeCode.Int32 } } }, Items = new[] { new ModelDataItem { CustomFields = new Dictionary <string, CustomFieldValue> { { "X", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 0 } }, { "Y", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 1 } }, { "Z", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 2 } } }, }, } }; modelMutator(modelData); Assert.AreEqual(3, modelData.CustomFields["X"].Value); Assert.AreEqual(3, modelData.Items[0].CustomFields["X"].Value); }
public void TestEnumCustomFieldWithWhereAndStringConverter() { var configuratorCollection = new TestDataConfiguratorCollection <SourceTestData>(null, null, pathFormatterCollection, configurator => { var subConfigurator = configurator.GoTo(x => x.As.Each()); subConfigurator.Target(x => x.EnumCustomField).Required(x => new TestText { Text = x.Info }); }); IStringConverter enumStringConverter = new EnumStringConverter(); var converterCollection = new TestConverterCollection <TargetTestData, SourceTestData>(pathFormatterCollection, configurator => { var subConfigurator = configurator.GoTo(x => x.As.Each(), x => x.As.Where(y => y.IsRemoved != true).Current()); subConfigurator.Target(x => x.Info).Set(x => x.Info); }, enumStringConverter); var mutatorsTree = configuratorCollection.GetMutatorsTree(MutatorsContext.Empty); var migratedTree = converterCollection.Migrate(mutatorsTree, MutatorsContext.Empty); var mutatorWithPath = migratedTree.GetAllMutatorsWithPathsForWeb(x => x).Single(); // Should be: Expression<Func<TargetTestData, AEnum>> expectedPathToMutator = x => (AEnum)(x.As.Each().CustomFields["EnumCustomField"].Value ?? AEnum.Unknown); Expression <Func <TargetTestData, AEnum> > expectedPathToMutator = x => (AEnum)(enumStringConverter.ConvertFromString <AEnum>((string)x.As.Where(y => y.IsRemoved != true).Each().CustomFields["EnumCustomField"].Value) ?? (object)AEnum.Unknown); mutatorWithPath.PathToMutator.AssertEquivalentExpressions(expectedPathToMutator.Body.Simplify()); Expression <Func <TargetTestData, object> > expectedPathToNode = x => x.As.Each().CustomFields["EnumCustomField"].Value; mutatorWithPath.PathToNode.AssertEquivalentExpressions(expectedPathToNode.Body); var requiredIfConfiguration = (RequiredIfConfiguration)mutatorWithPath.Mutator; // Should be: Expression<Func<TargetTestData, AEnum>> expectedMutatorPath = x => (AEnum)(x.As.Current().CustomFields["EnumCustomField"].Value ?? AEnum.Unknown); Expression <Func <TargetTestData, AEnum> > expectedMutatorPath = x => (AEnum)(enumStringConverter.ConvertFromString <AEnum>((string)x.As.Where(y => y.IsRemoved != true).Current().CustomFields["EnumCustomField"].Value) ?? AEnum.Unknown); requiredIfConfiguration.Path.AssertEquivalentExpressions(expectedMutatorPath.Simplify()); // Should be: Expression<Func<TargetTestData, bool>> expectedMutatorCondition = x => x.As.Each().IsRemoved != true; Expression <Func <TargetTestData, bool> > expectedMutatorCondition = null; requiredIfConfiguration.Condition.AssertEquivalentExpressions(expectedMutatorCondition); // Should be: Expression<Func<TargetTestData, TestText>> expectedMutatorMessage = x => new TestText {Text = x.As.Each().Info}; Expression <Func <TargetTestData, TestText> > expectedMutatorMessage = x => new TestText { Text = x.As.Where(y => y.IsRemoved != true).Current().Info }; requiredIfConfiguration.Message.AssertEquivalentExpressions(expectedMutatorMessage); }
public void TestNormalFieldsWithWhere() { var configuratorCollection = new TestDataConfiguratorCollection <SourceTestData>(null, null, pathFormatterCollection, configurator => { var subConfigurator = configurator.GoTo(x => x.As.Each()); subConfigurator.Target(x => x.SomethingNormal).Required(x => new TestText { Text = x.Info }); }); var converterCollection = new TestConverterCollection <TargetTestData, SourceTestData>(pathFormatterCollection, configurator => { var subConfigurator = configurator.GoTo(x => x.As.Each(), x => x.As.Where(y => y.IsRemoved != true).Current()); subConfigurator.Target(x => x.Info).Set(x => x.Info); subConfigurator.Target(x => x.SomethingNormal).Set(x => x.SomethingNormal); }); var mutatorsTree = configuratorCollection.GetMutatorsTree(MutatorsContext.Empty); var migratedTree = converterCollection.Migrate(mutatorsTree, MutatorsContext.Empty); var mutatorWithPath = migratedTree.GetAllMutatorsWithPathsForWeb(x => x).Single(); Expression <Func <TargetTestData, string> > expectedPathToMutator = x => x.As.Current().SomethingNormal; mutatorWithPath.PathToMutator.AssertEquivalentExpressions(expectedPathToMutator.Body); Expression <Func <TargetTestData, object> > expectedPathToNode = x => x.As.Each().SomethingNormal; mutatorWithPath.PathToNode.AssertEquivalentExpressions(expectedPathToNode.Body); var requiredIfConfiguration = (RequiredIfConfiguration)mutatorWithPath.Mutator; Expression <Func <TargetTestData, string> > expectedMutatorPath = x => x.As.Current().SomethingNormal; requiredIfConfiguration.Path.AssertEquivalentExpressions(expectedMutatorPath); Expression <Func <TargetTestData, bool> > expectedMutatorCondition = x => x.As.Current().IsRemoved != true; requiredIfConfiguration.Condition.AssertEquivalentExpressions(expectedMutatorCondition); Expression <Func <TargetTestData, TestText> > expectedMutatorMessage = x => new TestText { Text = x.As.Current().Info }; requiredIfConfiguration.Message.AssertEquivalentExpressions(expectedMutatorMessage); }
public void TestProperty() { var configuratorCollection = new TestDataConfiguratorCollection <SourceTestData>(null, null, pathFormatterCollection, configurator => { var subConfigurator = configurator.GoTo(x => x.As.Each()); subConfigurator.Target(x => x.Bs).Required(x => new TestText { Text = x.Info }); }); var converterCollection = new TestConverterCollection <TargetTestData, SourceTestData>(pathFormatterCollection, configurator => { var subConfigurator = configurator.GoTo(x => x.As.Each(), x => x.As.Where(y => y.IsRemoved != true).Current()); subConfigurator.Target(x => x.Bs.Each().Value).Set(x => x.Bs.Current()); subConfigurator.Target(x => x.Info).Set(x => x.Info); }); var mutatorsTree = configuratorCollection.GetMutatorsTree(MutatorsContext.Empty); var migratedTree = converterCollection.Migrate(mutatorsTree, MutatorsContext.Empty); var mutatorWithPath = migratedTree.GetAllMutatorsWithPaths().Single(); ((RequiredIfConfiguration)mutatorWithPath.Mutator).Condition.Compile(); ((RequiredIfConfiguration)mutatorWithPath.Mutator).Message.Compile(); }
public void TestWebDataMutator() { LambdaCompiler.DebugOutputDirectory = @"c:\temp"; var dataConfiguratorCollectionFactory = new TestDataConfiguratorCollectionFactory(); var dataConfiguratorCollection = new TestDataConfiguratorCollection <Data>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { configurator.Target(data => data.X).Set(data => data.Y + data.Z); configurator.Target(data => data.Items.Each().X).Set(data => data.Items.Current().Y + data.Items.Current().Z); configurator.Target(data => data.Sum).Set(data => data.DecimalArr.Sum()); configurator.Target(data => data.ComplexArrSum).Set(data => data.ComplexArr.Sum(x => x.Y)); } ); var webDataConfiguratorCollection = new TestDataConfiguratorCollection <WebData>(dataConfiguratorCollectionFactory, converterCollectionFactory, pathFormatterCollection, configurator => { }); dataConfiguratorCollectionFactory.Register(dataConfiguratorCollection); dataConfiguratorCollectionFactory.Register(webDataConfiguratorCollection); var webMutator = webDataConfiguratorCollection.GetMutatorsTree <Data, WebData>(MutatorsContext.Empty, MutatorsContext.Empty, MutatorsContext.Empty).GetTreeMutator(); var webData = new WebData { CustomFields = new Lazy <Dictionary <string, CustomFieldValue> >(() => new Dictionary <string, CustomFieldValue> { { "X", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 0 } }, { "Y", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 1 } }, { "Z", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 2 } }, { "Sum", new CustomFieldValue { TypeCode = TypeCode.Decimal, Value = 0m } }, { "ComplexArrSum", new CustomFieldValue { TypeCode = TypeCode.Decimal, Value = 0m } }, { "DecimalArr", new CustomFieldValue { TypeCode = TypeCode.Decimal, IsArray = true, Value = new object[] { 1m, 2m, 3m } } }, { "ComplexArr", new CustomFieldValue { TypeCode = TypeCode.Object, IsArray = true, TypeCodes = new Dictionary <string, TypeCode> { { "Y", TypeCode.Decimal } }, Value = new[] { new Hashtable { { "Y", 1m }, }, new Hashtable { { "Y", 2m } } } } } }), Items = new[] { new WebDataItem { CustomFields = new Dictionary <string, CustomFieldValue> { { "X", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 0 } }, { "Y", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 1 } }, { "Z", new CustomFieldValue { TypeCode = TypeCode.Int32, Value = 2 } } }, }, } }; webMutator(webData); Assert.AreEqual(3, webData.CustomFields.Value["X"].Value); Assert.AreEqual(3, webData.Items[0].CustomFields["X"].Value); Assert.AreEqual(6m, webData.CustomFields.Value["Sum"].Value); Assert.AreEqual(3m, webData.CustomFields.Value["ComplexArrSum"].Value); }