Skip to content

Commit efea2bf

Browse files
committed
Add unit tests for new nullable annotations
1 parent 9abe628 commit efea2bf

File tree

1 file changed

+161
-1
lines changed

1 file changed

+161
-1
lines changed

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsCodegen.cs

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2301,7 +2301,7 @@ partial class MyViewModel : ObservableObject
23012301
[ObservableProperty]
23022302
double @object;
23032303
2304-
partial void OnObjectChanged(object oldValue, object NewValue)
2304+
partial void OnObjectChanged(double oldValue, double NewValue)
23052305
{
23062306
}
23072307
}
@@ -3018,6 +3018,166 @@ public string Name
30183018
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
30193019
}
30203020

3021+
[TestMethod]
3022+
public void ObservableProperty_NotNullablePropert_OfReferenceType_WithChangedMethods()
3023+
{
3024+
string source = """
3025+
using CommunityToolkit.Mvvm.ComponentModel;
3026+
3027+
namespace MyApp;
3028+
3029+
partial class MyViewModel : ObservableObject
3030+
{
3031+
[ObservableProperty]
3032+
string name;
3033+
3034+
partial void OnNameChanged(string? oldValue, string NewValue)
3035+
{
3036+
}
3037+
}
3038+
""";
3039+
3040+
string result = """
3041+
// <auto-generated/>
3042+
#pragma warning disable
3043+
#nullable enable
3044+
namespace MyApp
3045+
{
3046+
/// <inheritdoc/>
3047+
partial class MyViewModel
3048+
{
3049+
/// <inheritdoc cref="name"/>
3050+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3051+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
3052+
public string Name
3053+
{
3054+
get => name;
3055+
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("name")]
3056+
set
3057+
{
3058+
if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(name, value))
3059+
{
3060+
string? __oldValue = name;
3061+
OnNameChanging(value);
3062+
OnNameChanging(__oldValue, value);
3063+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
3064+
name = value;
3065+
OnNameChanged(value);
3066+
OnNameChanged(__oldValue, value);
3067+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
3068+
}
3069+
}
3070+
}
3071+
3072+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
3073+
/// <param name="value">The new property value being set.</param>
3074+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
3075+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3076+
partial void OnNameChanging(string value);
3077+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
3078+
/// <param name="oldValue">The previous property value that is being replaced.</param>
3079+
/// <param name="newValue">The new property value being set.</param>
3080+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
3081+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3082+
partial void OnNameChanging(string? oldValue, string newValue);
3083+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
3084+
/// <param name="value">The new property value that was set.</param>
3085+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
3086+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3087+
partial void OnNameChanged(string value);
3088+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
3089+
/// <param name="oldValue">The previous property value that was replaced.</param>
3090+
/// <param name="newValue">The new property value that was set.</param>
3091+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
3092+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3093+
partial void OnNameChanged(string? oldValue, string newValue);
3094+
}
3095+
}
3096+
""";
3097+
3098+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
3099+
}
3100+
3101+
[TestMethod]
3102+
public void ObservableProperty_NotNullablePropert_OfUnconstrainedGenericType_WithChangedMethods()
3103+
{
3104+
string source = """
3105+
using CommunityToolkit.Mvvm.ComponentModel;
3106+
3107+
namespace MyApp;
3108+
3109+
partial class MyViewModel<T> : ObservableObject
3110+
{
3111+
[ObservableProperty]
3112+
T value;
3113+
3114+
partial void OnValueChanged(T? oldValue, T NewValue)
3115+
{
3116+
}
3117+
}
3118+
""";
3119+
3120+
string result = """
3121+
// <auto-generated/>
3122+
#pragma warning disable
3123+
#nullable enable
3124+
namespace MyApp
3125+
{
3126+
/// <inheritdoc/>
3127+
partial class MyViewModel<T>
3128+
{
3129+
/// <inheritdoc cref="value"/>
3130+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3131+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
3132+
public T Value
3133+
{
3134+
get => value;
3135+
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("value")]
3136+
set
3137+
{
3138+
if (!global::System.Collections.Generic.EqualityComparer<T>.Default.Equals(this.value, value))
3139+
{
3140+
T? __oldValue = this.value;
3141+
OnValueChanging(value);
3142+
OnValueChanging(__oldValue, value);
3143+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Value);
3144+
this.value = value;
3145+
OnValueChanged(value);
3146+
OnValueChanged(__oldValue, value);
3147+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Value);
3148+
}
3149+
}
3150+
}
3151+
3152+
/// <summary>Executes the logic for when <see cref="Value"/> is changing.</summary>
3153+
/// <param name="value">The new property value being set.</param>
3154+
/// <remarks>This method is invoked right before the value of <see cref="Value"/> is changed.</remarks>
3155+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3156+
partial void OnValueChanging(T value);
3157+
/// <summary>Executes the logic for when <see cref="Value"/> is changing.</summary>
3158+
/// <param name="oldValue">The previous property value that is being replaced.</param>
3159+
/// <param name="newValue">The new property value being set.</param>
3160+
/// <remarks>This method is invoked right before the value of <see cref="Value"/> is changed.</remarks>
3161+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3162+
partial void OnValueChanging(T? oldValue, T newValue);
3163+
/// <summary>Executes the logic for when <see cref="Value"/> just changed.</summary>
3164+
/// <param name="value">The new property value that was set.</param>
3165+
/// <remarks>This method is invoked right after the value of <see cref="Value"/> is changed.</remarks>
3166+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3167+
partial void OnValueChanged(T value);
3168+
/// <summary>Executes the logic for when <see cref="Value"/> just changed.</summary>
3169+
/// <param name="oldValue">The previous property value that was replaced.</param>
3170+
/// <param name="newValue">The new property value that was set.</param>
3171+
/// <remarks>This method is invoked right after the value of <see cref="Value"/> is changed.</remarks>
3172+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3173+
partial void OnValueChanged(T? oldValue, T newValue);
3174+
}
3175+
}
3176+
""";
3177+
3178+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel`1.g.cs", result));
3179+
}
3180+
30213181
/// <summary>
30223182
/// Generates the requested sources
30233183
/// </summary>

0 commit comments

Comments
 (0)