public async Task TestExternMethod() { await VerifyCS.VerifyCodeFixAsync(@" using System; using System.Runtime.InteropServices; internal class Program { [DllImport(""User32.dll"", CharSet = CharSet.Unicode)] static extern int [|MessageBox|](IntPtr h, string m, string c, int type); } ", @" using System; using System.Runtime.InteropServices; internal class Program { [DllImport(""User32.dll"", CharSet = CharSet.Unicode)] private static extern int [|MessageBox|](IntPtr h, string m, string c, int type); } "); }
public async Task AnonymousFunction_Task_BlockBody() { await VerifyCS.VerifyCodeFixAsync( @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task> foo = (Func<Task>)async {|CS1998:delegate|} { if (System.DateTime.Now.Ticks > 0) { return; } }; } }", @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task> foo = (Func<Task>)delegate { if (System.DateTime.Now.Ticks > 0) { return Task.CompletedTask; } return Task.CompletedTask; }; } }"); }
public async Task ParenthesisedLambda_Task_BlockBody() { await VerifyCS.VerifyCodeFixAsync( @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task> foo = async () {|CS1998:=>|} { if (System.DateTime.Now.Ticks > 0) { return; } }; } }", @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task> foo = () => { if (System.DateTime.Now.Ticks > 0) { return Task.CompletedTask; } return Task.CompletedTask; }; } }"); }
public async Task TestAssignment() { await VerifyCS.VerifyCodeFixAsync( @"class Program { void M(int i) { int j; [|switch|] (i) { case 1: j = 4; break; case 2: j = 5; break; case 3: j = 6; break; } throw null; } }", @"class Program { void M(int i) { var j = i switch { 1 => 4, 2 => 5, 3 => 6, _ => throw null, }; } }"); }
public async Task LocalFunction_Task_BlockBody() { await VerifyCS.VerifyCodeFixAsync( @"using System.Threading.Tasks; class C { public void M1() { async Task {|CS1998:Goo|}() { if (System.DateTime.Now.Ticks > 0) { return; } } } }", @"using System.Threading.Tasks; class C { public void M1() { Task Goo() { if (System.DateTime.Now.Ticks > 0) { return Task.CompletedTask; } return Task.CompletedTask; } } }" ); }
public async Task TestParenthesizedExpression2() { await VerifyCS.VerifyCodeFixAsync( @"class Program { int M() { [|switch|] (1 + 1) { default: return 0; } } }", @"class Program { int M() { return (1 + 1) switch { _ => 0, }; } }"); }
public async Task TestParenthesizedExpression1() { await VerifyCS.VerifyCodeFixAsync( @"class Program { int M(object i) { [|switch|] (i.GetType()) { default: return 0; } } }", @"class Program { int M(object i) { return i.GetType() switch { _ => 0, }; } }"); }
public async Task TestBasicCase() { await VerifyCS.VerifyCodeFixAsync( @" class C { void M(string[] args) { [|var|] temp = args[0]; args[0] = args[1]; args[1] = temp; } } ", @" class C { void M(string[] args) { (args[1], args[0]) = (args[0], args[1]); } } "); }
public async Task LocalFunction_Task_ExpressionBody() { await VerifyCS.VerifyCodeFixAsync( @"using System; using System.Threading.Tasks; class C { public void M1() { async Task {|CS1998:Goo|}() => Console.WriteLine(1); } }", @"using System; using System.Threading.Tasks; class C { public void M1() { Task Goo() { Console.WriteLine(1); return Task.CompletedTask; } } }"); }
public async Task TestRemoveWithIsPattern1() { await VerifyCS.VerifyCodeFixAsync( @" class C { void M(object o) { if (o [|!|]is string s) { } } }", @" class C { void M(object o) { if (o is string s) { } } }"); }
public async Task TestArbitraryParens() { await VerifyCS.VerifyCodeFixAsync( @" class C { void M(string[] args) { [|var|] temp = (args[0]); ((args[0])) = (((args[1]))); ((((args[1])))) = (((((temp))))); } } ", @" class C { void M(string[] args) { (args[1], args[0]) = (args[0], args[1]); } } "); }
public async Task ParenthesisedLambda_Task_ExpressionBody() { await VerifyCS.VerifyCodeFixAsync( @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task> foo = async () {|CS1998:=>|} Console.WriteLine(1); } }", @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task> foo = () => { Console.WriteLine(1); return Task.CompletedTask; }; } }"); }
public async Task ParenthesisedLambda_TaskOfT_ExpressionBody() { await VerifyCS.VerifyCodeFixAsync( @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task<int>> foo = async () {|CS1998:=>|} 1; } }", @"using System; using System.Threading.Tasks; class C { public void M1() { Func<Task<int>> foo = () => Task.FromResult(1); } }"); }
public async Task PrimitiveTypeWithUsing() { var text = @"using System; class Test { void Method() { var typeName = [|typeof(int).Name|]; } } "; var expected = @"using System; class Test { void Method() { var typeName = nameof(Int32); } } "; await VerifyCS.VerifyCodeFixAsync(text, expected); }
public async Task Method_Task_ExpressionBody() { await VerifyCS.VerifyCodeFixAsync( @" using System; using System.Threading.Tasks; class C { async Task {|CS1998:Goo|}() => Console.WriteLine(""Hello""); }", @" using System; using System.Threading.Tasks; class C { Task Goo() { Console.WriteLine(""Hello""); return Task.CompletedTask; } }"); }
public async Task TestDeclarationInOuterScope() { await VerifyCS.VerifyCodeFixAsync( @"using System; using System.IO; class Program { static SeekOrigin origin; static long offset; static long position; static long length; public static void Test() { long target; try { [|switch|] (origin) { case SeekOrigin.Begin: target = offset; break; case SeekOrigin.Current: target = checked(offset + position); break; case SeekOrigin.End: target = checked(offset + length); break; default: throw new ArgumentOutOfRangeException(nameof(origin)); } } catch (OverflowException) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (target < 0) { throw new ArgumentOutOfRangeException(nameof(offset)); } } }", @"using System; using System.IO; class Program { static SeekOrigin origin; static long offset; static long position; static long length; public static void Test() { long target; try { target = origin switch { SeekOrigin.Begin => offset, SeekOrigin.Current => checked(offset + position), SeekOrigin.End => checked(offset + length), _ => throw new ArgumentOutOfRangeException(nameof(origin)), }; } catch (OverflowException) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (target < 0) { throw new ArgumentOutOfRangeException(nameof(offset)); } } }"); }
public async Task TestNested_01() { await VerifyCS.VerifyCodeFixAsync( @"class Program { int M(int i, int j) { int r; [|switch|] (i) { case 1: r = 1; break; case 2: r = 2; break; case 3: r = 3; break; default: r = 4; break; } int x, y; switch (i) { case 1: x = 1; y = 1; break; case 2: x = 1; y = 1; break; case 3: x = 1; y = 1; break; default: x = 1; y = 1; break; } [|switch|] (i) { default: throw null; case 1: [|switch|] (j) { case 10: return 10; case 20: return 20; case 30: return 30; } return 0; case 2: [|switch|] (j) { case 10: return 10; case 20: return 20; case 30: return 30; case var _: return 0; } case 3: [|switch|] (j) { case 10: return 10; case 20: return 20; case 30: return 30; case var v: return 0; } } } }", @"class Program { int M(int i, int j) { var r = i switch { 1 => 1, 2 => 2, 3 => 3, _ => 4, }; int x, y; switch (i) { case 1: x = 1; y = 1; break; case 2: x = 1; y = 1; break; case 3: x = 1; y = 1; break; default: x = 1; y = 1; break; } return i switch { 1 => j switch { 10 => 10, 20 => 20, 30 => 30, _ => 0, }, 2 => j switch { 10 => 10, 20 => 20, 30 => 30, var _ => 0, }, 3 => j switch { 10 => 10, 20 => 20, 30 => 30, var v => 0, }, _ => throw null, }; } }"); }
public async Task TestAllConstructs() { await VerifyCS.VerifyCodeFixAsync( @"using System; namespace Outer { namespace Inner1.Inner2 { partial class [|C|] : I { class [|NestedClass|] { } struct [|NestedStruct|] { } int [|f1|]; int [|f2|], f3; public int f4; event Action [|e1|], e2; public event Action e3; event Action [|e4|] { add { } remove { } } public event Action e5 { add { } remove { } } event Action I.e6 { add { } remove { } } static C() { } [|C|]() { } public C(int i) { } ~C() { } void [|M1|]() { } public void M2() { } void I.M3() { } partial void M4(); partial void M4() { } int [|P1|] { get; } public int P2 { get; } int I.P3 { get; } int [|this|][int i] => throw null; public int this[string s] => throw null; int I.this[bool b] => throw null; } interface [|I|] { event Action e6; void M3(); int P3 { get; } int this[bool b] { get; } } delegate void [|D|](); enum [|E|] { EMember } } }", @"using System; namespace Outer { namespace Inner1.Inner2 { internal partial class C : I { private class NestedClass { } private struct NestedStruct { } private int f1; private int f2, f3; public int f4; private event Action e1, e2; public event Action e3; private event Action e4 { add { } remove { } } public event Action e5 { add { } remove { } } event Action I.e6 { add { } remove { } } static C() { } private C() { } public C(int i) { } ~C() { } private void M1() { } public void M2() { } void I.M3() { } partial void M4(); partial void M4() { } private int P1 { get; } public int P2 { get; } int I.P3 { get; } private int this[int i] => throw null; public int this[string s] => throw null; int I.this[bool b] => throw null; } internal interface I { event Action e6; void M3(); int P3 { get; } int this[bool b] { get; } } internal delegate void D(); internal enum E { EMember } } }"); }
private static async Task TestMissingAsync(string initialMarkup) => await VerifyCS.VerifyCodeFixAsync(initialMarkup, initialMarkup);
public async Task Method_Task_MultipleAndNested() { await VerifyCS.VerifyCodeFixAsync( @" using System; using System.Threading.Tasks; class C { async Task {|CS1998:Goo|}() { if (DateTime.Now.Ticks > 0) { return; } } async Task {|CS1998:Foo|}() { Console.WriteLine(1); } async Task {|CS1998:Bar|}() { async Task {|CS1998:Baz|}() { Func<Task<int>> g = async () {|CS1998:=>|} 5; } } async Task<string> {|CS1998:Tur|}() { async Task<string> {|CS1998:Duck|}() { async Task<string> {|CS1998:En|}() { return ""Developers!""; } return ""Developers! Developers!""; } return ""Developers! Developers! Developers!""; } async Task {|CS1998:Nurk|}() { Func<Task<int>> f = async () {|CS1998:=>|} 4; if (DateTime.Now.Ticks > f().Result) { } } }", @" using System; using System.Threading.Tasks; class C { Task Goo() { if (DateTime.Now.Ticks > 0) { return Task.CompletedTask; } return Task.CompletedTask; } Task Foo() { Console.WriteLine(1); return Task.CompletedTask; } Task Bar() { Task Baz() { Func<Task<int>> g = () => Task.FromResult(5); return Task.CompletedTask; } return Task.CompletedTask; } Task<string> Tur() { Task<string> Duck() { Task<string> En() { return Task.FromResult(""Developers!""); } return Task.FromResult(""Developers! Developers!""); } return Task.FromResult(""Developers! Developers! Developers!""); } Task Nurk() { Func<Task<int>> f = () => Task.FromResult(4); if (DateTime.Now.Ticks > f().Result) { } return Task.CompletedTask; } }"); }