Beispiel #1
0
        public void TestWeakStrongVariable()
        {
            var listener    = new Listener();
            var source      = new CompositeReactiveObject(listener, "first");
            var sameSource  = new CompositeReactiveObject(listener, "first");
            var otherSource = new CompositeReactiveObject(listener, "second");
            var expression  = Reactive.Expression(() =>
            {
                ReactiveManager.WasRead(source);
                return(1);
            });
            var listenerValue = expression.Evaluate();

            Assert.AreEqual(1, listenerValue);

            var counter     = 0;
            var expectation = 0;

            expression.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            Assert.AreEqual(++expectation, counter);
            ReactiveManager.WasChanged(sameSource);
            Assert.AreEqual(++expectation, counter);

            ReactiveManager.WasChanged(otherSource);
            Assert.AreEqual(expectation, counter);
        }
Beispiel #2
0
        public void Test()
        {
            var inputSquared = Reactive.Expression(() => Input * Input);

            inputSquared.Subscribe(getSquare => Console.WriteLine("square = " + getSquare())); //Prints 'square = 1'
            Input = 2;                                                                         //Prints 'square = 2'
        }
Beispiel #3
0
        public void TestIndex()
        {
            var reactiveList = new List <int>()
            {
                1, 2, 3, 4
            }.ToReactive();
            var elementAtIndex2 = Reactive.Expression(() => reactiveList[2]);
            var counter         = 0;

            elementAtIndex2.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            var expectation = 1;

            reactiveList[2] = 5;
            Assert.AreEqual(++expectation, counter);
            reactiveList[1] = 6;
            Assert.AreEqual(expectation, counter);
            reactiveList[3] = 7;
            Assert.AreEqual(expectation, counter);
            reactiveList.Add(8);
            Assert.AreEqual(expectation, counter);
            reactiveList.RemoveAt(reactiveList.Count - 1);
            Assert.AreEqual(expectation, counter);
            reactiveList.RemoveAt(0);
            Assert.AreEqual(++expectation, counter);
        }
        public void Test()
        {
            var product = Reactive.Expression(() => UsingABackingField * UsingAnAttributeAndPostSharp);

            product.Subscribe(getProduct => Console.WriteLine("product = " + getProduct())); //Prints 'product = 1'
            UsingAnAttributeAndPostSharp = 2;                                                //Prints 'product = 2'
            UsingABackingField           = 2;                                                //Prints 'product = 4'
        }
Beispiel #5
0
        public void Square()
        {
            var input        = Reactive.Variable(1);
            var inputSquared = Reactive.Expression(() => input * input);

            inputSquared.Subscribe(getSquare => Console.WriteLine("square = " + getSquare())); // Prints 'square = 1'

            input.Value = 2;                                                                   //Prints 'square = 4'
            input.Value = 3;                                                                   //Prints 'square = 9'
        }
Beispiel #6
0
        public void Composition()
        {
            var input      = Reactive.Variable(1);
            var timesTwo   = Reactive.Expression(() => input * 2);
            var timesThree = Reactive.Expression(() => input * 3);
            var sumOfBoth  = Reactive.Expression(() => timesTwo.Evaluate() + timesThree.Evaluate());

            sumOfBoth.Subscribe(getValue => Console.WriteLine("sumOfBoth = " + getValue())); //Prints 'sumOfBoth = 5'
            input.Value = 2;                                                                 //Prints 'sumOfBoth = 10'
            input.Value = 3;                                                                 //Prints 'sumOfBoth = 15'
        }
Beispiel #7
0
        public void Precise()
        {
            var left        = Reactive.Variable(false);
            var right       = Reactive.Variable(false);
            var leftOrRight = Reactive.Expression(() => left || right);

            leftOrRight.Subscribe(getValue => Console.WriteLine("leftOrRight = " + getValue()));
            //Prints 'leftOrRight = False'

            right.Value = true;  //Prints 'leftOrRight = True'
            left.Value  = true;  //Prints 'leftOrRight = True'
            right.Value = false; //Prints nothing
        }
