コード例 #1
0
        public void Resolve()
        {
            using (var directory = new DisposableDirectory(Temp))
            {
                var assembly1 = directory.CreateFile("_1.dll");
                var assembly2 = directory.CreateFile("_2.dll");

                // With NuGetPackageResolver.
                var resolver = new RuntimeMetadataReferenceResolver(
                    new RelativePathResolver(ImmutableArray.Create(directory.Path), baseDirectory: directory.Path),
                    new PackageResolver(ImmutableDictionary<string, ImmutableArray<string>>.Empty.Add("nuget:N/1.0", ImmutableArray.Create(assembly1.Path, assembly2.Path))),
                    gacFileResolver: null,
                    useCoreResolver: false);

                // Recognized NuGet reference.
                var actualReferences = resolver.ResolveReference("nuget:N/1.0", baseFilePath: null, properties: MetadataReferenceProperties.Assembly);
                AssertEx.SetEqual(actualReferences.SelectAsArray(r => r.FilePath), assembly1.Path, assembly2.Path);
                // Unrecognized NuGet reference.
                actualReferences = resolver.ResolveReference("nuget:N/2.0", baseFilePath: null, properties: MetadataReferenceProperties.Assembly);
                Assert.True(actualReferences.IsEmpty);
                // Recognized file path. 
                actualReferences = resolver.ResolveReference("_2.dll", baseFilePath: null, properties: MetadataReferenceProperties.Assembly);
                AssertEx.SetEqual(actualReferences.SelectAsArray(r => r.FilePath), assembly2.Path);
                // Unrecognized file path. 
                actualReferences = resolver.ResolveReference("_3.dll", baseFilePath: null, properties: MetadataReferenceProperties.Assembly);
                Assert.True(actualReferences.IsEmpty);

                // Without NuGetPackageResolver.
                resolver = new RuntimeMetadataReferenceResolver(
                    new RelativePathResolver(ImmutableArray.Create(directory.Path), baseDirectory: directory.Path),
                    packageResolver: null,
                    gacFileResolver: null,
                    useCoreResolver: false);

                // Unrecognized NuGet reference.
                actualReferences = resolver.ResolveReference("nuget:N/1.0", baseFilePath: null, properties: MetadataReferenceProperties.Assembly);
                Assert.True(actualReferences.IsEmpty);
                // Recognized file path. 
                actualReferences = resolver.ResolveReference("_2.dll", baseFilePath: null, properties: MetadataReferenceProperties.Assembly);
                AssertEx.SetEqual(actualReferences.SelectAsArray(r => r.FilePath), assembly2.Path);
                // Unrecognized file path. 
                actualReferences = resolver.ResolveReference("_3.dll", baseFilePath: null, properties: MetadataReferenceProperties.Assembly);
                Assert.True(actualReferences.IsEmpty);
            }
        }
コード例 #2
0
ファイル: SourceGeneratorTests.cs プロジェクト: RoryVL/roslyn
        public void RunAnalyzersAfterGeneratingSource()
        {
            string text =
@"class C
{
}";
            using (var directory = new DisposableDirectory(Temp))
            {
                var file = directory.CreateFile("c.cs");
                file.WriteAllText(text);

                int analyzerCalls = 0;
                ImmutableArray<SyntaxTree> treesToAnalyze;
                var analyzer = new MyAnalyzer(c =>
                {
                    analyzerCalls++;
                    Assert.True(treesToAnalyze.IsDefault);
                    treesToAnalyze = ImmutableArray.CreateRange(c.Compilation.SyntaxTrees);
                });

                int generatorCalls = 0;
                var generator = new MyGenerator(c =>
                {
                    generatorCalls++;
                    c.AddCompilationUnit("__c", CSharpSyntaxTree.ParseText("class __C { }"));
                });

                var compiler = new MyCompiler(
                    baseDirectory: directory.Path,
                    args: new[] { "/nologo", "/preferreduilang:en", "/t:library", file.Path },
                    analyzers: ImmutableArray.Create<DiagnosticAnalyzer>(analyzer),
                    generators: ImmutableArray.Create<SourceGenerator>(generator));

                var builder = new StringBuilder();
                using (var writer = new StringWriter(builder))
                {
                    compiler.Run(writer);
                }
                var output = builder.ToString();
                // No errors from analyzer.
                Assert.Equal("", output);

                Assert.Equal(1, generatorCalls);
                Assert.Equal(1, analyzerCalls);
                Assert.Equal(2, treesToAnalyze.Length);
            }
        }
コード例 #3
0
 public void RelativePath()
 {
     using (var directory = new DisposableDirectory(Temp))
     {
         const string scriptName = "c.csx";
         var script = directory.CreateFile(scriptName).WriteAllText("Print(3);");
         var scriptPath = PathUtilities.CombinePathsUnchecked(PathUtilities.GetFileName(directory.Path), scriptName);
         var workingDirectory = PathUtilities.GetDirectoryName(directory.Path);
         Assert.False(PathUtilities.IsAbsolute(scriptPath));
         var runner = CreateRunner(
             args: new[] { scriptPath },
             workingDirectory: workingDirectory);
         runner.RunInteractive();
         AssertEx.AssertEqualToleratingWhitespaceDifferences("3", runner.Console.Out.ToString());
     }
 }