Exemplo n.º 1
0
        public static void Create()
        {
            var diagnostic = NoPropertyMapsFoundDiagnostic.Create(SyntaxFactory.ClassDeclaration("A"));

            Assert.Multiple(() =>
            {
                Assert.That(diagnostic.GetMessage(), Is.EqualTo(NoPropertyMapsFoundDiagnostic.Message));
                Assert.That(diagnostic.Descriptor.Title.ToString(CultureInfo.CurrentCulture), Is.EqualTo(NoPropertyMapsFoundDiagnostic.Title));
                Assert.That(diagnostic.Id, Is.EqualTo(NoPropertyMapsFoundDiagnostic.Id));
                Assert.That(diagnostic.Severity, Is.EqualTo(DiagnosticSeverity.Error));
                Assert.That(diagnostic.Descriptor.Category, Is.EqualTo(DescriptorConstants.Usage));
            });
        }
Exemplo n.º 2
0
        private static void ValidatePairs(SyntaxNode currentNode, INamedTypeSymbol source, INamedTypeSymbol destination,
                                          Maps.Builder maps)
        {
            var key = (source, destination);

            if (maps.ContainsKey(key))
            {
                var diagnostics = maps[key].diagnostics.ToList();
                diagnostics.Add(DuplicatedAttributeDiagnostic.Create(currentNode, maps[key].node));
                maps[key] = (diagnostics.ToImmutableArray(), maps[key].node, maps[key].maps);
            }
            else
            {
                var diagnostics = ImmutableArray.CreateBuilder <Diagnostic>();

                if (!destination.Constructors.Any(_ => _.DeclaredAccessibility == Accessibility.Public && _.Parameters.Length == 0))
                {
                    diagnostics.Add(NoArgumentConstructorDiagnostic.Create(currentNode));
                }

                var propertyMaps = ImmutableArray.CreateBuilder <string>();

                var destinationProperties = destination.GetMembers().OfType <IPropertySymbol>()
                                            .Where(_ => _.SetMethod is not null &&
                                                   (_.SetMethod.DeclaredAccessibility == Accessibility.Public ||
                                                    (destination.ContainingAssembly.ExposesInternalsTo(source.ContainingAssembly) && _.SetMethod.DeclaredAccessibility == Accessibility.Friend)))
                                            .ToList();

                foreach (var sourceProperty in source.GetMembers().OfType <IPropertySymbol>()
                         .Where(_ => _.GetMethod is not null &&
                                (_.GetMethod.DeclaredAccessibility == Accessibility.Public ||
                                 (source.ContainingAssembly.ExposesInternalsTo(destination.ContainingAssembly) && _.GetMethod.DeclaredAccessibility == Accessibility.Friend))))
                {
                    var destinationProperty = destinationProperties.FirstOrDefault(
                        _ => _.Name == sourceProperty.Name &&
                        _.Type.Equals(sourceProperty.Type, SymbolEqualityComparer.Default) &&
                        (sourceProperty.NullableAnnotation != NullableAnnotation.Annotated ||
                         sourceProperty.NullableAnnotation == NullableAnnotation.Annotated && _.NullableAnnotation == NullableAnnotation.Annotated));

                    if (destinationProperty is not null)
                    {
                        propertyMaps.Add($"{destinationProperty.Name} = self.{sourceProperty.Name},");
                        destinationProperties.Remove(destinationProperty);
                    }
                    else
                    {
                        diagnostics.Add(NoMatchDiagnostic.Create(sourceProperty, "source", source));
                    }
                }

                foreach (var remainingDestinationProperty in destinationProperties)
                {
                    diagnostics.Add(NoMatchDiagnostic.Create(remainingDestinationProperty, "destination", destination));
                }

                if (propertyMaps.Count == 0)
                {
                    diagnostics.Add(NoPropertyMapsFoundDiagnostic.Create(currentNode));
                }

                maps.Add((source, destination), (diagnostics.ToImmutable(), currentNode, propertyMaps.ToImmutable()));
            }
        }