Skip to content

Commit b56876c

Browse files
committed
More tests with verbatim strings. Close #28.
1 parent e86cc61 commit b56876c

4 files changed

Lines changed: 98 additions & 25 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP002MissingParameterTests/CodeFix.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public class CodeFix
1212
private static readonly CodeFixProvider Fix = new TemplateTextFix();
1313

1414
[TestCase("\"api/{↓value}\"", "\"api/{text}\"")]
15+
[TestCase("@\"api/{↓value}\"", "@\"api/{text}\"")]
1516
[TestCase("\"api/{↓value:alpha}\"", "\"api/{text:alpha}\"")]
16-
public void WhenWrongName(string before, string after)
17+
public void When(string before, string after)
1718
{
1819
var code = @"
1920
namespace ValidCode

AspNetCoreAnalyzers.Tests/ASP002MissingParameterTests/ValidCode.cs

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

11+
[TestCase("\"api/{text}\"")]
12+
[TestCase("@\"api/{text}\"")]
13+
[TestCase("\"api/{text:alpha}\"")]
14+
public void When(string after)
15+
{
16+
var code = @"
17+
namespace ValidCode
18+
{
19+
using System.Threading.Tasks;
20+
using Microsoft.AspNetCore.Mvc;
21+
using Microsoft.EntityFrameworkCore;
22+
23+
[ApiController]
24+
public class OrdersController : Controller
25+
{
26+
[HttpGet(""api/{text}"")]
27+
public IActionResult GetValue(string text)
28+
{
29+
return this.Ok(text);
30+
}
31+
}
32+
}".AssertReplace("\"api/{text}\"", after);
33+
34+
AnalyzerAssert.Valid(Analyzer, code);
35+
}
36+
1137
[Test]
1238
public void ImplicitFromRoute()
1339
{

AspNetCoreAnalyzers.Tests/ASP003ParameterTypeTests/CodeFix.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,29 @@ public class CodeFix
1111
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(ASP003ParameterType.Descriptor);
1212
private static readonly CodeFixProvider Fix = new ParameterTypeFix();
1313

14-
[TestCase("{id:int}", "int id")]
15-
[TestCase("api/orders/{id:int}", "int id")]
16-
[TestCase("api/orders/{id:int:min(1)}", "int id")]
17-
[TestCase("api/orders/{id:bool}", "bool id")]
18-
[TestCase("api/orders/{id:datetime}", "System.DateTime id")]
19-
[TestCase("api/orders/{id:decimal}", "decimal id")]
20-
[TestCase("api/orders/{id:double}", "double id")]
21-
[TestCase("api/orders/{id:float}", "float id")]
22-
[TestCase("api/orders/{id:guid}", "System.Guid id")]
23-
[TestCase("api/orders/{id:long}", "long id")]
24-
[TestCase("api/orders/{id:minlength(1)}", "string id")]
25-
[TestCase("api/orders/{id:maxlength(1)}", "string id")]
26-
[TestCase("api/orders/{id:length(1)}", "string id")]
27-
[TestCase("api/orders/{id:length(1,3)}", "string id")]
28-
[TestCase("api/orders/{id:min(1)}", "long id")]
29-
[TestCase("api/orders/{id:max(10)}", "long id")]
30-
[TestCase("api/orders/{id:range(0,10)}", "long id")]
31-
[TestCase("api/orders/{id:alpha}", "string id")]
32-
[TestCase("api/orders/{id:regex(a-(0|1))}", "string id")]
33-
[TestCase("api/orders/{id:regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{4}$)}", "string id")]
34-
public void ExplicitType(string template, string parameter)
14+
[TestCase("\"{id:int}\"", "int id")]
15+
[TestCase("\"api/orders/{id:int}\"", "int id")]
16+
[TestCase("\"api/orders/{id:int:min(1)}\"", "int id")]
17+
[TestCase("\"api/orders/{id:bool}\"", "bool id")]
18+
[TestCase("\"api/orders/{id:datetime}\"", "System.DateTime id")]
19+
[TestCase("\"api/orders/{id:decimal}\"", "decimal id")]
20+
[TestCase("\"api/orders/{id:double}\"", "double id")]
21+
[TestCase("\"api/orders/{id:float}\"", "float id")]
22+
[TestCase("\"api/orders/{id:guid}\"", "System.Guid id")]
23+
[TestCase("\"api/orders/{id:long}\"", "long id")]
24+
[TestCase("\"api/orders/{id:minlength(1)}\"", "string id")]
25+
[TestCase("\"api/orders/{id:maxlength(1)}\"", "string id")]
26+
[TestCase("\"api/orders/{id:length(1)}\"", "string id")]
27+
[TestCase("\"api/orders/{id:length(1,3)}\"", "string id")]
28+
[TestCase("\"api/orders/{id:min(1)}\"", "long id")]
29+
[TestCase("\"api/orders/{id:max(10)}\"", "long id")]
30+
[TestCase("\"api/orders/{id:range(0,10)}\"", "long id")]
31+
[TestCase("\"api/orders/{id:alpha}\"", "string id")]
32+
[TestCase("\"api/orders/{id:regex(a-(0|1))}\"", "string id")]
33+
[TestCase("\"api/orders/{id:regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{4}$)}\"", "string id")]
34+
[TestCase("@\"api/orders/{id:regex(^\\d{{3}}-\\d{{2}}-\\d{4}$)}\"", "string id")]
35+
[TestCase("@\"api/orders/{id:regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{4}$)}\"", "string id")]
36+
public void When(string template, string parameter)
3537
{
3638
var code = @"
3739
namespace ValidCode
@@ -47,7 +49,7 @@ public IActionResult GetId(↓byte id)
4749
return this.Ok(id);
4850
}
4951
}
50-
}".AssertReplace("api/orders/{id:int}", template);
52+
}".AssertReplace("\"api/orders/{id:int}\"", template);
5153

