Skip to content

Commit

Permalink
Change AZC0002 to make method ending with required RequestContext pass (
Browse files Browse the repository at this point in the history
#6771)

* Change AZC0002 to make method ending with required RequestContext pass

* Update

* Update test

* Fix

* Update

* Update

* Update
  • Loading branch information
pshao25 authored Aug 23, 2023
1 parent c394a92 commit c2d1eac
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,88 @@ namespace Azure.ClientSdk.Analyzers.Tests
public class AZC0002Tests
{
[Fact]
public async Task AZC0002ProducedForMethodsWithoutCancellationToken()
public async Task AZC0002ProducedForMethodsWithoutCancellationTokenOrRequestContext()
{
const string code = @"
using Azure;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}()
public virtual Task<Response> {|AZC0002:GetAsync|}()
{
return null;
}
public virtual void {|AZC0002:Get|}()
public virtual Response {|AZC0002:Get|}()
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

[Fact]
public async Task AZC0002ProducedForMethodsWithNonOptionalCancellationToken()
public async Task AZC0002ProducedForMethodsWithWrongNameCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
public virtual Task<Response> {|AZC0002:GetAsync|}(CancellationToken cancellation = default)
{
return null;
}
public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken)
public virtual Response {|AZC0002:Get|}(CancellationToken cancellation = default)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

[Fact]
public async Task AZC0002ProducedForMethodsWithWrongNameParameter()
public async Task AZC0002ProducedForMethodsWithWrongNameRequestContext()
{
const string code = @"
using Azure;
using Azure.Core;
using System.Threading;
using System.Threading.Tasks;
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellation = default)
public virtual Task<Response> {|AZC0002:GetAsync|}(RequestContext cancellation = default)
{
return null;
}
public virtual void {|AZC0002:Get|}(CancellationToken cancellation = default)
public virtual Response {|AZC0002:Get|}(RequestContext cancellation = default)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

}

[Fact]
public async Task AZC0002ProducedForMethodsWhereRequestContextIsNotLast()
public async Task AZC0002ProducedForMethodsWithNonOptionalCancellationToken()
{
const string code = @"
using Azure;
Expand All @@ -98,58 +102,50 @@ namespace RandomNamespace
{
public class SomeClient
{
public virtual Task {|AZC0002:GetAsync|}(RequestContext context = default, string text = default)
public virtual Task<Response> {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
{
return null;
}
public virtual void {|AZC0002:Get|}(RequestContext context = default, string text = default)
public virtual Response {|AZC0002:Get|}(CancellationToken cancellationToken)
{
return null;
}
}
}";
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)
public virtual Task<Response> {|AZC0002:GetAsync|}(RequestContext context = default, string text = default)
{
return null;
}
public virtual void Get(string s)
{
}
public virtual Task GetAsync(string s, CancellationToken cancellationToken)
public virtual Response {|AZC0002:Get|}(RequestContext context = default, string text = default)
{
return null;
}
public virtual void Get(string s, CancellationToken cancellationToken)
{
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

}

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

[Fact]
public async Task AZC0002NotProducedForMethodsWithCancellationToken()
{
const string code = @"
using Azure;
using System.Threading;
using System.Threading.Tasks;
public virtual Task GetAsync(string s, RequestContext context = default)
namespace RandomNamespace
{
public class SomeClient
{
public virtual Task<Response> GetAsync(CancellationToken cancellationToken = default)
{
return null;
}
public virtual void Get(string s, RequestContext context = default)
public virtual Response Get(CancellationToken cancellationToken = default)
{
return null;
}
}
}";
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<Response> Get1Async(string s, CancellationToken cancellationToken = default)
{
return null;
}
public virtual void {|AZC0002:Get|}(string s)
public virtual Response Get1(string s, CancellationToken cancellationToken = default)
{
return null;
}
public virtual Task {|AZC0002:GetAsync|}(CancellationToken cancellationToken)
public virtual Task<Response> Get1Async(string s, RequestContext context)
{
return null;
}
public virtual void {|AZC0002:Get|}(CancellationToken cancellationToken)
public virtual Response Get1(string s, RequestContext context)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}

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

}

[Fact]
public async Task AZC0002NotProducedForMethodsWithRequestContext()
public async Task AZC0002NotProducedIfThereIsAnOverloadWithCancellationToken()
{
const string code = @"
using Azure;
Expand All @@ -257,18 +274,28 @@ namespace RandomNamespace
{
public class SomeClient
{
public virtual Task GetAsync(RequestContext context = default)
public virtual Task<Response> GetAsync(string s)
{
return null;
}
public virtual void Get(RequestContext context = default)
public virtual Response Get(string s)
{
return null;
}
public virtual Task<Response> GetAsync(string s, CancellationToken cancellationToken)
{
return null;
}
public virtual Response Get(string s, CancellationToken cancellationToken)
{
return null;
}
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
.RunAsync();
}
}
Expand Down
Loading

0 comments on commit c2d1eac

Please sign in to comment.