Skip to content

Commit fab668d

Browse files
committed
Add unit test for 'required' fields
1 parent bf49905 commit fab668d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,51 @@ partial class C : ObservableObject
650650

651651
await test.RunAsync();
652652
}
653+
654+
// See https://github.com/CommunityToolkit/dotnet/issues/971
655+
[TestMethod]
656+
public async Task SimpleField_WithNoReferences_WithRequiredModifier()
657+
{
658+
string original = """
659+
using CommunityToolkit.Mvvm.ComponentModel;
660+
661+
partial class C : ObservableObject
662+
{
663+
[ObservableProperty]
664+
internal required string foo;
665+
}
666+
""";
667+
668+
string @fixed = """
669+
using CommunityToolkit.Mvvm.ComponentModel;
670+
671+
partial class C : ObservableObject
672+
{
673+
[ObservableProperty]
674+
public required partial string Foo { get; set; }
675+
}
676+
""";
677+
678+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
679+
{
680+
TestCode = original,
681+
FixedCode = @fixed,
682+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
683+
};
684+
685+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
686+
test.ExpectedDiagnostics.AddRange(new[]
687+
{
688+
// /0/Test0.cs(5,6): info MVVMTK0042: The field C.C.foo 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)
689+
CSharpCodeFixVerifier.Diagnostic().WithSpan(5, 6, 5, 24).WithArguments("C", "C.foo"),
690+
});
691+
692+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
693+
{
694+
// /0/Test0.cs(6,36): error CS9248: Partial property 'C.Foo' must have an implementation part.
695+
DiagnosticResult.CompilerError("CS9248").WithSpan(6, 36, 6, 39).WithArguments("C.Foo"),
696+
});
697+
698+
await test.RunAsync();
699+
}
653700
}

0 commit comments

Comments
 (0)