5254
var fixedCode = @"
5355
namespace ValidCode
@@ -63,7 +65,7 @@ public IActionResult GetId(byte id)
6365
return this.Ok(id);
6466
}
6567
}
66-
}".AssertReplace("api/orders/{id:int}", template)
68+
}".AssertReplace("\"api/orders/{id:int}\"", template)
6769
.AssertReplace("byte id", parameter);
6870
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
6971
}

AspNetCoreAnalyzers.Tests/ASP003ParameterTypeTests/ValidCode.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@ public class ValidCode
88
{
99
private static readonly DiagnosticAnalyzer Analyzer = new AttributeAnalyzer();
1010

11+
[TestCase("\"{id}\"", "int id")]
12+
[TestCase("\"{id}\"", "string id")]
13+
[TestCase("\"{id?}\"", "string id")]
14+
[TestCase("@\"{id}\"", "int id")]
15+
[TestCase("\"{id:int}\"", "int id")]
16+
[TestCase("\"api/orders/{id:int}\"", "int id")]
17+
[TestCase("\"api/orders/{id:int:min(1)}\"", "int id")]
18+
[TestCase("\"api/orders/{id:bool}\"", "bool id")]
19+
[TestCase("\"api/orders/{id:datetime}\"", "System.DateTime id")]
20+
[TestCase("\"api/orders/{id:decimal}\"", "decimal id")]
21+
[TestCase("\"api/orders/{id:double}\"", "double id")]
22+
[TestCase("\"api/orders/{id:float}\"", "float id")]
23+
[TestCase("\"api/orders/{id:guid}\"", "System.Guid id")]
24+
[TestCase("\"api/orders/{id:long}\"", "long id")]
25+
[TestCase("\"api/orders/{id:minlength(1)}\"", "string id")]
26+
[TestCase("\"api/orders/{id:maxlength(1)}\"", "string id")]
27+
[TestCase("\"api/orders/{id:length(1)}\"", "string id")]
28+
[TestCase("\"api/orders/{id:length(1,3)}\"", "string id")]
29+
[TestCase("\"api/orders/{id:min(1)}\"", "long id")]
30+
[TestCase("\"api/orders/{id:max(10)}\"", "long id")]
31+
[TestCase("\"api/orders/{id:range(0,10)}\"", "long id")]
32+
[TestCase("\"api/orders/{id:alpha}\"", "string id")]
33+
[TestCase("\"api/orders/{id:regex(a-(0|1))}\"", "string id")]
34+
public void When(string template, string parameter)
35+
{
36+
var code = @"
37+
namespace ValidCode
38+
{
39+
using Microsoft.AspNetCore.Mvc;
40+
41+
[ApiController]
42+
public class OrdersController : Controller
43+
{
44+
[HttpGet(""api/orders/{id:int}"")]
45+
public IActionResult GetId(byte id)
46+
{
47+
return this.Ok(id);
48+
}
49+
}
50+
}".AssertReplace("\"api/orders/{id:int}\"", template)
51+
.AssertReplace("byte id", parameter);
52+
AnalyzerAssert.Valid(Analyzer, code);
53+
}
54+
1155
[Test]
1256
public void ImplicitType()
1357
{
@@ -219,7 +263,7 @@ public async Task<IActionResult> GetOrder(long id)
219263
[TestCase("api/orders/{id:alpha}")]
220264
[TestCase("api/orders/{id:regex(a-(0|1))}")]
221265
[TestCase("api/orders/{id:required}")]
222-
public void ExplicitString(string template)
266+
public void ImplicitString(string template)
223267
{
224268
var order = @"
225269
namespace ValidCode

0 commit comments

Comments
 (0)