@@ -8,9 +8,11 @@ namespace AspNetCoreAnalyzers.Tests.Helpers
88 public class UrlTemplateTests
99 {
1010 [ TestCase ( "{id}/info" , new [ ] { "{id}" , "info" } ) ]
11+ [ TestCase ( "{id?}/info" , new [ ] { "{id?}" , "info" } ) ]
12+ [ TestCase ( "{id:int}/info" , new [ ] { "{id:int}" , "info" } ) ]
1113 [ TestCase ( "api/orders/{id}" , new [ ] { "api" , "orders" , "{id}" } ) ]
1214 [ TestCase ( "api/orders/{id}/info" , new [ ] { "api" , "orders" , "{id}" , "info" } ) ]
13- public void TryParse ( string text , string [ ] expected )
15+ public void TryParseWhenInt ( string text , string [ ] expected )
1416 {
1517 var syntaxTree = CSharpSyntaxTree . ParseText ( @"
1618namespace ValidCode
@@ -35,5 +37,32 @@ public async Task<IActionResult> GetOrder([FromRoute]int id)
3537 var parameter = template . Path . Single ( x => x . Parameter . HasValue ) . Parameter . Value ;
3638 Assert . AreEqual ( "id" , parameter . Name . Text ) ;
3739 }
40+
41+ [ TestCase ( "orders/{id:alpha}" , new [ ] { "orders" , "{id:alpha}" } ) ]
42+ public void TryParseWhenString ( string text , string [ ] expected )
43+ {
44+ var syntaxTree = CSharpSyntaxTree . ParseText ( @"
45+ namespace ValidCode
46+ {
47+ using System.Threading.Tasks;
48+ using Microsoft.AspNetCore.Mvc;
49+
50+ [ApiController]
51+ public class OrdersController : Controller
52+ {
53+ [HttpGet(""orders/{id:alpha}"")]
54+ public async Task<IActionResult> GetOrder(string id)
55+ {
56+ }
57+ }
58+ }" . AssertReplace ( "api/orders/{id}" , text ) ) ;
59+ var literal = syntaxTree . FindLiteralExpression ( text ) ;
60+ Assert . AreEqual ( true , UrlTemplate . TryParse ( literal , out var template ) ) ;
61+ CollectionAssert . AreEqual ( expected , template . Path . Select ( x => x . Text . Text ) ) ;
62+
63+ // ReSharper disable once PossibleInvalidOperationException
64+ var parameter = template . Path . Single ( x => x . Parameter . HasValue ) . Parameter . Value ;
65+ Assert . AreEqual ( "id" , parameter . Name . Text ) ;
66+ }
3867 }
3968}
0 commit comments