public void WhenDependentOnMultipleProactivesAndAnyChange_ValueChangesCorrectly( [Random(1)] int initialValueOne, [Random(1)] int initialValueTwo, [Random(1)] int increment) { Proactive <int> firstProactive = new Proactive <int>(initialValueOne); Proactive <int> secondProactive = new Proactive <int>(initialValueTwo); Func <int> sumValues = () => firstProactive.Value + secondProactive.Value; Reactive <int> reactiveBeingTested = new Reactive <int>(sumValues); int expectedValue = sumValues(); int actualValue = reactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(expectedValue)); TestContext.WriteLine($"Expected Value {expectedValue}, Actual Value {actualValue}"); firstProactive.Value += increment; expectedValue = sumValues(); actualValue = reactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(expectedValue)); TestContext.WriteLine($"Expected Value {expectedValue}, Actual Value {actualValue}"); secondProactive.Value += increment; expectedValue = sumValues(); actualValue = reactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(expectedValue)); TestContext.WriteLine($"Expected Value {expectedValue}, Actual Value {actualValue}"); }
public static Reactive <T> CreateReactiveThatDependsOn <T>(Proactive <T> proactiveSourceValue) { Reactive <T> reactiveBeingCollected = CreateReactiveThatGetsValueOf(proactiveSourceValue); T triggerAReaction = reactiveBeingCollected.Value; return(reactiveBeingCollected); }
public void IfInvalidatedWhenNotReflexive_DoesNotUpdateValue() { int numberOfExecutions = 0; Proactive <int> proactive = new Proactive <int>(42); Reactive <int> reactiveToTest = new Reactive <int>(IncrementNumExecutionsAndReturn); Assert.That(numberOfExecutions, Is.Zero); Assert.That(reactiveToTest.IsReflexive, Is.False, $"The value of {nameof(reactiveToTest.IsReflexive)} was {reactiveToTest.IsReflexive}"); int triggerProcess = reactiveToTest.Value; TestContext.WriteLine("Initial value calculated."); Assert.That(numberOfExecutions, Is.EqualTo(1)); proactive.Value = 13; TestContext.WriteLine("Proactive value updated."); Assert.That(numberOfExecutions, Is.EqualTo(1)); triggerProcess = reactiveToTest.Value; TestContext.WriteLine("Reactive value updated."); Assert.That(numberOfExecutions, Is.EqualTo(2)); int IncrementNumExecutionsAndReturn() { numberOfExecutions++; TestContext.WriteLine("The process was executed."); return(proactive.Value); } }
public void AfterUpdating_NotifiesSubscribersOfValueChange() { bool methodWasExecuted = false; int initialValue = 13; int updatedValue = 42; Proactive <int> proactive = new Proactive <int>(initialValue); Reactive <int> reactiveToTest = CreateReactiveThatGetsValueOf(proactive); int result = reactiveToTest.Value; Assert.That(result, Is.EqualTo(initialValue)); reactiveToTest.Subscriptions.Subscribe(TestPublishedValues); proactive.Value = updatedValue; result = reactiveToTest.Value; Assert.That(result, Is.EqualTo(updatedValue)); Assert.That(methodWasExecuted, "The subscriber method was not executed."); void TestPublishedValues(int newValue, int oldValue) { Assert.That(newValue, Is.EqualTo(updatedValue)); TestContext.WriteLine($"New value is {newValue}."); Assert.That(oldValue, Is.EqualTo(initialValue)); TestContext.WriteLine($"Old value is {oldValue}."); methodWasExecuted = true; } }
public static void WhileUpdatingAReactive_RunActionOnReactive <T>(Action <Reactive <T> > actionToRun, T valueToUse) { ManualResetEvent updateStarted = new ManualResetEvent(false); ManualResetEvent conditionChecked = new ManualResetEvent(false); Proactive <T> proactiveWithValue = new Proactive <T>(valueToUse); Reactive <T> reactiveBeingTested = new Reactive <T>(WaitAndReturnSourceValue); StartNewThreadThatRuns(UpdateReactive); updateStarted.WaitOne(); actionToRun(reactiveBeingTested); conditionChecked.Set(); return; T WaitAndReturnSourceValue() { updateStarted.Set(); conditionChecked.WaitOne(); return(proactiveWithValue.Value); } void UpdateReactive() { T valueToTest = reactiveBeingTested.Value; Assert.That(valueToTest, Is.EqualTo(valueToUse)); } }
public void IfInvalidatedWhileReflexive_AutomaticallyReacts() { int initialValue = 42; int updatedValue = 13; int numberOfExecutions = 0; Proactive <int> proactive = new Proactive <int>(initialValue); Reactive <int> reactiveToTest = new Reactive <int>(IncrementNumExecutionsAndReturn); Assert.That(numberOfExecutions, Is.Zero); int triggerProcess = reactiveToTest.Value; TestContext.WriteLine("Initial value calculated."); Assert.That(numberOfExecutions, Is.EqualTo(1)); reactiveToTest.IsReflexive = true; proactive.Value = updatedValue; Assert.That(numberOfExecutions, Is.EqualTo(2)); Assert.That(reactiveToTest.Value, Is.EqualTo(updatedValue), ErrorMessages.ValueDidNotMatch <Reactive <int> >("the value returned by the function it was given.")); int IncrementNumExecutionsAndReturn() { numberOfExecutions++; TestContext.WriteLine("The process was executed."); return(proactive.Value); } }
public void WhenValueRetrievedDuringAReaction_BecomesConsequential() { Proactive <int> proactiveBeingTested = new Proactive <int>(42); Reactive <int> dependentReactive = new Reactive <int>(() => proactiveBeingTested); int triggerValueUpdate = dependentReactive.Value; Assert.That(proactiveBeingTested.IsConsequential); }
public void WhenPassedAValueDuringConstruction_HasThatValue() { int valueToTest = 5; Proactive <int> proactiveBeingTested = new Proactive <int>(valueToTest); int actualValue = proactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(valueToTest)); TestContext.WriteLine($"Expected Value {valueToTest}, Actual Value {actualValue}"); }
public void WhenGettingValueFromAProactive_HasTheCorrectValue([Random(1)] int value) { Proactive <int> proactiveBeingTested = new Proactive <int>(value); Reactive <int> reactiveBeingTested = CreateReactiveThatGetsValueOf(proactiveBeingTested); int actualValue = reactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(value)); TestContext.WriteLine($"Expected Value {value}, Actual Value {actualValue}"); }
public void WhenGivenANameDuringConstruction_HasThatName() { string givenName = "Some Proactive"; Proactive <int> proactiveBeingTested = new Proactive <int>(42, givenName); string actualName = proactiveBeingTested.Name; Assert.That(actualName, Is.EqualTo(givenName)); TestContext.WriteLine($"Expected Value => {givenName},\nActual Value => {actualName}"); //- TODO : Test the other constructors. }
public void WhenManuallyInvalidated_IsInvalid() { Proactive <int> proactive = new Proactive <int>(42); Reactive <int> reactiveBeingTested = new Reactive <int>(() => proactive); int triggerValueUpdate = reactiveBeingTested.Value; Assert.That(reactiveBeingTested.IsValid, "The reactive was not valid after initial construction. "); reactiveBeingTested.Invalidate(); Assert.That(reactiveBeingTested.IsValid == false, "The reactive was not marked as invalid after being invalidated."); TestContext.WriteLine($"The {nameof(Reactive)} was valid => {reactiveBeingTested.IsValid}"); }
public void WhenGettingValueFromMultipleProactives_HasTheCorrectValue([Random(1)] int firstValue, [Random(1)] int secondValue) { Proactive <int> firstProactive = new Proactive <int>(firstValue); Proactive <int> secondProactive = new Proactive <int>(secondValue); Func <int> sumValues = () => firstProactive.Value + secondProactive.Value; Reactive <int> reactiveBeingTested = new Reactive <int>(sumValues); int expectedValue = firstValue + secondValue; int actualValue = reactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(expectedValue)); TestContext.WriteLine($"Expected Value {expectedValue}, Actual Value {actualValue}"); }
public void WhenValueSet_IsNoLongerConsequential() { Proactive <int> proactiveBeingTested = new Proactive <int>(5); Reactive <int> reactiveThatRetrievesValue = CreateReactiveThatGetsValueOf(proactiveBeingTested); Assert.That(proactiveBeingTested.IsConsequential, Is.False, ErrorMessages.FactorWasConsequential <Proactive <int> >("before being used. ")); int triggerAReaction = reactiveThatRetrievesValue.Value; Assert.That(proactiveBeingTested.IsConsequential, Is.True, ErrorMessages.FactorWasNotConsequential <Proactive <int> >("despite being used to calculate a value. ")); proactiveBeingTested.Value = 10; Assert.That(proactiveBeingTested.IsConsequential, Is.False, ErrorMessages.FactorWasConsequential <Proactive <int> >("despite its value changing. ")); }
public void AfterDependentFactorChangesItsValue_IsInvalid([Random(1)] int initialValue, [Random(1)] int updatedValue) { Proactive <int> proactive = new Proactive <int>(initialValue); Reactive <int> reactiveBeingTested = new Reactive <int>(() => proactive); int triggerValueUpdate = reactiveBeingTested.Value; Assert.That(reactiveBeingTested.IsValid, "The reactive was not valid after initial construction. "); proactive.Value = updatedValue; Assert.That(reactiveBeingTested.IsValid == false, "The reactive was not marked as invalid after being invalidated."); TestContext.WriteLine($"The {nameof(Reactive)} was valid => {reactiveBeingTested.IsValid}"); }
public static Proactive <int> CreateChainOfReactions() { Proactive <int> proactiveValue = new Proactive <int>(5); Reactive <int> reactiveNotBeingCollected = CreateReactiveThatGetsValueOf(proactiveValue); int triggerAReaction = reactiveNotBeingCollected.Value; for (int i = 0; i < 3; i++) { reactiveNotBeingCollected = CreateReactiveThatGetsValueOf(reactiveNotBeingCollected); } Assert.True(proactiveValue.IsConsequential); return(proactiveValue); }
public void WhenGivenANewValue_HasThatValue() { int initialValue = 5; int updatedValue = 7; Proactive <int> proactiveBeingTested = new Proactive <int>(initialValue); int actualValue = proactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(initialValue)); TestContext.WriteLine($"Expected Value {initialValue}, Actual Value {actualValue}"); proactiveBeingTested.Value = updatedValue; actualValue = proactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(updatedValue)); TestContext.WriteLine($"Expected Value {updatedValue}, Actual Value {actualValue}"); }
public void WhenProactiveValueChanges_ItsValueAlsoChanges([Random(1)] int initialValue, [Random(1)] int increment) { Proactive <int> proactiveBeingTested = new Proactive <int>(initialValue); Reactive <int> reactiveBeingTested = CreateReactiveThatGetsValueOf(proactiveBeingTested); int actualValue = reactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(initialValue)); TestContext.WriteLine($"Expected Value {initialValue}, Actual Value {actualValue}"); int updatedValue = proactiveBeingTested.Value += increment; proactiveBeingTested.Value = updatedValue; actualValue = reactiveBeingTested.Value; Assert.That(actualValue, Is.EqualTo(updatedValue)); TestContext.WriteLine($"Expected Value {updatedValue}, Actual Value {actualValue}"); }
public void WhenGivenAValueEqualToCurrentValue_DependentsAreNotInvalidated() { int initialValue = 5; Proactive <int> proactiveBeingTested = new Proactive <int>(initialValue); Reactive <int> dependentReactive = CreateReactiveThatGetsValueOf(proactiveBeingTested); Assert.False(proactiveBeingTested.IsConsequential, $"The {NameOf<Proactive<int>>()} was marked as consequential before being used. "); int triggerAReaction = dependentReactive.Value; Assert.That(dependentReactive.IsValid, ErrorMessages.ReactorWasNotValid <Reactive <int> >()); Assert.That(proactiveBeingTested.IsConsequential, ErrorMessages.FactorWasNotConsequential <Proactive <int> >("despite being used to calculate a value. ")); proactiveBeingTested.Value = 5; Assert.That(dependentReactive.IsValid, $"Setting the value of a {NameOf<Proactive<int>>()} invalidated its dependents even though " + "the value set was equal to the old value. "); Assert.That(proactiveBeingTested.IsConsequential, ErrorMessages.FactorWasNotConsequential <Proactive <int> >("despite being used to calculate a value. ")); }
private static WeakReference <Proactive <int> > GenerateChainOfFactors(HashSet <WeakReference <Reactive <int> > > references) { Proactive <int> proactiveValue = new Proactive <int>(5); WeakReference <Proactive <int> > referenceToProactive = new WeakReference <Proactive <int> >(proactiveValue); Reactive <int> createdReactive = CreateReactiveThatGetsValueOf(proactiveValue); int triggerAReaction = createdReactive.Value; for (int i = 0; i < 3; i++) { createdReactive = CreateReactiveThatDependsOn(createdReactive); references.Add(new WeakReference <Reactive <int> >(createdReactive)); } foreach (var reference in references) { reference.TryGetTarget(out var reactive); Assert.That(reactive, Is.Not.Null); } Assert.That(proactiveValue.IsConsequential); return(referenceToProactive); }
public ReactiveManipulator(T valueToUse) { linkedProactive = new Proactive <T>(valueToUse); Reactive = new Reactive <T>(WaitAndReturnSourceValue); }
public void WhenCreated_IsNotConsequential() { Proactive <int> proactiveBeingTested = new Proactive <int>(5); Assert.That(proactiveBeingTested.IsConsequential, Is.False); }
public static WeakReference <Reactive <T> > CreateUnreferencedReactive <T>(Proactive <T> proactiveSourceValue) => new WeakReference <Reactive <T> >(CreateReactiveThatDependsOn(proactiveSourceValue));
public static Reactive <T> CreateReactiveThatGetsValueOf <T>(Proactive <T> proactiveSourceValue) => new Reactive <T>(() => proactiveSourceValue.Value);
public static WeakReference <Reactive <T> > CreateUnreferencedReactiveInfluencedBy <T>(Proactive <T> proactiveSourceValue) { return(CreateUnreferencedReactive(proactiveSourceValue)); }