@@ -254,6 +254,25 @@ public partial class SampleViewModel : ObservableObject
254
254
}
255
255
""" ;
256
256
257
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview ) ;
258
+ }
259
+
260
+ [ TestMethod ]
261
+ public async Task InvalidPropertyLevelObservablePropertyAttributeAnalyzer_OnStaticProperty_Warns ( )
262
+ {
263
+ const string source = """
264
+ using CommunityToolkit.Mvvm.ComponentModel;
265
+
266
+ namespace MyApp
267
+ {
268
+ public partial class SampleViewModel : ObservableObject
269
+ {
270
+ [{|MVVMTK0043:ObservableProperty|}]
271
+ public static partial string {|CS9248:Name|} { get; set; }
272
+ }
273
+ }
274
+ """ ;
275
+
257
276
await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview , [ ] , [ "CS9248" ] ) ;
258
277
}
259
278
@@ -798,4 +817,185 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomProperty
798
817
LanguageVersion . CSharp12 ,
799
818
editorconfig : [ ( "_MvvmToolkitIsUsingWindowsRuntimePack" , true ) ] ) ;
800
819
}
820
+
821
+ [ TestMethod ]
822
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_OnValidProperty_DoesNotWarn ( )
823
+ {
824
+ const string source = """
825
+ using CommunityToolkit.Mvvm.ComponentModel;
826
+
827
+ namespace MyApp
828
+ {
829
+ public partial class SampleViewModel : ObservableObject
830
+ {
831
+ [ObservableProperty]
832
+ public partial string {|CS9248:Name|} { get; set; }
833
+ }
834
+ }
835
+ """ ;
836
+
837
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview , [ ] , [ "CS9248" ] ) ;
838
+ }
839
+
840
+ [ TestMethod ]
841
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_OnUnannotatedPartialPropertyWithImplementation_DoesNotWarn ( )
842
+ {
843
+ const string source = """
844
+ using CommunityToolkit.Mvvm.ComponentModel;
845
+
846
+ namespace MyApp
847
+ {
848
+ public partial class SampleViewModel : ObservableObject
849
+ {
850
+ public partial string Name { get; set; }
851
+
852
+ public partial string Name
853
+ {
854
+ get => field;
855
+ set { }
856
+ }
857
+ }
858
+ }
859
+ """ ;
860
+
861
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview ) ;
862
+ }
863
+
864
+ [ TestMethod ]
865
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_OnImplementedProperty_GeneratedByMvvmToolkitGenerator_DoesNotWarn ( )
866
+ {
867
+ const string source = """
868
+ using System.CodeDom.Compiler;
869
+ using CommunityToolkit.Mvvm.ComponentModel;
870
+
871
+ namespace MyApp
872
+ {
873
+ public partial class SampleViewModel : ObservableObject
874
+ {
875
+ [ObservableProperty]
876
+ public partial string Name { get; set; }
877
+
878
+ [GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "1.0.0")]
879
+ public partial string Name
880
+ {
881
+ get => field;
882
+ set { }
883
+ }
884
+ }
885
+ }
886
+ """ ;
887
+
888
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview ) ;
889
+ }
890
+
891
+ [ TestMethod ]
892
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_OnImplementedProperty_GeneratedByAnotherGenerator_Warns ( )
893
+ {
894
+ const string source = """
895
+ using System.CodeDom.Compiler;
896
+ using CommunityToolkit.Mvvm.ComponentModel;
897
+
898
+ namespace MyApp
899
+ {
900
+ public partial class SampleViewModel : ObservableObject
901
+ {
902
+ [{|MVVMTK0052:ObservableProperty|}]
903
+ public partial string Name { get; set; }
904
+
905
+ [GeneratedCode("Some.Other.Generator", "1.0.0")]
906
+ public partial string Name
907
+ {
908
+ get => field;
909
+ set { }
910
+ }
911
+ }
912
+ }
913
+ """ ;
914
+
915
+ // This test is having issues, let's invoke the analyzer directly to make it easier to narrow down the problem
916
+ await CSharpAnalyzerWithLanguageVersionTest < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > . VerifyAnalyzerAsync ( source , LanguageVersion . Preview ) ;
917
+ }
918
+
919
+ [ TestMethod ]
920
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_OnImplementedProperty_Warns ( )
921
+ {
922
+ const string source = """
923
+ using CommunityToolkit.Mvvm.ComponentModel;
924
+
925
+ namespace MyApp
926
+ {
927
+ public partial class SampleViewModel : ObservableObject
928
+ {
929
+ [{|MVVMTK0052:ObservableProperty|}]
930
+ public partial string Name { get; set; }
931
+
932
+ public partial string Name
933
+ {
934
+ get => field;
935
+ set { }
936
+ }
937
+ }
938
+ }
939
+ """ ;
940
+
941
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview ) ;
942
+ }
943
+
944
+ [ TestMethod ]
945
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_ReturnsByRef_Warns ( )
946
+ {
947
+ const string source = """
948
+ using CommunityToolkit.Mvvm.ComponentModel;
949
+
950
+ namespace MyApp
951
+ {
952
+ public partial class SampleViewModel : ObservableObject
953
+ {
954
+ [{|MVVMTK0053:ObservableProperty|}]
955
+ public partial ref int {|CS9248:Name|} { get; {|CS8147:set|}; }
956
+ }
957
+ }
958
+ """ ;
959
+
960
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview , [ ] , [ "CS8147" , "CS9248" ] ) ;
961
+ }
962
+
963
+ [ TestMethod ]
964
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_ReturnsByRefReadOnly_Warns ( )
965
+ {
966
+ const string source = """
967
+ using CommunityToolkit.Mvvm.ComponentModel;
968
+
969
+ namespace MyApp
970
+ {
971
+ public partial class SampleViewModel : ObservableObject
972
+ {
973
+ [{|MVVMTK0053:ObservableProperty|}]
974
+ public partial ref readonly int {|CS9248:Name|} { get; {|CS8147:set|}; }
975
+ }
976
+ }
977
+ """ ;
978
+
979
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview , [ ] , [ "CS8147" , "CS9248" ] ) ;
980
+ }
981
+
982
+ [ TestMethod ]
983
+ public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_ReturnsByRefLike_Warns ( )
984
+ {
985
+ const string source = """
986
+ using System;
987
+ using CommunityToolkit.Mvvm.ComponentModel;
988
+
989
+ namespace MyApp
990
+ {
991
+ public partial class SampleViewModel : ObservableObject
992
+ {
993
+ [{|MVVMTK0054:ObservableProperty|}]
994
+ public partial Span<char> {|CS9248:Name|} { get; set; }
995
+ }
996
+ }
997
+ """ ;
998
+
999
+ await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration < InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer > ( source , LanguageVersion . Preview , [ ] , [ "CS9248" ] ) ;
1000
+ }
801
1001
}
0 commit comments