-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add AsValue Extensions (#59)
Co-authored-by: Glenn [email protected]
- Loading branch information
1 parent
9a8a423
commit e1c0746
Showing
42 changed files
with
2,328 additions
and
1,328 deletions.
There are no files selected for viewing
This file contains 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 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
48 changes: 48 additions & 0 deletions
48
src/ReactiveMarbles.Mvvm.Benchmarks/Performance/AsValuePerformanceBenchmark.cs
This file contains 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,48 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Jobs; | ||
using ReactiveMarbles.Mvvm.Benchmarks.Memory; | ||
using ReactiveMarbles.PropertyChanged; | ||
using ReactiveUI; | ||
|
||
namespace ReactiveMarbles.Mvvm.Benchmarks.Performance | ||
{ | ||
[SimpleJob(RuntimeMoniker.NetCoreApp31)] | ||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] | ||
public class AsValuePerformanceBenchmark | ||
{ | ||
[Benchmark(Baseline = true)] | ||
[BenchmarkCategory("Performance")] | ||
public void AsValueBenchmark() | ||
{ | ||
var thing = new DummyRxObject(); | ||
var sut = thing.WhenChanged(x => x.NotSerialized, x => x.IsOnlyOneWord, (not, one) => not + one).AsValue(onChanged: _ => { }); | ||
} | ||
|
||
[Benchmark] | ||
[BenchmarkCategory("Performance")] | ||
public void AsValueWhenWordChangedBenchmark() | ||
{ | ||
var thing = new DummyRxObject(); | ||
thing.IsOnlyOneWord = "Two Words"; | ||
} | ||
|
||
[Benchmark] | ||
[BenchmarkCategory("Performance")] | ||
public void ToPropertyBenchmark() | ||
{ | ||
var thing = new DummyReactiveObject(); | ||
var sut = thing.WhenChanged(x => x.NotSerialized, x => x.IsOnlyOneWord, (not, one) => not + one).ToProperty(thing, x => x.ObservableProperty); | ||
} | ||
|
||
[Benchmark] | ||
[BenchmarkCategory("Performance")] | ||
public void ToPropertyWhenWordChangedBenchmark() | ||
{ | ||
var thing = new DummyReactiveObject(); | ||
thing.IsOnlyOneWord = "Two Words"; | ||
} | ||
} | ||
} |
This file contains 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 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
219 changes: 219 additions & 0 deletions
219
src/ReactiveMarbles.Mvvm.Tests/AsLazyValueExtensionsTests.cs
This file contains 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,219 @@ | ||
// Copyright (c) 2019-2021 ReactiveUI Association Incorporated. All rights reserved. | ||
// ReactiveUI Association Incorporated licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System.Reactive.Concurrency; | ||
using System.Reactive.Linq; | ||
using FluentAssertions; | ||
using Microsoft.Reactive.Testing; | ||
using ReactiveMarbles.PropertyChanged; | ||
using Xunit; | ||
|
||
namespace ReactiveMarbles.Mvvm.Tests; | ||
|
||
/// <summary> | ||
/// Tests for the <see cref="AsValueExtensions"/>. | ||
/// </summary> | ||
public class AsLazyValueExtensionsTests | ||
{ | ||
/// <summary> | ||
/// Tests the default value. | ||
/// </summary> | ||
[Fact] | ||
public void GivenNoChanges_WhenAsValue_ThenFullNameIsEmpty() | ||
{ | ||
// Given, When | ||
var sut = new AsLazyValueTestObject(); | ||
|
||
// Then | ||
sut.FullName.Should().BeNullOrEmpty(); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the property is produced from the sequence. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenSequence_WhenAsValue_ThenPropertyProduced(string first, string last) | ||
{ | ||
// Given | ||
var sut = new AsLazyValueTestObject(); | ||
|
||
// When | ||
sut.FirstName = first; | ||
sut.LastName = last; | ||
|
||
// Then | ||
sut.FullName.Should().Be(first + last); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the property is produced from the sequence. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenFirstName_WhenAsValue_ThenPropertyProduced(string first, string last) | ||
{ | ||
// Given | ||
var sut = new AsLazyValueTestObject(); | ||
|
||
// When | ||
sut.FirstName = first; | ||
|
||
// Then | ||
sut.FullName.Should().Be(first); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the property is produced from the sequence. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenLastName_WhenAsValue_ThenPropertyProduced(string first, string last) | ||
{ | ||
// Given | ||
var sut = new AsLazyValueTestObject(); | ||
|
||
// When | ||
sut.LastName = last; | ||
|
||
// Then | ||
sut.FullName.Should().Be(last); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the value of the value binder. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenOnChanged_WhenAsValue_ThenValueCorrect(string first, string last) | ||
{ | ||
// Given | ||
var testObject = new AsLazyValueTestObject(); | ||
var sut = | ||
testObject | ||
.WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) | ||
.AsLazyValue(onChanged: _ => { }); | ||
|
||
// When | ||
testObject.FirstName = first; | ||
testObject.LastName = last; | ||
|
||
// Then | ||
sut.Value.Should().Be(first + last); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the value of the value binder. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenOnChangedAndInitialValue_WhenAsValue_ThenValueCorrect(string first, string last) | ||
{ | ||
// Given | ||
var testObject = new AsLazyValueTestObject(); | ||
var sut = | ||
testObject | ||
.WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) | ||
.AsLazyValue(onChanged: _ => { }, initialValue: () => string.Empty); | ||
|
||
// When | ||
testObject.FirstName = first; | ||
testObject.LastName = last; | ||
|
||
// Then | ||
sut.Value.Should().Be(first + last); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the value of the value binder. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenOnChangedAndOnChangingAndInitialValue_WhenAsValue_ThenValueCorrect(string first, string last) | ||
{ | ||
// Given | ||
var testObject = new AsLazyValueTestObject(); | ||
var sut = | ||
testObject | ||
.WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) | ||
.AsLazyValue(onChanging: _ => { }, onChanged: _ => { }, initialValue: () => string.Empty); | ||
|
||
// When | ||
testObject.FirstName = first; | ||
testObject.LastName = last; | ||
|
||
// Then | ||
sut.Value.Should().Be(first + last); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the value of the value binder. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenOnChangedAndOnChangingAndSchedulerAndInitialValue_WhenAsValue_ThenValueCorrect(string first, string last) | ||
{ | ||
// Given | ||
const string start = "start"; | ||
var testScheduler = new TestScheduler(); | ||
var testObject = new AsLazyValueTestObject(); | ||
var sut = | ||
testObject | ||
.WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) | ||
.AsLazyValue(onChanged: _ => { }, testScheduler, () => start); | ||
|
||
sut.Value.Should().Be(start); | ||
|
||
// When | ||
testObject.FirstName = first; | ||
testObject.LastName = last; | ||
testScheduler.Start(); | ||
|
||
// Then | ||
sut.Value.Should().Be(first + last); | ||
} | ||
|
||
/// <summary> | ||
/// Tests the value of the value binder. | ||
/// </summary> | ||
/// <param name="first">The first name.</param> | ||
/// <param name="last">The last name.</param> | ||
[Theory] | ||
[MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] | ||
public void GivenAllParameters_WhenAsValue_ThenValueCorrect(string first, string last) | ||
{ | ||
// Given | ||
const string start = "start"; | ||
var testScheduler = new TestScheduler(); | ||
var testObject = new AsLazyValueTestObject(); | ||
var sut = | ||
testObject | ||
.WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) | ||
.AsLazyValue(_ => { }, _ => { }, testScheduler, () => start); | ||
|
||
sut.Value.Should().Be(start); | ||
|
||
// When | ||
testObject.FirstName = first; | ||
testObject.LastName = last; | ||
testScheduler.Start(); | ||
|
||
// Then | ||
sut.Value.Should().Be(first + last); | ||
} | ||
} |
Oops, something went wrong.