Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed generated properties within regions #4227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.AddAttributeLists(validationAttributes.Select(static a => AttributeList(SingletonSeparatedList(a))).ToArray())
.WithLeadingTrivia(leadingTrivia)
.WithLeadingTrivia(leadingTrivia.Where(static trivia => !trivia.IsKind(SyntaxKind.RegionDirectiveTrivia) && !trivia.IsKind(SyntaxKind.EndRegionDirectiveTrivia)))
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddAccessorListAccessors(
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
Expand Down
6 changes: 6 additions & 0 deletions UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#pragma warning disable SA1124

namespace UnitTests.Mvvm
{
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1601", Justification = "Type only used for testing")]
Expand Down Expand Up @@ -78,6 +80,8 @@ private async Task DelayAndIncrementCounterAsync()
Counter += 1;
}

#region Test region

/// <summary>
/// This is multi line with also other stuff below
/// </summary>
Expand All @@ -100,6 +104,8 @@ private async Task DelayAndIncrementCounterWithValueAsync(int count)
Counter += count;
}

#endregion

[ICommand]
private async Task DelayAndIncrementCounterWithValueAndTokenAsync(int count, CancellationToken token)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#pragma warning disable SA1124

#nullable enable

namespace UnitTests.Mvvm
Expand Down Expand Up @@ -58,6 +60,86 @@ public void Test_ObservablePropertyAttribute_Events()
Assert.AreEqual(changed.Item2, 42);
}

// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225
[TestCategory("Mvvm")]
[TestMethod]
public void Test_ObservablePropertyAttributeWithinRegion_Events()
{
var model = new SampleModel();

(PropertyChangingEventArgs, int) changing = default;
(PropertyChangedEventArgs, int) changed = default;

model.PropertyChanging += (s, e) =>
{
Assert.IsNull(changing.Item1);
Assert.IsNull(changed.Item1);
Assert.AreSame(model, s);
Assert.IsNotNull(s);
Assert.IsNotNull(e);

changing = (e, model.Counter);
};

model.PropertyChanged += (s, e) =>
{
Assert.IsNotNull(changing.Item1);
Assert.IsNull(changed.Item1);
Assert.AreSame(model, s);
Assert.IsNotNull(s);
Assert.IsNotNull(e);

changed = (e, model.Counter);
};

model.Counter = 42;

Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Counter));
Assert.AreEqual(changing.Item2, 0);
Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Counter));
Assert.AreEqual(changed.Item2, 42);
}

// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225
[TestCategory("Mvvm")]
[TestMethod]
public void Test_ObservablePropertyAttributeRightBelowRegion_Events()
{
var model = new SampleModel();

(PropertyChangingEventArgs, string?) changing = default;
(PropertyChangedEventArgs, string?) changed = default;

model.PropertyChanging += (s, e) =>
{
Assert.IsNull(changing.Item1);
Assert.IsNull(changed.Item1);
Assert.AreSame(model, s);
Assert.IsNotNull(s);
Assert.IsNotNull(e);

changing = (e, model.Name);
};

model.PropertyChanged += (s, e) =>
{
Assert.IsNotNull(changing.Item1);
Assert.IsNull(changed.Item1);
Assert.AreSame(model, s);
Assert.IsNotNull(s);
Assert.IsNotNull(e);

changed = (e, model.Name);
};

model.Name = "Bob";

Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Name));
Assert.AreEqual(changing.Item2, null);
Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Name));
Assert.AreEqual(changed.Item2, "Bob");
}

[TestCategory("Mvvm")]
[TestMethod]
public void Test_AlsoNotifyChangeForAttribute_Events()
Expand Down Expand Up @@ -162,6 +244,16 @@ public partial class SampleModel : ObservableObject
/// </summary>
[ObservableProperty]
private int data;

#region More properties

[ObservableProperty]
private int counter;

#endregion

[ObservableProperty]
private string? name;
}

[INotifyPropertyChanged]
Expand Down