|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
| 4 | +using System.Linq.Expressions; |
4 | 5 | using System.Reactive;
|
5 | 6 | using System.Reactive.Linq;
|
6 | 7 | using System.Reactive.Threading.Tasks;
|
|
9 | 10 | using FluentAssertions.Execution;
|
10 | 11 | using FluentAssertions.Primitives;
|
11 | 12 | using FluentAssertions.Specialized;
|
| 13 | +using JetBrains.Annotations; |
12 | 14 | using Microsoft.Reactive.Testing;
|
13 | 15 |
|
14 | 16 | namespace FluentAssertions.Reactive
|
@@ -106,18 +108,18 @@ public async Task<AndWhichConstraint<ReactiveAssertions<TPayload>, IEnumerable<T
|
106 | 108 | }
|
107 | 109 |
|
108 | 110 | /// <summary>
|
109 |
| - /// Asserts that at least <paramref name="numberOfNotifications"/> notifications are pushed to the <see cref="FluentTestObserver{TPayload}"/> within the next 1 second.<br /> |
| 111 | + /// Asserts that at least <paramref name="numberOfNotifications"/> notifications are pushed to the <see cref="FluentTestObserver{TPayload}"/> within the next 1 seconds.<br /> |
110 | 112 | /// This includes any previously recorded notifications since it has been created or cleared.
|
111 | 113 | /// </summary>
|
112 | 114 | /// <param name="numberOfNotifications">the number of notifications the observer should have recorded by now</param>
|
113 | 115 | /// <param name="because"></param>
|
114 | 116 | /// <param name="becauseArgs"></param>
|
115 | 117 | public AndWhichConstraint<ReactiveAssertions<TPayload>, IEnumerable<TPayload>> Push(int numberOfNotifications, string because = "", params object[] becauseArgs)
|
116 |
| - => Push(numberOfNotifications, TimeSpan.FromSeconds(10), because, becauseArgs); |
| 118 | + => Push(numberOfNotifications, TimeSpan.FromSeconds(1), because, becauseArgs); |
117 | 119 |
|
118 | 120 | /// <inheritdoc cref="Push(int,string,object[])"/>
|
119 | 121 | public Task<AndWhichConstraint<ReactiveAssertions<TPayload>, IEnumerable<TPayload>>> PushAsync(int numberOfNotifications, string because = "", params object[] becauseArgs)
|
120 |
| - => PushAsync(numberOfNotifications, TimeSpan.FromSeconds(10), because, becauseArgs); |
| 122 | + => PushAsync(numberOfNotifications, TimeSpan.FromSeconds(1), because, becauseArgs); |
121 | 123 |
|
122 | 124 | /// <summary>
|
123 | 125 | /// Asserts that at least 1 notification is pushed to the <see cref="FluentTestObserver{TPayload}"/> within the next 1 second.<br />
|
@@ -248,6 +250,133 @@ public AndConstraint<ReactiveAssertions<TPayload>> NotComplete(TimeSpan timeout,
|
248 | 250 | public AndConstraint<ReactiveAssertions<TPayload>> NotComplete(string because = "", params object[] becauseArgs)
|
249 | 251 | => NotComplete(TimeSpan.FromMilliseconds(100), because, becauseArgs);
|
250 | 252 |
|
| 253 | + |
| 254 | + /// <summary> |
| 255 | + /// Asserts that at least one notification matching <paramref name="predicate"/> was pushed to the <see cref="FluentTestObserver{TPayload}"/> |
| 256 | + /// within the specified <paramref name="timeout"/>.<br /> |
| 257 | + /// This includes any previously recorded notifications since it has been created or cleared. |
| 258 | + /// </summary> |
| 259 | + /// <param name="predicate">A predicate to match the items in the collection against.</param> |
| 260 | + /// <param name="timeout">the maximum time to wait for the notification to arrive</param> |
| 261 | + /// <param name="because"> |
| 262 | + /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion |
| 263 | + /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. |
| 264 | + /// </param> |
| 265 | + /// <param name="becauseArgs"> |
| 266 | + /// Zero or more objects to format using the placeholders in <paramref name="because"/>. |
| 267 | + /// </param> |
| 268 | + /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is <c>null</c>.</exception> |
| 269 | + public AndConstraint<ReactiveAssertions<TPayload>> PushMatch( |
| 270 | + [NotNull] Expression<Func<TPayload, bool>> predicate, |
| 271 | + TimeSpan timeout, |
| 272 | + string because = "", |
| 273 | + params object[] becauseArgs) |
| 274 | + { |
| 275 | + if (predicate == null) throw new ArgumentNullException(nameof(predicate)); |
| 276 | + |
| 277 | + IList<TPayload> notifications = new List<TPayload>(); |
| 278 | + |
| 279 | + try |
| 280 | + { |
| 281 | + Func<TPayload, bool> func = predicate.Compile(); |
| 282 | + notifications = Observer.RecordedNotificationStream |
| 283 | + .Select(r => r.Value) |
| 284 | + .Dematerialize() |
| 285 | + .Where(func) |
| 286 | + .Take(1) |
| 287 | + .Timeout(timeout) |
| 288 | + .Catch<TPayload, TimeoutException>(exception => Observable.Empty<TPayload>()) |
| 289 | + .ToList() |
| 290 | + .ToTask() |
| 291 | + .ExecuteInDefaultSynchronizationContext(); |
| 292 | + } |
| 293 | + catch (Exception e) |
| 294 | + { |
| 295 | + if (e is AggregateException aggregateException) |
| 296 | + e = aggregateException.InnerException; |
| 297 | + Execute.Assertion |
| 298 | + .BecauseOf(because, becauseArgs) |
| 299 | + .FailWith("Expected {context:observable} to push an item matching {0}{reason}, but it failed with a {1}.", predicate.Body, e); |
| 300 | + } |
| 301 | + |
| 302 | + Execute.Assertion |
| 303 | + .BecauseOf(because, becauseArgs) |
| 304 | + .ForCondition(notifications.Any()) |
| 305 | + .FailWith("Expected {context:observable} to push an item matching {0}{reason} within {1}.", predicate.Body, timeout); |
| 306 | + |
| 307 | + return new AndConstraint<ReactiveAssertions<TPayload>>(this); |
| 308 | + } |
| 309 | + |
| 310 | + /// <summary> |
| 311 | + /// Asserts that at least one notification matching <paramref name="predicate"/> was pushed to the <see cref="FluentTestObserver{TPayload}"/> |
| 312 | + /// within the next 1 second.<br /> |
| 313 | + /// This includes any previously recorded notifications since it has been created or cleared. |
| 314 | + /// </summary> |
| 315 | + /// <param name="predicate">A predicate to match the items in the collection against.</param> |
| 316 | + /// <param name="timeout">the maximum time to wait for the notification to arrive</param> |
| 317 | + /// <param name="because"> |
| 318 | + /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion |
| 319 | + /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. |
| 320 | + /// </param> |
| 321 | + /// <param name="becauseArgs"> |
| 322 | + /// Zero or more objects to format using the placeholders in <paramref name="because"/>. |
| 323 | + /// </param> |
| 324 | + /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is <c>null</c>.</exception> |
| 325 | + public AndConstraint<ReactiveAssertions<TPayload>> PushMatch( |
| 326 | + [NotNull] Expression<Func<TPayload, bool>> predicate, |
| 327 | + string because = "", |
| 328 | + params object[] becauseArgs) |
| 329 | + => PushMatch(predicate, TimeSpan.FromSeconds(1), because, becauseArgs); |
| 330 | + |
| 331 | + /// <inheritdoc cref="PushMatch(Expression{Func{TPayload, bool}},TimeSpan,string,object[])"/> |
| 332 | + public async Task<AndConstraint<ReactiveAssertions<TPayload>>> PushMatchAsync( |
| 333 | + [NotNull] Expression<Func<TPayload, bool>> predicate, |
| 334 | + TimeSpan timeout, |
| 335 | + string because = "", |
| 336 | + params object[] becauseArgs) |
| 337 | + { |
| 338 | + if (predicate == null) |
| 339 | + throw new ArgumentNullException(nameof(predicate)); |
| 340 | + |
| 341 | + IList<TPayload> notifications = new List<TPayload>(); |
| 342 | + |
| 343 | + try |
| 344 | + { |
| 345 | + Func<TPayload, bool> func = predicate.Compile(); |
| 346 | + notifications = await Observer.RecordedNotificationStream |
| 347 | + .Select(r => r.Value) |
| 348 | + .Dematerialize() |
| 349 | + .Where(func) |
| 350 | + .Take(1) |
| 351 | + .Timeout(timeout) |
| 352 | + .Catch<TPayload, TimeoutException>(exception => Observable.Empty<TPayload>()) |
| 353 | + .ToList() |
| 354 | + .ToTask().ConfigureAwait(false); |
| 355 | + } |
| 356 | + catch (Exception e) |
| 357 | + { |
| 358 | + if (e is AggregateException aggregateException) |
| 359 | + e = aggregateException.InnerException; |
| 360 | + Execute.Assertion |
| 361 | + .BecauseOf(because, becauseArgs) |
| 362 | + .FailWith("Expected {context:observable} to push an item matching {0}{reason}, but it failed with a {1}.", predicate.Body, e); |
| 363 | + } |
| 364 | + |
| 365 | + Execute.Assertion |
| 366 | + .BecauseOf(because, becauseArgs) |
| 367 | + .ForCondition(notifications.Any()) |
| 368 | + .FailWith("Expected {context:observable} to push an item matching {0}{reason} within {1}.", predicate.Body, timeout); |
| 369 | + |
| 370 | + return new AndWhichConstraint<ReactiveAssertions<TPayload>, IEnumerable<TPayload>>(this, notifications); |
| 371 | + } |
| 372 | + |
| 373 | + /// <inheritdoc cref="PushMatch(Expression{Func{TPayload, bool}},string,object[])"/> |
| 374 | + public Task<AndConstraint<ReactiveAssertions<TPayload>>> PushMatchAsync( |
| 375 | + [NotNull] Expression<Func<TPayload, bool>> predicate, |
| 376 | + string because = "", |
| 377 | + params object[] becauseArgs) |
| 378 | + => PushMatchAsync(predicate, TimeSpan.FromSeconds(1), because, becauseArgs); |
| 379 | + |
251 | 380 | protected Task<IList<Recorded<Notification<TPayload>>>> GetRecordedNotifications(TimeSpan timeout) =>
|
252 | 381 | Observer.RecordedNotificationStream
|
253 | 382 | .TakeUntil(recorded => recorded.Value.Kind == NotificationKind.OnError)
|
|
0 commit comments