-
-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy pathTimes.cs
313 lines (279 loc) · 11.8 KB
/
Times.cs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using Moq.Properties;
namespace Moq
{
/// <summary>
/// Defines the number of invocations allowed by a mocked method.
/// </summary>
public readonly struct Times : IEquatable<Times>
{
readonly int from;
readonly int to;
readonly Kind kind;
Times(Kind kind, int from, int to)
{
this.from = from;
this.to = to;
this.kind = kind;
}
/// <summary>Deconstructs this instance.</summary>
/// <param name="from">This output parameter will receive the minimum required number of calls satisfying this instance (i.e. the lower inclusive bound).</param>
/// <param name="to">This output parameter will receive the maximum allowed number of calls satisfying this instance (i.e. the upper inclusive bound).</param>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public void Deconstruct(out int from, out int to)
{
if (this.kind == default)
{
// This branch makes `default(Times)` equivalent to `Times.AtLeastOnce()`,
// which is the implicit default across Moq's API for overloads that don't
// accept a `Times` instance. While user code shouldn't use `default(Times)`
// (but instead either specify `Times` explicitly or not at all), it is
// easy enough to correct:
Debug.Assert(this.kind == Kind.AtLeastOnce);
from = 1;
to = int.MaxValue;
}
else
{
from = this.from;
to = this.to;
}
}
/// <summary>
/// Specifies that a mocked method should be invoked <paramref name="callCount"/> times
/// as minimum.
/// </summary>
/// <param name="callCount">The minimum number of times.</param>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times AtLeast(int callCount)
{
if (callCount < 1)
{
throw new ArgumentOutOfRangeException(nameof(callCount));
}
return new Times(Kind.AtLeast, callCount, int.MaxValue);
}
/// <summary>
/// Specifies that a mocked method should be invoked one time as minimum.
/// </summary>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times AtLeastOnce()
{
return new Times(Kind.AtLeastOnce, 1, int.MaxValue);
}
/// <summary>
/// Specifies that a mocked method should be invoked <paramref name="callCount"/> times
/// as maximum.
/// </summary>
/// <param name="callCount">The maximum number of times.</param>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times AtMost(int callCount)
{
if (callCount < 0)
{
throw new ArgumentOutOfRangeException(nameof(callCount));
}
return new Times(Kind.AtMost, 0, callCount);
}
/// <summary>
/// Specifies that a mocked method should be invoked one time as maximum.
/// </summary>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times AtMostOnce()
{
return new Times(Kind.AtMostOnce, 0, 1);
}
/// <summary>
/// Specifies that a mocked method should be invoked between
/// <paramref name="callCountFrom"/> and <paramref name="callCountTo"/> times.
/// </summary>
/// <param name="callCountFrom">The minimum number of times.</param>
/// <param name="callCountTo">The maximum number of times.</param>
/// <param name="rangeKind">The kind of range. See <see cref="Range"/>.</param>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times Between(int callCountFrom, int callCountTo, Range rangeKind)
{
if (rangeKind == Range.Exclusive)
{
if (callCountFrom <= 0 || callCountTo <= callCountFrom)
{
throw new ArgumentOutOfRangeException(nameof(callCountFrom));
}
if (callCountTo - callCountFrom == 1)
{
throw new ArgumentOutOfRangeException(nameof(callCountTo));
}
return new Times(Kind.BetweenExclusive, callCountFrom + 1, callCountTo - 1);
}
if (callCountFrom < 0 || callCountTo < callCountFrom)
{
throw new ArgumentOutOfRangeException(nameof(callCountFrom));
}
return new Times(Kind.BetweenInclusive, callCountFrom, callCountTo);
}
/// <summary>
/// Specifies that a mocked method should be invoked exactly
/// <paramref name="callCount"/> times.
/// </summary>
/// <param name="callCount">The times that a method or property can be called.</param>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times Exactly(int callCount)
{
if (callCount < 0)
{
throw new ArgumentOutOfRangeException(nameof(callCount));
}
return new Times(Kind.Exactly, callCount, callCount);
}
/// <summary>
/// Specifies that a mocked method should not be invoked.
/// </summary>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times Never()
{
return new Times(Kind.Never, 0, 0);
}
/// <summary>
/// Specifies that a mocked method should be invoked exactly one time.
/// </summary>
/// <returns>An object defining the allowed number of invocations.</returns>
public static Times Once()
{
return new Times(Kind.Once, 1, 1);
}
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified <see cref="Times"/> value.
/// </summary>
/// <param name="other">A <see cref="Times"/> value to compare to this instance.</param>
/// <returns>
/// <see langword="true"/> if <paramref name="other"/> has the same value as this instance;
/// otherwise, <see langword="false"/>.
/// </returns>
public bool Equals(Times other)
{
var (from, to) = this;
var (otherFrom, otherTo) = other;
return from == otherFrom && to == otherTo;
}
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified <see cref="Times"/> value.
/// </summary>
/// <param name="obj">An object to compare to this instance.</param>
/// <returns>
/// <see langword="true"/> if <paramref name="obj"/> has the same value as this instance;
/// otherwise, <see langword="false"/>.
/// </returns>
public override bool Equals(object obj)
{
return obj is Times other && this.Equals(other);
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms
/// and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
var (from, to) = this;
return from.GetHashCode() ^ to.GetHashCode();
}
/// <summary>
/// Determines whether two specified <see cref="Times"/> objects have the same value.
/// </summary>
/// <param name="left">The first <see cref="Times"/>.</param>
/// <param name="right">The second <see cref="Times"/>.</param>
/// <returns>
/// <see langword="true"/> if <paramref name="left"/> has the same value as <paramref name="right"/>;
/// otherwise, <see langword="false"/>.
/// </returns>
public static bool operator ==(Times left, Times right)
{
return left.Equals(right);
}
/// <summary>
/// Determines whether two specified <see cref="Times"/> objects have different values.
/// </summary>
/// <param name="left">The first <see cref="Times"/>.</param>
/// <param name="right">The second <see cref="Times"/>.</param>
/// <returns>
/// <see langword="true"/> if the value of <paramref name="left"/> is different from
/// <paramref name="right"/>'s; otherwise, <see langword="false"/>.
/// </returns>
public static bool operator !=(Times left, Times right)
{
return !left.Equals(right);
}
/// <inheritdoc/>
public override string ToString()
{
return this.kind switch
{
Kind.AtLeastOnce => "AtLeastOnce",
Kind.AtLeast => $"AtLeast({this.from})",
Kind.AtMost => $"AtMost({this.to})",
Kind.AtMostOnce => "AtMostOnce",
Kind.BetweenExclusive => $"Between({this.from - 1}, {this.to + 1}, Exclusive)",
Kind.BetweenInclusive => $"Between({this.from}, {this.to}, Inclusive)",
Kind.Exactly => $"Exactly({this.from})",
Kind.Once => "Once",
Kind.Never => "Never",
_ => throw new InvalidOperationException(),
};
}
internal string GetExceptionMessage(int callCount)
{
var (from, to) = this;
if (this.kind == Kind.BetweenExclusive)
{
--from;
++to;
}
var message = this.kind switch
{
Kind.AtLeastOnce => Resources.NoMatchingCallsAtLeastOnce,
Kind.AtLeast => Resources.NoMatchingCallsAtLeast,
Kind.AtMost => Resources.NoMatchingCallsAtMost,
Kind.AtMostOnce => Resources.NoMatchingCallsAtMostOnce,
Kind.BetweenExclusive => Resources.NoMatchingCallsBetweenExclusive,
Kind.BetweenInclusive => Resources.NoMatchingCallsBetweenInclusive,
Kind.Exactly => Resources.NoMatchingCallsExactly,
Kind.Once => Resources.NoMatchingCallsOnce,
Kind.Never => Resources.NoMatchingCallsNever,
_ => throw new InvalidOperationException(),
};
return string.Format(CultureInfo.CurrentCulture, message, from, to, callCount);
}
/// <summary>
/// Checks whether the specified number of invocations matches the constraint described by this instance.
/// </summary>
/// <param name="count">The number of invocations to check.</param>
/// <returns>
/// <see langword="true"/> if <paramref name="count"/> matches the constraint described by this instance;
/// otherwise, <see langword="false"/>.
/// </returns>
public bool Validate(int count)
{
var (from, to) = this;
return from <= count && count <= to;
}
enum Kind
{
AtLeastOnce,
AtLeast,
AtMost,
AtMostOnce,
BetweenExclusive,
BetweenInclusive,
Exactly,
Once,
Never,
}
}
}