Skip to content

Commit 00a81f0

Browse files
committed
More tests.
1 parent 80ea865 commit 00a81f0

5 files changed

Lines changed: 70 additions & 38 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP004ParameterSyntaxTests/CodeFix.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CodeFix
2121
[TestCase("api/orders/{id:length(1,2}", "api/orders/{id:length(1,2)}")]
2222
[TestCase("api/orders/{id:range(1,2}", "api/orders/{id:range(1,2)}")]
2323
[TestCase("api/orders/{id:regex((a|b)-c}", "api/orders/{id:regex((a|b)-c)}")]
24-
public void WhenFixable(string before, string after)
24+
public void When(string before, string after)
2525
{
2626
var code = @"
2727
namespace ValidCode
@@ -56,29 +56,5 @@ public IActionResult GetId(long id)
5656
}".AssertReplace("api/orders/{id:long}", after);
5757
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
5858
}
59-
60-
[TestCase("api/orders/{id:↓wrong}")]
61-
[TestCase("api/orders/{id:min1)}")]
62-
[TestCase("api/orders/{id:max1)}")]
63-
public void WhenNot(string before)
64-
{
65-
var code = @"
66-
namespace ValidCode
67-
{
68-
using Microsoft.AspNetCore.Mvc;
69-
70-
[ApiController]
71-
public class OrdersController : Controller
72-
{
73-
[HttpGet(""api/orders/↓{id:wrong}"")]
74-
public IActionResult GetId(long id)
75-
{
76-
return this.Ok(id);
77-
}
78-
}
79-
}".AssertReplace("api/orders/↓{id:wrong}", before);
80-
81-
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
82-
}
8359
}
8460
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace AspNetCoreAnalyzers.Tests.ASP004ParameterSyntaxTests
2+
{
3+
using Gu.Roslyn.Asserts;
4+
using Microsoft.CodeAnalysis.Diagnostics;
5+
using NUnit.Framework;
6+
7+
public class Diagnostics
8+
{
9+
private static readonly DiagnosticAnalyzer Analyzer = new AttributeAnalyzer();
10+
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(ASP004ParameterSyntax.Descriptor);
11+
12+
[TestCase("api/orders/{id:↓wrong}")]
13+
[TestCase("api/orders/{id:min1)}")]
14+
[TestCase("api/orders/{id:max1)}")]
15+
[TestCase("api/orders/{id:↓:int)}")]
16+
[TestCase("api/orders/{id:int:↓)}")]
17+
[TestCase("api/orders/{id:int:↓:)}")]
18+
public void When(string before)
19+
{
20+
var code = @"
21+
namespace ValidCode
22+
{
23+
using Microsoft.AspNetCore.Mvc;
24+
25+
[ApiController]
26+
public class OrdersController : Controller
27+
{
28+
[HttpGet(""api/orders/↓{id:wrong}"")]
29+
public IActionResult GetId(long id)
30+
{
31+
return this.Ok(id);
32+
}
33+
}
34+
}".AssertReplace("api/orders/↓{id:wrong}", before);
35+
36+
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
37+
}
38+
}
39+
}

AspNetCoreAnalyzers.Tests/ASP004ParameterSyntaxTests/ValidCode.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@ public class ValidCode
88
{
99
private static readonly DiagnosticAnalyzer Analyzer = new AttributeAnalyzer();
1010

11-
[TestCase("{value:bool}", "bool")]
12-
[TestCase("{value:datetime}", "System.DateTime")]
13-
[TestCase("{value:decimal}", "decimal")]
14-
[TestCase("{value:double}", "double")]
15-
[TestCase("{value:float}", "float")]
16-
[TestCase("{value:int}", "int")]
11+
[TestCase("{value}", "string")]
12+
[TestCase("{value?}", "string")]
13+
[TestCase("{value:bool}", "bool")]
14+
[TestCase("{value:datetime}", "System.DateTime")]
15+
[TestCase("{value:decimal}", "decimal")]
16+
[TestCase("{value:double}", "double")]
17+
[TestCase("{value:float}", "float")]
18+
[TestCase("{value:int}", "int")]
1719
[TestCase("api/orders/{value:int:min(1)}", "int")]
1820
[TestCase("api/orders/{value:int:max(1)}", "int")]
1921
[TestCase("api/orders/{value:int:range(1,10)}", "int")]
2022
[TestCase("api/orders/{value:int:required}", "int")]
21-
[TestCase("{value:long}", "long")]
23+
[TestCase("{value:long}", "long")]
2224
[TestCase("api/orders/{value:min(1)}", "long")]
2325
[TestCase("api/orders/{value:max(1)}", "long")]
2426
[TestCase("api/orders/{value:range(1,10)}", "long")]
2527
[TestCase("api/orders/{value:required}", "long")]
26-
[TestCase("{value:guid}", "System.Guid")]
28+
[TestCase("{value:guid}", "System.Guid")]
2729
[TestCase("api/orders/{value:minlength(1)}", "string")]
2830
[TestCase("api/orders/{value:maxlength(1)}", "string")]
2931
[TestCase("api/orders/{value:length(1)}", "string")]
@@ -45,13 +47,13 @@ namespace ValidCode
4547
public class OrdersController : Controller
4648
{
4749
[HttpGet(""api/{value}"")]
48-
public async Task<int> GetOrder(int value)
50+
public IActionResult GetValue(string value)
4951
{
50-
return value;
52+
return this.Ok(value);
5153
}
5254
}
53-
}".AssertReplace("int", type)
54-
.AssertReplace("{value}", parameter);
55+
}".AssertReplace("{value}", parameter)
56+
.AssertReplace("string", type);
5557
AnalyzerAssert.Valid(Analyzer, code);
5658
}
5759
}

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ private static bool HasWrongSyntax(PathSegment segment, out Location location, o
241241
}
242242
}
243243

244-
if (!text.Equals("int", StringComparison.OrdinalIgnoreCase) &&
244+
if (!text.Equals("?", StringComparison.OrdinalIgnoreCase) &&
245+
!text.Equals("int", StringComparison.OrdinalIgnoreCase) &&
245246
!text.Equals("bool", StringComparison.OrdinalIgnoreCase) &&
246247
!text.Equals("datetime", StringComparison.OrdinalIgnoreCase) &&
247248
!text.Equals("decimal", StringComparison.OrdinalIgnoreCase) &&

ValidCode/IdentityController.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace ValidCode
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
[ApiController]
6+
public class IdentityController : Controller
7+
{
8+
[HttpGet("string/{text:minlength(1):maxlength(2)}")]
9+
public IActionResult GetString(string text)
10+
{
11+
return this.Ok(text);
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)