Skip to content

Commit 3c986b2

Browse files
committed
Add unit test for accessor attributes
1 parent 9c0b241 commit 3c986b2

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,6 +3296,104 @@ public T Value
32963296
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel`1.g.cs", result));
32973297
}
32983298

3299+
[TestMethod]
3300+
public void ObservablePropertyWithForwardedAttributes_OnPropertyAccessors()
3301+
{
3302+
string source = """
3303+
using System.ComponentModel;
3304+
using CommunityToolkit.Mvvm.ComponentModel;
3305+
3306+
#nullable enable
3307+
3308+
namespace MyApp;
3309+
3310+
partial class MyViewModel : ObservableObject
3311+
{
3312+
[ObservableProperty]
3313+
[property: Test("Property1")]
3314+
[property: Test("Property2")]
3315+
[property: Test("Property3")]
3316+
[get: Test("Get1")]
3317+
[get: Test("Get2")]
3318+
[set: Test("Set1")]
3319+
[set: Test("Set2")]
3320+
private object? a;
3321+
}
3322+
3323+
public class TestAttribute : Attribute
3324+
{
3325+
public TestAttribute(string value)
3326+
{
3327+
}
3328+
}
3329+
""";
3330+
3331+
string result = """
3332+
// <auto-generated/>
3333+
#pragma warning disable
3334+
#nullable enable
3335+
namespace MyApp
3336+
{
3337+
/// <inheritdoc/>
3338+
partial class MyViewModel
3339+
{
3340+
/// <inheritdoc cref="a"/>
3341+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3342+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
3343+
[global::MyApp.TestAttribute("Property1")]
3344+
[global::MyApp.TestAttribute("Property2")]
3345+
[global::MyApp.TestAttribute("Property3")]
3346+
public object? A
3347+
{
3348+
[global::MyApp.TestAttribute("Get1")]
3349+
[global::MyApp.TestAttribute("Get2")]
3350+
get => a;
3351+
[global::MyApp.TestAttribute("Set1")]
3352+
[global::MyApp.TestAttribute("Set2")]
3353+
set
3354+
{
3355+
if (!global::System.Collections.Generic.EqualityComparer<object?>.Default.Equals(a, value))
3356+
{
3357+
OnAChanging(value);
3358+
OnAChanging(default, value);
3359+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.A);
3360+
a = value;
3361+
OnAChanged(value);
3362+
OnAChanged(default, value);
3363+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.A);
3364+
}
3365+
}
3366+
}
3367+
3368+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
3369+
/// <param name="value">The new property value being set.</param>
3370+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
3371+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3372+
partial void OnAChanging(object? value);
3373+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
3374+
/// <param name="oldValue">The previous property value that is being replaced.</param>
3375+
/// <param name="newValue">The new property value being set.</param>
3376+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
3377+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3378+
partial void OnAChanging(object? oldValue, object? newValue);
3379+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
3380+
/// <param name="value">The new property value that was set.</param>
3381+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
3382+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3383+
partial void OnAChanged(object? value);
3384+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
3385+
/// <param name="oldValue">The previous property value that was replaced.</param>
3386+
/// <param name="newValue">The new property value that was set.</param>
3387+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
3388+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", <ASSEMBLY_VERSION>)]
3389+
partial void OnAChanged(object? oldValue, object? newValue);
3390+
}
3391+
}
3392+
""";
3393+
3394+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
3395+
}
3396+
32993397
/// <summary>
33003398
/// Generates the requested sources
33013399
/// </summary>

0 commit comments

Comments
 (0)