public void WithSyntaxErrors(DiagnosticAnalyzer analyzer) { var testCode = @" using System; using System.IO; public class Foo : SyntaxError { private readonly Stream stream = File.SyntaxError(string.Empty); private bool disposed; protected override void Dispose(bool disposing) { if (this.syntaxError) { return; } this.disposed = true; if (disposing) { this.stream.Dispose(); } base.Dispose(disposing); } }"; RoslynAssert.NoAnalyzerDiagnostics(analyzer, testCode); }
public static void IgnoreNewFormShow() { var winForm = @" namespace N { using System.Windows.Forms; public class Winform : Form { } }"; var code = @" namespace N { public class C { void M() { var form = new Winform(); form.Show(); } } }"; RoslynAssert.NoAnalyzerDiagnostics(Analyzer, winForm, code); }
private static void Assert(string code, string descriptorName) { var analyzer = new CommutativityAnalyzer(); var references = new[] { MetadataReference.CreateFromFile(typeof(string).Assembly.Location), MetadataReference.CreateFromFile(typeof(Console).Assembly.Location), }; var allCode = CommutativeAttribute + code; if (descriptorName != null) { var descriptor = Descriptors.ByName(descriptorName); RoslynAssert.Diagnostics( analyzer, ExpectedDiagnostic.Create(descriptor), allCode, metadataReferences: references); } else { RoslynAssert.NoAnalyzerDiagnostics(analyzer, allCode); } }
public void NoDiagnosticWhenExpectedIsErrorTypeArray() { var testCode = TestUtility.WrapInTestMethod(@" var actual = new[] { 3 }; var expected = HaveNoIdea(); Assert.That(actual, Is.EqualTo(new[] { expected }));"); RoslynAssert.NoAnalyzerDiagnostics(analyzer, testCode); }
public void NoDiagnosticWhenActualIsErrorType() { var testCode = TestUtility.WrapInTestMethod(@" var actual = HaveNoIdea(); var expected = 3; Assert.That(actual, Is.EqualTo(expected));"); RoslynAssert.NoAnalyzerDiagnostics(analyzer, testCode); }
public void WhenValid() { this.projectFile.Directory.FindFile("Properties\\Resources.resx").ReplaceText("\"Key\"", "\"Value\""); this.projectFile.Directory.FindFile("Properties\\Resources.sv.resx").ReplaceText("\"Key\"", "\"Value\""); this.projectFile.Directory.FindFile("Properties\\Resources.sv-SE.resx").ReplaceText("\"Key\"", "\"Value\""); this.projectFile.Directory.FindFile("Properties\\Resources.Designer.cs").ReplaceText("public static string Key", "public static string Value"); var sln = CodeFactory.CreateSolution(this.projectFile, MetadataReferences.FromAttributes()); RoslynAssert.NoAnalyzerDiagnostics(Analyzer, sln); }
public static void WhenValid(string key, string value) { projectFile.Directory.FindFile("Properties\\Resources.resx").ReplaceText("\"Key\"", $"\"{key}\""); projectFile.Directory.FindFile("Properties\\Resources.resx").ReplaceText("<value>Value</value>", $"<value>{value}</value>"); projectFile.Directory.FindFile("Properties\\Resources.sv.resx").ReplaceText("\"Key\"", $"\"{key}\""); projectFile.Directory.FindFile("Properties\\Resources.sv-SE.resx").ReplaceText("\"Key\"", $"\"{key}\""); projectFile.Directory.FindFile("Properties\\Resources.Designer.cs").ReplaceText("public static string Key", $"public static string {key}"); var solution = CodeFactory.CreateSolution(projectFile, MetadataReferences.FromAttributes()); RoslynAssert.NoAnalyzerDiagnostics(Analyzer, solution); }
public void DemoProject(DiagnosticAnalyzer analyzer) { switch (analyzer) { case LiteralAnalyzer _: case MemberAccessAnalyzer _: case ResourceAnalyzer _: // Just checking so that the analyzer does not throw here. _ = Analyze.GetDiagnostics(analyzer, DemoProjectSolution); break; default: RoslynAssert.NoAnalyzerDiagnostics(analyzer, DemoProjectSolution); break; } }
public static void LocalAddedToFormComponents(string expression) { var code = @" namespace N { using System.IO; using System.Windows.Forms; public class Winform : Form { Winform() { var stream = File.OpenRead(string.Empty); // Since this is added to components, it is automatically disposed of with the form. this.components.Add(stream); } } }".AssertReplace("this.components.Add(stream)", expression); RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code); }
public static void WhenAnalyzerThrows() { var code = @" namespace N { class C { } }"; var expected = "Expected no diagnostics, found:\r\n" + "AD0001 Analyzer 'Gu.Roslyn.Asserts.Tests.ThrowingAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Analyzer threw this.'.\r\n" + " at line 0 and character 0 in file | Code did not have position 0,0\r\n"; var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoAnalyzerDiagnostics(new ThrowingAnalyzer(), code)); Assert.AreEqual(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.NoAnalyzerDiagnostics(typeof(ThrowingAnalyzer), code)); Assert.AreEqual(expected, exception.Message); }
public static void SingleDocumentFieldNameMustNotBeginWithUnderscore() { var code = @" namespace N { class C { private readonly int _value = 1; } }"; var expected = "Expected no diagnostics, found:\r\n" + "SA1309 Field '_value' must not begin with an underscore\r\n" + " at line 5 and character 29 in file C.cs | private readonly int ↓_value = 1;\r\n"; var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoAnalyzerDiagnostics(new FieldNameMustNotBeginWithUnderscore(), code)); Assert.AreEqual(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.NoAnalyzerDiagnostics(typeof(FieldNameMustNotBeginWithUnderscore), code)); Assert.AreEqual(expected, exception.Message); }
public void Analyzer_AsNoTrackingAndIdEqualsNumber_DoesNotReport() { var code = TestCode("↓context.Things.AsNoTracking().First(x => x.Id == 42);"); RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code); }
public void Analyzer_IdEqualsLambdaParameter_DoesNotReport() { var code = TestCode("↓context.Things.First(x => x.Id == x.Name.Length);"); RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code); }
public static void AnalyzersSolution(DiagnosticAnalyzer analyzer) { RoslynAssert.NoAnalyzerDiagnostics(analyzer, AnalyzersProjectSolution); }
public static void Run(DiagnosticAnalyzer analyzer) { RoslynAssert.NoAnalyzerDiagnostics(analyzer, Solution); }