-
-
Notifications
You must be signed in to change notification settings - Fork 193
Evaluate value-changing conversions in property chains #1194
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
Open
dwcullop
wants to merge
1
commit into
main
Choose a base branch
from
u/dacullop/main/evaluate-value-changing-conversions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/DynamicData.Tests/Binding/WhenPropertyChangedBehaviorFixture.Conversions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright (c) 2011-2025 Roland Pheasant. All rights reserved. | ||
| // Roland Pheasant licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for full license information. | ||
|
|
||
| using System.ComponentModel; | ||
|
|
||
| using DynamicData.Binding; | ||
| using DynamicData.Tests.Domain; | ||
| using DynamicData.Tests.Utilities; | ||
|
|
||
| using FluentAssertions; | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace DynamicData.Tests.Binding; | ||
|
|
||
| public sealed partial class WhenPropertyChangedBehaviorFixture | ||
| { | ||
| /// <summary>Verifies that a numeric conversion is evaluated before reading a property of the converted value.</summary> | ||
| [Fact] | ||
| public void NumericConversionBeforePropertyAccess_InitialValue_IsObserved() | ||
| { | ||
| // Arrange | ||
| // An odd numerator produces an exactly representable, non-integral amount. | ||
| var amount = (_randomizer.Int(1, ushort.MaxValue) | 1) / 4d; | ||
| var model = new ObservablePrice { Amount = amount }; | ||
| var expectedScale = ((decimal)amount).Scale; | ||
|
|
||
| // Act | ||
| using var subscription = model.WhenValueChanged(static price => ((decimal)price.Amount).Scale) | ||
| .RecordValues(out var results); | ||
|
|
||
| // Assert | ||
| results.Error.Should().BeNull(because: "the next property belongs to the converted decimal, not the source double"); | ||
| results.RecordedValues.Should().Equal(new[] { expectedScale }, because: "the initial value must match the supplied expression"); | ||
| results.HasCompleted.Should().BeFalse(because: "further amount changes remain observable"); | ||
| } | ||
|
|
||
| /// <summary>Verifies that property changes remain observable through a value-changing numeric conversion.</summary> | ||
| /// <param name="notifyOnInitialValue">Whether subscribing requests an initial value notification.</param> | ||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public void NumericConversionBeforePropertyAccess_PropertyChanges_AreObserved(bool notifyOnInitialValue) | ||
| { | ||
| // Arrange | ||
| // Odd quarters and eighths have distinct decimal scales without floating-point rounding. | ||
| var initialAmount = (_randomizer.Int(1, ushort.MaxValue) | 1) / 4d; | ||
| var changedAmount = (_randomizer.Int(1, ushort.MaxValue) | 1) / 8d; | ||
| var model = new ObservablePrice { Amount = initialAmount }; | ||
| var initialScale = ((decimal)initialAmount).Scale; | ||
| var changedScale = ((decimal)changedAmount).Scale; | ||
| var expectedScales = notifyOnInitialValue ? new[] { initialScale, changedScale } : new[] { changedScale }; | ||
| using var subscription = model.WhenValueChanged(static price => ((decimal)price.Amount).Scale, notifyOnInitialValue) | ||
| .RecordValues(out var results); | ||
|
|
||
| // Act | ||
| model.Amount = changedAmount; | ||
|
|
||
| // Assert | ||
| results.Error.Should().BeNull(because: "conversion must be applied on both initial and subsequent chain reads"); | ||
| results.RecordedValues.Should().Equal(expectedScales, because: "notifications must match the expression and initial-value option"); | ||
| results.HasCompleted.Should().BeFalse(because: "amount changes do not complete the observation"); | ||
| } | ||
|
|
||
| /// <summary>Verifies that a numeric conversion at the end of a property path preserves initial and changed values.</summary> | ||
| [Fact] | ||
| public void NumericConversionAtLeaf_PropertyChanges_AreObserved() | ||
| { | ||
| // Arrange | ||
| var initialAmount = _randomizer.Double(); | ||
| var changedAmount = initialAmount + _randomizer.Double(1, 2); | ||
| var model = new ObservablePrice { Amount = initialAmount }; | ||
| var expectedAmounts = new[] { (decimal)initialAmount, (decimal)changedAmount }; | ||
| using var subscription = model.WhenValueChanged(static price => (decimal)price.Amount) | ||
| .RecordValues(out var results); | ||
|
|
||
| // Act | ||
| model.Amount = changedAmount; | ||
|
|
||
| // Assert | ||
| results.Error.Should().BeNull(because: "a conversion must also remain supported as the final expression step"); | ||
| results.RecordedValues.Should().Equal(expectedAmounts, because: "each observed value must be converted to the requested type"); | ||
| results.HasCompleted.Should().BeFalse(because: "further amount changes remain observable"); | ||
| } | ||
|
|
||
| /// <summary>Verifies that a reference cast preserves observation of properties on the runtime model.</summary> | ||
| [Fact] | ||
| public void ReferenceCastBeforePropertyAccess_PropertyChanges_AreObserved() | ||
| { | ||
| // Arrange | ||
| var person = Fakers.Person.Clone().WithSeed(_randomizer).Generate(); | ||
| INotifyPropertyChanged model = person; | ||
| var initialAge = person.Age; | ||
| var changedAge = initialAge + _randomizer.Int(1, byte.MaxValue); | ||
| using var subscription = model.WhenValueChanged(static source => ((Person)source).Age) | ||
| .RecordValues(out var results); | ||
|
|
||
| // Act | ||
| person.Age = changedAge; | ||
|
|
||
| // Assert | ||
| results.Error.Should().BeNull(because: "supported reference casts must preserve property observation"); | ||
| results.RecordedValues.Should().Equal(new[] { initialAge, changedAge }, because: "the runtime model remains the notification source"); | ||
| results.HasCompleted.Should().BeFalse(because: "further age changes remain observable"); | ||
| } | ||
|
|
||
| /// <summary>Verifies that an interface cast preserves observation after replacing an intermediate object.</summary> | ||
| [Fact] | ||
| public void InterfaceCastBeforePropertyAccess_ReplacementChildChanges_AreObserved() | ||
| { | ||
| // Arrange | ||
| var initialAge = _randomizer.Int(1, byte.MaxValue); | ||
| var replacementAge = initialAge + _randomizer.Int(1, byte.MaxValue); | ||
| var changedAge = replacementAge + _randomizer.Int(1, byte.MaxValue); | ||
| var parent = new ParentModel { Child = new ChildModel { Age = initialAge } }; | ||
| var replacement = new ChildModel { Age = replacementAge }; | ||
| using var subscription = parent.WhenValueChanged(static source => ((IHasAge)source.Child!).Age) | ||
| .RecordValues(out var results); | ||
| parent.Child = replacement; | ||
|
|
||
| // Act | ||
| replacement.Age = changedAge; | ||
|
|
||
| // Assert | ||
| results.Error.Should().BeNull(because: "supported interface casts must preserve nested property observation"); | ||
| results.RecordedValues.Should().Equal(new[] { initialAge, replacementAge, changedAge }, | ||
| because: "the interface property must follow the replacement child and its subsequent changes"); | ||
| results.HasCompleted.Should().BeFalse(because: "further child changes remain observable"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -245,6 +245,11 @@ public static class NotifyPropertyChangedEx | |
| /// For an object like Parent.Child.Sibling, sibling is an object so if Child is null, the value null and obtainable and is returned as null.</param> | ||
| /// <returns>A observable which also notifies when the property value changes.</returns> | ||
| /// <exception cref="ArgumentNullException">propertyAccessor.</exception> | ||
| /// <remarks> | ||
| /// Property paths can contain reference and numeric conversions. Value-changing conversions are evaluated | ||
| /// before accessing subsequent properties in the path. | ||
| /// </remarks> | ||
| /// <seealso cref="WhenValueChanged{TObject, TProperty}"/> | ||
| public static IObservable<PropertyValue<TObject, TProperty>> WhenPropertyChanged<TObject, TProperty>(this TObject source, Expression<Func<TObject, TProperty>> propertyAccessor, bool notifyOnInitialValue = true, Func<TProperty?>? fallbackValue = null) | ||
| where TObject : INotifyPropertyChanged | ||
| { | ||
|
|
@@ -267,6 +272,10 @@ public static IObservable<PropertyValue<TObject, TProperty>> WhenPropertyChanged | |
| /// For example when observing Parent.Child.Age, if Child is null the value is unobtainable as Age is a struct and cannot be set to Null. | ||
| /// For an object like Parent.Child.Sibling, sibling is an object so if Child is null, the value null and obtainable and is returned as null.</param> | ||
| /// <returns>An observable which emits the results.</returns> | ||
| /// <remarks> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
| /// Supports the property-path conversions described by <see cref="WhenPropertyChanged{TObject, TProperty}"/>. | ||
| /// </remarks> | ||
| /// <seealso cref="WhenPropertyChanged{TObject, TProperty}"/> | ||
| public static IObservable<TProperty?> WhenValueChanged<TObject, TProperty>(this TObject source, Expression<Func<TObject, TProperty>> propertyAccessor, bool notifyOnInitialValue = true, Func<TProperty>? fallbackValue = null) | ||
| where TObject : INotifyPropertyChanged | ||
| { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pointless documentation. "Value-changing conversions are evaluated before accessing subsequent properties in the path" is just describing how C# expressions work. Consumers should expect that when they supply an expression to a function, it is evaluated correctly.