Skip to content

Commit d61796f

Browse files
committed
Add unit tests for abstract source generation targets
1 parent 54baa39 commit d61796f

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_IRecipientGenerator.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Linq;
7+
using System.Reflection;
68
using CommunityToolkit.Mvvm.Messaging;
79
using Microsoft.VisualStudio.TestTools.UnitTesting;
810

@@ -52,9 +54,20 @@ public void Test_IRecipientGenerator_TypeWithMultipleClassDeclarations()
5254
_ = Messaging.__Internals.__IMessengerExtensions.CreateAllMessagesRegistratorWithToken<int>(recipient);
5355
}
5456

55-
public sealed class RecipientWithSomeMessages :
56-
IRecipient<MessageA>,
57-
IRecipient<MessageB>
57+
[TestMethod]
58+
public void Test_IRecipientGenerator_AbstractTypesDoNotTriggerCodeGeneration()
59+
{
60+
MethodInfo? createAllPropertiesValidatorMethod = typeof(Messaging.__Internals.__IMessengerExtensions)
61+
.GetMethods(BindingFlags.Static | BindingFlags.Public)
62+
.Where(static m => m.Name == "CreateAllMessagesRegistratorWithToken")
63+
.Where(static m => m.GetParameters() is { Length: 1 } parameters && parameters[0].ParameterType == typeof(AbstractModelWithValidatablePropertyIRecipientInterfaces))
64+
.FirstOrDefault();
65+
66+
// We need to validate that no methods are generated for abstract types, so we just check this method doesn't exist
67+
Assert.IsNull(createAllPropertiesValidatorMethod);
68+
}
69+
70+
public sealed class RecipientWithSomeMessages : IRecipient<MessageA>, IRecipient<MessageB>
5871
{
5972
public MessageA? A { get; private set; }
6073

@@ -91,4 +104,14 @@ public void Receive(MessageA message)
91104
partial class RecipientWithMultipleClassDeclarations
92105
{
93106
}
107+
108+
public abstract class AbstractModelWithValidatablePropertyIRecipientInterfaces : IRecipient<MessageA>, IRecipient<MessageB>
109+
{
110+
public abstract void Receive(MessageA message);
111+
112+
public void Receive(MessageB message)
113+
{
114+
115+
}
116+
}
94117
}

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservableValidator.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,19 @@ public void Test_ObservableValidator_VerifyTrimmingAnnotation()
526526
#endif
527527
}
528528

529+
[TestMethod]
530+
public void Test_ObservableRecipient_AbstractTypesDoNotTriggerCodeGeneration()
531+
{
532+
MethodInfo? createAllPropertiesValidatorMethod = typeof(ComponentModel.__Internals.__ObservableValidatorExtensions)
533+
.GetMethods(BindingFlags.Static | BindingFlags.Public)
534+
.Where(static m => m.Name == "CreateAllPropertiesValidator")
535+
.Where(static m => m.GetParameters() is { Length: 1 } parameters && parameters[0].ParameterType == typeof(AbstractModelWithValidatableProperty))
536+
.FirstOrDefault();
537+
538+
// We need to validate that no methods are generated for abstract types, so we just check this method doesn't exist
539+
Assert.IsNull(createAllPropertiesValidatorMethod);
540+
}
541+
529542
public class Person : ObservableValidator
530543
{
531544
private string? name;
@@ -777,4 +790,11 @@ public partial class PersonWithPartialDeclaration
777790
[Range(10, 1000)]
778791
public int Number { get; set; }
779792
}
793+
794+
public abstract class AbstractModelWithValidatableProperty : ObservableValidator
795+
{
796+
[Required]
797+
[MinLength(2)]
798+
public string? Name { get; set; }
799+
}
780800
}

0 commit comments

Comments
 (0)