|
| 1 | +// Copyright (c) 2026 ReactiveUI and contributors. All rights reserved. |
| 2 | +// Licensed to the ReactiveUI and contributors under one or more agreements. |
| 3 | +// The ReactiveUI and contributors licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Collections.Immutable; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | +using Microsoft.CodeAnalysis; |
| 12 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 13 | +using Microsoft.CodeAnalysis.CSharp; |
| 14 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 15 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 16 | +using NUnit.Framework; |
| 17 | +using ReactiveUI.SourceGenerators.CodeFixers; |
| 18 | + |
| 19 | +namespace ReactiveUI.SourceGenerator.Tests; |
| 20 | + |
| 21 | +/// <summary> |
| 22 | +/// Unit tests for <see cref="ReactiveAttributeMisuseCodeFixProvider" />. |
| 23 | +/// </summary> |
| 24 | +[TestFixture] |
| 25 | +public sealed class ReactiveAttributeMisuseCodeFixProviderTests |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Verifies `required` stays before `partial` when applying the code fix. |
| 29 | + /// </summary> |
| 30 | + [Test] |
| 31 | + public void WhenRequiredPropertyThenPartialInsertedAfterRequired() |
| 32 | + { |
| 33 | + const string source = """ |
| 34 | + using ReactiveUI; |
| 35 | + using ReactiveUI.SourceGenerators; |
| 36 | +
|
| 37 | + namespace TestNs; |
| 38 | +
|
| 39 | + public partial class TestVM : ReactiveObject |
| 40 | + { |
| 41 | + [Reactive(UseRequired = true)] |
| 42 | + public required string? PartialRequiredPropertyTest { get; set; } |
| 43 | + } |
| 44 | + """; |
| 45 | + |
| 46 | + var fixedSource = ApplyFix(source); |
| 47 | + |
| 48 | + Assert.That(fixedSource, Does.Contain("public required partial string? PartialRequiredPropertyTest")); |
| 49 | + Assert.That(fixedSource, Does.Not.Contain("public partial required string? PartialRequiredPropertyTest")); |
| 50 | + } |
| 51 | + |
| 52 | + private static string ApplyFix(string source) |
| 53 | + { |
| 54 | + var tree = CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp13)); |
| 55 | + var root = tree.GetRoot(); |
| 56 | + |
| 57 | + var analyzer = new ReactiveAttributeMisuseAnalyzer(); |
| 58 | + var compilation = CSharpCompilation.Create( |
| 59 | + "CodeFixTests", |
| 60 | + syntaxTrees: [tree], |
| 61 | + references: [ |
| 62 | + MetadataReference.CreateFromFile(typeof(object).Assembly.Location), |
| 63 | + MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), |
| 64 | + ], |
| 65 | + options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); |
| 66 | + |
| 67 | + var diagnostic = compilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(analyzer)) |
| 68 | + .GetAnalyzerDiagnosticsAsync() |
| 69 | + .GetAwaiter().GetResult() |
| 70 | + .Single(d => d.Id == "RXUISG0020"); |
| 71 | + |
| 72 | + using var workspace = new AdhocWorkspace(); |
| 73 | + var project = workspace.CurrentSolution |
| 74 | + .AddProject("p", "p", LanguageNames.CSharp) |
| 75 | + .WithParseOptions(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp13)) |
| 76 | + .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) |
| 77 | + .AddMetadataReference(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)) |
| 78 | + .AddMetadataReference(MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location)); |
| 79 | + |
| 80 | + var document = project.AddDocument("t.cs", source); |
| 81 | + |
| 82 | + CodeFixProvider provider = new ReactiveAttributeMisuseCodeFixProvider(); |
| 83 | + |
| 84 | + var actions = new List<Microsoft.CodeAnalysis.CodeActions.CodeAction>(); |
| 85 | + var context = new CodeFixContext( |
| 86 | + document, |
| 87 | + diagnostic, |
| 88 | + (a, _) => actions.Add(a), |
| 89 | + CancellationToken.None); |
| 90 | + |
| 91 | + provider.RegisterCodeFixesAsync(context).GetAwaiter().GetResult(); |
| 92 | + |
| 93 | + var operation = actions.Single().GetOperationsAsync(CancellationToken.None).GetAwaiter().GetResult().Single(); |
| 94 | + operation.Apply(document.Project.Solution.Workspace, CancellationToken.None); |
| 95 | + |
| 96 | + var updatedDoc = document.Project.Solution.Workspace.CurrentSolution.GetDocument(document.Id); |
| 97 | + return updatedDoc!.GetTextAsync(CancellationToken.None).GetAwaiter().GetResult().ToString(); |
| 98 | + } |
| 99 | +} |
0 commit comments