Skip to content

Commit 6b83f1a

Browse files
committed
Add unit test for 'this.' expressions
1 parent 0c97992 commit 6b83f1a

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
@@ -650,4 +650,74 @@ partial class C : ObservableObject
650650

651651
await test.RunAsync();
652652
}
653+
654+
[TestMethod]
655+
public async Task SimpleFieldWithSomeReferences_WithSomeThisExpressions()
656+
{
657+
string original = """
658+
using CommunityToolkit.Mvvm.ComponentModel;
659+
660+
partial class C : ObservableObject
661+
{
662+
[ObservableProperty]
663+
private int i;
664+
665+
public void M()
666+
{
667+
i = 42;
668+
this.i = 42;
669+
}
670+
671+
public int N() => i;
672+
673+
public int P() => this.i + Q(i) + Q(this.i);
674+
675+
private int Q(int i) => this.i + i;
676+
}
677+
""";
678+
679+
string @fixed = """
680+
using CommunityToolkit.Mvvm.ComponentModel;
681+
682+
partial class C : ObservableObject
683+
{
684+
[ObservableProperty]
685+
public partial int I { get; set; }
686+
687+
public void M()
688+
{
689+
I = 42;
690+
I = 42;
691+
}
692+
693+
public int N() => I;
694+
695+
public int P() => I + Q(I) + Q(I);
696+
697+
private int Q(int i) => I + i;
698+
}
699+
""";
700+
701+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
702+
{
703+
TestCode = original,
704+
FixedCode = @fixed,
705+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
706+
};
707+
708+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
709+
test.ExpectedDiagnostics.AddRange(new[]
710+
{
711+
// /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)
712+
CSharpCodeFixVerifier.Diagnostic().WithSpan(5, 6, 5, 24).WithArguments("C", "C.i"),
713+
});
714+
715+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
716+
{
717+
// /0/Test0.cs(6,24): error CS9248: Partial property 'C.I' must have an implementation part.
718+
DiagnosticResult.CompilerError("CS9248").WithSpan(6, 24, 6, 25).WithArguments("C.I"),
719+
});
720+
721+
await test.RunAsync();
722+
}
653723
}

0 commit comments

Comments
 (0)