@@ -22,7 +22,6 @@ public class Test_SourceGeneratorsCodegen
22
22
public void ObservablePropertyWithPartialMethodWithPreviousValuesNotUsed_DoesNotGenerateFieldReadAndMarksOldValueAsNullable ( )
23
23
{
24
24
string source = """
25
- using System.ComponentModel;
26
25
using CommunityToolkit.Mvvm.ComponentModel;
27
26
28
27
#nullable enable
@@ -36,6 +35,7 @@ partial class MyViewModel : ObservableObject
36
35
}
37
36
""" ;
38
37
38
+ #if NET6_0_OR_GREATER
39
39
string result = """
40
40
// <auto-generated/>
41
41
#pragma warning disable
@@ -50,6 +50,7 @@ partial class MyViewModel
50
50
public string Name
51
51
{
52
52
get => name;
53
+ [global::System.Diagnostics.CodeAnalysis.MemberNotNull("name")]
53
54
set
54
55
{
55
56
if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(name, value))
@@ -90,6 +91,213 @@ public string Name
90
91
}
91
92
}
92
93
""" ;
94
+ #else
95
+ string result = """
96
+ // <auto-generated/>
97
+ #pragma warning disable
98
+ #nullable enable
99
+ namespace MyApp
100
+ {
101
+ partial class MyViewModel
102
+ {
103
+ /// <inheritdoc cref="name"/>
104
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
105
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
106
+ public string Name
107
+ {
108
+ get => name;
109
+ set
110
+ {
111
+ if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(name, value))
112
+ {
113
+ OnNameChanging(value);
114
+ OnNameChanging(default, value);
115
+ OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
116
+ name = value;
117
+ OnNameChanged(value);
118
+ OnNameChanged(default, value);
119
+ OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
120
+ }
121
+ }
122
+ }
123
+
124
+ /// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
125
+ /// <param name="value">The new property value being set.</param>
126
+ /// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
127
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
128
+ partial void OnNameChanging(string value);
129
+ /// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
130
+ /// <param name="oldValue">The previous property value that is being replaced.</param>
131
+ /// <param name="newValue">The new property value being set.</param>
132
+ /// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
133
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
134
+ partial void OnNameChanging(string? oldValue, string newValue);
135
+ /// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
136
+ /// <param name="value">The new property value that was set.</param>
137
+ /// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
138
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
139
+ partial void OnNameChanged(string value);
140
+ /// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
141
+ /// <param name="oldValue">The previous property value that was replaced.</param>
142
+ /// <param name="newValue">The new property value that was set.</param>
143
+ /// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
144
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
145
+ partial void OnNameChanged(string? oldValue, string newValue);
146
+ }
147
+ }
148
+ """ ;
149
+ #endif
150
+
151
+ VerifyGenerateSources ( source , new [ ] { new ObservablePropertyGenerator ( ) } , ( "MyApp.MyViewModel.g.cs" , result ) ) ;
152
+ }
153
+
154
+ [ TestMethod ]
155
+ public void ObservablePropertyWithNullableReferenceType_DoesNotEmitMemberNotNullAttribute ( )
156
+ {
157
+ string source = """
158
+ using CommunityToolkit.Mvvm.ComponentModel;
159
+
160
+ #nullable enable
161
+
162
+ namespace MyApp;
163
+
164
+ partial class MyViewModel : ObservableObject
165
+ {
166
+ [ObservableProperty]
167
+ private string? name;
168
+ }
169
+ """ ;
170
+
171
+ string result = """
172
+ // <auto-generated/>
173
+ #pragma warning disable
174
+ #nullable enable
175
+ namespace MyApp
176
+ {
177
+ partial class MyViewModel
178
+ {
179
+ /// <inheritdoc cref="name"/>
180
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
181
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
182
+ public string? Name
183
+ {
184
+ get => name;
185
+ set
186
+ {
187
+ if (!global::System.Collections.Generic.EqualityComparer<string?>.Default.Equals(name, value))
188
+ {
189
+ OnNameChanging(value);
190
+ OnNameChanging(default, value);
191
+ OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
192
+ name = value;
193
+ OnNameChanged(value);
194
+ OnNameChanged(default, value);
195
+ OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
196
+ }
197
+ }
198
+ }
199
+
200
+ /// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
201
+ /// <param name="value">The new property value being set.</param>
202
+ /// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
203
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
204
+ partial void OnNameChanging(string? value);
205
+ /// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
206
+ /// <param name="oldValue">The previous property value that is being replaced.</param>
207
+ /// <param name="newValue">The new property value being set.</param>
208
+ /// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
209
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
210
+ partial void OnNameChanging(string? oldValue, string? newValue);
211
+ /// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
212
+ /// <param name="value">The new property value that was set.</param>
213
+ /// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
214
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
215
+ partial void OnNameChanged(string? value);
216
+ /// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
217
+ /// <param name="oldValue">The previous property value that was replaced.</param>
218
+ /// <param name="newValue">The new property value that was set.</param>
219
+ /// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
220
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
221
+ partial void OnNameChanged(string? oldValue, string? newValue);
222
+ }
223
+ }
224
+ """ ;
225
+
226
+ VerifyGenerateSources ( source , new [ ] { new ObservablePropertyGenerator ( ) } , ( "MyApp.MyViewModel.g.cs" , result ) ) ;
227
+ }
228
+
229
+ [ TestMethod ]
230
+ public void ObservablePropertyWithNonNullableValueType_DoesNotEmitMemberNotNullAttribute ( )
231
+ {
232
+ string source = """
233
+ using System;
234
+ using CommunityToolkit.Mvvm.ComponentModel;
235
+
236
+ #nullable enable
237
+
238
+ namespace MyApp;
239
+
240
+ partial class MyViewModel : ObservableObject
241
+ {
242
+ [ObservableProperty]
243
+ private Guid id;
244
+ }
245
+ """ ;
246
+
247
+ string result = """
248
+ // <auto-generated/>
249
+ #pragma warning disable
250
+ #nullable enable
251
+ namespace MyApp
252
+ {
253
+ partial class MyViewModel
254
+ {
255
+ /// <inheritdoc cref="id"/>
256
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
257
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
258
+ public global::System.Guid Id
259
+ {
260
+ get => id;
261
+ set
262
+ {
263
+ if (!global::System.Collections.Generic.EqualityComparer<global::System.Guid>.Default.Equals(id, value))
264
+ {
265
+ OnIdChanging(value);
266
+ OnIdChanging(default, value);
267
+ OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Id);
268
+ id = value;
269
+ OnIdChanged(value);
270
+ OnIdChanged(default, value);
271
+ OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Id);
272
+ }
273
+ }
274
+ }
275
+
276
+ /// <summary>Executes the logic for when <see cref="Id"/> is changing.</summary>
277
+ /// <param name="value">The new property value being set.</param>
278
+ /// <remarks>This method is invoked right before the value of <see cref="Id"/> is changed.</remarks>
279
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
280
+ partial void OnIdChanging(global::System.Guid value);
281
+ /// <summary>Executes the logic for when <see cref="Id"/> is changing.</summary>
282
+ /// <param name="oldValue">The previous property value that is being replaced.</param>
283
+ /// <param name="newValue">The new property value being set.</param>
284
+ /// <remarks>This method is invoked right before the value of <see cref="Id"/> is changed.</remarks>
285
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
286
+ partial void OnIdChanging(global::System.Guid oldValue, global::System.Guid newValue);
287
+ /// <summary>Executes the logic for when <see cref="Id"/> just changed.</summary>
288
+ /// <param name="value">The new property value that was set.</param>
289
+ /// <remarks>This method is invoked right after the value of <see cref="Id"/> is changed.</remarks>
290
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
291
+ partial void OnIdChanged(global::System.Guid value);
292
+ /// <summary>Executes the logic for when <see cref="Id"/> just changed.</summary>
293
+ /// <param name="oldValue">The previous property value that was replaced.</param>
294
+ /// <param name="newValue">The new property value that was set.</param>
295
+ /// <remarks>This method is invoked right after the value of <see cref="Id"/> is changed.</remarks>
296
+ [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
297
+ partial void OnIdChanged(global::System.Guid oldValue, global::System.Guid newValue);
298
+ }
299
+ }
300
+ """ ;
93
301
94
302
VerifyGenerateSources ( source , new [ ] { new ObservablePropertyGenerator ( ) } , ( "MyApp.MyViewModel.g.cs" , result ) ) ;
95
303
}
0 commit comments