Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MockEnumerable.cs « Tests.System.Reactive « Source « NET « Rx - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d91052877ba676723ac112d1c773ad75c3eee8e (plain)
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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Reactive.Concurrency;
using Microsoft.Reactive.Testing;
using System;

namespace ReactiveTests
{
    public class MockEnumerable<T> : IEnumerable<T>
    {
        public readonly TestScheduler Scheduler;
        public readonly List<Subscription> Subscriptions = new List<Subscription>();

        IEnumerable<T> underlyingEnumerable;

        public MockEnumerable(TestScheduler scheduler, IEnumerable<T> underlyingEnumerable)
        {
            if (scheduler == null)
                throw new ArgumentNullException("scheduler");
            if (underlyingEnumerable == null)
                throw new ArgumentNullException("underlyingEnumerable");

            this.Scheduler = scheduler;
            this.underlyingEnumerable = underlyingEnumerable;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return new MockEnumerator(Scheduler, Subscriptions, underlyingEnumerable.GetEnumerator());
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        class MockEnumerator : IEnumerator<T>
        {
            List<Subscription> subscriptions;
            IEnumerator<T> enumerator;
            TestScheduler scheduler;
            int index;
            bool disposed = false;

            public MockEnumerator(TestScheduler scheduler, List<Subscription> subscriptions, IEnumerator<T> enumerator)
            {
                this.subscriptions = subscriptions;
                this.enumerator = enumerator;
                this.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();
            }
        }

    }
}