Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active statements in switch expression #43154

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
350 changes: 347 additions & 3 deletions src/EditorFeatures/CSharpTest/EditAndContinue/ActiveStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ unsafe static void Main(string[] args)

for (int i = 0; i < 10; <AS:10>i++</AS:10>) { }

<AS:11>foreach</AS:11> (var i in new[] { 1, 2 }) { }
foreach (var i <AS:11>in</AS:11> new[] { 1, 2 }) { }

using (<AS:12>var z = new C()</AS:12>) { }

Expand Down Expand Up @@ -370,7 +370,7 @@ static void Goo(int a)
Diagnostic(RudeEditKind.DeleteActiveStatement, "while (true)"),
Diagnostic(RudeEditKind.DeleteActiveStatement, "do"),
Diagnostic(RudeEditKind.DeleteActiveStatement, "for (int i = 0; i < 10; i++ )"),
Diagnostic(RudeEditKind.DeleteActiveStatement, "foreach (var i in new[] { 1, 2 })"),
Diagnostic(RudeEditKind.DeleteActiveStatement, "foreach (var i in new[] { 1, 2 })"),
Diagnostic(RudeEditKind.DeleteActiveStatement, "using ( var z = new C() )"),
Diagnostic(RudeEditKind.DeleteActiveStatement, "fixed ( char* p = \"s\" )"),
Diagnostic(RudeEditKind.DeleteActiveStatement, "label"));
Expand Down Expand Up @@ -4119,6 +4119,60 @@ static void F()
edits.VerifyRudeDiagnostics(active);
}

[Fact]
public void ForEach_DeleteBody()
{
string src1 = @"
class C
{
static void F()
{
foreach (var s in new[] { 1 }) <AS:0>G();</AS:0>
}
}
";
string src2 = @"
class C
{
static void F()
{
foreach (var s in new[] { 1 }) <AS:0>;</AS:0>
}
}
";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active);
}

[Fact]
public void ForEachVariable_DeleteBody()
{
string src1 = @"
class C
{
static void F()
{
foreach ((var a1, var a2) in new[] { (1,1) }) <AS:0>G();</AS:0>
}
}
";
string src2 = @"
class C
{
static void F()
{
foreach ((var a1, var a2) in new[] { (1,1) }) <AS:0>;</AS:0>
}
}
";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active);
}

#endregion

#region For Statement
Expand Down Expand Up @@ -5385,6 +5439,33 @@ public static void Main()
edits.VerifyRudeDiagnostics(active);
}

[Fact]
public void DoWhileBody_Delete()
{
string src1 = @"
class C
{
static void F()
{
do <AS:0>G();</AS:0> while (true);
}
}
";
string src2 = @"
class C
{
static void F()
{
do <AS:0>;</AS:0> while (true);
}
}
";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active);
}

