public void OFPSimpleChildPropertyTest()
        {
            new TestScheduler().With(sched =>
            {
                var fixture = new HostTestFixture
                {
                    Child = new TestFixture()
                };
                fixture.ObservableForProperty(x => x.Child.IsOnlyOneWord)
                .ToObservableChangeSet(ImmediateScheduler.Instance)
                .Bind(out var changes)
                .Subscribe();

                fixture.Child.IsOnlyOneWord = "Foo";
                sched.Start();
                Assert.Equal(1, changes.Count);

                fixture.Child.IsOnlyOneWord = "Bar";
                sched.Start();
                Assert.Equal(2, changes.Count);

                fixture.Child.IsOnlyOneWord = "Baz";
                sched.Start();
                Assert.Equal(3, changes.Count);

                fixture.Child.IsOnlyOneWord = "Baz";
                sched.Start();
                Assert.Equal(3, changes.Count);

                Assert.True(changes.All(x => x.Sender == fixture));
                Assert.True(changes.All(x => x.GetPropertyName() == "Child.IsOnlyOneWord"));
                changes.Select(x => x.Value).AssertAreEqual(new[] { "Foo", "Bar", "Baz" });
            });
        }
コード例 #2
0
        public void BindToIsNotFooledByIntermediateObjectSwitching()
        {
            new TestScheduler().With(sched =>
            {
                var input   = new ScheduledSubject <string>(sched);
                var fixture = new HostTestFixture {
                    Child = new TestFixture()
                };

                input.BindTo(fixture, x => x.Child.IsNotNullString);

                Assert.Null(fixture.Child.IsNotNullString);

                input.OnNext("Foo");
                sched.Start();
                Assert.Equal("Foo", fixture.Child.IsNotNullString);

                fixture.Child = new TestFixture();
                sched.Start();
                Assert.Equal("Foo", fixture.Child.IsNotNullString);

                input.OnNext("Bar");
                sched.Start();
                Assert.Equal("Bar", fixture.Child.IsNotNullString);
            });
        }
        public void OFPChangingTheHostPropertyShouldFireAChildChangeNotificationOnlyIfThePreviousChildIsDifferent()
        {
            new TestScheduler().With(sched =>
            {
                var fixture = new HostTestFixture
                {
                    Child = new TestFixture()
                };
                fixture.ObservableForProperty(x => x.Child.IsOnlyOneWord)
                .ToObservableChangeSet(ImmediateScheduler.Instance)
                .Bind(out var changes)
                .Subscribe();

                fixture.Child.IsOnlyOneWord = "Foo";
                sched.Start();
                Assert.Equal(1, changes.Count);

                fixture.Child.IsOnlyOneWord = "Bar";
                sched.Start();
                Assert.Equal(2, changes.Count);

                fixture.Child = new TestFixture
                {
                    IsOnlyOneWord = "Bar"
                };
                sched.Start();
                Assert.Equal(2, changes.Count);
            });
        }
        public void WhenAnySmokeTest()
        {
            new TestScheduler().With(
                sched =>
            {
                var fixture = new HostTestFixture
                {
                    Child = new TestFixture()
                };
                fixture.SomeOtherParam        = 5;
                fixture.Child.IsNotNullString = "Foo";

                var output1 = new List <IObservedChange <HostTestFixture, int> >();
                var output2 = new List <IObservedChange <HostTestFixture, string> >();
                fixture.WhenAny(
                    x => x.SomeOtherParam,
                    x => x.Child.IsNotNullString,
                    (sop, nns) => new
                {
                    sop,
                    nns
                }).Subscribe(
                    x =>
                {
                    output1.Add(x.sop);
                    output2.Add(x.nns);
                });

                sched.Start();
                Assert.Equal(1, output1.Count);
                Assert.Equal(1, output2.Count);
                Assert.Equal(fixture, output1[0].Sender);
                Assert.Equal(fixture, output2[0].Sender);
                Assert.Equal(5, output1[0].Value);
                Assert.Equal("Foo", output2[0].Value);

                fixture.SomeOtherParam = 10;
                sched.Start();
                Assert.Equal(2, output1.Count);
                Assert.Equal(2, output2.Count);
                Assert.Equal(fixture, output1[1].Sender);
                Assert.Equal(fixture, output2[1].Sender);
                Assert.Equal(10, output1[1].Value);
                Assert.Equal("Foo", output2[1].Value);

                fixture.Child.IsNotNullString = "Bar";
                sched.Start();
                Assert.Equal(3, output1.Count);
                Assert.Equal(3, output2.Count);
                Assert.Equal(fixture, output1[2].Sender);
                Assert.Equal(fixture, output2[2].Sender);
                Assert.Equal(10, output1[2].Value);
                Assert.Equal("Bar", output2[2].Value);
            });
        }
        public void SubscriptionToWhenAnyShouldReturnCurrentValue()
        {
            var obj           = new HostTestFixture();
            var observedValue = 1;

            obj.WhenAnyValue(x => x.SomeOtherParam).Subscribe(x => observedValue = x);

            obj.SomeOtherParam = 42;

            Assert.True(observedValue == obj.SomeOtherParam);
        }
