public void InitialValue() { var stored = new StoredProperty <int>(1); var passthrough = new PassthroughProperty <int>(stored); passthrough.Value.Should().Be(1); }
public void InitialValueCalculated() { var stored = new StoredProperty <bool>(); DerivedProperty <bool> negated = DerivedProperty <bool> .Create(stored, storedValue => !storedValue); negated.Value.Should().Be(true); }
public void Fibonacci() { var zero = new StoredProperty <int>(0); var one = new StoredProperty <int>(1); var f = new Property <int> [17]; f[0] = zero; f[1] = one; f[2] = DerivedProperty <int> .Create(f[0], f[1], (i0, i1) => i0 + i1); f[3] = DerivedProperty <int> .Create(f[0], f[1], f[2], (i0, i1, i2) => i1 + i2); f[4] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], (i0, i1, i2, i3) => i2 + i3); f[5] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], (i0, i1, i2, i3, i4) => i3 + i4); f[6] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], (i0, i1, i2, i3, i4, i5) => i4 + i5); f[7] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], (i0, i1, i2, i3, i4, i5, i6) => i5 + i6); f[8] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], (i0, i1, i2, i3, i4, i5, i6, i7) => i6 + i7); f[9] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], (i0, i1, i2, i3, i4, i5, i6, i7, i8) => i7 + i8); f[10] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) => i8 + i9); f[11] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10], (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10) => i9 + i10); f[12] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10], f[11], (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11) => i10 + i11); f[13] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10], f[11], f[12], (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12) => i11 + i12); f[14] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10], f[11], f[12], f[13], (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) => i12 + i13); f[15] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10], f[11], f[12], f[13], f[14], (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14) => i13 + i14); f[16] = DerivedProperty <int> .Create(f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10], f[11], f[12], f[13], f[14], f[15], (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15) => i14 + i15); f[2].Value.Should().Be(1); f[3].Value.Should().Be(2); f[4].Value.Should().Be(3); f[5].Value.Should().Be(5); f[6].Value.Should().Be(8); f[7].Value.Should().Be(13); f[8].Value.Should().Be(21); f[9].Value.Should().Be(34); f[10].Value.Should().Be(55); f[11].Value.Should().Be(89); f[12].Value.Should().Be(144); f[13].Value.Should().Be(233); f[14].Value.Should().Be(377); f[15].Value.Should().Be(610); f[16].Value.Should().Be(987); }
private void multiLevelProperty() { var person = new Person("FirstName", "LastName"); var currentUser = new StoredProperty <Person>(person); var currentUserFullName = new MultiLevelProperty <string>(() => currentUser.Value.fullName); Console.WriteLine($"Welcome, {currentUserFullName.Value}"); // Welcome, FirstName LastName }
private void storedProperty() { var stored = new StoredProperty <string>("world"); Console.WriteLine($"Hello {stored.Value}"); // Hello world stored.Value = "world!"; Console.WriteLine($"Hello {stored.Value}"); // Hello world! }
private void passthroughPropertySynchronizationContext() { var backing = new StoredProperty <double>(3.0); var passthrough = new PassthroughProperty <double>(backing) { EventSynchronizationContext = SynchronizationContext.Current }; }
public void UpdatedValueReturned() { var property = new StoredProperty <string>("hello"); property.Value.Should().Be("hello"); property.Value = "world"; property.Value.Should().Be("world"); }
public void GetUntypedValue() { var stored = new StoredProperty <int>(1); DerivedProperty <int> property = DerivedProperty <int> .Create(stored, i => i + 100); var rawProperty = (Property)property; rawProperty.Value.Should().Be(101); }
public void DecrementLong() { var property = new StoredProperty <long>(9); property.PropertyChanged += delegate { changeEventsFired++; }; property.Decrement().Should().Be(8); property.Value.Should().Be(8); changeEventsFired.Should().Be(1); }
public void ExchangeInt() { var property = new StoredProperty <int>(7); property.PropertyChanged += delegate { changeEventsFired++; }; property.Exchange(8).Should().Be(7); property.Value.Should().Be(8); changeEventsFired.Should().Be(1); }
public void CompareExchangeDouble() { var property = new StoredProperty <double>(7); property.PropertyChanged += delegate { changeEventsFired++; }; property.CompareExchange(8, 7, 0.0001).Should().Be(7); property.Value.Should().Be(8); changeEventsFired.Should().Be(1); }
public void GetObjectValueWithGenericCasting() { var storedProperty = new StoredProperty <int>(); storedProperty.Value = 3; Property property = storedProperty; property.Value.Should().Be(3); }
private void connectableProperty() { var connectable = new ConnectableProperty <int>(0); var a = new StoredProperty <int>(8); Console.WriteLine(connectable.Value); // 0 connectable.Connect(a); Console.WriteLine(connectable.Value); // 8 }
public void AddLong() { var property = new StoredProperty <long>(5); property.PropertyChanged += delegate { changeEventsFired++; }; property.Add(3).Should().Be(8); property.Value.Should().Be(8); changeEventsFired.Should().Be(1); }
private void derivedProperty() { var backing = new StoredProperty <int>(8); DerivedProperty <int> derived = DerivedProperty <int> .Create(backing, (bValue) => Math.Abs(bValue)); Console.WriteLine($"The absolute value of {backing.Value} is {derived.Value}."); // The absolute value of 8 is 8. backing.Value = -9; Console.WriteLine($"The absolute value of {backing.Value} is {derived.Value}."); // The absolute value of -9 is 9. }
private void passthroughProperty() { var backing = new StoredProperty <double>(3.0); var passthrough = new PassthroughProperty <double>(backing); Console.WriteLine($"{passthrough.Value} liters"); // 3 liters backing.Value = 5.0; Console.WriteLine($"{passthrough.Value} liters"); // 5 liters }
public void CompareExchangeLong() { var property = new StoredProperty <long>(7); property.PropertyChanged += delegate { changeEventsFired++; }; property.CompareExchange(8, 7).Should().Be(7); property.Value.Should().Be(8); changeEventsFired.Should().Be(1); }
public void AddLongWithNoChangeDoesNotFireEvents() { var property = new StoredProperty <int>(8); property.PropertyChanged += delegate { changeEventsFired++; }; property.Add(0).Should().Be(8); property.Value.Should().Be(8); changeEventsFired.Should().Be(0); }
public void ValueIsUpdated() { var stored = new StoredProperty <int>(1); var passthrough = new PassthroughProperty <int>(stored); stored.Value.Should().Be(1); stored.Value = 2; passthrough.Value.Should().Be(2); }
public void ValueRecalculatedWhenDependenciesUpdated() { var stored = new StoredProperty <string>("hello"); DerivedProperty <string> uppercased = DerivedProperty <string> .Create(stored, storedValue => storedValue.ToUpper()); uppercased.Value.Should().Be("HELLO"); stored.Value = "world"; uppercased.Value.Should().Be("WORLD"); }
private void changeHandler() { var property = new StoredProperty <int>(); property.PropertyChanged += (object sender, KoKoPropertyChangedEventArgs <int> args) => { Console.WriteLine($"Property value changed from {args.OldValue} to {args.NewValue}."); }; INotifyPropertyChanged property2 = property; property2.PropertyChanged += (object sender, PropertyChangedEventArgs args) => { Console.WriteLine($"Property value changed to {property.Value}."); }; }
public void CompareExchangeGeneric() { var initialValue = new List <int>(); var newValue = new List <int>(); var property = new StoredProperty <List <int> >(initialValue); property.PropertyChanged += delegate { changeEventsFired++; }; property.CompareExchange(newValue, initialValue).Should().BeSameAs(initialValue); property.Value.Should().BeSameAs(newValue); changeEventsFired.Should().Be(1); }
public void TransitiveDerivedDependencies() { var name = new StoredProperty <string>("world"); DerivedProperty <string> uppercased = DerivedProperty <string> .Create(name, nameValue => nameValue.ToUpper()); DerivedProperty <string> greeting = DerivedProperty <string> .Create(uppercased, uppercasedValue => $"HELLO {uppercasedValue}!"); greeting.Value.Should().Be("HELLO WORLD!"); name.Value = "Ben"; greeting.Value.Should().Be("HELLO BEN!"); }
public void ExchangeObject() { var initialValue = new List <int>(); var newValue = new List <double>(); var property = new StoredProperty <object>(initialValue); property.PropertyChanged += delegate { changeEventsFired++; }; property.Exchange(newValue).Should().BeSameAs(initialValue); property.Value.Should().BeSameAs(newValue); changeEventsFired.Should().Be(1); }
private void tentativeProperty() { var backing = new StoredProperty <int>(8); var tentative = new TentativeProperty <int>(backing, TimeSpan.FromMilliseconds(500)); Console.WriteLine(tentative.Value); // 8 backing.Value = 9; Console.WriteLine(tentative.Value); // 9 tentative.Value = 10; backing.Value = 11; Console.WriteLine(tentative.Value); // 10 Thread.Sleep(1000); Console.WriteLine(tentative.Value); // 11 }
public void SetUnchangedValueDoesNotTriggerEvents() { var property = new StoredProperty <double>(8.0); property.Value.Should().Be(8.0); int eventsTriggeredCount = 0; property.PropertyChanged += (sender, args) => { eventsTriggeredCount++; property.Value.Should().Be(8.0); }; property.Value = 8.0; eventsTriggeredCount.Should().Be(0); }
public void SetUnchangedValueDoesNotTriggerEvents() { var stored = new StoredProperty <double>(8.0); DerivedProperty <double> abs = DerivedProperty <double> .Create(stored, Math.Abs); abs.Value.Should().Be(8.0); int eventsTriggeredCount = 0; abs.PropertyChanged += (sender, args) => { eventsTriggeredCount++; abs.Value.Should().Be(8.0); }; stored.Value = -8.0; eventsTriggeredCount.Should().Be(0); }
public void UsesSynchronizationContextIfConfigured() { var stored = new StoredProperty <int>(1); int eventsTriggeredCount = 0; var synchronizationContext = new MySyncContext(); stored.EventSynchronizationContext = synchronizationContext; stored.PropertyChanged += (sender, args) => { eventsTriggeredCount++; }; synchronizationContext.PostCount.Should().Be(0); synchronizationContext.SendCount.Should().Be(0); stored.Value = 2; eventsTriggeredCount.Should().Be(1); synchronizationContext.SendCount.Should().Be(1); synchronizationContext.PostCount.Should().Be(0); stored.EventSynchronizationContext.Should().BeSameAs(synchronizationContext); }
public void MultipleDependencies() { var a = new StoredProperty <bool>(); var b = new StoredProperty <bool>(); DerivedProperty <bool> and = DerivedProperty <bool> .Create(a, b, (aValue, bValue) => aValue && bValue); DerivedProperty <bool> or = DerivedProperty <bool> .Create(a, b, (aValue, bValue) => aValue || bValue); and.Value.Should().BeFalse(); or.Value.Should().BeFalse(); a.Value = true; and.Value.Should().BeFalse(); or.Value.Should().BeTrue(); b.Value = true; and.Value.Should().BeTrue(); or.Value.Should().BeTrue(); }
public void EventsTriggered() { var property = new StoredProperty <double>(8.0); property.Value.Should().Be(8.0); int eventsTriggeredCount = 0; int inotifyEventsTriggeredCount = 0; property.PropertyChanged += (sender, args) => { eventsTriggeredCount++; property.Value.Should().Be(9.0); }; ((Property)property).PropertyChanged += (sender, args) => { inotifyEventsTriggeredCount++; }; property.Value = 9.0; eventsTriggeredCount.Should().Be(1); inotifyEventsTriggeredCount.Should().Be(1); }