Skip to content

Commit

Permalink
Fixed generated properties within regions (#4227)
Browse files Browse the repository at this point in the history
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 -->

<!-- 👉 It is imperative to resolve ONE ISSUE PER PR and avoid making multiple changes unless the changes interrelate with each other -->

<!-- 📝 Please always keep the "☑️ Allow edits by maintainers" button checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 -->

## Fixes #4225

<!-- Add the relevant issue number after the word "Fixes" mentioned above (for ex: "## Fixes #1234") which will automatically close the issue once the PR is merged. -->

<!-- Add a brief overview here of the feature/bug & fix. -->

## PR Type

What kind of change does this PR introduce?

<!-- Please uncomment one or more options below that apply to this PR. -->

- Bugfix
<!-- - Feature -->
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->

## What is the current behavior?
Compilation fails If the Microsoft MVVM Toolkit source generator attribute `[ObservableProperty]` is in a `#region` block.
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->

## What is the new behavior?
The generated code is now correct when regions are used.
<!-- Describe how was this issue resolved or changed? -->

## PR Checklist

Please check if your PR fulfills the following requirements: <!-- and remove the ones that are not applicable to the current PR -->

- [X] Tested code with current [supported SDKs](../#supported)
- [X] New component
  - [X] Pull Request has been submitted to the documentation repository [instructions](../blob/main/Contributing.md#docs). Link: <!-- docs PR link -->
  - [X] Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that)
  - [X] If control, added to Visual Studio Design project
- [X] Sample in sample app has been added / updated (for bug fixes / features)
  - [X] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/CommunityToolkit/WindowsCommunityToolkit-design-assets)
- [X] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/CommunityToolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
- [X] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [X] Header has been added to all new source files (run _build/UpdateHeaders.bat_)
- [X] Contains **NO** breaking changes
  • Loading branch information
msftbot[bot] authored Sep 16, 2021
2 parents f18604f + 28bcbaf commit 0d73af7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,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

0 comments on commit 0d73af7

Please sign in to comment.