Skip to content

Commit 1bfc5a4

Browse files
committed
Add unit tests for inheritance scenarios
1 parent fb0facd commit 1bfc5a4

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

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

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,119 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomProperty
747747
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
748748
}
749749

750+
[TestMethod]
751+
public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_TargetingWindows_NotIncluded_DoesNotWarn()
752+
{
753+
const string source = """
754+
using System;
755+
using CommunityToolkit.Mvvm.ComponentModel;
756+
using CommunityToolkit.Mvvm.Input;
757+
using WinRT;
758+
759+
namespace MyApp
760+
{
761+
[GeneratedBindableCustomProperty(["Name"], [])]
762+
public partial class SampleViewModel : ObservableObject
763+
{
764+
[ObservableProperty]
765+
private string name;
766+
767+
[RelayCommand]
768+
private void DoStuff()
769+
{
770+
}
771+
}
772+
}
773+
774+
namespace WinRT
775+
{
776+
public class GeneratedBindableCustomPropertyAttribute(string[] a, string[] b) : Attribute
777+
{
778+
}
779+
}
780+
""";
781+
782+
await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer>.VerifyAnalyzerAsync(
783+
source,
784+
LanguageVersion.CSharp12,
785+
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
786+
}
787+
788+
[TestMethod]
789+
public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_TargetingWindows_BaseType_Field_NotIncluded_DoesNotWarn()
790+
{
791+
const string source = """
792+
using System;
793+
using CommunityToolkit.Mvvm.ComponentModel;
794+
using WinRT;
795+
796+
namespace MyApp
797+
{
798+
[GeneratedBindableCustomProperty(["OtherName"], [])]
799+
public partial class SampleViewModel : BaseViewModel
800+
{
801+
}
802+
803+
public partial class BaseViewModel : ObservableObject
804+
{
805+
[ObservableProperty]
806+
private string name;
807+
}
808+
}
809+
810+
namespace WinRT
811+
{
812+
public class GeneratedBindableCustomPropertyAttribute(string[] a, string[] b) : Attribute
813+
{
814+
}
815+
}
816+
""";
817+
818+
await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer>.VerifyAnalyzerAsync(
819+
source,
820+
LanguageVersion.CSharp12,
821+
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
822+
}
823+
824+
[TestMethod]
825+
public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_TargetingWindows_BaseType_Method_NotIncluded_DoesNotWarn()
826+
{
827+
const string source = """
828+
using System;
829+
using CommunityToolkit.Mvvm.ComponentModel;
830+
using CommunityToolkit.Mvvm.Input;
831+
using WinRT;
832+
833+
namespace MyApp
834+
{
835+
[GeneratedBindableCustomProperty(["OtherMethod"], [])]
836+
public partial class SampleViewModel : BaseViewModel
837+
{
838+
}
839+
840+
public partial class BaseViewModel : ObservableObject
841+
{
842+
[RelayCommand]
843+
private void DoStuff()
844+
{
845+
}
846+
}
847+
}
848+
849+
namespace WinRT
850+
{
851+
public class GeneratedBindableCustomPropertyAttribute(string[] a, string[] b) : Attribute
852+
{
853+
}
854+
}
855+
""";
856+
857+
await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer>.VerifyAnalyzerAsync(
858+
source,
859+
LanguageVersion.CSharp12,
860+
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
861+
}
862+
750863
[TestMethod]
751864
public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_TargetingWindows_BaseType_Field_Warns()
752865
{
@@ -781,6 +894,42 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomProperty
781894
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
782895
}
783896

897+
[TestMethod]
898+
public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_TargetingWindows_BaseType_Field_Included_Warns()
899+
{
900+
const string source = """
901+
using System;
902+
using CommunityToolkit.Mvvm.ComponentModel;
903+
using WinRT;
904+
905+
namespace MyApp
906+
{
907+
[GeneratedBindableCustomProperty(["Name"], [])]
908+
public partial class {|MVVMTK0047:SampleViewModel|} : BaseViewModel
909+
{
910+
}
911+
912+
public partial class BaseViewModel : ObservableObject
913+
{
914+
[ObservableProperty]
915+
private string name;
916+
}
917+
}
918+
919+
namespace WinRT
920+
{
921+
public class GeneratedBindableCustomPropertyAttribute(string[] a, string[] b) : Attribute
922+
{
923+
}
924+
}
925+
""";
926+
927+
await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer>.VerifyAnalyzerAsync(
928+
source,
929+
LanguageVersion.CSharp12,
930+
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
931+
}
932+
784933
[TestMethod]
785934
public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_TargetingWindows_BaseType_Method_Warns()
786935
{
@@ -818,6 +967,45 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomProperty
818967
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
819968
}
820969

970+
[TestMethod]
971+
public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_TargetingWindows_BaseType_Method_Included_Warns()
972+
{
973+
const string source = """
974+
using System;
975+
using CommunityToolkit.Mvvm.ComponentModel;
976+
using CommunityToolkit.Mvvm.Input;
977+
using WinRT;
978+
979+
namespace MyApp
980+
{
981+
[GeneratedBindableCustomProperty(["DoStuffCommand"], [])]
982+
public partial class {|MVVMTK0048:SampleViewModel|} : BaseViewModel
983+
{
984+
}
985+
986+
public partial class BaseViewModel : ObservableObject
987+
{
988+
[RelayCommand]
989+
private void DoStuff()
990+
{
991+
}
992+
}
993+
}
994+
995+
namespace WinRT
996+
{
997+
public class GeneratedBindableCustomPropertyAttribute(string[] a, string[] b) : Attribute
998+
{
999+
}
1000+
}
1001+
""";
1002+
1003+
await CSharpAnalyzerWithLanguageVersionTest<WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer>.VerifyAnalyzerAsync(
1004+
source,
1005+
LanguageVersion.CSharp12,
1006+
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
1007+
}
1008+
8211009
[TestMethod]
8221010
public async Task InvalidPartialPropertyLevelObservablePropertyAttributeAnalyzer_OnValidProperty_DoesNotWarn()
8231011
{

0 commit comments

Comments
 (0)