Beispiel #8
0
        public void TestIndex()
        {
            var reactiveList = new List <int> {
                0, 1, 2
            }.ToReactive();
            var elementAtIndex1 = Reactive.Expression(() => reactiveList[1]);

            //Prints 'item at index 2 changed to 1'
            elementAtIndex1.Subscribe(getValue => Console.WriteLine("item at index 1 changed to " + getValue()));

            reactiveList[1] = 3;       //Prints 'item at index 1 changed to 3'
            reactiveList[2] = 4;       //Prints nothing
            reactiveList.Add(5);       //Prints nothing
            reactiveList.Insert(0, 6); //Prints 'item at index 1 changed to 0'
        }
        public void TestEnumerator()
        {
            var reactiveList = new HashSet <int> {
                1, 2, 3, 4
            }.ToReactive();
            var sumFirstTwo = Reactive.Expression(() => reactiveList.Take(2).Sum());
            var counter     = 0;

            sumFirstTwo.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            var expectation = 1;

            reactiveList.Remove(2);
            Assert.AreEqual(++expectation, counter);
            reactiveList.Add(6);
            Assert.AreEqual(++expectation, counter);
            reactiveList.Clear();
            Assert.AreEqual(++expectation, counter);
        }
Beispiel #10
0
        public void TestContains()
        {
            var set = new HashSet <int> {
                1, 2, 3, 4
            }.ToReactive();
            var containsTwo = Reactive.Expression(() => set.Contains(2));
            var counter     = 0;

            containsTwo.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            var expectation = 1;

            set.Remove(2);
            Assert.AreEqual(++expectation, counter);
            set.Add(2);
            Assert.AreEqual(++expectation, counter);
            set.Clear();
            Assert.AreEqual(++expectation, counter);
        }
Beispiel #11
0
        public void TestCount()
        {
            var reactiveList    = new HashSet <int>().ToReactive();
            var countExpression = Reactive.Expression(() => reactiveList.Count);
            var counter         = 0;

            countExpression.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            var expectation = 1;

            reactiveList.Add(1);
            Assert.AreEqual(++expectation, counter);
            reactiveList.Add(2);
            Assert.AreEqual(++expectation, counter);
            Assert.AreEqual(expectation, counter);
            reactiveList.Remove(1);
            Assert.AreEqual(++expectation, counter);
            reactiveList.Clear();
            Assert.AreEqual(++expectation, counter);
        }
Beispiel #12
0
        public void TestEnumerator()
        {
            var reactiveList = new Dictionary <int, string> {
                { 1, "first" }, { 2, "second" }, { 3, "third" }, { 4, "fourth" }
            }.ToReactive();
            var sumFirstTwo = Reactive.Expression(() => reactiveList.Take(2).Sum(kv => kv.Key));
            var counter     = 0;

            sumFirstTwo.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            var expectation = 1;

            reactiveList[3] = "fifth";
            Assert.AreEqual(expectation, counter);
            reactiveList.Add(6, "sixth");
            Assert.AreEqual(expectation, counter);
            reactiveList[1] = "seventh";
            Assert.AreEqual(++expectation, counter);
            reactiveList.Clear();
            Assert.AreEqual(++expectation, counter);
        }
Beispiel #13
0
        public void TestCount()
        {
            var reactiveDictionary = new Dictionary <int, string>().ToReactive();
            var countExpression    = Reactive.Expression(() => reactiveDictionary.Count);
            var counter            = 0;

            countExpression.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            var expectation = 1;

            reactiveDictionary.Add(1, "first");
            Assert.AreEqual(++expectation, counter);
            reactiveDictionary.Add(2, "second");
            Assert.AreEqual(++expectation, counter);
            reactiveDictionary[0] = "third";
            Assert.AreEqual(expectation, counter);
            reactiveDictionary.Remove(2);
            Assert.AreEqual(++expectation, counter);
            reactiveDictionary.Clear();
            Assert.AreEqual(++expectation, counter);
        }
Beispiel #14
0
        public void TestKey()
        {
            var reactiveList = new Dictionary <int, string> {
                { 1, "first" }, { 2, "second" }, { 3, "third" }, { 4, "fourth" }
            }.ToReactive();
            var countExpression = Reactive.Expression(() => reactiveList[2]);
            var counter         = 0;

            countExpression.Subscribe(getValue => ReactiveManagerTest.Const(getValue, () => counter++));
            var expectation = 1;

            reactiveList[2] = "fifth";
            Assert.AreEqual(++expectation, counter);
            reactiveList[1] = "sixth";
            Assert.AreEqual(expectation, counter);
            reactiveList[3] = "seventh";
            Assert.AreEqual(expectation, counter);
            reactiveList.Add(8, "eight");
            Assert.AreEqual(expectation, counter);
            reactiveList.Remove(8);
            Assert.AreEqual(expectation, counter);
            reactiveList.Remove(1);
            Assert.AreEqual(expectation, counter);
        }