|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
5 | 7 | using System.Linq; |
6 | 8 | using System.Threading; |
7 | 9 | using System.Threading.Tasks; |
@@ -231,6 +233,195 @@ The end. |
231 | 233 | expected: "", |
232 | 234 | fileKind: RazorFileKind.Legacy); |
233 | 235 |
|
| 236 | + [Fact] |
| 237 | + public Task Component_WithContent() |
| 238 | + => VerifyRenamesAsync( |
| 239 | + input: $""" |
| 240 | + This is a Razor document. |
| 241 | +
|
| 242 | + <Component>Hello</Compon$$ent> |
| 243 | + <Component> |
| 244 | + Hello |
| 245 | + </Component> |
| 246 | +
|
| 247 | + The end. |
| 248 | + """, |
| 249 | + additionalFiles: [ |
| 250 | + (FilePath("Component.razor"), "") |
| 251 | + ], |
| 252 | + newName: "DifferentName", |
| 253 | + expected: """ |
| 254 | + This is a Razor document. |
| 255 | +
|
| 256 | + <DifferentName>Hello</DifferentName> |
| 257 | + <DifferentName> |
| 258 | + Hello |
| 259 | + </DifferentName> |
| 260 | +
|
| 261 | + The end. |
| 262 | + """, |
| 263 | + renames: [ |
| 264 | + ("Component.razor", "DifferentName.razor") |
| 265 | + ]); |
| 266 | + |
| 267 | + [Fact] |
| 268 | + public Task Component_WithContent_FullyQualified() |
| 269 | + => VerifyRenamesAsync( |
| 270 | + input: $""" |
| 271 | + This is a Razor document. |
| 272 | +
|
| 273 | + <My.Namespace.Component>Hello</My.Namespace.Compon$$ent> |
| 274 | + <My.Namespace.Component> |
| 275 | + Hello |
| 276 | + </My.Namespace.Component> |
| 277 | +
|
| 278 | + The end. |
| 279 | + """, |
| 280 | + additionalFiles: [ |
| 281 | + (FilePath("Component.razor"), """ |
| 282 | + @namespace My.Namespace |
| 283 | + """) |
| 284 | + ], |
| 285 | + newName: "DifferentName", |
| 286 | + expected: """ |
| 287 | + This is a Razor document. |
| 288 | +
|
| 289 | + <My.Namespace.DifferentName>Hello</My.Namespace.DifferentName> |
| 290 | + <My.Namespace.DifferentName> |
| 291 | + Hello |
| 292 | + </My.Namespace.DifferentName> |
| 293 | +
|
| 294 | + The end. |
| 295 | + """, |
| 296 | + renames: [ |
| 297 | + ("Component.razor", "DifferentName.razor") |
| 298 | + ]); |
| 299 | + |
| 300 | + [Fact] |
| 301 | + public Task Component_MultipleUsage() |
| 302 | + => VerifyRenamesAsync( |
| 303 | + input: $""" |
| 304 | + This is a Razor document. |
| 305 | +
|
| 306 | + <Comp$$onent /> |
| 307 | + <Component></Component> |
| 308 | + <Component> |
| 309 | + </Component> |
| 310 | +
|
| 311 | + The end. |
| 312 | + """, |
| 313 | + additionalFiles: [ |
| 314 | + (FilePath("Component.razor"), ""), |
| 315 | + (FilePath("OtherComponent.razor"), """ |
| 316 | + <Component /> |
| 317 | + <Component></Component> |
| 318 | + <Component> |
| 319 | + </Component> |
| 320 | + """) |
| 321 | + ], |
| 322 | + newName: "DifferentName", |
| 323 | + expected: """ |
| 324 | + This is a Razor document. |
| 325 | +
|
| 326 | + <DifferentName /> |
| 327 | + <DifferentName></DifferentName> |
| 328 | + <DifferentName> |
| 329 | + </DifferentName> |
| 330 | +
|
| 331 | + The end. |
| 332 | + """, |
| 333 | + renames: [ |
| 334 | + ("Component.razor", "DifferentName.razor") |
| 335 | + ], |
| 336 | + additionalExpectedFiles: [ |
| 337 | + (FileUri("OtherComponent.razor"), """ |
| 338 | + <DifferentName /> |
| 339 | + <DifferentName></DifferentName> |
| 340 | + <DifferentName> |
| 341 | + </DifferentName> |
| 342 | + """) |
| 343 | + ]); |
| 344 | + |
| 345 | + [Fact] |
| 346 | + public Task Component_FullyQualified() |
| 347 | + => VerifyRenamesAsync( |
| 348 | + input: $""" |
| 349 | + This is a Razor document. |
| 350 | +
|
| 351 | + <My.Namespace.Comp$$onent /> |
| 352 | + <My.Namespace.Component></My.Namespace.Component> |
| 353 | + <My.Namespace.Component> |
| 354 | + </My.Namespace.Component> |
| 355 | +
|
| 356 | + The end. |
| 357 | + """, |
| 358 | + additionalFiles: [ |
| 359 | + (FilePath("Component.razor"), """ |
| 360 | + @namespace My.Namespace |
| 361 | + """) |
| 362 | + ], |
| 363 | + newName: "DifferentName", |
| 364 | + expected: """ |
| 365 | + This is a Razor document. |
| 366 | +
|
| 367 | + <My.Namespace.DifferentName /> |
| 368 | + <My.Namespace.DifferentName></My.Namespace.DifferentName> |
| 369 | + <My.Namespace.DifferentName> |
| 370 | + </My.Namespace.DifferentName> |
| 371 | +
|
| 372 | + The end. |
| 373 | + """, |
| 374 | + renames: [ |
| 375 | + ("Component.razor", "DifferentName.razor") |
| 376 | + ]); |
| 377 | + |
| 378 | + [Fact] |
| 379 | + public Task Component_MultipleUsage_FullyQualified() |
| 380 | + => VerifyRenamesAsync( |
| 381 | + input: $""" |
| 382 | + This is a Razor document. |
| 383 | +
|
| 384 | + <My.Namespace.Comp$$onent /> |
| 385 | + <My.Namespace.Component></My.Namespace.Component> |
| 386 | + <My.Namespace.Component> |
| 387 | + </My.Namespace.Component> |
| 388 | +
|
| 389 | + The end. |
| 390 | + """, |
| 391 | + additionalFiles: [ |
| 392 | + (FilePath("Component.razor"), """ |
| 393 | + @namespace My.Namespace |
| 394 | + """), |
| 395 | + (FilePath("OtherComponent.razor"), """ |
| 396 | + <My.Namespace.Component /> |
| 397 | + <My.Namespace.Component></My.Namespace.Component> |
| 398 | + <My.Namespace.Component> |
| 399 | + </My.Namespace.Component> |
| 400 | + """) |
| 401 | + ], |
| 402 | + newName: "DifferentName", |
| 403 | + expected: """ |
| 404 | + This is a Razor document. |
| 405 | +
|
| 406 | + <My.Namespace.DifferentName /> |
| 407 | + <My.Namespace.DifferentName></My.Namespace.DifferentName> |
| 408 | + <My.Namespace.DifferentName> |
| 409 | + </My.Namespace.DifferentName> |
| 410 | +
|
| 411 | + The end. |
| 412 | + """, |
| 413 | + renames: [ |
| 414 | + ("Component.razor", "DifferentName.razor") |
| 415 | + ], |
| 416 | + additionalExpectedFiles: [ |
| 417 | + (FileUri("OtherComponent.razor"), """ |
| 418 | + <My.Namespace.DifferentName /> |
| 419 | + <My.Namespace.DifferentName></My.Namespace.DifferentName> |
| 420 | + <My.Namespace.DifferentName> |
| 421 | + </My.Namespace.DifferentName> |
| 422 | + """) |
| 423 | + ]); |
| 424 | + |
234 | 425 | private async Task VerifyRenamesAsync( |
235 | 426 | string input, |
236 | 427 | string newName, |
@@ -271,53 +462,45 @@ private async Task VerifyRenamesAsync( |
271 | 462 | { |
272 | 463 | Assert.NotNull(renames); |
273 | 464 |
|
| 465 | + var expectedRenames = renames.ToList(); |
274 | 466 | foreach (var change in changes) |
275 | 467 | { |
276 | 468 | if (change.TryGetThird(out var renameEdit)) |
277 | 469 | { |
278 | | - Assert.Contains(renames, |
| 470 | + var found = Assert.Single(renames, |
279 | 471 | r => renameEdit.OldDocumentUri.GetRequiredParsedUri().GetDocumentFilePath().EndsWith(r.oldName) && |
280 | 472 | renameEdit.NewDocumentUri.GetRequiredParsedUri().GetDocumentFilePath().EndsWith(r.newName)); |
| 473 | + expectedRenames.Remove(found); |
281 | 474 | } |
282 | 475 | } |
283 | | - } |
284 | 476 |
|
285 | | - await ProcessRazorDocumentEditsAsync(inputText, expected, document, additionalExpectedFiles, result, DisposalToken).ConfigureAwait(false); |
| 477 | + Assert.Empty(expectedRenames); |
| 478 | + } |
286 | 479 |
|
| 480 | + var expectedChanges = (additionalExpectedFiles ?? []).Concat([(document.CreateUri(), expected)]); |
| 481 | + await VerifyWorkspaceEditAsync(result, document.Project.Solution, expectedChanges, DisposalToken); |
287 | 482 | } |
288 | 483 |
|
289 | | - private static async Task ProcessRazorDocumentEditsAsync(SourceText inputText, string expected, TextDocument razorDocument, (Uri fileUri, string contents)[]? additionalExpectedFiles, WorkspaceEdit result, CancellationToken cancellationToken) |
| 484 | + private static async Task VerifyWorkspaceEditAsync(WorkspaceEdit workspaceEdit, Solution solution, IEnumerable<(Uri fileUri, string contents)> expectedChanges, CancellationToken cancellationToken) |
290 | 485 | { |
291 | | - var razorDocumentUri = razorDocument.CreateUri(); |
292 | | - var solution = razorDocument.Project.Solution; |
293 | | - |
294 | | - Assert.True(result.TryGetTextDocumentEdits(out var textDocumentEdits)); |
| 486 | + Assert.True(workspaceEdit.TryGetTextDocumentEdits(out var textDocumentEdits)); |
295 | 487 | foreach (var textDocumentEdit in textDocumentEdits) |
296 | 488 | { |
297 | | - if (textDocumentEdit.TextDocument.DocumentUri.GetRequiredParsedUri() == razorDocumentUri) |
298 | | - { |
299 | | - foreach (var edit in textDocumentEdit.Edits) |
300 | | - { |
301 | | - inputText = inputText.WithChanges(inputText.GetTextChange((TextEdit)edit)); |
302 | | - } |
303 | | - } |
304 | | - else if (additionalExpectedFiles is not null) |
305 | | - { |
306 | | - foreach (var (uri, contents) in additionalExpectedFiles) |
307 | | - { |
308 | | - var additionalDocument = solution.GetTextDocuments(uri).First(); |
309 | | - var text = await additionalDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); |
| 489 | + var (uri, _) = expectedChanges.Single(e => e.fileUri == textDocumentEdit.TextDocument.DocumentUri.GetRequiredParsedUri()); |
310 | 490 |
|
311 | | - foreach (var edit in textDocumentEdit.Edits) |
312 | | - { |
313 | | - text = text.WithChanges(text.GetTextChange((TextEdit)edit)); |
314 | | - } |
| 491 | + var document = solution.GetTextDocuments(uri).First(); |
| 492 | + var text = await document.GetTextAsync(cancellationToken); |
315 | 493 |
|
316 | | - AssertEx.EqualOrDiff(contents, text.ToString()); |
317 | | - } |
318 | | - } |
| 494 | + text = text.WithChanges(textDocumentEdit.Edits.Select(e => text.GetTextChange((TextEdit)e))); |
| 495 | + |
| 496 | + solution = solution.WithAdditionalDocumentText(document.Id, text); |
319 | 497 | } |
320 | 498 |
|
321 | | - AssertEx.EqualOrDiff(expected, inputText.ToString()); |
| 499 | + foreach (var (uri, contents) in expectedChanges) |
| 500 | + { |
| 501 | + var document = solution.GetTextDocuments(uri).First(); |
| 502 | + var text = await document.GetTextAsync(cancellationToken); |
| 503 | + AssertEx.EqualOrDiff(contents, text.ToString()); |
| 504 | + } |
322 | 505 | } |
323 | 506 | } |
0 commit comments