From fe6181181944932165f23b6debd1205aa931be87 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 17 Feb 2020 13:37:44 -0800 Subject: [PATCH 1/2] Convert AddObsoleteAttributeTests to the new test framework --- eng/Versions.props | 2 +- .../AddObsoleteAttributeTests.cs | 113 +++++++++++------- 2 files changed, 71 insertions(+), 44 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index 106d12fb72c13..6db4d3d6970bb 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -28,7 +28,7 @@ 3.0.0-beta2.20059.3+77df2220 3.3.1 - 1.0.1-beta1.20114.4 + 1.0.1-beta1.20118.1 3.5.0-beta3-20078-04 16.4.248 5.0.0-alpha1.19409.1 diff --git a/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs b/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs index cc872b0f6f4a8..c32962c6b9611 100644 --- a/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs +++ b/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs @@ -3,29 +3,26 @@ // See the LICENSE file in the project root for more information. using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp.AddObsoleteAttribute; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics; using Microsoft.CodeAnalysis.Test.Utilities; +using Microsoft.CodeAnalysis.Testing; using Xunit; +using VerifyCS = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.CSharpCodeFixVerifier< + Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer, + Microsoft.CodeAnalysis.CSharp.AddObsoleteAttribute.CSharpAddObsoleteAttributeCodeFixProvider>; namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.AddObsoleteAttribute { - public class AddObsoleteAttributeTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest + public class AddObsoleteAttributeTests { - internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace) - => (null, new CSharpAddObsoleteAttributeCodeFixProvider()); - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassNoMessage() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete] class Base {} -class Derived : [||]Base { +class Derived : {|CS0612:Base|} { } ", @" @@ -41,12 +38,12 @@ class Derived : Base { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassWithMessage() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete(""message"")] class Base {} -class Derived : [||]Base { +class Derived : {|CS0618:Base|} { } ", @" @@ -62,12 +59,12 @@ class Derived : Base { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassWithMessageAndErrorFalse() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete(""message"", error: false)] class Base {} -class Derived : [||]Base { +class Derived : {|CS0618:Base|} { } ", @" @@ -83,26 +80,26 @@ class Derived : Base { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassWithMessageAndErrorTrue() { - await TestMissingInRegularAndScriptAsync( -@" + var code = @" [System.Obsolete(""message"", error: true)] class Base {} -class Derived : [||]Base { +class Derived : {|CS0619:Base|} { } -"); +"; + await VerifyCS.VerifyCodeFixAsync(code, code); } [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassUsedInField() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete] class Base { public static int i; } class Derived { - int i = [||]Base.i; + int i = {|CS0612:Base|}.i; } ", @" @@ -119,14 +116,14 @@ class Derived { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassUsedInMethod() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete] class Base { public static int i; } class Derived { void Goo() { - int i = [||]Base.i; + int i = {|CS0612:Base|}.i; } } ", @@ -146,7 +143,7 @@ void Goo() { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteOverride() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" class Base { [System.Obsolete] @@ -154,7 +151,7 @@ protected virtual void ObMethod() { } } class Derived : Base { - protected override void [||]ObMethod() { } + protected override void {|CS0672:ObMethod|}() { } } ", @" @@ -173,15 +170,15 @@ protected override void ObMethod() { } [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassFixAll1() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete] class Base { public static int i; } class Derived { void Goo() { - int i = {|FixAllInDocument:|}Base.i; - int j = Base.i; + int i = {|CS0612:Base|}.i; + int j = {|CS0612:Base|}.i; } } ", @@ -202,15 +199,15 @@ void Goo() { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassFixAll2() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete] class Base { public static int i; } class Derived { void Goo() { - int i = Base.i; - int j = {|FixAllInDocument:|}Base.i; + int i = {|CS0612:Base|}.i; + int j = {|CS0612:Base|}.i; } } ", @@ -231,18 +228,18 @@ void Goo() { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteClassFixAll3() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" [System.Obsolete] class Base { public static int i; } class Derived { void Goo() { - int i = {|FixAllInDocument:|}Base.i; + int i = {|CS0612:Base|}.i; } void Bar() { - int j = Base.i; + int j = {|CS0612:Base|}.i; } } ", @@ -267,7 +264,7 @@ void Bar() { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteCollectionAddMethod() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete] @@ -277,11 +274,18 @@ public void Add(int i) { } class Derived { void Goo() { var c = new Collection { - [||]1, 2, 3 + {|CS1064:1|}, {|CS1064:2|}, {|CS1064:3|} }; } } ", + new[] + { + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), + }, @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete] @@ -302,7 +306,7 @@ void Goo() { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteCollectionAddMethodWithMessage() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"")] @@ -312,11 +316,18 @@ public void Add(int i) { } class Derived { void Goo() { var c = new Collection { - [||]1, 2, 3 + {|CS1062:1|}, {|CS1062:2|}, {|CS1062:3|} }; } } ", + new[] + { + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), + }, @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"")] @@ -337,7 +348,7 @@ void Goo() { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteCollectionAddMethodWithMessageAndErrorFalse() { - await TestInRegularAndScript1Async( + await VerifyCS.VerifyCodeFixAsync( @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"", error: false)] @@ -347,11 +358,18 @@ public void Add(int i) { } class Derived { void Goo() { var c = new Collection { - [||]1, 2, 3 + {|CS1062:1|}, {|CS1062:2|}, {|CS1062:3|} }; } } ", + new[] + { + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), + }, @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"", error: false)] @@ -372,8 +390,7 @@ void Goo() { [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)] public async Task TestObsoleteCollectionAddMethodWithMessageAndErrorTrue() { - await TestMissingInRegularAndScriptAsync( -@" + var code = @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"", error: true)] public void Add(int i) { } @@ -382,11 +399,21 @@ public void Add(int i) { } class Derived { void Goo() { var c = new Collection { - [||]1, 2, 3 + {|CS1063:1|}, {|CS1063:2|}, {|CS1063:3|} }; } } -"); +"; + await VerifyCS.VerifyCodeFixAsync( + source: code, + new[] + { + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), + // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' + DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), + }, + fixedSource: code); } } } From 3630a6e35ba58773b3a779ee2a05294fe61aef3b Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 18 Feb 2020 11:18:09 -0800 Subject: [PATCH 2/2] Fix compilation errors in tests --- .../AddObsoleteAttributeTests.cs | 55 ++++++++----------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs b/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs index c32962c6b9611..b6a13e8e09a14 100644 --- a/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs +++ b/src/EditorFeatures/CSharpTest/AddObsoleteAttribute/AddObsoleteAttributeTests.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; -using Microsoft.CodeAnalysis.Testing; using Xunit; using VerifyCS = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.CSharpCodeFixVerifier< Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer, @@ -269,6 +268,9 @@ await VerifyCS.VerifyCodeFixAsync( class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete] public void Add(int i) { } + + public System.Collections.Generic.IEnumerator GetEnumerator() => throw null; + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null; } class Derived { @@ -279,17 +281,13 @@ void Goo() { } } ", - new[] - { - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), - }, @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete] public void Add(int i) { } + + public System.Collections.Generic.IEnumerator GetEnumerator() => throw null; + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null; } class Derived { @@ -311,6 +309,9 @@ await VerifyCS.VerifyCodeFixAsync( class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"")] public void Add(int i) { } + + public System.Collections.Generic.IEnumerator GetEnumerator() => throw null; + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null; } class Derived { @@ -321,17 +322,13 @@ void Goo() { } } ", - new[] - { - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), - }, @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"")] public void Add(int i) { } + + public System.Collections.Generic.IEnumerator GetEnumerator() => throw null; + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null; } class Derived { @@ -353,6 +350,9 @@ await VerifyCS.VerifyCodeFixAsync( class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"", error: false)] public void Add(int i) { } + + public System.Collections.Generic.IEnumerator GetEnumerator() => throw null; + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null; } class Derived { @@ -363,17 +363,13 @@ void Goo() { } } ", - new[] - { - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), - }, @" class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"", error: false)] public void Add(int i) { } + + public System.Collections.Generic.IEnumerator GetEnumerator() => throw null; + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null; } class Derived { @@ -394,6 +390,9 @@ public async Task TestObsoleteCollectionAddMethodWithMessageAndErrorTrue() class Collection : System.Collections.Generic.IEnumerable { [System.Obsolete(""message"", error: true)] public void Add(int i) { } + + public System.Collections.Generic.IEnumerator GetEnumerator() => throw null; + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null; } class Derived { @@ -404,16 +403,8 @@ void Goo() { } } "; - await VerifyCS.VerifyCodeFixAsync( - source: code, - new[] - { - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.Generic.IEnumerable.GetEnumerator()"), - // Test0.cs(2,20): error CS0535: 'Collection' does not implement interface member 'IEnumerable.GetEnumerator()' - DiagnosticResult.CompilerError("CS0535").WithSpan(2, 20, 2, 63).WithArguments("Collection", "System.Collections.IEnumerable.GetEnumerator()"), - }, - fixedSource: code); + + await VerifyCS.VerifyCodeFixAsync(code, code); } } }