[Fact]
public void SwitchCase_Update1()
{
Expand Down Expand Up @@ -5461,7 +5542,7 @@ public static void Main()

#endregion

#region Switch When Clauses, Patterns
#region Switch Statement When Clauses, Patterns

[Fact]
public void SwitchWhenClause_PatternUpdate1()
Expand Down Expand Up @@ -5981,6 +6062,238 @@ public static int F(object obj)

#endregion

#region Switch Expression

[Fact]
public void SwitchExpression()
{
var src = @"
class C
{
public static int Main()
{
return F() <AS:4>switch
{
int a <AS:0>when F1()</AS:0> => <AS:1>F2()</AS:1>,
bool b => <AS:2>F3()</AS:2>,
_ => <AS:3>F4()</AS:3>
}</AS:4>;
}
}";

var edits = GetTopEdits(src, src);
var active = GetActiveStatements(src, src);

edits.VerifyRudeDiagnostics(active);
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_MemberExpressionBody()
{
var src1 = @"
class C
{
public static int Main() => <AS:0>F() switch { 0 => 1, _ => 2}</AS:0>;
}";
var src2 = @"
class C
{
public static int Main() => <AS:0>G() switch { 0 => 10, _ => 20}</AS:0>;
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.SwitchExpressionUpdate, "switch", FeaturesResources.method));
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_LambdaBody()
{
var src1 = @"
class C
{
public static Func<int> M() => () => <AS:0>F() switch { 0 => 1, _ => 2}</AS:0>;
}";
var src2 = @"
class C
{
public static Func<int> M() => () => <AS:0>G() switch { 0 => 10, _ => 20}</AS:0>;
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active);
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_QueryLambdaBody()
{
var src1 = @"
class C
{
public static IEnumerable<int> M()
{
return
from a in new[] { 1 }
where <AS:0>F() <AS:1>switch { 0 => true, _ => false}</AS:0,1>
select a;
}
}";
var src2 = @"
class C
{
public static IEnumerable<int> M()
{
return
from a in new[] { 1 }
where <AS:0>F() <AS:1>switch { 0 => true, _ => false}</AS:0,1>
select a;
}
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active);
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_NestedInGoverningExpression()
{
var src1 = @"
class C
{
public static int Main() => <AS:3>(F() <AS:1>switch { 0 => 1, _ => 2 }</AS:1>) <AS:2>switch { 1 => <AS:0>10</AS:0>, _ => 20 }</AS:2,3>;
}";
var src2 = @"
class C
{
public static int Main() => <AS:3>(G() <AS:1>switch { 0 => 10, _ => 20 }</AS:1>) <AS:2>switch { 10 => <AS:0>100</AS:0>, _ => 200 }</AS:2,3>;
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.ActiveStatementUpdate, "switch { 0 => 10, _ => 20 }"),
Diagnostic(RudeEditKind.SwitchExpressionUpdate, "switch", FeaturesResources.method),
Diagnostic(RudeEditKind.SwitchExpressionUpdate, "switch", FeaturesResources.method));
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_NestedInArm()
{
var src1 = @"
class C
{
public static int Main() => F1() switch
{
1 when F2() <AS:0>switch { 0 => true, _ => false }</AS:0> => F3() <AS:1>switch { 0 => 1, _ => 2 }</AS:1>,
_ => 20
};
}";
var src2 = @"
class C
{
public static int Main() => F1() switch
{
1 when F2() <AS:0>switch { 0 => true, _ => false }</AS:0> => F3() <AS:1>switch { 0 => 1, _ => 2 }</AS:1>,
_ => 20
};
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active);
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_Delete1()
{
var src1 = @"
class C
{
public static int Main()
{
return Method() switch { true => G(), _ => F2() switch { 1 => <AS:0>0</AS:0>, _ => 2 } };
}
}";
var src2 = @"
class C
{
public static int Main()
{
return Method() switch { true => G(), _ => <AS:0>1</AS:0> };
}
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.SwitchExpressionUpdate, "switch", FeaturesResources.method));
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_Delete2()
{
var src1 = @"
class C
{
public static int Main()
{
return F1() switch { 1 => 0, _ => F2() switch { 1 => <AS:0>0</AS:0>, _ => 2 } };
}
}";
var src2 = @"
class C
{
public static int Main()
{
return F1() switch { 1 => <AS:0>0</AS:0>, _ => 1 };
}
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.SwitchExpressionUpdate, "switch", FeaturesResources.method));
}

[Fact]
[WorkItem(43099, "https://github.com/dotnet/roslyn/issues/43099")]
public void SwitchExpression_Delete3()
{
var src1 = @"
class C
{
public static int Main()
{
return F1() switch { 1 when F2() switch { 1 => <AS:0>true</AS:0>, _ => false } => 0, _ => 2 };
}
}";
var src2 = @"
class C
{
public static int Main()
{
return F1() switch { 1 <AS:0>when F3()</AS:0> => 0, _ => 1 };
}
}";
var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.SwitchExpressionUpdate, "switch", FeaturesResources.method));
}

#endregion

#region Try

[Fact]
Expand Down Expand Up @@ -10299,6 +10612,37 @@ public static void F()
Diagnostic(RudeEditKind.DeleteActiveStatement, "{"));
}

[Fact]
public void Block_Delete()
{
string src1 = @"
class C
{
public static void F()
{
G(1);
<AS:0>{</AS:0> G(2); }
G(3);
}
}
";
string src2 = @"
class C
{
public static void F()
{
G(1);
<AS:0>G(3);</AS:0>
}
}
";

var edits = GetTopEdits(src1, src2);
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active);
}

#endregion
}
}
Loading