public async Task TestDoWhileOnClosingBraceWithAllowSettingAsync() { var testSettings = @" { ""settings"": { ""layoutRules"": { ""allowDoWhileOnClosingBrace"": true } } }"; var testCode = @"public class Foo { private void Bar() { var x = 0; do { x = 1; } while (x == 0); } }"; var test = new CSharpTest { TestCode = testCode, Settings = testSettings, }; await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private async Task TestFixAllExecuterAsync(int codepage, FixAllScope scope) { var test = new CSharpTest { TestSources = { SourceText.From("class Foo { }", Encoding.GetEncoding(codepage)), SourceText.From("class Bar { }", Encoding.GetEncoding(codepage)), }, ExpectedDiagnostics = { Diagnostic().WithLocation("Test0.cs", 1, 1), Diagnostic().WithLocation("Test1.cs", 1, 1), }, FixedSources = { SourceText.From("class Foo { }", Encoding.UTF8), SourceText.From("class Bar { }", Encoding.UTF8), }, NumberOfFixAllIterations = 1, NumberOfFixAllInDocumentIterations = 2, }; test.Exclusions &= ~AnalysisExclusions.Suppression; await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestGlobalStatementAndRecordSpacingInTopLevelProgramAsync() { var testCode = @"return 0; {|#0:record|} A(); "; var fixedCode = @"return 0; record A(); "; var test = new CSharpTest() { ReferenceAssemblies = ReferenceAssemblies.Net.Net50, TestState = { OutputKind = OutputKind.ConsoleApplication, Sources = { testCode }, }, FixedCode = fixedCode, }; var expectedDiagnostics = this.GetExpectedResultTestGlobalStatementAndRecordSpacingInTopLevelProgram(); test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostics); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestMultipleUsingStatementsWithDefaultSettingsAsync(bool suppressSA1519) { var testCode = @"using System; public class Foo { public void Bar(int i) { using (default(IDisposable)) using (default(IDisposable)) { } } }"; var test = new CSharpTest { TestCode = testCode, }; if (suppressSA1519) { test.DisabledDiagnostics.Add(SA1519BracesMustNotBeOmittedFromMultiLineChildStatement.DiagnosticId); } await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private async Task TestCommaInStatementOrDeclAsync(string originalStatement, DiagnosticResult[] expected, string fixedStatement) { string template = @"namespace Foo {{ class Bar {{ void Baz() {{ {0} }} // The following fields and method are referenced by the tests and need definitions. int a, b; void f(int x, int y) {{ }} }} }} "; string originalCode = string.Format(template, originalStatement); string fixedCode = string.Format(template, fixedStatement); var test = new CSharpTest { TestCode = originalCode, FixedCode = fixedCode, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, DiagnosticResult[] remainingDiagnostics, CancellationToken cancellationToken) { string testSettings = $@" {{ ""settings"": {{ ""orderingRules"": {{ ""usingDirectivesPlacement"": ""{this.usingDirectivesPlacement}"" }} }} }} "; var test = new CSharpTest { TestCode = source, FixedCode = fixedSource, Settings = testSettings, }; if (source == fixedSource) { test.FixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll; test.FixedState.MarkupHandling = MarkupMode.Allow; test.BatchFixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll; test.BatchFixedState.MarkupHandling = MarkupMode.Allow; } test.ExpectedDiagnostics.AddRange(expected); test.RemainingDiagnostics.AddRange(remainingDiagnostics); return(test.RunAsync(cancellationToken)); }
private Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken) { var systemUsingDirectivesFirst = this.systemUsingDirectivesFirst ?? true; var testSettings = $@" {{ ""settings"": {{ ""orderingRules"": {{ ""usingDirectivesPlacement"": ""{(this.usingsInsideNamespace ? "insideNamespace" : "outsideNamespace")}"", ""systemUsingDirectivesFirst"" : {systemUsingDirectivesFirst.ToString().ToLowerInvariant()}, ""blankLinesBetweenUsingGroups"": ""require"" }} }} }} "; var test = new CSharpTest { TestCode = source, FixedCode = fixedSource, Settings = testSettings, }; test.ExpectedDiagnostics.AddRange(expected); return(test.RunAsync(cancellationToken)); }
protected async Task TestKeywordStatementAsync(string statement, DiagnosticResult[] expected, string fixedStatement, string returnType = "void", bool asyncMethod = false) { string testCodeFormat = @" using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Namespace {{ {2}class ClassName {{ {0}{4} Foo() {{ {1} }} {3} }} }} "; string unsafeModifier = asyncMethod ? string.Empty : "unsafe "; string asyncModifier = asyncMethod ? "async " : string.Empty; string awaitMethod = asyncMethod ? string.Empty : "int await(Task task) { return 0; }"; string testCode = string.Format(testCodeFormat, asyncModifier, statement, unsafeModifier, awaitMethod, returnType); string fixedTest = string.Format(testCodeFormat, asyncModifier, fixedStatement, unsafeModifier, awaitMethod, returnType); var test = new CSharpTest { TestCode = testCode, FixedCode = fixedTest, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestClassVirtualInheritedMembersAsync(bool compilerWarning, string memberName, string memberData) { var testCode = $@"using System; public class ParentClass {{ /// <summary> /// Some documentation. /// </summary> public virtual {memberData} }} public class ChildClass : ParentClass {{ public override {memberData} }} "; var fixedCode = $@"using System; public class ParentClass {{ /// <summary> /// Some documentation. /// </summary> public virtual {memberData} }} public class ChildClass : ParentClass {{ /// <inheritdoc/> public override {memberData} }} "; var descriptor = compilerWarning ? CS1591 : SA1600; var test = new CSharpTest { TestCode = testCode, ExpectedDiagnostics = { Diagnostic(descriptor).WithArguments("ParentClass").WithLocation(2, 14), Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14), Diagnostic(descriptor).WithArguments(memberName).WithLocation(12, 40), }, FixedCode = fixedCode, RemainingDiagnostics = { Diagnostic(descriptor).WithArguments("ParentClass").WithLocation(2, 14), Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14), }, }; if (compilerWarning) { test.DisabledDiagnostics.Add(SA1600.Id); test.SolutionTransforms.Add(SetCompilerDocumentationWarningToError); } await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private async Task TestFixAllExecuterAsync(int codepage, FixAllScope scope) { // Currently unused _ = scope; var test = new CSharpTest { TestSources = { SourceText.From("class Foo { }", Encoding.GetEncoding(codepage)), SourceText.From("class Bar { }", Encoding.GetEncoding(codepage)), }, ExpectedDiagnostics = { Diagnostic().WithLocation("/0/Test0.cs", 1, 1), Diagnostic().WithLocation("/0/Test1.cs", 1, 1), }, FixedSources = { SourceText.From("class Foo { }", Encoding.UTF8), SourceText.From("class Bar { }", Encoding.UTF8), }, NumberOfFixAllIterations = 1, NumberOfFixAllInDocumentIterations = 2, }; test.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task VerifyInvalidCodeConstructionsAsync() { var testCode = @"using System; public class TestClass { public static EventHandler[] TestMethod() => delegate { }; } "; DiagnosticResult[] expected = { Diagnostic().WithSpan(4, 50, 4, 58), DiagnosticResult.CompilerError("CS1660").WithMessage("Cannot convert anonymous method to type 'EventHandler[]' because it is not a delegate type").WithSpan(4, 50, 4, 62), }; var test = new CSharpTest { TestCode = testCode, FixedCode = testCode, }; test.ExpectedDiagnostics.AddRange(expected); test.RemainingDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestDirectiveTriviaAsync() { var testCode = @" public class TestClass { public void TestMethod1( #if true ) #endif { } public void TestMethod2() { TestMethod1( #if true #endif ); } } "; var fixedCode = @" public class TestClass { public void TestMethod1( #if true ) #endif { } public void TestMethod2() { TestMethod1( #if true #endif ); } } "; DiagnosticResult[] expected = { Diagnostic().WithLocation(6, 9), Diagnostic().WithLocation(16, 13), }; var test = new CSharpTest { TestCode = testCode, FixedCode = fixedCode, NumberOfIncrementalIterations = 1, NumberOfFixAllIterations = 1, }; test.ExpectedDiagnostics.AddRange(expected); test.RemainingDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestPositionalRecord1Async() { var testCode = @" public record {|#0:r|}(int A) { public r(int a, int b) : this(A: a) { } } "; var fixedCode = @" public record R(int A) { public R(int a, int b) : this(A: a) { } } "; var test = new CSharpTest() { ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50, TestCode = testCode, FixedCode = fixedCode, }; var expectedDiagnostics = this.GetExpectedResultTestPositionalRecord1(); test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostics); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestTypeMemberOrderWrongOrderClassAsync() { string testCode = @"public class OuterType { public string TestField; ~OuterType() { } public OuterType() { } public interface ITest { } public delegate void TestDelegate(); public event TestDelegate TestEvent { add { } remove { } } public enum TestEnum { } public static OuterType operator +(OuterType t1, OuterType t2) { return t1; } public static explicit operator bool(OuterType t1) { return t1.TestField != null; } public string TestProperty { get; set; } public struct TestStruct { } public void TestMethod () { } public class TestClass { } public string this[string arg] { get { return ""foo""; } set { } } } "; var expected = new[] { Diagnostic().WithLocation(5, 12).WithArguments("constructor", "destructor"), Diagnostic().WithLocation(7, 26).WithArguments("delegate", "interface"), Diagnostic().WithLocation(11, 5).WithArguments("conversion", "operator"), Diagnostic().WithLocation(12, 19).WithArguments("property", "conversion"), Diagnostic().WithLocation(14, 17).WithArguments("method", "struct"), Diagnostic().WithLocation(16, 19).WithArguments("indexer", "class"), }; string fixedCode = @"public class OuterType { public string TestField; public OuterType() { } ~OuterType() { } public delegate void TestDelegate(); public event TestDelegate TestEvent { add { } remove { } } public enum TestEnum { } public interface ITest { } public string TestProperty { get; set; } public string this[string arg] { get { return ""foo""; } set { } } public static explicit operator bool(OuterType t1) { return t1.TestField != null; } public static OuterType operator +(OuterType t1, OuterType t2) { return t1; } public void TestMethod () { } public struct TestStruct { } public class TestClass { } } "; var test = new CSharpTest { TestCode = testCode, FixedCode = fixedCode, NumberOfIncrementalIterations = 8, NumberOfFixAllIterations = 3, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private async Task TestKeywordDeclarationAsync(string statement, DiagnosticResult[] expected, string fixedStatement) { string testCodeFormat = @" using System; using System.Linq; using System.Threading.Tasks; namespace Namespace {{ class ClassName {{ {0} }} }} "; string testCode = string.Format(testCodeFormat, statement); string fixedTest = string.Format(testCodeFormat, fixedStatement); var test = new CSharpTest { TestCode = testCode, FixedCode = fixedTest, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private async Task TestWhitespaceInStatementOrDeclAsync(string originalStatement, string fixedStatement, params DiagnosticResult[] expected) { string template = @"namespace Foo {{ class Bar {{ void DoIt() {{ {0} }} }} }} "; string originalCode = string.Format(template, originalStatement); string fixedCode = string.Format(template, fixedStatement ?? originalStatement); var test = new CSharpTest { TestCode = originalCode, FixedCode = fixedCode, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestFixAllWithMultipleEncodingsAsync() { var test = new CSharpTest { TestSources = { SourceText.From("class Foo { }", Encoding.Unicode), SourceText.From("class Bar { }", Encoding.Unicode), SourceText.From("class FooBar { }", Encoding.UTF7), }, ExpectedDiagnostics = { Diagnostic().WithLocation("/0/Test0.cs", 1, 1), Diagnostic().WithLocation("/0/Test1.cs", 1, 1), Diagnostic().WithLocation("/0/Test2.cs", 1, 1), }, FixedSources = { SourceText.From("class Foo { }", Encoding.UTF8), SourceText.From("class Bar { }", Encoding.UTF8), SourceText.From("class FooBar { }", Encoding.UTF8), }, NumberOfFixAllIterations = 2, NumberOfFixAllInDocumentIterations = 3, }; test.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private async Task TestWhitespaceInStatementOrDeclAsync(string originalStatement, string fixedStatement, params DiagnosticResult[] expected) { string template = @"namespace Foo {{ class Bar {{ unsafe void DoIt() {{ {0} }} Baz GetA() {{ return null; }} Baz GetB() {{ return null; }} unsafe int* GetPointer() {{ return null; }} class Baz {{ public object this[int i] {{ get {{ return null; }} }} public object Test {{ get {{ return null; }} }} }} }} }} "; string originalCode = string.Format(template, originalStatement); string fixedCode = string.Format(template, fixedStatement ?? originalStatement); var test = new CSharpTest { TestCode = originalCode, FixedCode = fixedCode, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
private Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken) { var test = new CSharpTest(this) { TestCode = source, }; test.ExpectedDiagnostics.AddRange(expected); return(test.RunAsync(cancellationToken)); }
internal static Task VerifyCSharpDiagnosticAsync(LanguageVersion?languageVersion, string source, DiagnosticResult[] expected, CancellationToken cancellationToken) { var test = new CSharpTest(languageVersion) { TestCode = source, }; test.ExpectedDiagnostics.AddRange(expected); return(test.RunAsync(cancellationToken)); }
public async Task TestInterfaceInheritedMembersAsync(bool compilerWarning, string memberName, string parentData, string childData) { var testCode = $@"using System; public interface IParent {{ /// <summary> /// Some documentation. /// </summary> {parentData} }} public class ChildClass : IParent {{ public {childData} }} "; var fixedCode = $@"using System; public interface IParent {{ /// <summary> /// Some documentation. /// </summary> {parentData} }} public class ChildClass : IParent {{ /// <inheritdoc/> public {childData} }} "; var descriptor = compilerWarning ? CS1591 : SA1600; var test = new CSharpTest { TestCode = testCode, ExpectedDiagnostics = { Diagnostic(descriptor).WithArguments("IParent").WithLocation(2, 18), Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14), Diagnostic(descriptor).WithArguments(memberName).WithLocation(12, 31), }, FixedCode = fixedCode, RemainingDiagnostics = { Diagnostic(descriptor).WithArguments("IParent").WithLocation(2, 18), Diagnostic(descriptor).WithArguments("ChildClass").WithLocation(10, 14), }, }; test.DisabledDiagnostics.Add(compilerWarning ? SA1600.Id : CS1591.Id); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
internal static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken) { var test = new CSharpTest { TestCode = source, FixedCode = fixedSource, }; test.ExpectedDiagnostics.AddRange(expected); return(test.RunAsync(cancellationToken)); }
public async Task TestQueryExpressionWithInlineCommentWithMultiLineFixAsync() { var testCode = @"namespace TestNamespace { using System.Linq; public class TestClass { private int[] testArray = { 1, 2, 3, 4, 5 }; public void TestMethod() { var x = from element in testArray where (element > 1) /* test */ select element; } } } "; var fixedTestCode = @"namespace TestNamespace { using System.Linq; public class TestClass { private int[] testArray = { 1, 2, 3, 4, 5 }; public void TestMethod() { var x = from element in testArray where (element > 1) /* test */ select element; } } } "; DiagnosticResult[] expectedDiagnostics = { Diagnostic(SA110xQueryClauses.SA1103Descriptor).WithLocation(11, 21), }; var test = new CSharpTest { TestCode = testCode, CodeActionIndex = 1, FixedCode = fixedTestCode, }; test.ExpectedDiagnostics.AddRange(expectedDiagnostics); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestGroupQueryExpressionAsync() { var testCode = @"namespace TestNamespace { using System.Linq; public class TestClass { private int[][] testArray = { new[] { 1, 2 }, new[] { 3, 4, 5 }, new[] { 6, 7 }, new[] { 8, 9 } }; public void TestMethod() { var x = from element in testArray where (element[0] > 1) group element by element.Length; } } } "; var fixedTestCode = @"namespace TestNamespace { using System.Linq; public class TestClass { private int[][] testArray = { new[] { 1, 2 }, new[] { 3, 4, 5 }, new[] { 6, 7 }, new[] { 8, 9 } }; public void TestMethod() { var x = from element in testArray where (element[0] > 1) group element by element.Length; } } } "; DiagnosticResult[] expectedDiagnostics = { Diagnostic(SA110xQueryClauses.SA1103Descriptor).WithLocation(11, 21), }; var test = new CSharpTest { TestCode = testCode, CodeActionIndex = 1, FixedCode = fixedTestCode, }; test.ExpectedDiagnostics.AddRange(expectedDiagnostics); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestRangeAsync() { const string testCode = @"using System; public class TestClass { public void TestMethod() { var a = new int[0]; var b = a[(..)]; var range = (1..10); } } "; const string fixedCode = @"using System; public class TestClass { public void TestMethod() { var a = new int[0]; var b = a[..]; var range = 1..10; } } "; DiagnosticResult[] expected = { Diagnostic(DiagnosticId).WithSpan(8, 19, 8, 23), Diagnostic(ParenthesesDiagnosticId).WithLocation(8, 19), Diagnostic(ParenthesesDiagnosticId).WithLocation(8, 22), Diagnostic(DiagnosticId).WithSpan(9, 21, 9, 28), Diagnostic(ParenthesesDiagnosticId).WithLocation(9, 21), Diagnostic(ParenthesesDiagnosticId).WithLocation(9, 27), }; var test = new CSharpTest() { ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31, TestCode = testCode, FixedCode = fixedCode, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestFileWithUtf8EncodingWithoutBOMAsync() { var testCode = SourceText.From("class TypeName { }", new UTF8Encoding(false)); var fixedCode = SourceText.From(testCode.ToString(), Encoding.UTF8); var expected = Diagnostic().WithLocation(1, 1); var test = new CSharpTest { TestSources = { testCode }, ExpectedDiagnostics = { expected }, FixedSources = { fixedCode }, }; test.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestFileWithWrongEncodingAsync(int codepage) { var testCode = SourceText.From("class TypeName { }", Encoding.GetEncoding(codepage)); var fixedCode = SourceText.From(testCode.ToString(), Encoding.UTF8); var expected = Diagnostic().WithLocation(1, 1); var test = new CSharpTest { TestSources = { testCode }, ExpectedDiagnostics = { expected }, FixedSources = { fixedCode }, }; test.Exclusions &= ~AnalysisExclusions.Suppression; await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestCodeFixProviderWithNonWhitespaceTriviaAsync() { var testCode = @"using System.Diagnostics; public class Foo { public void Bar(int i) { #pragma warning restore if (i == 0) Debug.Assert(true); else { Debug.Assert(false); } } }"; var fixedTestCode = @"using System.Diagnostics; public class Foo { public void Bar(int i) { #pragma warning restore if (i == 0) { Debug.Assert(true); } else { Debug.Assert(false); } } }"; var test = new CSharpTest { TestCode = testCode, ExpectedDiagnostics = { Diagnostic().WithLocation(8, 13) }, FixedCode = fixedTestCode, }; test.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
internal static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken) { var test = new CSharpTest { TestCode = source, FixedCode = fixedSource, }; if (source == fixedSource) { test.FixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll; test.FixedState.MarkupHandling = MarkupMode.Allow; test.BatchFixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll; test.BatchFixedState.MarkupHandling = MarkupMode.Allow; } test.ExpectedDiagnostics.AddRange(expected); return(test.RunAsync(cancellationToken)); }
public async Task TestThatDiagnosticIsReported_MultipleFieldsWithConflictAsync(string modifiers) { var testCode = @"public class Foo {{ {0} string bar, Bar, barValue; {0} string carValue, Car, car; }}"; var fixedCode = @"public class Foo {{ {0} string BarValue, Bar, BarValueValue; {0} string CarValue, Car, Car1; }}"; var batchFixedCode = @"public class Foo {{ {0} string BarValue, Bar, BarValueValue; {0} string CarValueValue, Car, CarValue; }}"; DiagnosticResult[] expected = { Diagnostic().WithArguments("bar").WithLocation(4, 8), Diagnostic().WithArguments("barValue").WithLocation(4, 18), Diagnostic().WithArguments("carValue").WithLocation(6, 8), Diagnostic().WithArguments("car").WithLocation(6, 23), }; var test = new CSharpTest { TestCode = string.Format(testCode, modifiers), FixedCode = string.Format(fixedCode, modifiers), BatchFixedCode = string.Format(batchFixedCode, modifiers), NumberOfFixAllIterations = 2, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }