| 1 | using System.Collections.Immutable; |
| 2 | using Basic.Reference.Assemblies; |
| 3 | using Microsoft.CodeAnalysis; |
| 4 | using Microsoft.CodeAnalysis.CSharp; |
| 5 | using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | using Xunit; |
| 7 | |
| 8 | namespace CascadeIDE.ArchitectureAnalyzers.Tests; |
| 9 | |
| 10 | public sealed class EnvironmentReadinessPipelineAnalyzerTests |
| 11 | { |
| 12 | private static async Task<ImmutableArray<Diagnostic>> RunAnalyzerAsync(params (string Path, string Text)[] files) |
| 13 | { |
| 14 | var trees = new List<SyntaxTree>(files.Length); |
| 15 | foreach (var (path, text) in files) |
| 16 | trees.Add(CSharpSyntaxTree.ParseText(text, path: path)); |
| 17 | |
| 18 | var compilation = CSharpCompilation.Create( |
| 19 | "TestAssembly", |
| 20 | trees, |
| 21 | Net80.References.All, |
| 22 | new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); |
| 23 | |
| 24 | var analyzer = new EnvironmentReadinessPipelineAnalyzer(); |
| 25 | var withAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(analyzer)); |
| 26 | return await withAnalyzers.GetAnalyzerDiagnosticsAsync(); |
| 27 | } |
| 28 | |
| 29 | [Fact] |
| 30 | public async Task CASCOPE006_SnapshotBuilder_InMainWindowViewModel_Reports() |
| 31 | { |
| 32 | var diags = await RunAnalyzerAsync(( |
| 33 | @"D:\repo\ViewModels\MainWindowViewModel.EnvironmentReadiness.cs", |
| 34 | """ |
| 35 | namespace CascadeIDE.ViewModels; |
| 36 | |
| 37 | public sealed class MainWindowViewModel |
| 38 | { |
| 39 | public object X() => EnvironmentReadinessSnapshotBuilder.BuildAllRowsAsync(); |
| 40 | } |
| 41 | |
| 42 | public static class EnvironmentReadinessSnapshotBuilder |
| 43 | { |
| 44 | public static object BuildAllRowsAsync() => new object(); |
| 45 | } |
| 46 | """)); |
| 47 | |
| 48 | var d = Assert.Single(diags); |
| 49 | Assert.Equal(EnvironmentReadinessPipelineAnalyzer.LegacySnapshotBuilderId, d.Id); |
| 50 | } |
| 51 | |
| 52 | [Fact] |
| 53 | public async Task ChannelAndSurfacePath_InMainWindowViewModel_DoesNotReport() |
| 54 | { |
| 55 | var diags = await RunAnalyzerAsync(( |
| 56 | @"D:\repo\ViewModels\MainWindowViewModel.EnvironmentReadiness.cs", |
| 57 | """ |
| 58 | namespace CascadeIDE.ViewModels; |
| 59 | |
| 60 | public sealed class MainWindowViewModel |
| 61 | { |
| 62 | private readonly ReadinessChannel _channel = new(); |
| 63 | private readonly ReadinessSurface _surface = new(); |
| 64 | |
| 65 | public void Refresh() |
| 66 | { |
| 67 | var rows = _channel.Build(0); |
| 68 | _surface.Compose(rows); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public sealed class ReadinessChannel |
| 73 | { |
| 74 | public int Build(int context) => context; |
| 75 | } |
| 76 | |
| 77 | public sealed class ReadinessSurface |
| 78 | { |
| 79 | public void Compose(int payload) { } |
| 80 | } |
| 81 | """)); |
| 82 | |
| 83 | Assert.Empty(diags); |
| 84 | } |
| 85 | } |
| 86 | |