コード例 #6
0
        public void GetValueShouldReturnTheValueFromAPath()
        {
            var input = new HostTestFixture
            {
                Child = new TestFixture {
                    IsNotNullString = "Foo"
                },
            };

            Expression <Func <HostTestFixture, string> > expression = x => x.Child.IsNotNullString;
            var fixture = new ObservedChange <HostTestFixture, string>(input, expression.Body);

            Assert.Equal("Foo", fixture.GetValue());
        }
コード例 #7
0
        public void SetValuePathSmokeTest()
        {
            var output = new HostTestFixture
            {
                Child = new TestFixture {
                    IsNotNullString = "Foo"
                },
            };

            Expression <Func <TestFixture, string> > expression = x => x.IsOnlyOneWord;
            var fixture = new ObservedChange <TestFixture, string>(new TestFixture {
                IsOnlyOneWord = "Bar"
            }, expression.Body);

            fixture.SetValueToProperty(output, x => x.Child.IsNotNullString);
            Assert.Equal("Bar", output.Child.IsNotNullString);
        }
        public void OFPReplacingTheHostShouldResubscribeTheObservable()
        {
            new TestScheduler().With(sched =>
            {
                var fixture = new HostTestFixture
                {
                    Child = new TestFixture()
                };
                fixture.ObservableForProperty(x => x.Child.IsOnlyOneWord)
                .ToObservableChangeSet(ImmediateScheduler.Instance)
                .Bind(out var changes)
                .Subscribe();

                fixture.Child.IsOnlyOneWord = "Foo";
                sched.Start();
                Assert.Equal(1, changes.Count);

                fixture.Child.IsOnlyOneWord = "Bar";
                sched.Start();
                Assert.Equal(2, changes.Count);

                // Tricky! This is a change too, because from the perspective
                // of the binding, we've went from "Bar" to null
                fixture.Child = new TestFixture();
                sched.Start();
                Assert.Equal(3, changes.Count);

                // Here we've set the value but it shouldn't change
                fixture.Child.IsOnlyOneWord = null;
                sched.Start();
                Assert.Equal(3, changes.Count);

                fixture.Child.IsOnlyOneWord = "Baz";
                sched.Start();
                Assert.Equal(4, changes.Count);

                fixture.Child.IsOnlyOneWord = "Baz";
                sched.Start();
                Assert.Equal(4, changes.Count);

                Assert.True(changes.All(x => x.Sender == fixture));
                Assert.True(changes.All(x => x.GetPropertyName() == "Child.IsOnlyOneWord"));
                changes.Select(x => x.Value).AssertAreEqual(new[] { "Foo", "Bar", null, "Baz" });
            });
        }
コード例 #9
0
        public void DisposingDisconnectsTheBindTo()
        {
            new TestScheduler().With(sched =>
            {
                var input   = new ScheduledSubject <string>(sched);
                var fixture = new HostTestFixture {
                    Child = new TestFixture()
                };

                var subscription = input.BindTo(fixture, x => x.Child.IsNotNullString);

                Assert.Null(fixture.Child.IsNotNullString);

                input.OnNext("Foo");
                sched.Start();
                Assert.Equal("Foo", fixture.Child.IsNotNullString);

                subscription.Dispose();

                input.OnNext("Bar");
                sched.Start();
                Assert.Equal("Foo", fixture.Child.IsNotNullString);
            });
        }