Skip to content

Commit 2e3ca71

Browse files
committed
Add unit tests for generated trim annotations
1 parent 095ac53 commit 2e3ca71

File tree

1 file changed

+285
-0
lines changed

1 file changed

+285
-0
lines changed

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

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,291 @@ internal static class __KnownINotifyPropertyChangedArgs
27332733
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result), ("__KnownINotifyPropertyChangingArgs.g.cs", null), ("__KnownINotifyPropertyChangedArgs.g.cs", changedArgs));
27342734
}
27352735

2736+
[TestMethod]
2737+
public void ObservableProperty_NotifyDataErrorInfo_EmitsTrimAnnotationsWhenNeeded()
2738+
{
2739+
string source = """
2740+
using System.ComponentModel.DataAnnotations;
2741+
using CommunityToolkit.Mvvm.ComponentModel;
2742+
2743+
#nullable enable
2744+
2745+
namespace MyApp;
2746+
2747+
partial class MyViewModel : ObservableValidator
2748+
{
2749+
[ObservableProperty]
2750+
[NotifyDataErrorInfo]
2751+
[Required]
2752+
private string? name;
2753+
}
2754+
""";
2755+
2756+
#if NET6_0_OR_GREATER
2757+
string result = """
2758+
// <auto-generated/>
2759+
#pragma warning disable
2760+
#nullable enable
2761+
namespace MyApp
2762+
{
2763+
/// <inheritdoc/>
2764+
partial class MyViewModel
2765+
{
2766+
/// <inheritdoc cref="name"/>
2767+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2768+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2769+
[global::System.ComponentModel.DataAnnotations.RequiredAttribute()]
2770+
public string? Name
2771+
{
2772+
get => name;
2773+
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")]
2774+
set
2775+
{
2776+
if (!global::System.Collections.Generic.EqualityComparer<string?>.Default.Equals(name, value))
2777+
{
2778+
OnNameChanging(value);
2779+
OnNameChanging(default, value);
2780+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
2781+
name = value;
2782+
ValidateProperty(value, "Name");
2783+
OnNameChanged(value);
2784+
OnNameChanged(default, value);
2785+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
2786+
}
2787+
}
2788+
}
2789+
2790+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2791+
/// <param name="value">The new property value being set.</param>
2792+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
2793+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2794+
partial void OnNameChanging(string? value);
2795+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2796+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2797+
/// <param name="newValue">The new property value being set.</param>
2798+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
2799+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2800+
partial void OnNameChanging(string? oldValue, string? newValue);
2801+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
2802+
/// <param name="value">The new property value that was set.</param>
2803+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
2804+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2805+
partial void OnNameChanged(string? value);
2806+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
2807+
/// <param name="oldValue">The previous property value that was replaced.</param>
2808+
/// <param name="newValue">The new property value that was set.</param>
2809+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
2810+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2811+
partial void OnNameChanged(string? oldValue, string? newValue);
2812+
}
2813+
}
2814+
""";
2815+
#else
2816+
string result = """
2817+
// <auto-generated/>
2818+
#pragma warning disable
2819+
#nullable enable
2820+
namespace MyApp
2821+
{
2822+
/// <inheritdoc/>
2823+
partial class MyViewModel
2824+
{
2825+
/// <inheritdoc cref="name"/>
2826+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2827+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2828+
[global::System.ComponentModel.DataAnnotations.RequiredAttribute()]
2829+
public string? Name
2830+
{
2831+
get => name;
2832+
set
2833+
{
2834+
if (!global::System.Collections.Generic.EqualityComparer<string?>.Default.Equals(name, value))
2835+
{
2836+
OnNameChanging(value);
2837+
OnNameChanging(default, value);
2838+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
2839+
name = value;
2840+
ValidateProperty(value, "Name");
2841+
OnNameChanged(value);
2842+
OnNameChanged(default, value);
2843+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
2844+
}
2845+
}
2846+
}
2847+
2848+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2849+
/// <param name="value">The new property value being set.</param>
2850+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
2851+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2852+
partial void OnNameChanging(string? value);
2853+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2854+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2855+
/// <param name="newValue">The new property value being set.</param>
2856+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
2857+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2858+
partial void OnNameChanging(string? oldValue, string? newValue);
2859+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
2860+
/// <param name="value">The new property value that was set.</param>
2861+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
2862+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2863+
partial void OnNameChanged(string? value);
2864+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
2865+
/// <param name="oldValue">The previous property value that was replaced.</param>
2866+
/// <param name="newValue">The new property value that was set.</param>
2867+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
2868+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2869+
partial void OnNameChanged(string? oldValue, string? newValue);
2870+
}
2871+
}
2872+
""";
2873+
#endif
2874+
2875+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
2876+
}
2877+
2878+
[TestMethod]
2879+
public void ObservableProperty_NotifyDataErrorInfo_EmitsTrimAnnotationsWhenNeeded_AppendToNullableAttribute()
2880+
{
2881+
string source = """
2882+
using System.ComponentModel.DataAnnotations;
2883+
using CommunityToolkit.Mvvm.ComponentModel;
2884+
2885+
#nullable enable
2886+
2887+
namespace MyApp;
2888+
2889+
partial class MyViewModel : ObservableValidator
2890+
{
2891+
[ObservableProperty]
2892+
[NotifyDataErrorInfo]
2893+
[Required]
2894+
private string name;
2895+
}
2896+
""";
2897+
2898+
#if NET6_0_OR_GREATER
2899+
string result = """
2900+
// <auto-generated/>
2901+
#pragma warning disable
2902+
#nullable enable
2903+
namespace MyApp
2904+
{
2905+
/// <inheritdoc/>
2906+
partial class MyViewModel
2907+
{
2908+
/// <inheritdoc cref="name"/>
2909+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2910+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2911+
[global::System.ComponentModel.DataAnnotations.RequiredAttribute()]
2912+
public string Name
2913+
{
2914+
get => name;
2915+
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("name")]
2916+
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")]
2917+
set
2918+
{
2919+
if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(name, value))
2920+
{
2921+
OnNameChanging(value);
2922+
OnNameChanging(default, value);
2923+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
2924+
name = value;
2925+
ValidateProperty(value, "Name");
2926+
OnNameChanged(value);
2927+
OnNameChanged(default, value);
2928+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
2929+
}
2930+
}
2931+
}
2932+
2933+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2934+
/// <param name="value">The new property value being set.</param>
2935+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
2936+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2937+
partial void OnNameChanging(string value);
2938+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2939+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2940+
/// <param name="newValue">The new property value being set.</param>
2941+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
2942+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2943+
partial void OnNameChanging(string? oldValue, string newValue);
2944+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
2945+
/// <param name="value">The new property value that was set.</param>
2946+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
2947+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2948+
partial void OnNameChanged(string value);
2949+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
2950+
/// <param name="oldValue">The previous property value that was replaced.</param>
2951+
/// <param name="newValue">The new property value that was set.</param>
2952+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
2953+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2954+
partial void OnNameChanged(string? oldValue, string newValue);
2955+
}
2956+
}
2957+
""";
2958+
#else
2959+
string result = """
2960+
// <auto-generated/>
2961+
#pragma warning disable
2962+
#nullable enable
2963+
namespace MyApp
2964+
{
2965+
/// <inheritdoc/>
2966+
partial class MyViewModel
2967+
{
2968+
/// <inheritdoc cref="name"/>
2969+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2970+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2971+
[global::System.ComponentModel.DataAnnotations.RequiredAttribute()]
2972+
public string Name
2973+
{
2974+
get => name;
2975+
set
2976+
{
2977+
if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(name, value))
2978+
{
2979+
OnNameChanging(value);
2980+
OnNameChanging(default, value);
2981+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
2982+
name = value;
2983+
ValidateProperty(value, "Name");
2984+
OnNameChanged(value);
2985+
OnNameChanged(default, value);
2986+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
2987+
}
2988+
}
2989+
}
2990+
2991+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2992+
/// <param name="value">The new property value being set.</param>
2993+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
2994+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
2995+
partial void OnNameChanging(string value);
2996+
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
2997+
/// <param name="oldValue">The previous property value that is being replaced.</param>
2998+
/// <param name="newValue">The new property value being set.</param>
2999+
/// <remarks>This method is invoked right before the value of <see cref="Name"/> is changed.</remarks>
3000+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3001+
partial void OnNameChanging(string? oldValue, string newValue);
3002+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
3003+
/// <param name="value">The new property value that was set.</param>
3004+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
3005+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3006+
partial void OnNameChanged(string value);
3007+
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
3008+
/// <param name="oldValue">The previous property value that was replaced.</param>
3009+
/// <param name="newValue">The new property value that was set.</param>
3010+
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
3011+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3012+
partial void OnNameChanged(string? oldValue, string newValue);
3013+
}
3014+
}
3015+
""";
3016+
#endif
3017+
3018+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
3019+
}
3020+
27363021
/// <summary>
27373022
/// Generates the requested sources
27383023
/// </summary>

0 commit comments

Comments
 (0)