Skip to content

Commit

Permalink
Update AZC00002 to match method pair with required requestcontext and…
Browse files Browse the repository at this point in the history
… optional convenience method (#6156)

* Update AZC00002 to match method pair with required requestcontext and
optional convenience method

* Update

* Resolve comments

* Resolve comments

* Update

* Update

* Fix

* Fix
  • Loading branch information
pshao25 authored Jun 13, 2023
1 parent 2cfe329 commit 59b104e
Show file tree
Hide file tree
Showing 8 changed files with 944 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Azure.ClientSdk.Analyzers.Tests
public class AZC0002Tests
{
[Fact]
public async Task AZC0002ProducedForMethodsWithoutCancellationToken()
public async Task AZC0002ProducedForMethodsWithoutCancellationTokenOrRequestContext()
{
const string code = @"
using System.Threading.Tasks;
Expand All @@ -35,7 +35,7 @@ await Verifier.CreateAnalyzer(code)
}

[Fact]
public async Task AZC0002ProducedForMethodsWithNonOptionalCancellationToken()
public async Task AZC0002ProducedForMethodsWithWrongNameCancellationToken()
{
const string code = @"
using System.Threading;
Expand All @@ -45,12 +45,12 @@ namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellation = default)
{
return null;
}
public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken)
public virtual void {|AZC0002:Get|}(CancellationToken cancellation = default)
{
}
}
Expand All @@ -61,22 +61,23 @@ await Verifier.CreateAnalyzer(code)
}

[Fact]
public async Task AZC0002ProducedForMethodsWithWrongNameParameter()
public async Task AZC0002ProducedForMethodsWithWrongNameRequestContext()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellation = default)
public virtual Task {|AZC0002:GetAsync|}(RequestContext cancellation = default)
{
return null;
}
public virtual void {|AZC0002:Get|}(CancellationToken cancellation = default)
public virtual void {|AZC0002:Get|}(RequestContext cancellation = default)
{
}
}
Expand All @@ -85,25 +86,24 @@ await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}


[Fact]
public async Task AZC0002ProducedForMethodsWhereRequestContextIsNotLast()
public async Task AZC0002ProducedForMethodsWithNonOptionalCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(RequestContext context = default, string text = default)
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
{
return null;
}
public virtual void {|AZC0002:Get|}(RequestContext context = default, string text = default)
public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken)
{
}
}
Expand All @@ -112,44 +112,36 @@ await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}


[Fact]
public async Task AZC0002DoesntFireIfThereIsAnOverloadWithCancellationToken()
public async Task AZC0002ProducedForMethodsWhereRequestContextIsNotLast()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(string s)
{
return null;
}
public virtual void Get(string s)
{
}
public virtual Task GetAsync(string s, CancellationToken cancellationToken)
public virtual Task {|AZC0002:GetAsync|}(RequestContext context = default, string text = default)
{
return null;
}
public virtual void Get(string s, CancellationToken cancellationToken)
public virtual void {|AZC0002:Get|}(RequestContext context = default, string text = default)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

}

[Fact]
public async Task AZC0002DoesntFireIfThereIsAnOverloadWithRequestContext()
public async Task AZC0002ProducedForMethodsWhereCancellationTokenIsNotLast()
{
const string code = @"
using Azure;
Expand All @@ -160,91 +152,103 @@ namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(string s)
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken = default, string text = default)
{
return null;
}
public virtual void Get(string s)
public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken = default, string text = default)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

public virtual Task GetAsync(string s, RequestContext context = default)
[Fact]
public async Task AZC0002NotProducedForMethodsWithCancellationToken()
{
const string code = @"
using System.Threading;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(CancellationToken cancellationToken = default)
{
return null;
}
public virtual void Get(string s, RequestContext context = default)
public virtual void Get(CancellationToken cancellationToken = default)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

}

[Fact]
public async Task AZC0002ProducedWhenCancellationTokenOverloadsDontMatch()
public async Task AZC0002NotProducedForMethodsWithRequestContextAndCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(string s)
public virtual Task Get1Async(string s, CancellationToken cancellationToken = default)
{
return null;
}
public virtual void {|AZC0002:Get|}(string s)
public virtual void Get1(string s, CancellationToken cancellationToken = default)
{
}
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
public virtual Task Get1Async(string s, RequestContext context)
{
return null;
}
public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken)
public virtual void Get1(string s, RequestContext context)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
public virtual Task Get2Async(CancellationToken cancellationToken = default)
{
return null;
}
[Fact]
public async Task AZC0002NotProducedForMethodsWithCancellationToken()
public virtual void Get2(CancellationToken cancellationToken = default)
{
const string code = @"
using System.Threading;
using System.Threading.Tasks;
}
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(CancellationToken cancellationToken = default)
public virtual Task Get2Async(RequestContext context)
{
return null;
}
public virtual void Get(CancellationToken cancellationToken = default)
public virtual void Get2(RequestContext context)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.WithDisabledDiagnostics("AZC0018")
.WithDisabledDiagnostics("AD0001")
.RunAsync();
}

}

[Fact]
public async Task AZC0002NotProducedForMethodsWithRequestContext()
{
Expand All @@ -265,10 +269,20 @@ public virtual Task GetAsync(RequestContext context = default)
public virtual void Get(RequestContext context = default)
{
}
public virtual Task Get2Async(RequestContext context)
{
return null;
}
public virtual void Get2(RequestContext context)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.WithDisabledDiagnostics("AZC0018")
.RunAsync();
}
}
Expand Down
Loading

0 comments on commit 59b104e

Please sign in to comment.