예제 #1
0
        private AttributeSyntax TransformExplicitAttribute(AttributeSyntax node)
        {
            var location = node.GetLocation();
            var original = node.ToFullString();

            // MSTest V2 does not support "[Explicit]".
            // Convert "[Explicit]" to "[Ignore("EXPLICIT")]"
            // Convert "[Explicit("yadayada")]" to "[Ignore("EXPLICIT: yadayada")]"

            string text        = "EXPLICIT";
            var    description = node.GetPositionExpression(0);

            if (description != null)
            {
                text += ": " + description.GetFirstToken().ValueText;
            }

            var literalExpression = SyntaxFactory.LiteralExpression(
                SyntaxKind.StringLiteralExpression,
                SyntaxFactory.Literal("\"" + text + "\"", text));

            var arguments = new SeparatedSyntaxList <AttributeArgumentSyntax>();

            arguments = arguments.Add(SyntaxFactory.AttributeArgument(literalExpression));

            node = node.WithName(SyntaxFactory.IdentifierName("Ignore")).WithArgumentList(
                SyntaxFactory.AttributeArgumentList(arguments));

            m_diagnostics.Add(Diagnostic.Create(DiagnosticsDescriptors.TransformedUnsupported, location, original,
                                                node.ToFullString()));

            return(node);
        }
            public override SyntaxNode VisitAttribute(AttributeSyntax node)
            {
                if (node.Name is IdentifierNameSyntax newNameNode)
                {
                    if (newNameNode.Identifier.ValueText.EndsWith(Attribute_Keywork))
                    {
                        var newName = newNameNode.Identifier.ValueText.TrimEnd(Attribute_Keywork);

                        node = node.WithName(SyntaxFactory.IdentifierName(newName));
                    }
                }

                return(base.VisitAttribute(node));
            }
        public override SyntaxNode VisitAttribute(AttributeSyntax node)
        {
            var typeName   = string.Empty;
            var identifier = node.Name + "Attribute";

            if (TryGetValue(identifier, out typeName))
            {
                if (typeName.EndsWith("Attribute"))
                {
                    typeName = typeName.Remove(typeName.Length - 9);
                    return(node.WithName(SyntaxFactory.ParseName(typeName)
                                         .WithLeadingTrivia(node.GetLeadingTrivia())
                                         .WithTrailingTrivia(node.GetTrailingTrivia())));
                }
            }
            return(base.VisitAttribute(node));
        }
            public override SyntaxNode VisitAttribute(AttributeSyntax node)
            {
                if (node.Name is IdentifierNameSyntax newNameNode)
                {
                    if (newNameNode.Identifier.ValueText.EndsWith(Attribute_Keywork))
                    {
                        var orginalNodeTypeInfo = semanticModel.GetTypeInfo(node.Name);

                        if (orginalNodeTypeInfo.Type == null)
                        {
                            base.VisitAttribute(node);
                        }

                        if (orginalNodeTypeInfo.Type.Name == newNameNode.Identifier.ValueText)
                        {
                            var newName = newNameNode.Identifier.ValueText.TrimEnd(Attribute_Keywork);

                            if (isReportOnlyMode)
                            {
                                var lineSpan = node.GetFileLinePosSpan();

                                AddReport(new ChangesReport(node)
                                {
                                    LineNumber = lineSpan.StartLinePosition.Line,
                                    Column     = lineSpan.StartLinePosition.Character,
                                    Message    = "Attributes should not ended with \"Attribute\"",
                                    Generator  = nameof(RemoveAttributeKeywork)
                                });
                            }

                            node = node.WithName(SyntaxFactory.IdentifierName(newName));
                        }
                    }
                }

                return(base.VisitAttribute(node));
            }
예제 #5
0
            public override SyntaxNode VisitAttribute(AttributeSyntax node)
            {
                if (node.Name is IdentifierNameSyntax newNameNode)
                {
                    if (newNameNode.Identifier.ValueText.EndsWith(Attribute_Keywork))
                    {
                        var orginalNodeTypeInfo = semanticModel.GetTypeInfo(node.Name);

                        if (orginalNodeTypeInfo.Type == null)
                        {
                            base.VisitAttribute(node);
                        }

                        if (orginalNodeTypeInfo.Type.Name == newNameNode.Identifier.ValueText)
                        {
                            var newName = newNameNode.Identifier.ValueText.TrimEnd(Attribute_Keywork);

                            node = node.WithName(SyntaxFactory.IdentifierName(newName));
                        }
                    }
                }

                return(base.VisitAttribute(node));
            }
예제 #6
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);
            }
        }
 public AttributeSyntax Transform(AttributeSyntax node)
 {
     return(node.Name.WithoutTrivia().ToString() == "SetUp"
   ? node.WithName(IdentifierName("OneTimeSetUp"))
   : node);
 }
예제 #8
0
        public static AttributeSyntax TranslateAttribute(AttributeSyntax attribute)
        {
            string attributeName = Dictionary.TranslateAttributeName(attribute.Name.ToString());

            return(attribute.WithName(SyntaxFactory.IdentifierName(attributeName)));
        }
 public AttributeSyntax Transform(AttributeSyntax node)
 {
     return(node.Name.ToString() == "TestFixtureSetUp"
   ? node.WithName(SyntaxFactory.IdentifierName("OneTimeSetUp"))
   : node);
 }