Skip to content

Commit

Permalink
Merge pull request dotnet#41753 from sharwell/add-obsolete
Browse files Browse the repository at this point in the history
Convert AddObsoleteAttributeTests to the new test framework
  • Loading branch information
sharwell authored Feb 19, 2020
2 parents f03330f + 3630a6e commit c7bd704
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<!-- Versions used by several individual references below -->
<RoslynDiagnosticsNugetPackageVersion>3.0.0-beta2.20059.3+77df2220</RoslynDiagnosticsNugetPackageVersion>
<CodeStyleLayerCodeAnalysisVersion>3.3.1</CodeStyleLayerCodeAnalysisVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.1-beta1.20114.4</MicrosoftCodeAnalysisTestingVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.1-beta1.20118.1</MicrosoftCodeAnalysisTestingVersion>
<CodeStyleAnalyzerVersion>3.5.0-beta3-20078-04</CodeStyleAnalyzerVersion>
<VisualStudioEditorPackagesVersion>16.4.248</VisualStudioEditorPackagesVersion>
<ILToolsPackageVersion>5.0.0-alpha1.19409.1</ILToolsPackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
// 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 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|} {
}
",
@"
Expand All @@ -41,12 +37,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|} {
}
",
@"
Expand All @@ -62,12 +58,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|} {
}
",
@"
Expand All @@ -83,26 +79,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;
}
",
@"
Expand All @@ -119,14 +115,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;
}
}
",
Expand All @@ -146,15 +142,15 @@ void Goo() {
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)]
public async Task TestObsoleteOverride()
{
await TestInRegularAndScript1Async(
await VerifyCS.VerifyCodeFixAsync(
@"
class Base {
[System.Obsolete]
protected virtual void ObMethod() { }
}
class Derived : Base {
protected override void [||]ObMethod() { }
protected override void {|CS0672:ObMethod|}() { }
}
",
@"
Expand All @@ -173,15 +169,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;
}
}
",
Expand All @@ -202,15 +198,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;
}
}
",
Expand All @@ -231,18 +227,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;
}
}
",
Expand All @@ -267,17 +263,20 @@ void Bar() {
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)]
public async Task TestObsoleteCollectionAddMethod()
{
await TestInRegularAndScript1Async(
await VerifyCS.VerifyCodeFixAsync(
@"
class Collection : System.Collections.Generic.IEnumerable<int> {
[System.Obsolete]
public void Add(int i) { }
public System.Collections.Generic.IEnumerator<int> GetEnumerator() => throw null;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null;
}
class Derived {
void Goo() {
var c = new Collection {
[||]1, 2, 3
{|CS1064:1|}, {|CS1064:2|}, {|CS1064:3|}
};
}
}
Expand All @@ -286,6 +285,9 @@ void Goo() {
class Collection : System.Collections.Generic.IEnumerable<int> {
[System.Obsolete]
public void Add(int i) { }
public System.Collections.Generic.IEnumerator<int> GetEnumerator() => throw null;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null;
}
class Derived {
Expand All @@ -302,17 +304,20 @@ void Goo() {
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)]
public async Task TestObsoleteCollectionAddMethodWithMessage()
{
await TestInRegularAndScript1Async(
await VerifyCS.VerifyCodeFixAsync(
@"
class Collection : System.Collections.Generic.IEnumerable<int> {
[System.Obsolete(""message"")]
public void Add(int i) { }
public System.Collections.Generic.IEnumerator<int> GetEnumerator() => throw null;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null;
}
class Derived {
void Goo() {
var c = new Collection {
[||]1, 2, 3
{|CS1062:1|}, {|CS1062:2|}, {|CS1062:3|}
};
}
}
Expand All @@ -321,6 +326,9 @@ void Goo() {
class Collection : System.Collections.Generic.IEnumerable<int> {
[System.Obsolete(""message"")]
public void Add(int i) { }
public System.Collections.Generic.IEnumerator<int> GetEnumerator() => throw null;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null;
}
class Derived {
Expand All @@ -337,17 +345,20 @@ void Goo() {
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)]
public async Task TestObsoleteCollectionAddMethodWithMessageAndErrorFalse()
{
await TestInRegularAndScript1Async(
await VerifyCS.VerifyCodeFixAsync(
@"
class Collection : System.Collections.Generic.IEnumerable<int> {
[System.Obsolete(""message"", error: false)]
public void Add(int i) { }
public System.Collections.Generic.IEnumerator<int> GetEnumerator() => throw null;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null;
}
class Derived {
void Goo() {
var c = new Collection {
[||]1, 2, 3
{|CS1062:1|}, {|CS1062:2|}, {|CS1062:3|}
};
}
}
Expand All @@ -356,6 +367,9 @@ void Goo() {
class Collection : System.Collections.Generic.IEnumerable<int> {
[System.Obsolete(""message"", error: false)]
public void Add(int i) { }
public System.Collections.Generic.IEnumerator<int> GetEnumerator() => throw null;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null;
}
class Derived {
Expand All @@ -372,21 +386,25 @@ void Goo() {
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddObsoleteAttribute)]
public async Task TestObsoleteCollectionAddMethodWithMessageAndErrorTrue()
{
await TestMissingInRegularAndScriptAsync(
@"
var code = @"
class Collection : System.Collections.Generic.IEnumerable<int> {
[System.Obsolete(""message"", error: true)]
public void Add(int i) { }
public System.Collections.Generic.IEnumerator<int> GetEnumerator() => throw null;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => throw null;
}
class Derived {
void Goo() {
var c = new Collection {
[||]1, 2, 3
{|CS1063:1|}, {|CS1063:2|}, {|CS1063:3|}
};
}
}
");
";

await VerifyCS.VerifyCodeFixAsync(code, code);
}
}
}

0 comments on commit c7bd704

Please sign in to comment.