|
| 1 | +namespace AspNetCoreAnalyzers.Tests.ASP004RouteParameterTypeTests |
| 2 | +{ |
| 3 | + using Gu.Roslyn.Asserts; |
| 4 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 5 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | + using NUnit.Framework; |
| 7 | + |
| 8 | + public class CodeFix |
| 9 | + { |
| 10 | + private static readonly DiagnosticAnalyzer Analyzer = new AttributeAnalyzer(); |
| 11 | + private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(ASP004RouteParameterType.Descriptor); |
| 12 | + private static readonly CodeFixProvider Fix = new TemplateTextFix(); |
| 13 | + |
| 14 | + [Explicit] |
| 15 | + [TestCase("\"{id:↓float}\"", "\"{id:int}\"", "int id")] |
| 16 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:int}\"", "int id")] |
| 17 | + [TestCase("\"api/orders/{id:↓float:min(1)}\"", "\"api/orders/{id:int:min(1)}\"", "int id")] |
| 18 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:long}\"", "long id")] |
| 19 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:bool}\"", "bool id")] |
| 20 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:datetime}\"", "System.DateTime id")] |
| 21 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:decimal}\"", "decimal id")] |
| 22 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:double}\"", "double id")] |
| 23 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:float}\"", "float id")] |
| 24 | + [TestCase("\"api/orders/{id:↓float}\"", "\"api/orders/{id:guid}\"", "System.Guid id")] |
| 25 | + public void When(string before, string after, string parameter) |
| 26 | + { |
| 27 | + var code = @" |
| 28 | +namespace ValidCode |
| 29 | +{ |
| 30 | + using Microsoft.AspNetCore.Mvc; |
| 31 | +
|
| 32 | + [ApiController] |
| 33 | + public class OrdersController : Controller |
| 34 | + { |
| 35 | + [HttpGet(""api/orders/{id}"")] |
| 36 | + public IActionResult GetId(byte id) |
| 37 | + { |
| 38 | + return this.Ok(id); |
| 39 | + } |
| 40 | + } |
| 41 | +}".AssertReplace("\"api/orders/{id}\"", before) |
| 42 | + .AssertReplace("byte id", parameter); |
| 43 | + |
| 44 | + var fixedCode = @" |
| 45 | +namespace ValidCode |
| 46 | +{ |
| 47 | + using Microsoft.AspNetCore.Mvc; |
| 48 | +
|
| 49 | + [ApiController] |
| 50 | + public class OrdersController : Controller |
| 51 | + { |
| 52 | + [HttpGet(""api/orders/{id}"")] |
| 53 | + public IActionResult GetId(byte id) |
| 54 | + { |
| 55 | + return this.Ok(id); |
| 56 | + } |
| 57 | + } |
| 58 | +}".AssertReplace("\"api/orders/{id}\"", after) |
| 59 | + .AssertReplace("byte id", parameter); |
| 60 | + AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode); |
| 61 | + } |
| 62 | + |
| 63 | + [TestCase("\"api/orders/{id:↓minlength(1)}\"")] |
| 64 | + [TestCase("\"api/orders/{id:↓maxlength(1)}\"")] |
| 65 | + [TestCase("\"api/orders/{id:↓length(1)}\"")] |
| 66 | + [TestCase("\"api/orders/{id:↓length(1,3)}\"")] |
| 67 | + [TestCase("\"api/orders/{id:↓min(1)}\"")] |
| 68 | + [TestCase("\"api/orders/{id:↓max(10)}\"")] |
| 69 | + [TestCase("\"api/orders/{id:↓range(0,10)}\"")] |
| 70 | + [TestCase("\"api/orders/{id:↓alpha}\"")] |
| 71 | + [TestCase("\"api/orders/{id:↓regex(a-(0|1))}\"")] |
| 72 | + [TestCase("\"api/orders/{id:↓regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{4}$)}\"")] |
| 73 | + [TestCase("@\"api/orders/{id:↓regex(^\\d{{3}}-\\d{{2}}-\\d{4}$)}\"")] |
| 74 | + [TestCase("@\"api/orders/{id:↓regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{4}$)}\"")] |
| 75 | + public void NoFixWhen(string template) |
| 76 | + { |
| 77 | + var code = @" |
| 78 | +namespace ValidCode |
| 79 | +{ |
| 80 | + using Microsoft.AspNetCore.Mvc; |
| 81 | +
|
| 82 | + [ApiController] |
| 83 | + public class OrdersController : Controller |
| 84 | + { |
| 85 | + [HttpGet(""api/orders/{id}"")] |
| 86 | + public IActionResult GetId(byte id) |
| 87 | + { |
| 88 | + return this.Ok(id); |
| 89 | + } |
| 90 | + } |
| 91 | +}".AssertReplace("\"api/orders/{id}\"", template); |
| 92 | + |
| 93 | + AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); |
| 94 | + } |
| 95 | + |
| 96 | + [TestCase("int?")] |
| 97 | + [TestCase("Nullable<int>")] |
| 98 | + public void WhenOptional(string parameter) |
| 99 | + { |
| 100 | + var code = @" |
| 101 | +namespace ValidCode |
| 102 | +{ |
| 103 | + using System; |
| 104 | + using Microsoft.AspNetCore.Mvc; |
| 105 | +
|
| 106 | + [ApiController] |
| 107 | + public class OrdersController : Controller |
| 108 | + { |
| 109 | + [HttpGet(""api/orders/{↓id}"")] |
| 110 | + public IActionResult GetId(int? id) |
| 111 | + { |
| 112 | + return this.Ok(id); |
| 113 | + } |
| 114 | + } |
| 115 | +}".AssertReplace("int?", parameter); |
| 116 | + |
| 117 | + var fixedCode = @" |
| 118 | +namespace ValidCode |
| 119 | +{ |
| 120 | + using System; |
| 121 | + using Microsoft.AspNetCore.Mvc; |
| 122 | +
|
| 123 | + [ApiController] |
| 124 | + public class OrdersController : Controller |
| 125 | + { |
| 126 | + [HttpGet(""api/orders/{id?}"")] |
| 127 | + public IActionResult GetId(int? id) |
| 128 | + { |
| 129 | + return this.Ok(id); |
| 130 | + } |
| 131 | + } |
| 132 | +}".AssertReplace("int?", parameter); |
| 133 | + AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments