Esempio n. 1
0
        private SyntaxNode HandleAttribute(AttributeSyntax node)
        {
            var existing = m_semanticModel.GetSymbolInfo(node.Name);

            string existingTypeName = existing.Symbol?.ContainingType?.ToDisplayString();
            var    location         = node.GetLocation();
            var    originalNode     = node;

            try
            {
                switch (existingTypeName)
                {
                case "NUnit.Framework.SetUpAttribute":
                    WarnIfNotSuitableForTestInitialize("TestInitialize", location, true);
                    node = node.WithName(SyntaxFactory.IdentifierName("TestInitialize"));
                    break;

                case "NUnit.Framework.TearDownAttribute":
                    WarnIfNotSuitableForTestInitialize("TestCleanup", location, false);
                    node = node.WithName(SyntaxFactory.IdentifierName("TestCleanup"));
                    break;

                case "NUnit.Framework.OneTimeSetUpAttribute":
                    WarnIfNotSuitableForClassInitialize("ClassInitialize", location, true);
                    node = node.WithName(SyntaxFactory.IdentifierName("ClassInitialize"));
                    break;

                case "NUnit.Framework.OneTimeTearDownAttribute":
                    WarnIfNotSuitableForClassInitialize("ClassCleanup", location, false);
                    node = node.WithName(SyntaxFactory.IdentifierName("ClassCleanup"));
                    break;

                case "NUnit.Framework.PropertyAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("TestProperty"))
                           .ConvertArgumentsToString(m_diagnostics, location);
                    break;

                case "NUnit.Framework.TimeoutAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("Timeout"))
                           .WithoutArgumentList(m_diagnostics, location);
                    Changed = true;
                    break;

                case "NUnit.Framework.TestFixtureAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("TestClass"))
                           .WithoutArgumentList(m_diagnostics, location);
                    Changed = true;
                    break;

                case "NUnit.Framework.TestCaseAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("DataRow"))
                           .RenameNameEquals("TestName", "DisplayName");
                    m_perMethodState.DataRowSeen = true;
                    Changed = true;
                    break;

                case "NUnit.Framework.TestCaseSourceAttribute":
                    node    = TransformTestCaseSourceAttribute(node);
                    Changed = true;
                    break;

                case "NUnit.Framework.TestAttribute":
                    m_perMethodState.Description = node.GetNameEqualsExpression("Description");
                    node = node.WithName(SyntaxFactory.IdentifierName("TestMethod"))
                           .WithoutArgumentList(m_diagnostics, location, "Description");
                    Changed = true;
                    break;

                case "NUnit.Framework.CategoryAttribute":
                    node    = node.WithName(SyntaxFactory.IdentifierName("TestCategory"));
                    Changed = true;
                    break;

                case "NUnit.Framework.ExplicitAttribute":
                    node    = TransformExplicitAttribute(node);
                    Changed = true;
                    break;

                case "NUnit.Framework.IgnoreAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("Ignore"))
                           .WithoutNameEquals("Until", m_diagnostics, location);
                    Changed = true;
                    break;

                case "NUnit.Framework.DescriptionAttribute"
                    // With MSTest DescriptionAttribute only supported on methods
                    when node.GetParentKind() == SyntaxKind.MethodDeclaration:
                    node = node.WithName(SyntaxFactory.IdentifierName("Description"));

                    Changed = true;
                    break;

                default:
                {
                    if (existingTypeName != null && existingTypeName.StartsWith("NUnit."))
                    {
                        // Replace (potential) unqualified name with qualified name.
                        // Otherwise, an attribute whose unqualified name is accidentally the same
                        // as that of some other, unrelated, attribute could semantically change (since we
                        // replace the "using NUnit.Framework" with "using <MSTest>").
                        var fullQualifiedName = SyntaxFactory.ParseName(existingTypeName);
                        m_diagnostics.Add(Diagnostic.Create(DiagnosticsDescriptors.UnsupportedAttribute, location,
                                                            node.ToFullString()));
                        node    = node.WithName(fullQualifiedName);
                        Changed = true;
                    }

                    break;
                }
                }

                return(node);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Failed to process '{originalNode}' [{location}]: {ex.Message}", ex);
            }
        }