|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Diagnostics; |
4 | 5 | using System.Globalization;
|
| 6 | +using System.Text; |
5 | 7 | using Microsoft.AspNetCore.Analyzer.Testing;
|
6 | 8 | using Microsoft.AspNetCore.Analyzers.RenderTreeBuilder;
|
7 | 9 | using Microsoft.AspNetCore.Analyzers.RouteEmbeddedLanguage.Infrastructure;
|
| 10 | +using Microsoft.CodeAnalysis; |
| 11 | +using Xunit.Abstractions; |
8 | 12 |
|
9 | 13 | namespace Microsoft.AspNetCore.Analyzers.RouteEmbeddedLanguage;
|
10 | 14 |
|
11 | 15 | public partial class RoutePatternAnalyzerTests
|
12 | 16 | {
|
| 17 | + private readonly ITestOutputHelper _testOutputHelper; |
| 18 | + |
13 | 19 | private TestDiagnosticAnalyzerRunner Runner { get; } = new(new RoutePatternAnalyzer());
|
14 | 20 |
|
| 21 | + public RoutePatternAnalyzerTests(ITestOutputHelper testOutputHelper) |
| 22 | + { |
| 23 | + _testOutputHelper = testOutputHelper; |
| 24 | + } |
| 25 | + |
15 | 26 | [Fact]
|
16 | 27 | public async Task CommentOnString_ReportResults()
|
17 | 28 | {
|
@@ -512,4 +523,125 @@ public object TestAction(int id)
|
512 | 523 | // Assert
|
513 | 524 | Assert.Empty(diagnostics);
|
514 | 525 | }
|
| 526 | + |
| 527 | + [Fact] |
| 528 | + public async Task ConcatString_PerformanceTest() |
| 529 | + { |
| 530 | + // Arrange |
| 531 | + var builder = new StringBuilder(); |
| 532 | + builder.AppendLine(""" |
| 533 | + class Program |
| 534 | + { |
| 535 | + static void Main() { } |
| 536 | + static readonly string _s = |
| 537 | + """); |
| 538 | + for (var i = 0; i < 2000; i++) |
| 539 | + { |
| 540 | + builder.AppendLine(" \"a{}bc\" +"); |
| 541 | + } |
| 542 | + builder.AppendLine(""" |
| 543 | + ""; |
| 544 | + } |
| 545 | + """); |
| 546 | + var source = TestSource.Read(builder.ToString()); |
| 547 | + |
| 548 | + // Act 1 |
| 549 | + // Warm up. |
| 550 | + var diagnostics1 = await Runner.GetDiagnosticsAsync(source.Source); |
| 551 | + |
| 552 | + // Assert 1 |
| 553 | + Assert.Empty(diagnostics1); |
| 554 | + |
| 555 | + // Act 2 |
| 556 | + // Measure analysis. |
| 557 | + var stopwatch = Stopwatch.StartNew(); |
| 558 | + |
| 559 | + var diagnostics2 = await Runner.GetDiagnosticsAsync(source.Source); |
| 560 | + _testOutputHelper.WriteLine($"Elapsed time: {stopwatch.Elapsed}"); |
| 561 | + |
| 562 | + // Assert 2 |
| 563 | + Assert.Empty(diagnostics2); |
| 564 | + } |
| 565 | + |
| 566 | + [Fact] |
| 567 | + public async Task ConcatString_DetectLanguage_NoWarningsBecauseConcatString() |
| 568 | + { |
| 569 | + // Arrange |
| 570 | + var builder = new StringBuilder(); |
| 571 | + builder.AppendLine(""" |
| 572 | + class Program |
| 573 | + { |
| 574 | + static void Main() { } |
| 575 | + // lang=Route |
| 576 | + static readonly string _s = |
| 577 | + """); |
| 578 | + for (var i = 0; i < 2000; i++) |
| 579 | + { |
| 580 | + builder.AppendLine(" \"a{}bc\" +"); |
| 581 | + } |
| 582 | + builder.AppendLine(""" |
| 583 | + ""; |
| 584 | + } |
| 585 | + """); |
| 586 | + var source = TestSource.Read(builder.ToString()); |
| 587 | + |
| 588 | + // Act |
| 589 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 590 | + |
| 591 | + // Assert |
| 592 | + Assert.Empty(diagnostics); |
| 593 | + } |
| 594 | + |
| 595 | + [Fact] |
| 596 | + public async Task NestedLangComment_NoWarningsBecauseConcatString() |
| 597 | + { |
| 598 | + // Arrange |
| 599 | + var builder = new StringBuilder(); |
| 600 | + builder.AppendLine(""" |
| 601 | + class Program |
| 602 | + { |
| 603 | + static void Main() { } |
| 604 | + static readonly string _s = |
| 605 | + "{/*MM0*/te*st0}" + |
| 606 | + // lang=Route |
| 607 | + "{/*MM1*/te*st1}" + |
| 608 | + "{/*MM2*/te*st2}" + |
| 609 | + "{test3}"; |
| 610 | + } |
| 611 | + """); |
| 612 | + var source = TestSource.Read(builder.ToString()); |
| 613 | + |
| 614 | + // Act |
| 615 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 616 | + |
| 617 | + // Assert |
| 618 | + Assert.Empty(diagnostics); |
| 619 | + } |
| 620 | + |
| 621 | + [Fact] |
| 622 | + public async Task TopLangComment_NoWarningsBecauseConcatString() |
| 623 | + { |
| 624 | + // Arrange |
| 625 | + var builder = new StringBuilder(); |
| 626 | + builder.AppendLine(""" |
| 627 | + class Program |
| 628 | + { |
| 629 | + static void Main() { } |
| 630 | + static readonly string _s = |
| 631 | + // lang=Route |
| 632 | + "{/*MM0*/te*st0}" + |
| 633 | + "{/*MM1*/te*st1}" + |
| 634 | + "{/*MM2*/te*st2}" + |
| 635 | + // lang=regex |
| 636 | + "{test3}"; |
| 637 | + } |
| 638 | + """); |
| 639 | + var source = TestSource.Read(builder.ToString()); |
| 640 | + |
| 641 | + // Act |
| 642 | + var diagnostics = await Runner.GetDiagnosticsAsync(source.Source); |
| 643 | + |
| 644 | + // Assert |
| 645 | + Assert.Empty(diagnostics); |
| 646 | + } |
515 | 647 | }
|
0 commit comments