protected override void ConvertProperty( PropertyConverter propertyConverter, IPropertyDeclaration propertyDeclaration) { Argument.IsNotNull(() => propertyConverter); propertyConverter.Convert(propertyDeclaration); }
public void Convert_DepthLimitZero_ReturnsStructureToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var value = new Nested { Value = 1, Next = new Nested { Value = 10, Next = new Nested { Value = 100 } } }; var structure = converter.Convert(value, 0) as StructureToken; Assert.NotNull(structure); Assert.Equal(2, structure.Properties.Count); Assert.Equal(nameof(Nested.Next), structure.Properties[0].Name); Assert.Equal(nameof(Nested.Value), structure.Properties[1].Name); var scalar = structure.Properties[0].Value as ScalarToken; Assert.NotNull(scalar); Assert.Null(scalar.Value); }
public void Convert_Array_ReturnsSequenceToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var token = converter.Convert(new[] { 1, 2, 3 }); Assert.NotNull(token); Assert.IsType <SequenceToken>(token); }
public void Convert_SystemType_ReturnsScalarToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var token = converter.Convert(typeof(string)); Assert.NotNull(token); Assert.IsType <ScalarToken>(token); }
public void Convert_Guid_ReturnsScalarToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var token = converter.Convert(Guid.NewGuid()); Assert.NotNull(token); Assert.IsType <ScalarToken>(token); }
public void Convert_DateTimeOffset_ReturnsScalarToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var token = converter.Convert(DateTimeOffset.Now); Assert.NotNull(token); Assert.IsType <ScalarToken>(token); }
public void Convert_EmptyPolicy_ReturnsScalarToken() { var logger = Mock.Of <ILogger>(); var config = Mock.Of <IConversionConfig>(); var converter = new PropertyConverter(config, logger); var token = converter.Convert(10); Assert.NotNull(token); Assert.IsType <ScalarToken>(token); }
public void Convert_ByteArray_ReturnsScalarToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var value = Enumerable.Range(0, 10).Select(b => (byte)b).ToArray(); var token = converter.Convert(value); Assert.NotNull(token); Assert.IsType <ScalarToken>(token); }
public void Convert_Delegate_ReturnsScalarToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); Action value = Convert_Delegate_ReturnsScalarToken; var token = converter.Convert(value); Assert.NotNull(token); Assert.IsType <ScalarToken>(token); }
public void Convert_DictionaryKeyIsScalar_ReturnsDictionaryToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var value = new Dictionary <int, string> { { 1, "hello" } }; var token = converter.Convert(value); Assert.NotNull(token); Assert.IsType <DictionaryToken>(token); }
public void Convert_DictionaryKeyIsComplex_ReturnsSequenceToken() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var value = new Dictionary <A, string> { { new A(), "hello" } }; var token = converter.Convert(value); Assert.NotNull(token); Assert.IsType <SequenceToken>(token); }
public void PropertyConverterNegativeTest() { PropertyConverter target = new PropertyConverter(); var source = new TestClassA(); var sourceProperty = source.GetType().GetProperty("InnerClass"); var result = new TestClassB(); var resultProperty = result.GetType().GetProperty("InnerClassCode"); source.InnerClass = new TestClassC(); target.Convert( source, sourceProperty, target, resultProperty, false); }
public void ConvertTestSimple() { PropertyConverter target = new PropertyConverter(); TestClassA source = new TestClassA(); var expected = "test"; source.Name = expected; PropertyInfo fromProperty = typeof(TestClassA).GetProperty("Name"); TestClassB targetObject = new TestClassB(); targetObject.Name = "wrongstring"; PropertyInfo toProperty = typeof(TestClassB).GetProperty("Name"); target.Convert(source, fromProperty, targetObject, toProperty); Assert.AreEqual(expected, targetObject.Name); Assert.AreEqual(expected, source.Name); }
public void Convert_CyclicStructure_DoesNotStackOverflow() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var value = new A { B = new B() }; value.B.A = value; var token = converter.Convert(value); Assert.NotNull(token); Assert.IsType <StructureToken>(token); }
public void ConvertTestComplex() { var target = new PropertyConverter(); var source = new TestClassA { InnerClass = new TestClassC() }; const string expected = "test"; source.InnerClass.Name = expected; var fromProperty = typeof(TestClassA).GetProperty("InnerClass"); var targetObject = new TestClassB { InnerClassName = "wrongstring" }; var toProperty = typeof(TestClassB).GetProperty("InnerClassName"); target.Convert(source, fromProperty, targetObject, toProperty); Assert.AreEqual(expected, targetObject.InnerClassName); Assert.AreEqual(expected, source.InnerClass.Name); }
public void Convert_CyclicCollections_DoNotStackOverflow() { var logger = Mock.Of <ILogger>(); var config = new DefaultConversionConfig(logger); var converter = new PropertyConverter(config, logger); var value = new C { D = new D { C = new List <C?>() } }; value.D.C.Add(value); var token = converter.Convert(value); Assert.NotNull(token); Assert.IsType <StructureToken>(token); }
/// <exception cref="System.ArgumentNullException">The <paramref name="context"/> is <c>null</c>.</exception> protected override void Process(CSharpGeneratorContext context) { Argument.IsNotNull(() => context); var factory = CSharpElementFactory.GetInstance(context.Root.GetPsiModule()); var typeOwners = context.InputElements.OfType<GeneratorDeclaredElement<ITypeOwner>>().ToList(); var includeInSerialization = bool.Parse(context.GetGlobalOptionValue(OptionIds.IncludePropertyInSerialization)); var notificationMethod = bool.Parse(context.GetGlobalOptionValue(OptionIds.ImplementPropertyChangedNotificationMethod)); var forwardEventArgument = bool.Parse(context.GetGlobalOptionValue(OptionIds.ForwardEventArgumentToImplementedPropertyChangedNotificationMethod)); var propertyConverter = new PropertyConverter(factory, context.PsiModule, (IClassDeclaration)context.ClassDeclaration); foreach (var typeOwner in typeOwners) { var propertyDeclaredElement = typeOwner.DeclaredElement; var propertyDeclaration = (IPropertyDeclaration)propertyDeclaredElement.GetDeclarations().FirstOrDefault(); if (propertyDeclaration != null) { propertyConverter.Convert(propertyDeclaration, includeInSerialization, notificationMethod, forwardEventArgument); } } }
public void Convert_FaultyPolicy_WillNotThrow() { IPropertyToken result; var policyMock = new Mock <IConversionPolicy>(); policyMock .Setup(m => m.TryConvert( It.IsAny <IPropertyConverter>(), It.IsAny <object>(), out result)) .Throws <NotSupportedException>(); var configMock = new Mock <IConversionConfig>(); configMock.Setup(m => m.Policies).Returns(new[] { policyMock.Object }); var logger = Mock.Of <ILogger>(); var config = configMock.Object; var converter = new PropertyConverter(config, logger); var token = converter.Convert(10); Assert.NotNull(token); }
protected override void Process(CSharpGeneratorContext context) { Argument.IsNotNull(() => context); var factory = CSharpElementFactory.GetInstance(context.Root.GetPsiModule()); var viewModelToModelAttributeClrType = TypeHelper.CreateTypeByCLRName(CatelMVVM.ViewModelToModelAttribute, context.PsiModule, UniversalModuleReferenceContext.Instance); var declaredElements = context.InputElements.OfType<GeneratorDeclaredElement>().ToList(); var classLikeDeclaration = context.ClassDeclaration; if (classLikeDeclaration != null) { var includeInSerialization = bool.Parse(context.GetGlobalOptionValue(OptionIds.IncludePropertyInSerialization)); var notificationMethod = bool.Parse(context.GetGlobalOptionValue(OptionIds.ImplementPropertyChangedNotificationMethod)); var forwardEventArgument = bool.Parse(context.GetGlobalOptionValue(OptionIds.ForwardEventArgumentToImplementedPropertyChangedNotificationMethod)); var propertyConverter = new PropertyConverter(factory, context.PsiModule, (IClassDeclaration)classLikeDeclaration); foreach (var declaredElement in declaredElements) { var model = (IProperty)declaredElement.GetGroupingObject(); var modelProperty = (IProperty)declaredElement.DeclaredElement; if (model != null) { Log.Debug("Computing property name"); string propertyName = string.Empty; var cSharpTypeMemberDeclarations = new List<ICSharpTypeMemberDeclaration>(); IClassLikeDeclaration currentClassDeclaration = classLikeDeclaration; do { cSharpTypeMemberDeclarations.AddRange(currentClassDeclaration.MemberDeclarations); var superType = currentClassDeclaration.SuperTypes.FirstOrDefault(type => type.IsClassType()); if (superType != null) { var superTypeTypeElement = superType.GetTypeElement(); if (superTypeTypeElement != null) { currentClassDeclaration = (IClassLikeDeclaration)superTypeTypeElement.GetDeclarations().FirstOrDefault(); } } } while (currentClassDeclaration != null); if (!cSharpTypeMemberDeclarations.Exists(declaration => declaration.DeclaredName == modelProperty.ShortName)) { propertyName = modelProperty.ShortName; } if (string.IsNullOrEmpty(propertyName) && !cSharpTypeMemberDeclarations.Exists(declaration => declaration.DeclaredName == model.ShortName + modelProperty.ShortName)) { propertyName = model.ShortName + modelProperty.ShortName; } int idx = 0; while (string.IsNullOrEmpty(propertyName)) { if (!cSharpTypeMemberDeclarations.Exists(declaration => declaration.DeclaredName == model.ShortName + modelProperty.ShortName + idx.ToString(CultureInfo.InvariantCulture))) { propertyName = model.ShortName + modelProperty.ShortName + idx.ToString(CultureInfo.InvariantCulture); } idx++; } Log.Debug("Adding property '{0}'", propertyName); var propertyDeclaration = (IPropertyDeclaration)factory.CreateTypeMemberDeclaration(ImplementationPatterns.AutoProperty, modelProperty.Type, propertyName); var modelMemberDeclaration = model.GetDeclarations().FirstOrDefault(); if (modelMemberDeclaration != null && modelMemberDeclaration.Parent != null) { var modelClassDeclaration = modelMemberDeclaration.Parent.Parent; if (modelClassDeclaration == classLikeDeclaration) { propertyDeclaration = ModificationUtil.AddChildAfter(modelClassDeclaration, modelMemberDeclaration, propertyDeclaration); } else if (classLikeDeclaration.Body != null && classLikeDeclaration.Body.FirstChild != null) { propertyDeclaration = ModificationUtil.AddChildAfter(classLikeDeclaration.Body.FirstChild, propertyDeclaration); } } var fixedArguments = new List<AttributeValue> { new AttributeValue(ConstantValueHelper.CreateStringValue(model.ShortName, context.PsiModule, UniversalModuleReferenceContext.Instance)) }; if (propertyName != modelProperty.ShortName) { fixedArguments.Add(new AttributeValue(ConstantValueHelper.CreateStringValue(modelProperty.ShortName, context.PsiModule, UniversalModuleReferenceContext.Instance))); } Log.Debug("Adding attribute ViewModelToModel to property '{0}'", propertyName); IAttribute attribute = factory.CreateAttribute(viewModelToModelAttributeClrType.GetTypeElement(), fixedArguments.ToArray(), new Pair<string, AttributeValue>[] { }); propertyDeclaration.AddAttributeAfter(attribute, null); propertyConverter.Convert(propertyDeclaration, includeInSerialization, notificationMethod, forwardEventArgument); } } } }