本文整理汇总了C#中TestContext.GenerateSource方法的典型用法代码示例。如果您正苦于以下问题:C# TestContext.GenerateSource方法的具体用法?C# TestContext.GenerateSource怎么用?C# TestContext.GenerateSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TestContext
的用法示例。
在下文中一共展示了TestContext.GenerateSource方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestNotReusedOnAssemblyDiffers
internal static void TestNotReusedOnAssemblyDiffers(string projectLanguage)
{
var metadataSources = new[]
{
@"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class D {}",
@"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class D {}"
};
using (var context = new TestContext(projectLanguage))
{
var projectId = ProjectId.CreateNewId();
var metadataProject = context.CurrentSolution
.AddProject(projectId, "Metadata", "Metadata", LanguageNames.CSharp).GetProject(projectId)
.AddMetadataReference(TestReferences.NetFx.v4_0_30319.mscorlib)
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release));
var references = new List<MetadataReference>();
foreach (var source in metadataSources)
{
var newDoc = metadataProject.AddDocument("MetadataSource", source);
metadataProject = newDoc.Project;
references.Add(MetadataReference.CreateFromImage(metadataProject.GetCompilationAsync().Result.EmitToArray()));
metadataProject = metadataProject.RemoveDocument(newDoc.Id);
}
var project = context.DefaultProject.AddMetadataReference(references[0]);
var a = context.GenerateSource("D", project);
project = project.RemoveMetadataReference(references[0]).AddMetadataReference(references[1]);
var b = context.GenerateSource("D", project);
context.VerifyDocumentNotReused(a, b);
}
}
示例2: TestSymbolIdMatchesMetadata
internal static void TestSymbolIdMatchesMetadata(string projectLanguage)
{
var metadataSource = @"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C { }";
var symbolName = "C";
using (var context = new TestContext(projectLanguage, SpecializedCollections.SingletonEnumerable(metadataSource)))
{
var metadataSymbol = context.ResolveSymbol(symbolName);
var metadataSymbolId = metadataSymbol.GetSymbolKey();
var generatedFile = context.GenerateSource(symbolName);
var generatedDocument = context.GetDocument(generatedFile);
var generatedCompilation = generatedDocument.Project.GetCompilationAsync().Result;
var generatedSymbol = generatedCompilation.Assembly.GetTypeByMetadataName(symbolName);
Assert.False(generatedSymbol.Locations.Where(loc => loc.IsInSource).IsEmpty());
Assert.True(SymbolKey.GetComparer(ignoreCase: true, ignoreAssemblyKeys: false).Equals(metadataSymbolId, generatedSymbol.GetSymbolKey()));
}
}
示例3: FormatMetadataAsSource
public void FormatMetadataAsSource()
{
using (var context = new TestContext(LanguageNames.CSharp))
{
var file = context.GenerateSource("System.Console", project: context.DefaultProject);
var document = context.GetDocument(file);
Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(document).Wait();
}
}
示例4: TestNotReusedGeneratingForDifferentLanguage
public void TestNotReusedGeneratingForDifferentLanguage()
{
using (var context = new TestContext(LanguageNames.CSharp))
{
var projectId = ProjectId.CreateNewId();
var project = context.CurrentSolution.AddProject(projectId, "ProjectB", "ProjectB", LanguageNames.VisualBasic).GetProject(projectId)
.WithMetadataReferences(context.DefaultProject.MetadataReferences)
.WithCompilationOptions(new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var a = context.GenerateSource(project: context.DefaultProject);
var b = context.GenerateSource(project: project);
context.VerifyDocumentNotReused(a, b);
}
}
示例5: TestReuseRepeatGeneration
public void TestReuseRepeatGeneration()
{
using (var context = new TestContext())
{
var a = context.GenerateSource();
var b = context.GenerateSource();
context.VerifyDocumentReused(a, b);
}
}
示例6: TestReuseGenerateMemberOfGeneratedType
public void TestReuseGenerateMemberOfGeneratedType()
{
var metadataSource = "public class C { public bool Is; }";
using (var context = new TestContext(LanguageNames.CSharp, SpecializedCollections.SingletonEnumerable(metadataSource)))
{
var a = context.GenerateSource("C");
var b = context.GenerateSource("C.Is");
context.VerifyDocumentReused(a, b);
}
}
示例7: TestThrowsOnGenerateNamespace
public void TestThrowsOnGenerateNamespace()
{
var namespaceSymbol = CodeGenerationSymbolFactory.CreateNamespaceSymbol("Outerspace");
using (var context = new TestContext())
{
Assert.Throws<ArgumentException>(() =>
{
try
{
context.GenerateSource(namespaceSymbol);
}
catch (AggregateException ae)
{
throw ae.InnerException;
}
});
}
}
示例8: TestNavigationViaReducedExtensionMethodVB
public void TestNavigationViaReducedExtensionMethodVB()
{
var metadata = @"Imports System.Runtime.CompilerServices
Namespace NS
Public Module StringExtensions
<Extension()>
Public Sub M(ByVal o As String, x As Integer)
End Sub
End Module
End Namespace";
var sourceWithSymbolReference = @"
Imports NS.StringExtensions
Public Module C
Sub M()
Dim s = ""Yay""
s.[|M|](1)
End Sub
End Module";
var expected = [email protected]"#Region ""{FeaturesResources.Assembly} ReferencedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null""
' {CodeAnalysisResources.InMemoryAssembly}
#End Region
Imports System.Runtime.CompilerServices
Namespace NS
<Extension>
Public Module StringExtensions
<Extension> Public Sub [|M|](o As String, x As Integer)
End Module
End Namespace";
using (var context = new TestContext(
LanguageNames.VisualBasic,
SpecializedCollections.SingletonEnumerable(metadata),
includeXmlDocComments: false,
sourceWithSymbolReference: sourceWithSymbolReference))
{
var navigationSymbol = context.GetNavigationSymbol();
var metadataAsSourceFile = context.GenerateSource(navigationSymbol);
context.VerifyResult(metadataAsSourceFile, expected);
}
}
示例9: TestNavigationViaReducedExtensionMethodCS
public void TestNavigationViaReducedExtensionMethodCS()
{
var metadata = @"using System;
public static class ObjectExtensions
{
public static void M(this object o, int x) { }
}";
var sourceWithSymbolReference = @"
class C
{
void M()
{
new object().[|M|](5);
}
}";
var expected = [email protected]"#region {FeaturesResources.Assembly} ReferencedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// {CodeAnalysisResources.InMemoryAssembly}
#endregion
public static class ObjectExtensions
{{
public static void [|M|](this object o, int x);
}}
";
using (var context = new TestContext(
LanguageNames.CSharp,
SpecializedCollections.SingletonEnumerable(metadata),
includeXmlDocComments: false,
sourceWithSymbolReference: sourceWithSymbolReference))
{
var navigationSymbol = context.GetNavigationSymbol();
var metadataAsSourceFile = context.GenerateSource(navigationSymbol);
context.VerifyResult(metadataAsSourceFile, expected);
}
}