forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableAsPropertyHelperTest.cs
128 lines (98 loc) · 3.74 KB
/
ObservableAsPropertyHelperTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System.Reactive.Linq;
using System.Reactive.Subjects;
using ReactiveUI;
using ReactiveUI.Testing;
using Xunit;
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Reactive.Testing;
namespace ReactiveUI.Tests
{
public class ObservableAsPropertyHelperTest
{
[Fact]
public void OAPHShouldFireChangeNotifications()
{
var input = new[] {1, 2, 3, 3, 4}.ToObservable();
var output = new List<int>();
(new TestScheduler()).With(sched => {
var fixture = new ObservableAsPropertyHelper<int>(input,
x => output.Add(x), -5);
sched.Start();
// 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);
});
}
[Fact]
public void OAPHShouldProvideLatestValue()
{
var sched = new TestScheduler();
var input = new Subject<int>();
var fixture = new ObservableAsPropertyHelper<int>(input,
_ => { }, -5, sched);
Assert.Equal(-5, fixture.Value);
(new[] { 1, 2, 3, 4 }).Run(x => input.OnNext(x));
sched.Start();
Assert.Equal(4, fixture.Value);
input.OnCompleted();
sched.Start();
Assert.Equal(4, fixture.Value);
}
[Fact]
public void OAPHShouldRethrowErrors()
{
var input = new Subject<int>();
var sched = new TestScheduler();
var fixture = new ObservableAsPropertyHelper<int>(input, _ => { }, -5, sched);
var errors = new List<Exception>();
Assert.Equal(-5, fixture.Value);
(new[] { 1, 2, 3, 4 }).Run(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);
}
[Fact]
public void NoThrownExceptionsSubscriberEqualsOAPHDeath()
{
(new TestScheduler()).With(sched => {
var input = new Subject<int>();
var fixture = new ObservableAsPropertyHelper<int>(input, _ => { }, -5);
Assert.Equal(-5, fixture.Value);
(new[] { 1, 2, 3, 4 }).Run(x => input.OnNext(x));
input.OnError(new Exception("Die!"));
bool failed = true;
try {
sched.Start();
} catch (Exception ex) {
failed = ex.InnerException.Message != "Die!";
}
Assert.False(failed);
Assert.Equal(4, fixture.Value);
});
}
[Fact]
public void OAPHShouldBeObservable()
{
(new TestScheduler()).With(sched => {
var input = sched.CreateHotObservable(
sched.OnNextAt(100, 5),
sched.OnNextAt(200, 10),
sched.OnNextAt(300, 15),
sched.OnNextAt(400, 20)
);
var result = new List<string>();
var inputOaph = new ObservableAsPropertyHelper<int>(input, x => { }, 0);
var fixture = new ObservableAsPropertyHelper<string>(inputOaph.Select(x => x.ToString()),
result.Add, "0");
sched.AdvanceToMs(500);
new[] {"0", "5", "10", "15", "20"}.AssertAreEqual(result);
});
}
}
}