|
| 1 | +// Copyright (c) 2019-2021 ReactiveUI Association Incorporated. All rights reserved. |
| 2 | +// ReactiveUI Association Incorporated licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using System.Reactive.Concurrency; |
| 6 | +using System.Reactive.Linq; |
| 7 | +using FluentAssertions; |
| 8 | +using Microsoft.Reactive.Testing; |
| 9 | +using ReactiveMarbles.PropertyChanged; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace ReactiveMarbles.Mvvm.Tests; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Tests for the <see cref="AsValueExtensions"/>. |
| 16 | +/// </summary> |
| 17 | +public class AsLazyValueExtensionsTests |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Tests the default value. |
| 21 | + /// </summary> |
| 22 | + [Fact] |
| 23 | + public void GivenNoChanges_WhenAsValue_ThenFullNameIsEmpty() |
| 24 | + { |
| 25 | + // Given, When |
| 26 | + var sut = new AsLazyValueTestObject(); |
| 27 | + |
| 28 | + // Then |
| 29 | + sut.FullName.Should().BeNullOrEmpty(); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Tests the property is produced from the sequence. |
| 34 | + /// </summary> |
| 35 | + /// <param name="first">The first name.</param> |
| 36 | + /// <param name="last">The last name.</param> |
| 37 | + [Theory] |
| 38 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 39 | + public void GivenSequence_WhenAsValue_ThenPropertyProduced(string first, string last) |
| 40 | + { |
| 41 | + // Given |
| 42 | + var sut = new AsLazyValueTestObject(); |
| 43 | + |
| 44 | + // When |
| 45 | + sut.FirstName = first; |
| 46 | + sut.LastName = last; |
| 47 | + |
| 48 | + // Then |
| 49 | + sut.FullName.Should().Be(first + last); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Tests the property is produced from the sequence. |
| 54 | + /// </summary> |
| 55 | + /// <param name="first">The first name.</param> |
| 56 | + /// <param name="last">The last name.</param> |
| 57 | + [Theory] |
| 58 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 59 | + public void GivenFirstName_WhenAsValue_ThenPropertyProduced(string first, string last) |
| 60 | + { |
| 61 | + // Given |
| 62 | + var sut = new AsLazyValueTestObject(); |
| 63 | + |
| 64 | + // When |
| 65 | + sut.FirstName = first; |
| 66 | + |
| 67 | + // Then |
| 68 | + sut.FullName.Should().Be(first); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Tests the property is produced from the sequence. |
| 73 | + /// </summary> |
| 74 | + /// <param name="first">The first name.</param> |
| 75 | + /// <param name="last">The last name.</param> |
| 76 | + [Theory] |
| 77 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 78 | + public void GivenLastName_WhenAsValue_ThenPropertyProduced(string first, string last) |
| 79 | + { |
| 80 | + // Given |
| 81 | + var sut = new AsLazyValueTestObject(); |
| 82 | + |
| 83 | + // When |
| 84 | + sut.LastName = last; |
| 85 | + |
| 86 | + // Then |
| 87 | + sut.FullName.Should().Be(last); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Tests the value of the value binder. |
| 92 | + /// </summary> |
| 93 | + /// <param name="first">The first name.</param> |
| 94 | + /// <param name="last">The last name.</param> |
| 95 | + [Theory] |
| 96 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 97 | + public void GivenOnChanged_WhenAsValue_ThenValueCorrect(string first, string last) |
| 98 | + { |
| 99 | + // Given |
| 100 | + var testObject = new AsLazyValueTestObject(); |
| 101 | + var sut = |
| 102 | + testObject |
| 103 | + .WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) |
| 104 | + .AsLazyValue(onChanged: _ => { }); |
| 105 | + |
| 106 | + // When |
| 107 | + testObject.FirstName = first; |
| 108 | + testObject.LastName = last; |
| 109 | + |
| 110 | + // Then |
| 111 | + sut.Value.Should().Be(first + last); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Tests the value of the value binder. |
| 116 | + /// </summary> |
| 117 | + /// <param name="first">The first name.</param> |
| 118 | + /// <param name="last">The last name.</param> |
| 119 | + [Theory] |
| 120 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 121 | + public void GivenOnChangedAndInitialValue_WhenAsValue_ThenValueCorrect(string first, string last) |
| 122 | + { |
| 123 | + // Given |
| 124 | + var testObject = new AsLazyValueTestObject(); |
| 125 | + var sut = |
| 126 | + testObject |
| 127 | + .WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) |
| 128 | + .AsLazyValue(onChanged: _ => { }, initialValue: () => string.Empty); |
| 129 | + |
| 130 | + // When |
| 131 | + testObject.FirstName = first; |
| 132 | + testObject.LastName = last; |
| 133 | + |
| 134 | + // Then |
| 135 | + sut.Value.Should().Be(first + last); |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Tests the value of the value binder. |
| 140 | + /// </summary> |
| 141 | + /// <param name="first">The first name.</param> |
| 142 | + /// <param name="last">The last name.</param> |
| 143 | + [Theory] |
| 144 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 145 | + public void GivenOnChangedAndOnChangingAndInitialValue_WhenAsValue_ThenValueCorrect(string first, string last) |
| 146 | + { |
| 147 | + // Given |
| 148 | + var testObject = new AsLazyValueTestObject(); |
| 149 | + var sut = |
| 150 | + testObject |
| 151 | + .WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) |
| 152 | + .AsLazyValue(onChanging: _ => { }, onChanged: _ => { }, initialValue: () => string.Empty); |
| 153 | + |
| 154 | + // When |
| 155 | + testObject.FirstName = first; |
| 156 | + testObject.LastName = last; |
| 157 | + |
| 158 | + // Then |
| 159 | + sut.Value.Should().Be(first + last); |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Tests the value of the value binder. |
| 164 | + /// </summary> |
| 165 | + /// <param name="first">The first name.</param> |
| 166 | + /// <param name="last">The last name.</param> |
| 167 | + [Theory] |
| 168 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 169 | + public void GivenOnChangedAndOnChangingAndSchedulerAndInitialValue_WhenAsValue_ThenValueCorrect(string first, string last) |
| 170 | + { |
| 171 | + // Given |
| 172 | + const string start = "start"; |
| 173 | + var testScheduler = new TestScheduler(); |
| 174 | + var testObject = new AsLazyValueTestObject(); |
| 175 | + var sut = |
| 176 | + testObject |
| 177 | + .WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) |
| 178 | + .AsLazyValue(onChanged: _ => { }, testScheduler, () => start); |
| 179 | + |
| 180 | + sut.Value.Should().Be(start); |
| 181 | + |
| 182 | + // When |
| 183 | + testObject.FirstName = first; |
| 184 | + testObject.LastName = last; |
| 185 | + testScheduler.Start(); |
| 186 | + |
| 187 | + // Then |
| 188 | + sut.Value.Should().Be(first + last); |
| 189 | + } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Tests the value of the value binder. |
| 193 | + /// </summary> |
| 194 | + /// <param name="first">The first name.</param> |
| 195 | + /// <param name="last">The last name.</param> |
| 196 | + [Theory] |
| 197 | + [MemberData(nameof(AsValueTestData.Data), MemberType=typeof(AsValueTestData))] |
| 198 | + public void GivenAllParameters_WhenAsValue_ThenValueCorrect(string first, string last) |
| 199 | + { |
| 200 | + // Given |
| 201 | + const string start = "start"; |
| 202 | + var testScheduler = new TestScheduler(); |
| 203 | + var testObject = new AsLazyValueTestObject(); |
| 204 | + var sut = |
| 205 | + testObject |
| 206 | + .WhenChanged(x => x.FirstName, x => x.LastName, (firstName, lastName) => firstName + lastName) |
| 207 | + .AsLazyValue(_ => { }, _ => { }, testScheduler, () => start); |
| 208 | + |
| 209 | + sut.Value.Should().Be(start); |
| 210 | + |
| 211 | + // When |
| 212 | + testObject.FirstName = first; |
| 213 | + testObject.LastName = last; |
| 214 | + testScheduler.Start(); |
| 215 | + |
| 216 | + // Then |
| 217 | + sut.Value.Should().Be(first + last); |
| 218 | + } |
| 219 | +} |
0 commit comments