public async Task TestContinuationQueryExpressionAsync() { 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 group element by element.Length into g orderby g.Key select g; // verify that the placement of the into keyword is irrelevant (and preserved) var y = from element in testArray group element by element.Length into g orderby g.Key select g; } } } "; 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 group element by element.Length into g orderby g.Key select g; // verify that the placement of the into keyword is irrelevant (and preserved) var y = from element in testArray group element by element.Length into g orderby g.Key select g; } } } "; DiagnosticResult[] expectedDiagnostics = { Diagnostic(SA110xQueryClauses.SA1103Descriptor).WithLocation(12, 17), }; var test = new CSharpTest { TestCode = testCode, CodeFixIndex = 1, FixedCode = fixedTestCode, }; test.ExpectedDiagnostics.AddRange(expectedDiagnostics); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestCodeFixSpecialCasesAsync() { var testCode = @" using System; internal class TypeName { private void Method(object o) { Action a = delegate() { Console.WriteLine(); }; Action b = delegate() { Console.WriteLine(); }; Action<int> c = delegate(int x) { Console.WriteLine(); }; Action<int, int> d = delegate(int x, int y) { Console.WriteLine(); }; Action<int, int> e = delegate (int x, int y = 0) { Console.WriteLine(); }; Action<int, int> f = delegate (int x, [Obsolete]int y) { Console.WriteLine(); }; Action<int, int> g = delegate (int x, params int y) { Console.WriteLine(); }; Action<int> h = delegate (int x, __arglist) { Console.WriteLine(); }; } }"; var fixedCode = @" using System; internal class TypeName { private void Method(object o) { Action a = () => { Console.WriteLine(); }; Action b = () => { Console.WriteLine(); }; Action<int> c = x => { Console.WriteLine(); }; Action<int, int> d = (x, y) => { Console.WriteLine(); }; Action<int, int> e = delegate (int x, int y = 0) { Console.WriteLine(); }; Action<int, int> f = delegate (int x, [Obsolete]int y) { Console.WriteLine(); }; Action<int, int> g = delegate (int x, params int y) { Console.WriteLine(); }; Action<int> h = delegate (int x, __arglist) { Console.WriteLine(); }; } }"; var expected = new[] { Diagnostic().WithLocation(8, 20), Diagnostic().WithLocation(9, 20), Diagnostic().WithLocation(10, 25), Diagnostic().WithLocation(11, 30), Diagnostic().WithLocation(12, 30), Diagnostic(CS1065).WithLocation(12, 53), Diagnostic().WithLocation(13, 30), Diagnostic(CS7014).WithLocation(13, 47), Diagnostic().WithLocation(14, 30), Diagnostic(CS1670).WithLocation(14, 47), Diagnostic().WithLocation(15, 25), Diagnostic(CS1669).WithLocation(15, 42), }; var expectedAfterFix = new[] { Diagnostic().WithLocation(12, 30), Diagnostic(CS1065).WithLocation(12, 53), Diagnostic().WithLocation(13, 30), Diagnostic(CS7014).WithLocation(13, 47), Diagnostic().WithLocation(14, 30), Diagnostic(CS1670).WithLocation(14, 47), Diagnostic().WithLocation(15, 25), Diagnostic(CS1669).WithLocation(15, 42), }; var test = new CSharpTest { TestCode = testCode, FixedCode = fixedCode, NumberOfFixAllIterations = 2, }; test.ExpectedDiagnostics.AddRange(expected); test.RemainingDiagnostics.AddRange(expectedAfterFix); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
public async Task TestDirectiveTriviaAsync() { var testCode = @" public class TestClass { public void TestMethod1(int a, int b #if true , int c) #endif { } public void TestMethod2() { TestMethod1(1, 2 #if true , 3 #else , 4 #endif ); } } "; var fixedCode = @" public class TestClass { public void TestMethod1(int a, int b #if true , int c) #endif { } public void TestMethod2() { TestMethod1(1, 2 #if true , 3 #else , 4 #endif ); } } "; DiagnosticResult[] expected = { Diagnostic().WithLocation(6, 9), Diagnostic().WithLocation(15, 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 TestThatDiagnosticIsReported_SingleFieldAsync(string modifiers) { var testCode = @"public class Foo {{ {0} string Bar; {0} string car; {0} string Dar; {0} string _ear; {0} string _Far; {0} string __gar; {0} string __Har; {0} string ___iar; {0} string ___Jar; }}"; DiagnosticResult[] expected = { Diagnostic().WithArguments("Bar").WithLocation(4, 8), Diagnostic().WithArguments("Dar").WithLocation(8, 8), Diagnostic().WithArguments("_Far").WithLocation(12, 8), Diagnostic().WithArguments("__Har").WithLocation(16, 8), Diagnostic().WithArguments("___Jar").WithLocation(20, 8), }; var fixedCode = @"public class Foo {{ {0} string bar; {0} string car; {0} string dar; {0} string _ear; {0} string _far; {0} string __gar; {0} string __har; {0} string ___iar; {0} string ___jar; }}"; var test = new CSharpTest { TestCode = string.Format(testCode, modifiers), FixedCode = string.Format(fixedCode, modifiers), NumberOfFixAllIterations = 4, }; test.ExpectedDiagnostics.AddRange(expected); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); }
protected override int Run(CSharpTest.Net.CSBuild.Build.BuildEngine engine) { engine.UnloadAll(); return 0; }
public async Task TestMultipleUsingStatementsWithExplicitSettingAsync(bool allowConsecutiveUsings, bool suppressSA1519, bool expectDiagnostic) { var consecutiveUsingsSettings = $@" {{ ""settings"": {{ ""layoutRules"": {{ ""allowConsecutiveUsings"": {(allowConsecutiveUsings ? "true" : "false")} }} }} }} "; var testCode = @"using System; public class Foo { public void Bar(int i) { using (default(IDisposable)) using (default(IDisposable)) { } } }"; var fixedCode = @"using System; public class Foo { public void Bar(int i) { using (default(IDisposable)) { using (default(IDisposable)) { } } } }"; if (expectDiagnostic) { var test = new CSharpTest { TestCode = testCode, ExpectedDiagnostics = { Diagnostic().WithLocation(7, 9) }, FixedCode = fixedCode, Settings = consecutiveUsingsSettings, }; if (suppressSA1519) { test.DisabledDiagnostics.Add(SA1519BracesMustNotBeOmittedFromMultiLineChildStatement.DiagnosticId); } await test.RunAsync(CancellationToken.None).ConfigureAwait(false); } else { var test = new CSharpTest { TestCode = testCode, Settings = consecutiveUsingsSettings, }; if (suppressSA1519) { test.DisabledDiagnostics.Add(SA1519BracesMustNotBeOmittedFromMultiLineChildStatement.DiagnosticId); } await test.RunAsync(CancellationToken.None).ConfigureAwait(false); // Test again with the fixedCode as the test code test.TestSources[0] = (test.TestSources[0].filename, SourceText.From(fixedCode)); await test.RunAsync(CancellationToken.None).ConfigureAwait(false); } }