public void NoThrownExceptionsSubscriberEqualsRxphDeath()
        {
            new TestScheduler().With(sched =>
            {
                var input   = new Subject <int>();
                var fixture = new RxPropertyHelper <int>(input, _ => { }, -5, scheduler: ImmediateScheduler.Instance);

                Assert.Equal(-5, fixture.Value);
                new[] { 1, 2, 3, 4 }.ForEach(x => input.OnNext(x));

                input.OnError(new Exception("Die!"));

                var failed = true;
                try
                {
                    sched.Start();
                }
                catch (Exception ex)
                {
                    failed = ex.InnerException.Message != "Die!";
                }

                Assert.False(failed);
                Assert.Equal(4, fixture.Value);
            });
        }
        public ScoreViewModel(string backgroundColor)
        {
            BackgroundColor = backgroundColor;

            this.WhenInitialized(disposables =>
            {
                IncrementScoreCommand = ReactiveCommand.Create(() => 1).DisposeWith(disposables);

                CanDecrement =
                    this.WhenInitializedSwitch(
                        this
                        .WhenAnyValue(x => x.Score)
                        .Select(score => score > 0),
                        false);

                DecrementScoreCommand = ReactiveCommand.Create(() => - 1, CanDecrement).DisposeWith(disposables);

                ResetScoreCommand = ReactiveCommand.Create(() => - Score).DisposeWith(disposables);

                _score =
                    Observable.Merge(
                        IncrementScoreCommand,
                        DecrementScoreCommand,
                        ResetScoreCommand)
                    .Scan(0, (acc, delta) => acc + delta)
                    .ToProperty(this, x => x.Score)
                    .DisposeWith(disposables);
            });
        }
        public void RxphShouldProvideInitialValueImmediatelyRegardlessOfScheduler()
        {
            var output = new List <int>();

            new TestScheduler().With(sched =>
            {
                var fixture = new RxPropertyHelper <int>(
                    Observable <int> .Never,
                    x => output.Add(x),
                    32);

                Assert.Equal(32, fixture.Value);
            });
        }
        public void RxphDeferSubscriptionParameterIsSubscribedIsNotTrueInitially()
        {
            var observable = Observable.Create <int>(o =>
            {
                o.OnNext(42);
                o.OnCompleted();

                return(Disposable.Empty);
            });

            var fixture = new RxPropertyHelper <int>(observable, _ => { }, 0, true);

            Assert.False(fixture.IsSubscribed);
            Assert.Equal(42, fixture.Value);
            Assert.True(fixture.IsSubscribed);
        }
        public void RxphShouldSubscribeImmediatelyToSource()
        {
            var isSubscribed = false;

            var observable = Observable.Create <int>(o =>
            {
                isSubscribed = true;
                o.OnNext(42);
                o.OnCompleted();

                return(Disposable.Empty);
            });

            var fixture = new RxPropertyHelper <int>(observable, _ => { }, 0);

            Assert.True(isSubscribed);
            Assert.Equal(42, fixture.Value);
        }
        public void RxphShouldSkipFirstValueIfItMatchesTheInitialValue()
        {
            var input = new[] { 1, 2, 3 }.ToObservable();
            var output = new List <int>();

            new TestScheduler().With(sched =>
            {
                var fixture = new RxPropertyHelper <int>(
                    input,
                    x => output.Add(x),
                    1);

                sched.Start();

                Assert.Equal(input.LastAsync().Wait(), fixture.Value);

                new[] { 1, 2, 3 }.AssertAreEqual(output);
            });
        }
        public void RxphDeferSubscriptionParameterDefersSubscriptionToSource()
        {
            var isSubscribed = false;

            var observable = Observable.Create <int>(o =>
            {
                isSubscribed = true;
                o.OnNext(42);
                o.OnCompleted();

                return(Disposable.Empty);
            });

            var fixture = new RxPropertyHelper <int>(observable, _ => { }, 0, true);

            Assert.False(isSubscribed);
            Assert.Equal(42, fixture.Value);
            Assert.True(isSubscribed);
        }
        public void RxphShouldFireChangeNotifications()
        {
            var input = new[] { 1, 2, 3, 3, 4 }.ToObservable();
            var output = new List <int>();

            new TestScheduler().With(sched =>
            {
                var fixture = new RxPropertyHelper <int>(
                    input,
                    x => output.Add(x),
                    -5);

                sched.Start();

                Assert.Equal(input.LastAsync().Wait(), fixture.Value);

                // Note: Why doesn't the list match the above one? We're supposed
                // to suppress duplicate notifications, of course :)
                new[] { -5, 1, 2, 3, 4 }.AssertAreEqual(output);
            });
        }
        public void RxphShouldProvideLatestValue()
        {
            var sched = new TestScheduler();
            var input = new Subject <int>();

            var fixture = new RxPropertyHelper <int>(
                input,
                _ => { },
                -5,
                scheduler: sched);

            Assert.Equal(-5, fixture.Value);

            new[] { 1, 2, 3, 4 }.ForEach(x => input.OnNext(x));

            sched.Start();
            Assert.Equal(4, fixture.Value);

            input.OnCompleted();
            sched.Start();
            Assert.Equal(4, fixture.Value);
        }
        public void RxphShouldRethrowErrors()
        {
            var input = new Subject <int>();
            var sched = new TestScheduler();

            var fixture = new RxPropertyHelper <int>(input, _ => { }, -5, scheduler: sched);
            var errors  = new List <Exception>();

            Assert.Equal(-5, fixture.Value);
            new[] { 1, 2, 3, 4 }.ForEach(x => input.OnNext(x));

            fixture.ThrownExceptions.Subscribe(errors.Add);

            sched.Start();

            Assert.Equal(4, fixture.Value);

            input.OnError(new Exception("Die!"));

            sched.Start();

            Assert.Equal(4, fixture.Value);
            Assert.Equal(1, errors.Count);
        }
        public RxphNameOfTestFixture()
        {
            this.WhenAnyValue(x => x.IsOnlyOneWord).Select(x => x ?? string.Empty).Select(x => x.Length >= 3 ? x.Substring(0, 3) : x).ToProperty(this, nameof(FirstThreeLettersOfOneWord), out _firstThreeLettersOfOneWord);

            _lastThreeLettersOfOneWord = this.WhenAnyValue(x => x.IsOnlyOneWord).Select(x => x ?? string.Empty).Select(x => x.Length >= 3 ? x.Substring(x.Length - 3, 3) : x).ToProperty(this, nameof(LastThreeLettersOfOneWord));
        }