|
| 1 | +namespace AspNetCoreAnalyzers.Tests.ASP001ParameterNameTests |
| 2 | +{ |
| 3 | + using Gu.Roslyn.Asserts; |
| 4 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 5 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | + using NUnit.Framework; |
| 7 | + |
| 8 | + internal class CodeFix |
| 9 | + { |
| 10 | + private static readonly DiagnosticAnalyzer Analyzer = new AttributeAnalyzer(); |
| 11 | + private static readonly ExpectedDiagnostic ExpectedDiagnostic = Gu.Roslyn.Asserts.ExpectedDiagnostic.Create(ASP001ParameterName.Descriptor); |
| 12 | + private static readonly CodeFixProvider RenameParameterFix = new RenameParameterFix(); |
| 13 | + |
| 14 | + [Test] |
| 15 | + public void RenameParameterImplicit() |
| 16 | + { |
| 17 | + var order = @" |
| 18 | +namespace ValidCode |
| 19 | +{ |
| 20 | + public class Order |
| 21 | + { |
| 22 | + public int Id { get; set; } |
| 23 | + } |
| 24 | +}"; |
| 25 | + |
| 26 | + var db = @" |
| 27 | +namespace ValidCode |
| 28 | +{ |
| 29 | + using Microsoft.EntityFrameworkCore; |
| 30 | +
|
| 31 | + public class Db : DbContext |
| 32 | + { |
| 33 | + public DbSet<Order> Orders { get; set; } |
| 34 | + } |
| 35 | +}"; |
| 36 | + var before = @" |
| 37 | +namespace ValidCode |
| 38 | +{ |
| 39 | + using System.Threading.Tasks; |
| 40 | + using Microsoft.AspNetCore.Mvc; |
| 41 | + using Microsoft.EntityFrameworkCore; |
| 42 | +
|
| 43 | + [ApiController] |
| 44 | + public class OrdersController : Controller |
| 45 | + { |
| 46 | + private readonly Db db; |
| 47 | +
|
| 48 | + public OrdersController(Db db) |
| 49 | + { |
| 50 | + this.db = db; |
| 51 | + } |
| 52 | +
|
| 53 | + [HttpGet(""api/orders/{id}"")] |
| 54 | + public async Task<IActionResult> GetOrder(int ↓wrong) |
| 55 | + { |
| 56 | + var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == wrong); |
| 57 | + if (match == null) |
| 58 | + { |
| 59 | + return this.NotFound(); |
| 60 | + } |
| 61 | +
|
| 62 | + return this.Ok(match); |
| 63 | + } |
| 64 | + } |
| 65 | +}"; |
| 66 | + |
| 67 | + var after = @" |
| 68 | +namespace ValidCode |
| 69 | +{ |
| 70 | + using System.Threading.Tasks; |
| 71 | + using Microsoft.AspNetCore.Mvc; |
| 72 | + using Microsoft.EntityFrameworkCore; |
| 73 | +
|
| 74 | + [ApiController] |
| 75 | + public class OrdersController : Controller |
| 76 | + { |
| 77 | + private readonly Db db; |
| 78 | +
|
| 79 | + public OrdersController(Db db) |
| 80 | + { |
| 81 | + this.db = db; |
| 82 | + } |
| 83 | +
|
| 84 | + [HttpGet(""api/orders/{id}"")] |
| 85 | + public async Task<IActionResult> GetOrder(int id) |
| 86 | + { |
| 87 | + var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id); |
| 88 | + if (match == null) |
| 89 | + { |
| 90 | + return this.NotFound(); |
| 91 | + } |
| 92 | +
|
| 93 | + return this.Ok(match); |
| 94 | + } |
| 95 | + } |
| 96 | +}"; |
| 97 | + AnalyzerAssert.CodeFix(Analyzer, RenameParameterFix, ExpectedDiagnostic, new[] { order, db, before }, after); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void RenameParameterExplicit() |
| 102 | + { |
| 103 | + var order = @" |
| 104 | +namespace ValidCode |
| 105 | +{ |
| 106 | + public class Order |
| 107 | + { |
| 108 | + public int Id { get; set; } |
| 109 | + } |
| 110 | +}"; |
| 111 | + |
| 112 | + var db = @" |
| 113 | +namespace ValidCode |
| 114 | +{ |
| 115 | + using Microsoft.EntityFrameworkCore; |
| 116 | +
|
| 117 | + public class Db : DbContext |
| 118 | + { |
| 119 | + public DbSet<Order> Orders { get; set; } |
| 120 | + } |
| 121 | +}"; |
| 122 | + var before = @" |
| 123 | +namespace ValidCode |
| 124 | +{ |
| 125 | + using System.Threading.Tasks; |
| 126 | + using Microsoft.AspNetCore.Mvc; |
| 127 | + using Microsoft.EntityFrameworkCore; |
| 128 | +
|
| 129 | + [ApiController] |
| 130 | + public class OrdersController : Controller |
| 131 | + { |
| 132 | + private readonly Db db; |
| 133 | +
|
| 134 | + public OrdersController(Db db) |
| 135 | + { |
| 136 | + this.db = db; |
| 137 | + } |
| 138 | +
|
| 139 | + [HttpGet(""api/orders/{id}"")] |
| 140 | + public async Task<IActionResult> GetOrder([FromRoute]int ↓wrong) |
| 141 | + { |
| 142 | + var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == wrong); |
| 143 | + if (match == null) |
| 144 | + { |
| 145 | + return this.NotFound(); |
| 146 | + } |
| 147 | +
|
| 148 | + return this.Ok(match); |
| 149 | + } |
| 150 | + } |
| 151 | +}"; |
| 152 | + |
| 153 | + var after = @" |
| 154 | +namespace ValidCode |
| 155 | +{ |
| 156 | + using System.Threading.Tasks; |
| 157 | + using Microsoft.AspNetCore.Mvc; |
| 158 | + using Microsoft.EntityFrameworkCore; |
| 159 | +
|
| 160 | + [ApiController] |
| 161 | + public class OrdersController : Controller |
| 162 | + { |
| 163 | + private readonly Db db; |
| 164 | +
|
| 165 | + public OrdersController(Db db) |
| 166 | + { |
| 167 | + this.db = db; |
| 168 | + } |
| 169 | +
|
| 170 | + [HttpGet(""api/orders/{id}"")] |
| 171 | + public async Task<IActionResult> GetOrder([FromRoute]int id) |
| 172 | + { |
| 173 | + var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id); |
| 174 | + if (match == null) |
| 175 | + { |
| 176 | + return this.NotFound(); |
| 177 | + } |
| 178 | +
|
| 179 | + return this.Ok(match); |
| 180 | + } |
| 181 | + } |
| 182 | +}"; |
| 183 | + AnalyzerAssert.CodeFix(Analyzer, RenameParameterFix, ExpectedDiagnostic, new[] { order, db, before }, after); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments