| 1 | using System.Collections.Immutable; |
| 2 | using System.Text; |
| 3 | using Basic.Reference.Assemblies; |
| 4 | using Microsoft.CodeAnalysis; |
| 5 | using Microsoft.CodeAnalysis.CSharp; |
| 6 | using Microsoft.CodeAnalysis.Diagnostics; |
| 7 | using Microsoft.CodeAnalysis.Text; |
| 8 | using Xunit; |
| 9 | |
| 10 | namespace CascadeIDE.ArchitectureAnalyzers.Tests; |
| 11 | |
| 12 | public sealed class MfdShellViewXamlLeanAnalyzerTests |
| 13 | { |
| 14 | private sealed class StringAdditionalText : AdditionalText |
| 15 | { |
| 16 | private readonly string _text; |
| 17 | |
| 18 | public StringAdditionalText(string path, string text) |
| 19 | { |
| 20 | Path = path; |
| 21 | _text = text; |
| 22 | } |
| 23 | |
| 24 | public override string Path { get; } |
| 25 | |
| 26 | public override SourceText GetText(CancellationToken cancellationToken = default) => |
| 27 | SourceText.From(_text, Encoding.UTF8, SourceHashAlgorithm.Sha1); |
| 28 | } |
| 29 | |
| 30 | private static async Task<ImmutableArray<Diagnostic>> RunWithAxamlAsync( |
| 31 | string xaml, |
| 32 | string fileName = "MfdShellView.axaml") |
| 33 | { |
| 34 | var tree = CSharpSyntaxTree.ParseText("namespace T { }", path: "dummy.cs"); |
| 35 | var compilation = CSharpCompilation.Create( |
| 36 | "TestAssembly", |
| 37 | [tree], |
| 38 | Net80.References.All, |
| 39 | new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); |
| 40 | |
| 41 | var additional = new StringAdditionalText( |
| 42 | $@"D:\repo\cascade-ide\Views\{fileName}", |
| 43 | xaml); |
| 44 | |
| 45 | var options = new AnalyzerOptions(ImmutableArray.Create<AdditionalText>(additional)); |
| 46 | var withAnalyzers = compilation.WithAnalyzers( |
| 47 | ImmutableArray.Create<DiagnosticAnalyzer>(new MfdShellViewXamlLeanAnalyzer()), |
| 48 | options); |
| 49 | |
| 50 | return await withAnalyzers.GetAnalyzerDiagnosticsAsync(); |
| 51 | } |
| 52 | |
| 53 | [Fact] |
| 54 | public async Task LeanShell_BelowLimit_NoDiagnostics() |
| 55 | { |
| 56 | var xaml = """ |
| 57 | <UserControl> |
| 58 | <Grid> |
| 59 | <Border><views:BuildMfdPageView/></Border> |
| 60 | </Grid> |
| 61 | </UserControl> |
| 62 | """; |
| 63 | var diags = await RunWithAxamlAsync(xaml); |
| 64 | Assert.Empty(diags); |
| 65 | } |
| 66 | |
| 67 | [Fact] |
| 68 | public async Task MfdShellView_TooManyLines_Reports017() |
| 69 | { |
| 70 | var lines = new StringBuilder(); |
| 71 | lines.AppendLine("<UserControl><Grid></Grid></UserControl>"); |
| 72 | for (var i = 0; i < MfdShellViewXamlLeanAnalyzer.MaxLineCountMfdShellView; i++) |
| 73 | lines.AppendLine("<!--x-->"); |
| 74 | |
| 75 | var diags = await RunWithAxamlAsync(lines.ToString()); |
| 76 | var d = Assert.Single(diags); |
| 77 | Assert.Equal(MfdShellViewXamlLeanAnalyzer.TooLongId, d.Id); |
| 78 | } |
| 79 | |
| 80 | [Fact] |
| 81 | public async Task MfdShellPageStack_TooManyLines_Reports017() |
| 82 | { |
| 83 | var lines = new StringBuilder(); |
| 84 | lines.AppendLine("<UserControl><Grid></Grid></UserControl>"); |
| 85 | for (var i = 0; i < MfdShellViewXamlLeanAnalyzer.MaxLineCountMfdShellPageStack; i++) |
| 86 | lines.AppendLine("<!--x-->"); |
| 87 | |
| 88 | var diags = await RunWithAxamlAsync(lines.ToString(), MfdShellViewXamlLeanAnalyzer.PageStackFileName); |
| 89 | var d = Assert.Single(diags); |
| 90 | Assert.Equal(MfdShellViewXamlLeanAnalyzer.TooLongId, d.Id); |
| 91 | } |
| 92 | |
| 93 | [Fact] |
| 94 | public async Task ListBox_Reports018() |
| 95 | { |
| 96 | var xaml = """ |
| 97 | <UserControl> |
| 98 | <ListBox></ListBox> |
| 99 | </UserControl> |
| 100 | """; |
| 101 | var diags = await RunWithAxamlAsync(xaml); |
| 102 | var d = Assert.Single(diags); |
| 103 | Assert.Equal(MfdShellViewXamlLeanAnalyzer.InlineControlsId, d.Id); |
| 104 | } |
| 105 | } |
| 106 | |