예제 #1
0
        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}");
        }
예제 #2
0
        public static Reactive <T> CreateReactiveThatDependsOn <T>(Proactive <T> proactiveSourceValue)
        {
            Reactive <T> reactiveBeingCollected = CreateReactiveThatGetsValueOf(proactiveSourceValue);
            T            triggerAReaction       = reactiveBeingCollected.Value;

            return(reactiveBeingCollected);
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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;
            }
        }
예제 #5
0
        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));
            }
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
        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);
        }
예제 #8
0
        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}");
        }
예제 #9
0
        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}");
        }
예제 #10
0
        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.
        }
예제 #11
0
        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}");
        }
예제 #12
0
        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}");
        }
예제 #13
0
        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. "));
        }
예제 #14
0
        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}");
        }
예제 #15
0
        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);
        }
예제 #16
0
        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}");
        }
예제 #17
0
        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}");
        }
예제 #18
0
        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. "));
        }
예제 #19
0
        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);
        }
예제 #20
0
 public ReactiveManipulator(T valueToUse)
 {
     linkedProactive = new Proactive <T>(valueToUse);
     Reactive        = new Reactive <T>(WaitAndReturnSourceValue);
 }
예제 #21
0
        public void WhenCreated_IsNotConsequential()
        {
            Proactive <int> proactiveBeingTested = new Proactive <int>(5);

            Assert.That(proactiveBeingTested.IsConsequential, Is.False);
        }
예제 #22
0
 public static WeakReference <Reactive <T> > CreateUnreferencedReactive <T>(Proactive <T> proactiveSourceValue) =>
 new WeakReference <Reactive <T> >(CreateReactiveThatDependsOn(proactiveSourceValue));
예제 #23
0
 public static Reactive <T> CreateReactiveThatGetsValueOf <T>(Proactive <T> proactiveSourceValue) => new Reactive <T>(() => proactiveSourceValue.Value);
예제 #24
0
 public static WeakReference <Reactive <T> > CreateUnreferencedReactiveInfluencedBy <T>(Proactive <T> proactiveSourceValue)
 {
     return(CreateUnreferencedReactive(proactiveSourceValue));
 }