本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Test.Utilities.MockCSharpCompiler类的典型用法代码示例。如果您正苦于以下问题:C# MockCSharpCompiler类的具体用法?C# MockCSharpCompiler怎么用?C# MockCSharpCompiler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MockCSharpCompiler类属于Microsoft.CodeAnalysis.CSharp.Test.Utilities命名空间,在下文中一共展示了MockCSharpCompiler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TrivialSourceFileOnlyCsc
public void TrivialSourceFileOnlyCsc()
{
var hello = Temp.CreateFile().WriteAllText(helloWorldCS).Path;
var touchedDir = Temp.CreateDirectory();
var touchedBase = Path.Combine(touchedDir.Path, "touched");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", hello,
string.Format(@"/touchedfiles:""{0}""", touchedBase) });
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
List<string> expectedReads;
List<string> expectedWrites;
BuildTouchedFiles(cmd,
Path.ChangeExtension(hello, "exe"),
out expectedReads,
out expectedWrites);
var exitCode = cmd.Run(outWriter);
Assert.Equal("", outWriter.ToString().Trim());
Assert.Equal(0, exitCode);
AssertTouchedFilesEqual(expectedReads,
expectedWrites,
touchedBase);
CleanupAllGeneratedFiles(hello);
}
示例2: AppConfigCsc
public void AppConfigCsc()
{
var hello = Temp.CreateFile().WriteAllText(helloWorldCS).Path;
var touchedDir = Temp.CreateDirectory();
var touchedBase = Path.Combine(touchedDir.Path, "touched");
var appConfigPath = Temp.CreateFile().WriteAllText(
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
<runtime>
<assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
<supportPortability PKT=""7cec85d7bea7798e"" enable=""false""/>
</assemblyBinding>
</runtime>
</configuration>").Path;
var silverlight = Temp.CreateFile().WriteAllBytes(ProprietaryTestResources.NetFX.silverlight_v5_0_5_0.System_v5_0_5_0_silverlight).Path;
var net4_0dll = Temp.CreateFile().WriteAllBytes(ProprietaryTestResources.NetFX.v4_0_30319.System).Path;
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var cmd = new MockCSharpCompiler(null, _baseDirectory,
new[] { "/nologo",
"/r:" + silverlight,
"/r:" + net4_0dll,
"/appconfig:" + appConfigPath,
"/touchedfiles:" + touchedBase,
hello });
List<string> expectedReads;
List<string> expectedWrites;
BuildTouchedFiles(cmd,
Path.ChangeExtension(hello, "exe"),
out expectedReads,
out expectedWrites);
expectedReads.Add(appConfigPath);
var exitCode = cmd.Run(outWriter);
Assert.Equal("", outWriter.ToString().Trim());
Assert.Equal(0, exitCode);
AssertTouchedFilesEqual(expectedReads,
expectedWrites,
touchedBase);
CleanupAllGeneratedFiles(hello);
}
示例3: NoDiagnostics
public void NoDiagnostics()
{
var helloWorldCS = @"using System;
class C
{
public static void Main(string[] args)
{
Console.WriteLine(""Hello, world"");
}
}";
var hello = Temp.CreateFile().WriteAllText(helloWorldCS).Path;
var errorLogDir = Temp.CreateDirectory();
var errorLogFile = Path.Combine(errorLogDir.Path, "ErrorLog.txt");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", hello,
$"/errorlog:{errorLogFile}" });
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var exitCode = cmd.Run(outWriter);
Assert.Equal("", outWriter.ToString().Trim());
Assert.Equal(0, exitCode);
var actualOutput = File.ReadAllText(errorLogFile).Trim();
var expectedHeader = GetExpectedErrorLogHeader(actualOutput, cmd);
var expectedIssues = @"
""issues"": [
]
}
]
}";
var expectedText = expectedHeader + expectedIssues;
Assert.Equal(expectedText, actualOutput);
CleanupAllGeneratedFiles(hello);
CleanupAllGeneratedFiles(errorLogFile);
}
示例4: VerifyOutput
private static string VerifyOutput(TempDirectory sourceDir, TempFile sourceFile,
bool includeCurrentAssemblyAsAnalyzerReference = true,
string[] additionalFlags = null,
int expectedInfoCount = 0,
int expectedWarningCount = 0,
int expectedErrorCount = 0)
{
var args = new[] {
"/nologo", "/preferreduilang:en", "/t:library",
sourceFile.Path
};
if (includeCurrentAssemblyAsAnalyzerReference)
{
args = args.Append("/a:" + Assembly.GetExecutingAssembly().Location);
}
if (additionalFlags != null)
{
args = args.Append(additionalFlags);
}
var csc = new MockCSharpCompiler(null, sourceDir.Path, args);
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var exitCode = csc.Run(outWriter);
var output = outWriter.ToString();
var expectedExitCode = expectedErrorCount > 0 ? 1 : 0;
Assert.True(
expectedExitCode == exitCode,
string.Format("Expected exit code to be '{0}' was '{1}'.{2} Output:{3}{4}",
expectedExitCode, exitCode, Environment.NewLine, Environment.NewLine, output));
Assert.DoesNotContain("hidden", output, StringComparison.Ordinal);
if (expectedInfoCount == 0)
{
Assert.DoesNotContain("info", output, StringComparison.Ordinal);
}
else
{
Assert.Equal(expectedInfoCount, OccurrenceCount(output, "info"));
}
if (expectedWarningCount == 0)
{
Assert.DoesNotContain("warning", output, StringComparison.Ordinal);
}
else
{
Assert.Equal(expectedWarningCount, OccurrenceCount(output, "warning"));
}
if (expectedErrorCount == 0)
{
Assert.DoesNotContain("error", output, StringComparison.Ordinal);
}
else
{
Assert.Equal(expectedErrorCount, OccurrenceCount(output, "error"));
}
return output;
}
示例5: NoInfoDiagnostics
public void NoInfoDiagnostics()
{
string filePath = Temp.CreateFile().WriteAllText(@"
using System.Diagnostics; // Unused.
").Path;
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", "/target:library", filePath });
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var exitCode = cmd.Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Equal("", outWriter.ToString().Trim());
CleanupAllGeneratedFiles(filePath);
}
示例6: PreferredUILang
public void PreferredUILang()
{
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
int exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/preferreduilang" }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.Contains("CS2006", outWriter.ToString(), StringComparison.Ordinal);
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/preferreduilang:" }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.Contains("CS2006", outWriter.ToString(), StringComparison.Ordinal);
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/preferreduilang:zz" }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.Contains("CS2038", outWriter.ToString(), StringComparison.Ordinal);
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/preferreduilang:en-zz" }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.Contains("CS2038", outWriter.ToString(), StringComparison.Ordinal);
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/preferreduilang:en-US" }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.DoesNotContain("CS2038", outWriter.ToString(), StringComparison.Ordinal);
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/preferreduilang:de" }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.DoesNotContain("CS2038", outWriter.ToString(), StringComparison.Ordinal);
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/preferreduilang:de-AT" }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.DoesNotContain("CS2038", outWriter.ToString(), StringComparison.Ordinal);
}
示例7: AnalyzerReportsMisformattedDiagnostic
public void AnalyzerReportsMisformattedDiagnostic()
{
var srcFile = Temp.CreateFile().WriteAllText(@"class C {}");
var srcDirectory = Path.GetDirectoryName(srcFile.Path);
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var csc = new MockCSharpCompiler(null, _baseDirectory, new[] { "/t:library", srcFile.Path },
analyzer: new AnalyzerReportingMisformattedDiagnostic());
var exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
var output = outWriter.ToString();
// Verify that the diagnostic reported by AnalyzerReportingMisformattedDiagnostic is reported with the message format string, instead of the formatted message.
Assert.Contains(AnalyzerThatThrowsInGetMessage.Rule.Id, output, StringComparison.Ordinal);
Assert.Contains(AnalyzerThatThrowsInGetMessage.Rule.MessageFormat.ToString(CultureInfo.InvariantCulture), output, StringComparison.Ordinal);
CleanupAllGeneratedFiles(srcFile.Path);
}
示例8: ReportAnalyzerOutput
public void ReportAnalyzerOutput()
{
var srcFile = Temp.CreateFile().WriteAllText(@"class C {}");
var srcDirectory = Path.GetDirectoryName(srcFile.Path);
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var csc = new MockCSharpCompiler(null, srcDirectory, new[] { "/reportanalyzer", "/t:library", "/a:" + Assembly.GetExecutingAssembly().Location, srcFile.Path });
var exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
var output = outWriter.ToString();
Assert.Contains(CodeAnalysisResources.AnalyzerExecutionTimeColumnHeader, output, StringComparison.Ordinal);
Assert.Contains(new WarningDiagnosticAnalyzer().ToString(), output, StringComparison.Ordinal);
CleanupAllGeneratedFiles(srcFile.Path);
}
示例9: TestCS2002
private static void TestCS2002(string[] commandLineArgs, string baseDirectory, int expectedExitCode, string[] compileDiagnostics, params DiagnosticDescription[] parseDiagnostics)
{
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var allCommandLineArgs = new[] { "/nologo", "/preferreduilang:en", "/t:library" }.Concat(commandLineArgs).ToArray();
// Verify command line parser diagnostics.
DefaultParse(allCommandLineArgs, baseDirectory).Errors.Verify(parseDiagnostics);
// Verify compile.
int exitCode = new MockCSharpCompiler(null, baseDirectory, allCommandLineArgs).Run(outWriter);
Assert.Equal(expectedExitCode, exitCode);
if (parseDiagnostics.IsEmpty())
{
// Verify compile diagnostics.
string outString = String.Empty;
for (int i = 0; i < compileDiagnostics.Length; i++)
{
if (i != 0)
{
outString += @"
";
}
outString += compileDiagnostics[i];
}
Assert.Equal(outString, outWriter.ToString().Trim());
}
else
{
Assert.Null(compileDiagnostics);
}
}
示例10: TestWarnAsError_CS1522
public void TestWarnAsError_CS1522()
{
string source = Temp.CreateFile(prefix: "", extension: ".cs").WriteAllText(@"
public class Test
{
// CS0169 (level 3)
private int x;
// CS0109 (level 4)
public new void Method() { }
public static int Main()
{
int i = 5;
// CS1522 (level 1)
switch (i) { }
return 0;
// CS0162 (level 2)
i = 6;
}
}
").Path;
var baseDir = Path.GetDirectoryName(source);
var fileName = Path.GetFileName(source);
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
int exitCode = new MockCSharpCompiler(null, baseDir, new[] { "/nologo", "/preferreduilang:en", "/warn:3", "/warnaserror", source.ToString() }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.Equal(fileName + "(12,20): error CS1522: Empty switch block", outWriter.ToString().Trim());
CleanupAllGeneratedFiles(source);
}
示例11: TestNoWarnParseDiagnostics
public void TestNoWarnParseDiagnostics()
{
string source = Temp.CreateFile(prefix: "", extension: ".cs").WriteAllText(@"
class Test
{
static void Main()
{
//Generates warning CS1522: Empty switch block
switch (1) { }
//Generates warning CS0642: Possible mistaken empty statement
while (false) ;
{ }
}
}
").Path;
var baseDir = Path.GetDirectoryName(source);
var fileName = Path.GetFileName(source);
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
int exitCode = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", "/nowarn:1522,642", source.ToString() }).Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Equal("", outWriter.ToString().Trim());
CleanupAllGeneratedFiles(source);
}
示例12: TestFilterParseDiagnostics
public void TestFilterParseDiagnostics()
{
string source = Temp.CreateFile(prefix: "", extension: ".cs").WriteAllText(@"
#pragma warning disable 440
using global = A; // CS0440
class A
{
static void Main() {
#pragma warning suppress 440
}
}").Path;
var baseDir = Path.GetDirectoryName(source);
var fileName = Path.GetFileName(source);
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
int exitCode = new MockCSharpCompiler(null, baseDir, new[] { "/nologo", "/preferreduilang:en", source.ToString() }).Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Equal(Path.GetFileName(source) + "(7,17): warning CS1634: Expected disable or restore", outWriter.ToString().Trim());
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, baseDir, new[] { "/nologo", "/nowarn:1634", source.ToString() }).Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Equal("", outWriter.ToString().Trim());
outWriter = new StringWriter(CultureInfo.InvariantCulture);
exitCode = new MockCSharpCompiler(null, baseDir, new[] { "/nologo", "/preferreduilang:en", Path.Combine(baseDir, "nonexistent.cs"), source.ToString() }).Run(outWriter);
Assert.Equal(1, exitCode);
Assert.Equal("error CS2001: Source file '" + Path.Combine(baseDir, "nonexistent.cs") + "' could not be found.", outWriter.ToString().Trim());
CleanupAllGeneratedFiles(source);
}
示例13: DefaultResponseFileNoConfig
public void DefaultResponseFileNoConfig()
{
MockCSharpCompiler csc = new MockCSharpCompiler(GetDefaultResponseFilePath(), _baseDirectory, new[] { "/noconfig" });
Assert.Equal(csc.Arguments.MetadataReferences.Select(r => r.Reference), new string[]
{
typeof(object).Assembly.Location,
}, StringComparer.OrdinalIgnoreCase);
}
示例14: DefaultResponseFile
public void DefaultResponseFile()
{
MockCSharpCompiler csc = new MockCSharpCompiler(GetDefaultResponseFilePath(), _baseDirectory, new string[0]);
AssertEx.Equal(csc.Arguments.MetadataReferences.Select(r => r.Reference), new string[]
{
typeof(object).Assembly.Location,
"Accessibility.dll",
"Microsoft.CSharp.dll",
"System.Configuration.dll",
"System.Configuration.Install.dll",
"System.Core.dll",
"System.Data.dll",
"System.Data.DataSetExtensions.dll",
"System.Data.Linq.dll",
"System.Data.OracleClient.dll",
"System.Deployment.dll",
"System.Design.dll",
"System.DirectoryServices.dll",
"System.dll",
"System.Drawing.Design.dll",
"System.Drawing.dll",
"System.EnterpriseServices.dll",
"System.Management.dll",
"System.Messaging.dll",
"System.Runtime.Remoting.dll",
"System.Runtime.Serialization.dll",
"System.Runtime.Serialization.Formatters.Soap.dll",
"System.Security.dll",
"System.ServiceModel.dll",
"System.ServiceModel.Web.dll",
"System.ServiceProcess.dll",
"System.Transactions.dll",
"System.Web.dll",
"System.Web.Extensions.Design.dll",
"System.Web.Extensions.dll",
"System.Web.Mobile.dll",
"System.Web.RegularExpressions.dll",
"System.Web.Services.dll",
"System.Windows.Forms.dll",
"System.Workflow.Activities.dll",
"System.Workflow.ComponentModel.dll",
"System.Workflow.Runtime.dll",
"System.Xml.dll",
"System.Xml.Linq.dll",
}, StringComparer.OrdinalIgnoreCase);
}
示例15: CheckFullpaths
public void CheckFullpaths()
{
string source = Temp.CreateFile(prefix: "", extension: ".cs").WriteAllText(@"
public class C
{
public static void Main()
{
string x;
}
}").Path;
var baseDir = Path.GetDirectoryName(source);
var fileName = Path.GetFileName(source);
// Checks the base case without /fullpaths (expect to see relative path name)
// c:\temp> csc.exe c:\temp\a.cs
// a.cs(6,16): warning CS0168: The variable 'x' is declared but never used
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var csc = new MockCSharpCompiler(null, baseDir, new[] { source, "/preferreduilang:en" });
int exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Contains(fileName + "(6,16): warning CS0168: The variable 'x' is declared but never used", outWriter.ToString(), StringComparison.Ordinal);
// Checks the base case without /fullpaths when the file is located in the sub-folder (expect to see relative path name)
// c:\temp> csc.exe c:\temp\example\a.cs
// example\a.cs(6,16): warning CS0168: The variable 'x' is declared but never used
outWriter = new StringWriter(CultureInfo.InvariantCulture);
csc = new MockCSharpCompiler(null, Directory.GetParent(baseDir).FullName, new[] { source, "/preferreduilang:en" });
exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Contains(fileName + "(6,16): warning CS0168: The variable 'x' is declared but never used", outWriter.ToString(), StringComparison.Ordinal);
Assert.DoesNotContain(source, outWriter.ToString(), StringComparison.Ordinal);
// Checks the base case without /fullpaths when the file is not located under the base directory (expect to see the full path name)
// c:\temp> csc.exe c:\test\a.cs
// c:\test\a.cs(6,16): warning CS0168: The variable 'x' is declared but never used
outWriter = new StringWriter(CultureInfo.InvariantCulture);
csc = new MockCSharpCompiler(null, Temp.CreateDirectory().Path, new[] { source, "/preferreduilang:en" });
exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Contains(source + "(6,16): warning CS0168: The variable 'x' is declared but never used", outWriter.ToString(), StringComparison.Ordinal);
// Checks the case with /fullpaths (expect to see the full paths)
// c:\temp> csc.exe c:\temp\a.cs /fullpaths
// c:\temp\a.cs(6,16): warning CS0168: The variable 'x' is declared but never used
outWriter = new StringWriter(CultureInfo.InvariantCulture);
csc = new MockCSharpCompiler(null, baseDir, new[] { source, "/fullpaths", "/preferreduilang:en" });
exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Contains(source + @"(6,16): warning CS0168: The variable 'x' is declared but never used", outWriter.ToString(), StringComparison.Ordinal);
// Checks the base case without /fullpaths when the file is located in the sub-folder (expect to see the full path name)
// c:\temp> csc.exe c:\temp\example\a.cs /fullpaths
// c:\temp\example\a.cs(6,16): warning CS0168: The variable 'x' is declared but never used
outWriter = new StringWriter(CultureInfo.InvariantCulture);
csc = new MockCSharpCompiler(null, Directory.GetParent(baseDir).FullName, new[] { source, "/preferreduilang:en", "/fullpaths" });
exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Contains(source + "(6,16): warning CS0168: The variable 'x' is declared but never used", outWriter.ToString(), StringComparison.Ordinal);
// Checks the base case without /fullpaths when the file is not located under the base directory (expect to see the full path name)
// c:\temp> csc.exe c:\test\a.cs /fullpaths
// c:\test\a.cs(6,16): warning CS0168: The variable 'x' is declared but never used
outWriter = new StringWriter(CultureInfo.InvariantCulture);
csc = new MockCSharpCompiler(null, Temp.CreateDirectory().Path, new[] { source, "/preferreduilang:en", "/fullpaths" });
exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);
Assert.Contains(source + "(6,16): warning CS0168: The variable 'x' is declared but never used", outWriter.ToString(), StringComparison.Ordinal);
CleanupAllGeneratedFiles(source);
}