internal static Func <SemanticModel, ISymbol> CreateField(Accessibility accessibility, DeclarationModifiers modifiers, Type type, string name)
 {
     return(s => CodeGenerationSymbolFactory.CreateFieldSymbol(
                null, accessibility, modifiers, GetTypeSymbol(type)(s), name));
 }
 private static Func <SemanticModel, ISymbol> CreateEnumField(string name, object value)
 {
     return(s => CodeGenerationSymbolFactory.CreateFieldSymbol(
                null, Accessibility.Public, new DeclarationModifiers(), GetTypeSymbol(typeof(int))(s), name, value != null, value));
 }
 internal static Func <SemanticModel, ITypeSymbol> CreateArrayType(Type type, int rank = 1)
 {
     return(s => CodeGenerationSymbolFactory.CreateArrayTypeSymbol(GetTypeSymbol(type)(s), rank));
 }
 internal static async Task TestAddAttributeAsync(
     string initial,
     string expected,
     Type attributeClass,
     SyntaxToken?target = null,
     bool compareTokens = true)
 {
     using (var context = await TestContext.CreateAsync(initial, expected, compareTokens))
     {
         var attr    = CodeGenerationSymbolFactory.CreateAttributeData((INamedTypeSymbol)GetTypeSymbol(attributeClass)(context.SemanticModel));
         var oldNode = context.GetDestinationNode();
         var newNode = CodeGenerator.AddAttributes(oldNode, context.Document.Project.Solution.Workspace, new[] { attr }, target)
                       .WithAdditionalAnnotations(Formatter.Annotation);
         context.Result = context.Document.WithSyntaxRoot(context.SemanticModel.SyntaxTree.GetRoot().ReplaceNode(oldNode, newNode));
     }
 }
        internal static async Task TestAddPropertyAsync(
            string initial,
            string expected,
            string name = "P",
            Accessibility defaultAccessibility = Accessibility.Public,
            Accessibility setterAccessibility  = Accessibility.Public,
            DeclarationModifiers modifiers     = default(DeclarationModifiers),
            string getStatements = null,
            string setStatements = null,
            Type type            = null,
            IPropertySymbol explicitInterfaceSymbol = null,
            IList <Func <SemanticModel, IParameterSymbol> > parameters = null,
            bool isIndexer = false,
            CodeGenerationOptions codeGenerationOptions = default(CodeGenerationOptions),
            bool compareTokens = true,
            IDictionary <OptionKey, object> options = null)
        {
            // This assumes that tests will not use place holders for get/set statements at the same time
            if (getStatements != null)
            {
                expected = expected.Replace("$$", getStatements);
            }

            if (setStatements != null)
            {
                expected = expected.Replace("$$", setStatements);
            }

            using (var context = await TestContext.CreateAsync(initial, expected, compareTokens))
            {
                if (options != null)
                {
                    foreach (var kvp in options)
                    {
                        context.Workspace.Options = context.Workspace.Options.WithChangedOption(kvp.Key, kvp.Value);
                    }
                }

                var typeSymbol          = GetTypeSymbol(type)(context.SemanticModel);
                var getParameterSymbols = GetParameterSymbols(parameters, context);
                var setParameterSymbols = getParameterSymbols == null ? null : new List <IParameterSymbol>(getParameterSymbols)
                {
                    Parameter(type, "value")(context.SemanticModel)
                };
                IMethodSymbol getAccessor = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    null,
                    defaultAccessibility,
                    new DeclarationModifiers(isAbstract: getStatements == null),
                    typeSymbol,
                    false,
                    null,
                    "get_" + name,
                    null,
                    getParameterSymbols,
                    statements: context.ParseStatements(getStatements));
                IMethodSymbol setAccessor = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    null,
                    setterAccessibility,
                    new DeclarationModifiers(isAbstract: setStatements == null),
                    GetTypeSymbol(typeof(void))(context.SemanticModel),
                    false,
                    null,
                    "set_" + name,
                    null,
                    setParameterSymbols,
                    statements: context.ParseStatements(setStatements));

                // If get is provided but set isn't, we don't want an accessor for set
                if (getStatements != null && setStatements == null)
                {
                    setAccessor = null;
                }

                // If set is provided but get isn't, we don't want an accessor for get
                if (getStatements == null && setStatements != null)
                {
                    getAccessor = null;
                }

                var property = CodeGenerationSymbolFactory.CreatePropertySymbol(
                    null,
                    defaultAccessibility,
                    modifiers,
                    typeSymbol,
                    false,
                    explicitInterfaceSymbol,
                    name,
                    getParameterSymbols,
                    getAccessor,
                    setAccessor,
                    isIndexer);
                context.Result = await context.Service.AddPropertyAsync(context.Solution, (INamedTypeSymbol)context.GetDestination(), property, codeGenerationOptions);
            }
        }
Beispiel #6
0
 internal static Func <SemanticModel, ISymbol> CreateField(Accessibility accessibility, DeclarationModifiers modifiers, Type type, string name)
 {
     return(s => CodeGenerationSymbolFactory.CreateFieldSymbol(
                default(ImmutableArray <AttributeData>), accessibility,
                modifiers, GetTypeSymbol(type)(s), name));
 }