Пример #1
0
    private void ValidatePairs(SyntaxNode currentNode, INamedTypeSymbol source, INamedTypeSymbol destination,
                               MappingContext context, Compilation compilation)
    {
        var diagnostics = ImmutableArray.CreateBuilder <Diagnostic>();

        if (!destination.Constructors.Any(_ => _.DeclaredAccessibility == Accessibility.Public ||
                                          destination.ContainingAssembly.ExposesInternalsTo(compilation.Assembly) && _.DeclaredAccessibility == Accessibility.Friend))
        {
            diagnostics.Add(NoAccessibleConstructorsDiagnostic.Create(currentNode));
        }

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

        var destinationProperties = destination.GetMembers().OfType <IPropertySymbol>()
                                    .Where(_ => _.SetMethod is not null &&
                                           (_.SetMethod.DeclaredAccessibility == Accessibility.Public ||
                                            (destination.ContainingAssembly.ExposesInternalsTo(compilation.Assembly) && _.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(compilation.Assembly) && _.GetMethod.DeclaredAccessibility == Accessibility.Friend))))
        {
            var destinationProperty = destinationProperties.FirstOrDefault(
                _ => _.Name == sourceProperty.Name &&
                context.MatchingPropertyTypeKind switch
            {
                MatchingPropertyTypeKind.Exact => _.Type.Equals(sourceProperty.Type, SymbolEqualityComparer.Default),
                _ => compilation.ClassifyCommonConversion(sourceProperty.Type, _.Type).IsImplicit
            } &&
    public static void Create()
    {
        var diagnostic = NoAccessibleConstructorsDiagnostic.Create(SyntaxFactory.ClassDeclaration("A"));

        Assert.Multiple(() =>
        {
            Assert.That(diagnostic.GetMessage(), Is.EqualTo(NoAccessibleConstructorsDiagnostic.Message));
            Assert.That(diagnostic.Descriptor.Title.ToString(CultureInfo.CurrentCulture), Is.EqualTo(NoAccessibleConstructorsDiagnostic.Title));
            Assert.That(diagnostic.Id, Is.EqualTo(NoAccessibleConstructorsDiagnostic.Id));
            Assert.That(diagnostic.Severity, Is.EqualTo(DiagnosticSeverity.Error));
            Assert.That(diagnostic.Descriptor.Category, Is.EqualTo(DescriptorConstants.Usage));
        });
    }