|
| 1 | +// Copyright (c) 2019-2026 ReactiveUI Association Incorporated. All rights reserved. |
| 2 | +// ReactiveUI Association Incorporated licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using ReactiveUI.SourceGenerators.WinForms; |
| 6 | + |
| 7 | +namespace ReactiveUI.SourceGenerator.Tests; |
| 8 | + |
| 9 | +/// <summary>Unit tests for <see cref="ControlHostAnalyzer" />.</summary> |
| 10 | +public sealed class ControlHostAnalyzerTests |
| 11 | +{ |
| 12 | + /// <summary>A WinForms host generated without ObservedProperty.</summary> |
| 13 | + private const string WithoutObservedPropertyId = "RXUISG0022"; |
| 14 | + |
| 15 | + /// <summary>Both hosts, and a class that is neither.</summary> |
| 16 | + private const string HostsSource = """ |
| 17 | + using ReactiveUI.SourceGenerators.WinForms; |
| 18 | +
|
| 19 | + namespace Hosts |
| 20 | + { |
| 21 | + [RoutedControlHost("System.Windows.Forms.UserControl")] |
| 22 | + public partial class RoutedHost { } |
| 23 | +
|
| 24 | + [ViewModelControlHost("System.Windows.Forms.UserControl")] |
| 25 | + public partial class ViewModelHost { } |
| 26 | +
|
| 27 | + [System.Obsolete] |
| 28 | + public class NotAHost { } |
| 29 | + } |
| 30 | + """; |
| 31 | + |
| 32 | + /// <summary>ReactiveUI.Binding without ObservedProperty: a release before 8.4.0.</summary> |
| 33 | + private const string BindingWithoutObservedProperty = """ |
| 34 | + namespace ReactiveUI.Binding |
| 35 | + { |
| 36 | + public interface IViewFor<T> { } |
| 37 | + } |
| 38 | + """; |
| 39 | + |
| 40 | + /// <summary>ReactiveUI.Binding 8.4.0 or later, with ObservedProperty.</summary> |
| 41 | + private const string BindingWithObservedProperty = """ |
| 42 | + namespace ReactiveUI.Binding |
| 43 | + { |
| 44 | + public interface IViewFor<T> { } |
| 45 | +
|
| 46 | + public static class ObservedProperty { } |
| 47 | + } |
| 48 | + """; |
| 49 | + |
| 50 | + /// <summary>Each host is reported when the compilation has no ReactiveUI.Binding with ObservedProperty.</summary> |
| 51 | + /// <param name="bindingSource">Source standing in for ReactiveUI.Binding, or none.</param> |
| 52 | + /// <returns>A task that represents the asynchronous test operation.</returns> |
| 53 | + [Test] |
| 54 | + [Arguments("")] |
| 55 | + [Arguments(BindingWithoutObservedProperty)] |
| 56 | + public async Task ReportsHostsWithoutObservedProperty(string bindingSource) |
| 57 | + { |
| 58 | + var reported = await GetReported(bindingSource); |
| 59 | + |
| 60 | + await Assert.That(reported).IsEquivalentTo(["RoutedHost", "ViewModelHost"]); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary>No host is reported when ReactiveUI.Binding has ObservedProperty.</summary> |
| 64 | + /// <returns>A task that represents the asynchronous test operation.</returns> |
| 65 | + [Test] |
| 66 | + public async Task DoesNotReportHostsWithObservedProperty() => |
| 67 | + await Assert.That(await GetReported(BindingWithObservedProperty)).IsEmpty(); |
| 68 | + |
| 69 | + /// <summary>The diagnostic names the ReactiveUI.Binding version that has ObservedProperty.</summary> |
| 70 | + /// <returns>A task that represents the asynchronous test operation.</returns> |
| 71 | + [Test] |
| 72 | + public async Task NamesTheMinimumBindingVersion() |
| 73 | + { |
| 74 | + var descriptor = new ControlHostAnalyzer().SupportedDiagnostics[0]; |
| 75 | + var message = Diagnostic.Create(descriptor, Location.None, "RoutedHost", "8.4.0").GetMessage(System.Globalization.CultureInfo.InvariantCulture); |
| 76 | + |
| 77 | + await Assert.That(descriptor.Id).IsEqualTo(WithoutObservedPropertyId); |
| 78 | + await Assert.That(descriptor.DefaultSeverity).IsEqualTo(DiagnosticSeverity.Info); |
| 79 | + await Assert.That(message).Contains("ReactiveUI.Binding 8.4.0 or later"); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary>A null analysis context is rejected.</summary> |
| 83 | + /// <returns>A task that represents the asynchronous test operation.</returns> |
| 84 | + [Test] |
| 85 | + public async Task InitializeRejectsANullContext() => |
| 86 | + await Assert.That(static () => new ControlHostAnalyzer().Initialize(null!)).Throws<ArgumentNullException>(); |
| 87 | + |
| 88 | + /// <summary>Analyzes the hosts with a ReactiveUI.Binding stand-in and gets the text each host is reported at.</summary> |
| 89 | + /// <param name="bindingSource">Source standing in for ReactiveUI.Binding, or none.</param> |
| 90 | + /// <returns>The source text at each reported location.</returns> |
| 91 | + private static async Task<List<string>> GetReported(string bindingSource) |
| 92 | + { |
| 93 | + var parseOptions = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp13); |
| 94 | + var tree = CSharpSyntaxTree.ParseText(HostsSource, parseOptions); |
| 95 | + var compilation = CSharpCompilation.Create( |
| 96 | + nameof(ControlHostAnalyzerTests), |
| 97 | + [tree, CSharpSyntaxTree.ParseText(bindingSource, parseOptions)], |
| 98 | + TestCompilationReferences.CreateForAssemblies(typeof(object).Assembly, typeof(RoutedControlHostAttribute).Assembly), |
| 99 | + new(OutputKind.DynamicallyLinkedLibrary)); |
| 100 | + var diagnostics = await compilation.WithAnalyzers([new ControlHostAnalyzer()]).GetAnalyzerDiagnosticsAsync(); |
| 101 | + var text = await tree.GetTextAsync(); |
| 102 | + |
| 103 | + var reported = new List<string>(); |
| 104 | + foreach (var diagnostic in diagnostics) |
| 105 | + { |
| 106 | + if (diagnostic.Id == WithoutObservedPropertyId) |
| 107 | + { |
| 108 | + reported.Add(text.ToString(diagnostic.Location.SourceSpan)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return reported; |
| 113 | + } |
| 114 | +} |
0 commit comments