Skip to content

Commit 8f480bc

Browse files
committed
Add unit test for 'this.' expressions
1 parent d2ff32c commit 8f480bc

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4110.UnitTests/Test_UsePartialPropertyForObservablePropertyCodeFixer.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,4 +697,74 @@ partial class C : ObservableObject
697697

698698
await test.RunAsync();
699699
}
700+
701+
[TestMethod]
702+
public async Task SimpleFieldWithSomeReferences_WithSomeThisExpressions()
703+
{
704+
string original = """
705+
using CommunityToolkit.Mvvm.ComponentModel;
706+
707+
partial class C : ObservableObject
708+
{
709+
[ObservableProperty]
710+
private int i;
711+
712+
public void M()
713+
{
714+
i = 42;
715+
this.i = 42;
716+
}
717+
718+
public int N() => i;
719+
720+
public int P() => this.i + Q(i) + Q(this.i);
721+
722+
private int Q(int i) => this.i + i;
723+
}
724+
""";
725+
726+
string @fixed = """
727+
using CommunityToolkit.Mvvm.ComponentModel;
728+
729+
partial class C : ObservableObject
730+
{
731+
[ObservableProperty]
732+
public partial int I { get; set; }
733+
734+
public void M()
735+
{
736+
I = 42;
737+
I = 42;
738+
}
739+
740+
public int N() => I;
741+
742+
public int P() => I + Q(I) + Q(I);
743+
744+
private int Q(int i) => I + i;
745+
}
746+
""";
747+
748+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
749+
{
750+
TestCode = original,
751+
FixedCode = @fixed,
752+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
753+
};
754+
755+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
756+
test.ExpectedDiagnostics.AddRange(new[]
757+
{
758+
// /0/Test0.cs(5,6): info MVVMTK0042: The field C.C.i using [ObservableProperty] can be converted to a partial property instead, which is recommended (doing so improves the developer experience and allows other generators and analyzers to correctly see the generated property as well)
759+
CSharpCodeFixVerifier.Diagnostic().WithSpan(5, 6, 5, 24).WithArguments("C", "C.i"),
760+
});
761+
762+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
763+
{
764+
// /0/Test0.cs(6,24): error CS9248: Partial property 'C.I' must have an implementation part.
765+
DiagnosticResult.CompilerError("CS9248").WithSpan(6, 24, 6, 25).WithArguments("C.I"),
766+
});
767+
768+
await test.RunAsync();
769+
}
700770
}

0 commit comments

Comments
 (0)