Skip to content

Commit 4bf74cf

Browse files
committed
Fixed [ObservableProperty] within regions
1 parent 0ce4553 commit 4bf74cf

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
434434
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
435435
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
436436
.AddAttributeLists(validationAttributes.Select(static a => AttributeList(SingletonSeparatedList(a))).ToArray())
437-
.WithLeadingTrivia(leadingTrivia)
437+
.WithLeadingTrivia(leadingTrivia.Where(static trivia => !trivia.IsKind(SyntaxKind.RegionDirectiveTrivia) && !trivia.IsKind(SyntaxKind.EndRegionDirectiveTrivia)))
438438
.AddModifiers(Token(SyntaxKind.PublicKeyword))
439439
.AddAccessorListAccessors(
440440
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)

UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.Toolkit.Mvvm.ComponentModel;
1212
using Microsoft.VisualStudio.TestTools.UnitTesting;
1313

14+
#pragma warning disable SA1124
15+
1416
#nullable enable
1517

1618
namespace UnitTests.Mvvm
@@ -58,6 +60,86 @@ public void Test_ObservablePropertyAttribute_Events()
5860
Assert.AreEqual(changed.Item2, 42);
5961
}
6062

63+
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225
64+
[TestCategory("Mvvm")]
65+
[TestMethod]
66+
public void Test_ObservablePropertyAttributeWithinRegion_Events()
67+
{
68+
var model = new SampleModel();
69+
70+
(PropertyChangingEventArgs, int) changing = default;
71+
(PropertyChangedEventArgs, int) changed = default;
72+
73+
model.PropertyChanging += (s, e) =>
74+
{
75+
Assert.IsNull(changing.Item1);
76+
Assert.IsNull(changed.Item1);
77+
Assert.AreSame(model, s);
78+
Assert.IsNotNull(s);
79+
Assert.IsNotNull(e);
80+
81+
changing = (e, model.Counter);
82+
};
83+
84+
model.PropertyChanged += (s, e) =>
85+
{
86+
Assert.IsNotNull(changing.Item1);
87+
Assert.IsNull(changed.Item1);
88+
Assert.AreSame(model, s);
89+
Assert.IsNotNull(s);
90+
Assert.IsNotNull(e);
91+
92+
changed = (e, model.Counter);
93+
};
94+
95+
model.Counter = 42;
96+
97+
Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Counter));
98+
Assert.AreEqual(changing.Item2, 0);
99+
Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Counter));
100+
Assert.AreEqual(changed.Item2, 42);
101+
}
102+
103+
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225
104+
[TestCategory("Mvvm")]
105+
[TestMethod]
106+
public void Test_ObservablePropertyAttributeRightBelowRegion_Events()
107+
{
108+
var model = new SampleModel();
109+
110+
(PropertyChangingEventArgs, string?) changing = default;
111+
(PropertyChangedEventArgs, string?) changed = default;
112+
113+
model.PropertyChanging += (s, e) =>
114+
{
115+
Assert.IsNull(changing.Item1);
116+
Assert.IsNull(changed.Item1);
117+
Assert.AreSame(model, s);
118+
Assert.IsNotNull(s);
119+
Assert.IsNotNull(e);
120+
121+
changing = (e, model.Name);
122+
};
123+
124+
model.PropertyChanged += (s, e) =>
125+
{
126+
Assert.IsNotNull(changing.Item1);
127+
Assert.IsNull(changed.Item1);
128+
Assert.AreSame(model, s);
129+
Assert.IsNotNull(s);
130+
Assert.IsNotNull(e);
131+
132+
changed = (e, model.Name);
133+
};
134+
135+
model.Name = "Bob";
136+
137+
Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Name));
138+
Assert.AreEqual(changing.Item2, null);
139+
Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Name));
140+
Assert.AreEqual(changed.Item2, "Bob");
141+
}
142+
61143
[TestCategory("Mvvm")]
62144
[TestMethod]
63145
public void Test_AlsoNotifyChangeForAttribute_Events()
@@ -162,6 +244,16 @@ public partial class SampleModel : ObservableObject
162244
/// </summary>
163245
[ObservableProperty]
164246
private int data;
247+
248+
#region More properties
249+
250+
[ObservableProperty]
251+
private int counter;
252+
253+
#endregion
254+
255+
[ObservableProperty]
256+
private string? name;
165257
}
166258

167259
[INotifyPropertyChanged]

0 commit comments

Comments
 (0)