Skip to content

Commit 6c13cb5

Browse files
authored
More tests for semi auto prop analyzer
1 parent ef36a6d commit 6c13cb5

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/Test_UseObservablePropertyOnSemiAutoPropertyCodeFixer.cs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,120 @@ public partial class SampleViewModel : ObservableObject
7474
await test.RunAsync();
7575
}
7676

77+
[TestMethod]
78+
public async Task SimplePropertyWithBlockAccessorSyntax()
79+
{
80+
string original = """
81+
using CommunityToolkit.Mvvm.ComponentModel;
82+
83+
namespace MyApp;
84+
85+
public class SampleViewModel : ObservableObject
86+
{
87+
public string Name
88+
{
89+
get
90+
{
91+
return field;
92+
}
93+
set => SetProperty(ref field, value);
94+
}
95+
}
96+
""";
97+
98+
string @fixed = """
99+
using CommunityToolkit.Mvvm.ComponentModel;
100+
101+
namespace MyApp;
102+
103+
public partial class SampleViewModel : ObservableObject
104+
{
105+
[ObservableProperty]
106+
public partial string Name { get; set; }
107+
}
108+
""";
109+
110+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
111+
{
112+
TestCode = original,
113+
FixedCode = @fixed,
114+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
115+
};
116+
117+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
118+
test.ExpectedDiagnostics.AddRange(new[]
119+
{
120+
// /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)
121+
CSharpCodeFixVerifier.Diagnostic().WithSpan(7, 19, 7, 23).WithArguments("MyApp.SampleViewModel", "Name"),
122+
});
123+
124+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
125+
{
126+
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.Name' must have an implementation part.
127+
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 31).WithArguments("MyApp.SampleViewModel.Name"),
128+
});
129+
130+
await test.RunAsync();
131+
}
132+
133+
[TestMethod]
134+
public async Task SimplePropertyWithNestedBlockSyntax()
135+
{
136+
string original = """
137+
using CommunityToolkit.Mvvm.ComponentModel;
138+
139+
namespace MyApp;
140+
141+
public class SampleViewModel : ObservableObject
142+
{
143+
public string Name
144+
{
145+
get
146+
{
147+
{
148+
return field;
149+
}
150+
}
151+
set => SetProperty(ref field, value);
152+
}
153+
}
154+
""";
155+
156+
string @fixed = """
157+
using CommunityToolkit.Mvvm.ComponentModel;
158+
159+
namespace MyApp;
160+
161+
public partial class SampleViewModel : ObservableObject
162+
{
163+
[ObservableProperty]
164+
public partial string Name { get; set; }
165+
}
166+
""";
167+
168+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
169+
{
170+
TestCode = original,
171+
FixedCode = @fixed,
172+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
173+
};
174+
175+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
176+
test.ExpectedDiagnostics.AddRange(new[]
177+
{
178+
// /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)
179+
CSharpCodeFixVerifier.Diagnostic().WithSpan(7, 19, 7, 23).WithArguments("MyApp.SampleViewModel", "Name"),
180+
});
181+
182+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
183+
{
184+
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.Name' must have an implementation part.
185+
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 31).WithArguments("MyApp.SampleViewModel.Name"),
186+
});
187+
188+
await test.RunAsync();
189+
}
190+
77191
[TestMethod]
78192
public async Task SimpleProperty_WithSemicolonTokenGetAccessor()
79193
{

0 commit comments

Comments
 (0)