예제 #1
0
        protected ExceptionExpectancyAtAttributeLevel(AttributeSyntax attribute)
        {
            AttributeNode = attribute ?? throw new ArgumentNullException(nameof(attribute));

            SyntaxHelper.ParseAttributeArguments(attribute, ParseAttributeArgumentSyntax);
            ParseTestFixtureClass(attribute.FirstAncestorOrSelf <ClassDeclarationSyntax>());
        }
예제 #2
0
        public static AttributeData?FindAttributeData(this AttributeSyntax syntax, ISymbol targetSymbol)
        {
            AttributeTargetSpecifierSyntax attributeTarget = syntax.FirstAncestorOrSelf <AttributeListSyntax>().Target;

            if (attributeTarget is not null)
            {
                switch (attributeTarget.Identifier.Kind())
                {
                case SyntaxKind.ReturnKeyword:
                    return(((IMethodSymbol)targetSymbol).GetReturnTypeAttributes().First(attributeSyntaxLocationMatches));

                case SyntaxKind.AssemblyKeyword:
                    return(targetSymbol.ContainingAssembly.GetAttributes().First(attributeSyntaxLocationMatches));

                case SyntaxKind.ModuleKeyword:
                    return(targetSymbol.ContainingModule.GetAttributes().First(attributeSyntaxLocationMatches));

                default:
                    return(null);
                }
            }
            // Sometimes an attribute is put on a symbol that is nested within the containing symbol.
            // For example, the ContainingSymbol for an AttributeSyntax on a parameter have a ContainingSymbol of the method.
            // Since this method is internal and the callers don't care about attributes on parameters, we just allow
            // this method to return null in those cases.
            return(targetSymbol.GetAttributes().FirstOrDefault(attributeSyntaxLocationMatches));

            bool attributeSyntaxLocationMatches(AttributeData attrData)
            {
                return(attrData.ApplicationSyntaxReference !.SyntaxTree == syntax.SyntaxTree && attrData.ApplicationSyntaxReference.Span == syntax.Span);
            }
        }
        public ITypeSymbol GetContainerTypeSymbol(SemanticModel semanticModel)
        {
            if (_sourceType != null)
            {
                return(semanticModel.GetSymbolInfo(_sourceType).Symbol as ITypeSymbol);
            }

            var containerType = _attributeNode.FirstAncestorOrSelf <TypeDeclarationSyntax>();

            return(semanticModel.GetDeclaredSymbol(containerType) as ITypeSymbol);
        }
예제 #4
0
        private static AttributeListSyntax CreateAttribute(string @namespace, AttributeSyntax attribute)
        {
            var list = attribute.FirstAncestorOrSelf <AttributeListSyntax>();

            if (list?.Attributes.Count != 1)
            {
                return(null);
            }

            if (!Attribute.TryFindArgument(attribute, 1, KnownSymbol.XmlnsDefinitionAttribute.ClrNamespaceArgumentName, out AttributeArgumentSyntax oldArgument))
            {
                return(null);
            }

            var newArgument = oldArgument.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@namespace)));

            return(list.WithAttributes(SyntaxFactory.SingletonSeparatedList(attribute.ReplaceNode(oldArgument, newArgument))).WithAdditionalAnnotations(Formatter.Annotation));
        }