forked from PeteGoo/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.cs
More file actions
193 lines (177 loc) · 8.51 KB
/
TestUtils.cs
File metadata and controls
193 lines (177 loc) · 8.51 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Reactive;
using System.Reactive.Disposables;
using Microsoft.Reactive.Testing;
using ReactiveUI;
using System.Reactive.Concurrency;
namespace ReactiveUI.Testing
{
public static class TestUtils
{
/// <summary>
/// WithScheduler overrides the default Deferred and Taskpool schedulers
/// with the given scheduler until the return value is disposed. This
/// is useful in a unit test runner to force RxXaml objects to schedule
/// via a TestScheduler object.
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <returns>An object that when disposed, restores the previous default
/// schedulers.</returns>
public static IDisposable WithScheduler(IScheduler sched)
{
var prevDef = RxApp.DeferredScheduler;
var prevTask = RxApp.TaskpoolScheduler;
RxApp.DeferredScheduler = sched;
RxApp.TaskpoolScheduler = sched;
return Disposable.Create(() => {
RxApp.DeferredScheduler = prevDef;
RxApp.TaskpoolScheduler = prevTask;
});
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Func. Use
/// this to initialize objects that store the default scheduler (most
/// RxXaml objects).
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The function to execute.</param>
/// <returns>The return value of the function.</returns>
public static TRet With<TRet>(this IScheduler sched, Func<IScheduler, TRet> block)
{
TRet ret;
using (WithScheduler(sched)) {
ret = block(sched);
}
return ret;
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Action.
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The action to execute.</param>
public static void With(this IScheduler sched, Action<IScheduler> block)
{
sched.With(x => { block(x); return 0; });
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Func. Use
/// this to initialize objects that store the default scheduler (most
/// RxXaml objects).
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The function to execute.</param>
/// <returns>The return value of the function.</returns>
public static TRet With<TRet>(this TestScheduler sched, Func<TestScheduler, TRet> block)
{
TRet ret;
using (WithScheduler(sched)) {
ret = block(sched);
}
return ret;
}
/// <summary>
/// With is an extension method that uses the given scheduler as the
/// default Deferred and Taskpool schedulers for the given Action.
/// </summary>
/// <param name="sched">The scheduler to use.</param>
/// <param name="block">The action to execute.</param>
public static void With(this TestScheduler sched, Action<TestScheduler> block)
{
sched.With(x => { block(x); return 0; });
}
/// <summary>
/// WithMessageBus allows you to override the default Message Bus
/// implementation until the object returned is disposed. If a
/// message bus is not specified, a default empty one is created.
/// </summary>
/// <param name="messageBus">The message bus to use, or null to create
/// a new one using the default implementation.</param>
/// <returns>An object that when disposed, restores the original
/// message bus.</returns>
public static IDisposable WithMessageBus(this TestScheduler sched, IMessageBus messageBus = null)
{
var origMessageBus = RxApp.MessageBus;
RxApp.MessageBus = messageBus ?? new MessageBus();
return Disposable.Create(() => RxApp.MessageBus = origMessageBus);
}
/// <summary>
/// WithMessageBus allows you to override the default Message Bus
/// implementation for a specified action. If a message bus is not
/// specified, a default empty one is created.
/// <param name="block">The action to execute.</param>
/// <param name="messageBus">The message bus to use, or null to create
/// a new one using the default implementation.</param>
public static void WithMessageBus(this TestScheduler sched, Action<IMessageBus> block, IMessageBus messageBus = null)
{
messageBus = messageBus ?? new MessageBus();
using(var _ = sched.WithMessageBus(messageBus)) {
block(messageBus);
}
}
/// <summary>
/// RunToMilliseconds moves the TestScheduler to the specified time in
/// milliseconds.
/// </summary>
/// <param name="milliseconds">The time offset to set the TestScheduler
/// to, in milliseconds. Note that this is *not* additive or
/// incremental, it sets the time.</param>
public static void RunToMilliseconds(this TestScheduler sched, double milliseconds)
{
Console.WriteLine("Running to time t={0}", milliseconds);
sched.AdvanceTo(sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)));
}
/// <summary>
/// OnNextAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <param name="milliseconds">The time offset to fire the notification
/// on the recorded notification.</param>
/// <param name="value">The value to produce.</param>
/// <returns>A recorded notification that can be provided to
/// TestScheduler.CreateHotObservable.</returns>
public static Recorded<Notification<T>> OnNextAt<T>(this TestScheduler sched, double milliseconds, T value)
{
return new Recorded<Notification<T>>(
sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnNext<T>(value));
}
/// <summary>
/// OnErrorAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <param name="milliseconds">The time offset to fire the notification
/// on the recorded notification.</param>
/// <param name="exception">The exception to terminate the Observable
/// with.</param>
/// <returns>A recorded notification that can be provided to
/// TestScheduler.CreateHotObservable.</returns>
public static Recorded<Notification<T>> OnErrorAt<T>(this TestScheduler sched, double milliseconds, Exception ex)
{
return new Recorded<Notification<T>>(
sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnError<T>(ex));
}
/// <summary>
/// OnCompletedAt is a method to help create simulated input Observables in
/// conjunction with CreateHotObservable or CreateColdObservable.
/// </summary>
/// <param name="milliseconds">The time offset to fire the notification
/// on the recorded notification.</param>
/// <returns>A recorded notification that can be provided to
/// TestScheduler.CreateHotObservable.</returns>
public static Recorded<Notification<T>> OnCompletedAt<T>(this TestScheduler sched, double milliseconds)
{
return new Recorded<Notification<T>>(
sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds)),
Notification.CreateOnCompleted<T>());
}
public static long FromTimeSpan(this TestScheduler sched, TimeSpan span)
{
return span.Ticks;
}
}
}
// vim: tw=120 ts=4 sw=4 et :