| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Reactive.Concurrency; |
| 5 |
using Microsoft.Reactive.Testing; |
| 6 |
using System; |
| 7 |
|
| 8 |
namespace ReactiveTests |
| 9 |
{ |
| 10 |
public class MockEnumerable<T> : IEnumerable<T> |
| 11 |
{ |
| 12 |
public readonly TestScheduler Scheduler; |
| 13 |
public readonly List<Subscription> Subscriptions = new List<Subscription>(); |
| 14 |
|
| 15 |
IEnumerable<T> underlyingEnumerable; |
| 16 |
|
| 17 |
public MockEnumerable(TestScheduler scheduler, IEnumerable<T> underlyingEnumerable) |
| 18 |
{ |
| 19 |
if (scheduler == null) |
| 20 |
throw new ArgumentNullException("scheduler"); |
| 21 |
if (underlyingEnumerable == null) |
| 22 |
throw new ArgumentNullException("underlyingEnumerable"); |
| 23 |
|
| 24 |
this.Scheduler = scheduler; |
| 25 |
this.underlyingEnumerable = underlyingEnumerable; |
| 26 |
} |
| 27 |
|
| 28 |
public IEnumerator<T> GetEnumerator() |
| 29 |
{ |
| 30 |
return new MockEnumerator(Scheduler, Subscriptions, underlyingEnumerable.GetEnumerator()); |
| 31 |
} |
| 32 |
|
| 33 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| 34 |
{ |
| 35 |
return GetEnumerator(); |
| 36 |
} |
| 37 |
|
| 38 |
class MockEnumerator : IEnumerator<T> |
| 39 |
{ |
| 40 |
List<Subscription> subscriptions; |
| 41 |
IEnumerator<T> enumerator; |
| 42 |
TestScheduler scheduler; |
| 43 |
int index; |
| 44 |
bool disposed = false; |
| 45 |
|
| 46 |
public MockEnumerator(TestScheduler scheduler, List<Subscription> subscriptions, IEnumerator<T> enumerator) |
| 47 |
{ |
| 48 |
this.subscriptions = subscriptions; |
| 49 |
this.enumerator = enumerator; |
| 50 |
this.scheduler = scheduler; |
| 51 |
|
| 52 |
index = subscriptions.Count; |
| 53 |
subscriptions.Add(new Subscription(scheduler.Clock)); |
| 54 |
} |
| 55 |
|
| 56 |
public T Current |
| 57 |
{ |
| 58 |
get |
| 59 |
{ |
| 60 |
if (disposed) |
| 61 |
throw new ObjectDisposedException("this"); |
| 62 |
return enumerator.Current; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public void Dispose() |
| 67 |
{ |
| 68 |
if (!disposed) |
| 69 |
{ |
| 70 |
disposed = true; |
| 71 |
enumerator.Dispose(); |
| 72 |
subscriptions[index] = new Subscription(subscriptions[index].Subscribe, scheduler.Clock); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
object System.Collections.IEnumerator.Current |
| 77 |
{ |
| 78 |
get { return Current; } |
| 79 |
} |
| 80 |
|
| 81 |
public bool MoveNext() |
| 82 |
{ |
| 83 |
if (disposed) |
| 84 |
throw new ObjectDisposedException("this"); |
| 85 |
return enumerator.MoveNext(); |
| 86 |
} |
| 87 |
|
| 88 |
public void Reset() |
| 89 |
{ |
| 90 |
if (disposed) |
| 91 |
throw new ObjectDisposedException("this"); |
| 92 |
enumerator.Reset(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
} |