- Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathTest_IRecipientGenerator.cs
117 lines (89 loc) · 3.67 KB
/
Test_IRecipientGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
usingSystem;
usingSystem.Linq;
usingSystem.Reflection;
usingCommunityToolkit.Mvvm.Messaging;
usingMicrosoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable CS0618
namespaceCommunityToolkit.Mvvm.UnitTests;
[TestClass]
publicpartialclassTest_IRecipientGenerator
{
[TestMethod]
publicvoidTest_IRecipientGenerator_GeneratedRegistration()
{
StrongReferenceMessengermessenger=new();
RecipientWithSomeMessagesrecipient=new();
MessageAmessageA=new();
MessageBmessageB=new();
Action<IMessenger,object,int>registrator=Messaging.__Internals.__IMessengerExtensions.CreateAllMessagesRegistratorWithToken<int>(recipient);
registrator(messenger,recipient,42);
Assert.IsTrue(messenger.IsRegistered<MessageA,int>(recipient,42));
Assert.IsTrue(messenger.IsRegistered<MessageB,int>(recipient,42));
Assert.IsNull(recipient.A);
Assert.IsNull(recipient.B);
_=messenger.Send(messageA,42);
Assert.AreSame(recipient.A,messageA);
Assert.IsNull(recipient.B);
_=messenger.Send(messageB,42);
Assert.AreSame(recipient.A,messageA);
Assert.AreSame(recipient.B,messageB);
}
[TestMethod]
publicvoidTest_IRecipientGenerator_TypeWithMultipleClassDeclarations()
{
RecipientWithMultipleClassDeclarationsrecipient=new();
// This test really just needs to verify this compiles and executes normally
_=Messaging.__Internals.__IMessengerExtensions.CreateAllMessagesRegistratorWithToken<int>(recipient);
}
[TestMethod]
publicvoidTest_IRecipientGenerator_AbstractTypesDoNotTriggerCodeGeneration()
{
MethodInfo?createAllPropertiesValidatorMethod=typeof(Messaging.__Internals.__IMessengerExtensions)
.GetMethods(BindingFlags.Static|BindingFlags.Public)
.Where(static m =>m.Name=="CreateAllMessagesRegistratorWithToken")
.Where(static m =>m.GetParameters()is{Length:1}parameters&¶meters[0].ParameterType==typeof(AbstractModelWithValidatablePropertyIRecipientInterfaces))
.FirstOrDefault();
// We need to validate that no methods are generated for abstract types, so we just check this method doesn't exist
Assert.IsNull(createAllPropertiesValidatorMethod);
}
publicsealedclassRecipientWithSomeMessages:IRecipient<MessageA>,IRecipient<MessageB>
{
publicMessageA?A{get;privateset;}
publicMessageB?B{get;privateset;}
publicvoidReceive(MessageAmessage)
{
A=message;
}
publicvoidReceive(MessageBmessage)
{
B=message;
}
}
publicsealedclassMessageA
{
}
publicsealedclassMessageB
{
}
publicsealedpartialclassRecipientWithMultipleClassDeclarations:IRecipient<MessageA>
{
publicvoidReceive(MessageAmessage)
{
}
}
// This empty partial type declarations needs to be present to ensure the generator
// correctly handles cases where the source type has multiple class declarations.
partialclassRecipientWithMultipleClassDeclarations
{
}
publicabstractclassAbstractModelWithValidatablePropertyIRecipientInterfaces:IRecipient<MessageA>,IRecipient<MessageB>
{
publicabstractvoidReceive(MessageAmessage);
publicvoidReceive(MessageBmessage)
{
}
}
}