Skip to content

Commit 1d3ce51

Browse files
committed
More tests.
1 parent aae475e commit 1d3ce51

2 files changed

Lines changed: 111 additions & 2 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP002MissingParameterTests/CodeFix.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,116 @@ public class OrdersController : Controller
3030
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
3131
}
3232

33+
[Test]
34+
public void WhenLastIsMissing()
35+
{
36+
var order = @"
37+
namespace ValidCode
38+
{
39+
public class Order
40+
{
41+
public int Id { get; set; }
42+
}
43+
}";
44+
45+
var db = @"
46+
namespace ValidCode
47+
{
48+
using Microsoft.EntityFrameworkCore;
49+
50+
public class Db : DbContext
51+
{
52+
public DbSet<Order> Orders { get; set; }
53+
}
54+
}";
55+
var code = @"
56+
namespace ValidCode
57+
{
58+
using System.Threading.Tasks;
59+
using Microsoft.AspNetCore.Mvc;
60+
using Microsoft.EntityFrameworkCore;
61+
62+
[ApiController]
63+
public class OrdersController : Controller
64+
{
65+
private readonly Db db;
66+
67+
public OrdersController(Db db)
68+
{
69+
this.db = db;
70+
}
71+
72+
[HttpGet(""api/orders/{orderId}/items/{itemId}"")]
73+
public async Task<IActionResult> GetOrder↓(int orderId)
74+
{
75+
var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == orderId);
76+
if (match == null)
77+
{
78+
return this.NotFound();
79+
}
80+
81+
return this.Ok(match);
82+
}
83+
}
84+
}";
85+
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, order, db, code);
86+
}
87+
88+
[Test]
89+
public void WhenFirstIsMissing()
90+
{
91+
var order = @"
92+
namespace ValidCode
93+
{
94+
public class Order
95+
{
96+
public int Id { get; set; }
97+
}
98+
}";
99+
100+
var db = @"
101+
namespace ValidCode
102+
{
103+
using Microsoft.EntityFrameworkCore;
104+
105+
public class Db : DbContext
106+
{
107+
public DbSet<Order> Orders { get; set; }
108+
}
109+
}";
110+
var code = @"
111+
namespace ValidCode
112+
{
113+
using System.Threading.Tasks;
114+
using Microsoft.AspNetCore.Mvc;
115+
using Microsoft.EntityFrameworkCore;
116+
117+
[ApiController]
118+
public class OrdersController : Controller
119+
{
120+
private readonly Db db;
121+
122+
public OrdersController(Db db)
123+
{
124+
this.db = db;
125+
}
126+
127+
[HttpGet(""api/orders/{orderId}/items/{itemId}"")]
128+
public async Task<IActionResult> GetOrder↓(int itemId)
129+
{
130+
var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == itemId);
131+
if (match == null)
132+
{
133+
return this.NotFound();
134+
}
135+
136+
return this.Ok(match);
137+
}
138+
}
139+
}";
140+
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, order, db, code);
141+
}
142+
33143
[TestCase("[FromHeader]")]
34144
[TestCase("[FromBody]")]
35145
public void WhenWrongAttribute(string attribute)

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ context.ContainingSymbol is IMethodSymbol method &&
5252
methodDeclaration.ParameterList.GetLocation()));
5353
}
5454

55-
if (pairs.TrySingle(x => x.Template != null, out _) &&
56-
pairs.TrySingle(x => x.Method == null, out _))
55+
if (pairs.TryFirst(x => x.Method == null, out _))
5756
{
5857
context.ReportDiagnostic(
5958
Diagnostic.Create(

0 commit comments

Comments
 (0)