123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.Collections.Generic;
- using Microsoft.Reactive.Testing;
- namespace ReactiveTests
- {
- public class MockEnumerable<T> : IEnumerable<T>
- {
- public readonly TestScheduler Scheduler;
- public readonly List<Subscription> Subscriptions = new();
- private readonly IEnumerable<T> _underlyingEnumerable;
- public MockEnumerable(TestScheduler scheduler, IEnumerable<T> underlyingEnumerable)
- {
- Scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
- _underlyingEnumerable = underlyingEnumerable ?? throw new ArgumentNullException(nameof(underlyingEnumerable));
- }
- public IEnumerator<T> GetEnumerator()
- {
- return new MockEnumerator(Scheduler, Subscriptions, _underlyingEnumerable.GetEnumerator());
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- private class MockEnumerator : IEnumerator<T>
- {
- private readonly List<Subscription> _subscriptions;
- private readonly IEnumerator<T> _enumerator;
- private readonly TestScheduler _scheduler;
- private readonly int _index;
- private bool _disposed;
- public MockEnumerator(TestScheduler scheduler, List<Subscription> subscriptions, IEnumerator<T> enumerator)
- {
- _subscriptions = subscriptions;
- _enumerator = enumerator;
- _scheduler = scheduler;
- _index = subscriptions.Count;
- subscriptions.Add(new Subscription(scheduler.Clock));
- }
- public T Current
- {
- get
- {
- if (_disposed)
- {
- throw new ObjectDisposedException("this");
- }
- return _enumerator.Current;
- }
- }
- public void Dispose()
- {
- if (!_disposed)
- {
- _disposed = true;
- _enumerator.Dispose();
- _subscriptions[_index] = new Subscription(_subscriptions[_index].Subscribe, _scheduler.Clock);
- }
- }
- object System.Collections.IEnumerator.Current
- {
- get { return Current; }
- }
- public bool MoveNext()
- {
- if (_disposed)
- {
- throw new ObjectDisposedException("this");
- }
- return _enumerator.MoveNext();
- }
- public void Reset()
- {
- if (_disposed)
- {
- throw new ObjectDisposedException("this");
- }
- _enumerator.Reset();
- }
- }
- }
- }
|