﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StyleCop.Analyzers.ReadabilityRules;

namespace StyleCop.Analyzers.Test
{
    [TestClass]
    public class ExportCodeFixProviderAttributeNameTest
    {
        [TestMethod]
        public void Test()
        {
            var issues = new List<string>();

            var codeFixProviders = typeof(SA1110OpeningParenthesisMustBeOnDeclarationLine)
                .Assembly
                .GetExportedTypes()
                .Where(t => typeof(CodeFixProvider).IsAssignableFrom(t))
                .ToList();

            foreach (var codeFixProvider in codeFixProviders)
            {
                var exportCodeFixProviderAttribute = codeFixProvider.GetCustomAttributes(false)
                    .OfType<ExportCodeFixProviderAttribute>()
                    .FirstOrDefault();
                if (exportCodeFixProviderAttribute == null)
                {
                    issues.Add(string.Format("{0} should have ExportCodeFixProviderAttribute attribute", codeFixProvider.Name));
                    continue;
                }

                if (!string.Equals(exportCodeFixProviderAttribute.Name, codeFixProvider.Name,
                        StringComparison.InvariantCulture))
                {
                    issues.Add(string.Format("Name parameter of ExportCodeFixProviderAttribute applied on {0} should be set to {0}", codeFixProvider.Name));
                }
            }

            if (issues.Any())
            {
                Assert.Fail(string.Join("\r\t", issues));
            }
        }
    }
}
﻿namespace StyleCop.Analyzers.Test
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CodeFixes;
    using StyleCop.Analyzers.ReadabilityRules;
    using Xunit;

    public class ExportCodeFixProviderAttributeNameTest
    {
        public static IEnumerable<object[]> CodeFixProviderTypeData
        {
            get
            {
                var codeFixProviders = typeof(SA1110OpeningParenthesisMustBeOnDeclarationLine)
                    .Assembly
                    .GetTypes()
                    .Where(t => typeof(CodeFixProvider).IsAssignableFrom(t));

                return codeFixProviders.Select(x => new[] { x });
            }
        }

        [Theory]
        [MemberData(nameof(CodeFixProviderTypeData))]
        public void Test(Type codeFixProvider)
        {
            var exportCodeFixProviderAttribute = codeFixProvider.GetCustomAttributes<ExportCodeFixProviderAttribute>(false).FirstOrDefault();

            Assert.NotNull(exportCodeFixProviderAttribute);
            Assert.Equal(codeFixProvider.Name, exportCodeFixProviderAttribute.Name);
            Assert.Equal(1, exportCodeFixProviderAttribute.Languages.Length);
            Assert.Equal(LanguageNames.CSharp, exportCodeFixProviderAttribute.Languages[0]);
        }
    }
}
Test if ExportCodeFixProviderAttribute is applied correctly - adjusted/corrected
Test if ExportCodeFixProviderAttribute is applied correctly - adjusted/corrected
