Skip to content

Commit 450fb9f

Browse files
committed
Add unit test for code fixer
1 parent 162ae4f commit 450fb9f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Threading.Tasks;
6+
using CommunityToolkit.Mvvm.ComponentModel;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.CodeAnalysis.Testing;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
using CSharpCodeFixTest = CommunityToolkit.Mvvm.SourceGenerators.UnitTests.Helpers.CSharpCodeFixWithLanguageVersionTest<
11+
CommunityToolkit.Mvvm.SourceGenerators.UseObservablePropertyOnSemiAutoPropertyAnalyzer,
12+
CommunityToolkit.Mvvm.CodeFixers.UsePartialPropertyForSemiAutoPropertyCodeFixer,
13+
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
14+
using CSharpCodeFixVerifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier<
15+
CommunityToolkit.Mvvm.SourceGenerators.UseObservablePropertyOnSemiAutoPropertyAnalyzer,
16+
CommunityToolkit.Mvvm.CodeFixers.UsePartialPropertyForSemiAutoPropertyCodeFixer,
17+
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
18+
19+
namespace CommunityToolkit.Mvvm.SourceGenerators.UnitTests;
20+
21+
[TestClass]
22+
public class Test_UseObservablePropertyOnSemiAutoPropertyCodeFixer
23+
{
24+
[TestMethod]
25+
public async Task SimpleProperty()
26+
{
27+
string original = """
28+
using CommunityToolkit.Mvvm.ComponentModel;
29+
30+
namespace MyApp;
31+
32+
public class SampleViewModel : ObservableObject
33+
{
34+
public string Name
35+
{
36+
get => field;
37+
set => SetProperty(ref field, value);
38+
}
39+
}
40+
""";
41+
42+
string @fixed = """
43+
using CommunityToolkit.Mvvm.ComponentModel;
44+
45+
namespace MyApp;
46+
47+
public partial class SampleViewModel : ObservableObject
48+
{
49+
[ObservableProperty]
50+
public partial string Name { get; set; }
51+
}
52+
""";
53+
54+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
55+
{
56+
TestCode = original,
57+
FixedCode = @fixed,
58+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
59+
};
60+
61+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
62+
test.ExpectedDiagnostics.AddRange(new[]
63+
{
64+
// /0/Test0.cs(7,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.Name can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)
65+
CSharpCodeFixVerifier.Diagnostic().WithSpan(7, 19, 7, 23).WithArguments("MyApp.SampleViewModel", "Name"),
66+
});
67+
68+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
69+
{
70+
// /0/Test0.cs(6,24): error CS9248: Partial property 'C.I' must have an implementation part.
71+
DiagnosticResult.CompilerError("CS9248").WithSpan(6, 24, 6, 25).WithArguments("C.I"),
72+
});
73+
74+
await test.RunAsync();
75+
}
76+
}

0 commit comments

Comments
 (0)