コード例 #1
0
            public async Task <MetadataAsSourceFile> GenerateSourceAsync(
                string?symbolMetadataName = null,
                Project?project           = null,
                bool signaturesOnly       = true,
                bool fileScopedNamespaces = false)
            {
                symbolMetadataName ??= AbstractMetadataAsSourceTests.DefaultSymbolMetadataName;
                project ??= this.DefaultProject;

                // Get an ISymbol corresponding to the metadata name
                var compilation = await project.GetRequiredCompilationAsync(CancellationToken.None);

                var diagnostics = compilation.GetDiagnostics().ToArray();

                Assert.Equal(0, diagnostics.Length);
                var symbol = await ResolveSymbolAsync(symbolMetadataName, compilation);

                Contract.ThrowIfNull(symbol);

                if (!signaturesOnly)
                {
                    foreach (var reference in compilation.References)
                    {
                        if (AssemblyResolver.TestAccessor.ContainsInMemoryImage(reference))
                        {
                            continue;
                        }

                        if (reference is PortableExecutableReference portableExecutable)
                        {
                            Assert.True(File.Exists(portableExecutable.FilePath), $"'{portableExecutable.FilePath}' does not exist for reference '{portableExecutable.Display}'");
                            Assert.True(Path.IsPathRooted(portableExecutable.FilePath), $"'{portableExecutable.FilePath}' is not a fully-qualified file name");
                        }
                        else
                        {
                            Assert.True(File.Exists(reference.Display), $"'{reference.Display}' does not exist");
                            Assert.True(Path.IsPathRooted(reference.Display), $"'{reference.Display}' is not a fully-qualified file name");
                        }
                    }
                }

                var options = MetadataAsSourceOptions.GetDefault(project.LanguageServices);

                if (fileScopedNamespaces)
                {
                    options = options with
                    {
                        GenerationOptions = options.GenerationOptions with
                        {
                            GenerationOptions = new CSharpCodeGenerationOptions
                            {
                                NamespaceDeclarations = new CodeStyleOption2 <NamespaceDeclarationPreference>(NamespaceDeclarationPreference.FileScoped, NotificationOption2.Silent)
                            }
                        }
                    };
                }

                // Generate and hold onto the result so it can be disposed of with this context
                var result = await _metadataAsSourceService.GetGeneratedFileAsync(project, symbol, signaturesOnly, options);

                return(result);
            }
コード例 #2
0
        protected static async Task <(SourceText?, TextSpan)> GetGeneratedSourceTextAsync(
            Project project,
            ISymbol symbol,
            Location sourceLocation,
            bool expectNullResult)
        {
            using var workspace = (TestWorkspace)project.Solution.Workspace;

            var service = workspace.GetService <IMetadataAsSourceFileService>();

            try
            {
                // Using default settings here because none of the tests exercise any of the settings
                var file = await service.GetGeneratedFileAsync(project, symbol, signaturesOnly : false, MetadataAsSourceOptions.GetDefault(project.LanguageServices), CancellationToken.None).ConfigureAwait(false);

                if (expectNullResult)
                {
                    Assert.Same(NullResultMetadataAsSourceFileProvider.NullResult, file);
                    return(null, default);
コード例 #3
0
            public Task <MetadataAsSourceFile> GenerateSourceAsync(ISymbol symbol, Project?project = null, bool signaturesOnly = true)
            {
                project ??= this.DefaultProject;
                Contract.ThrowIfNull(symbol);

                // Generate and hold onto the result so it can be disposed of with this context
                return(_metadataAsSourceService.GetGeneratedFileAsync(project, symbol, signaturesOnly, MetadataAsSourceOptions.GetDefault(project.LanguageServices)));
            }