forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeAsserts.cs
74 lines (68 loc) · 3.44 KB
/
RangeAsserts.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
using System;
using System.Collections.Generic;
using Xunit.Sdk;
namespace Xunit
{
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
/// <summary>
/// Verifies that a value is within a given range.
/// </summary>
/// <typeparam name="T">The type of the value to be compared</typeparam>
/// <param name="actual">The actual value to be evaluated</param>
/// <param name="low">The (inclusive) low value of the range</param>
/// <param name="high">The (inclusive) high value of the range</param>
/// <exception cref="InRangeException">Thrown when the value is not in the given range</exception>
public static void InRange<T>(T actual, T low, T high) where T : IComparable
{
InRange(actual, low, high, GetComparer<T>());
}
/// <summary>
/// Verifies that a value is within a given range, using a comparer.
/// </summary>
/// <typeparam name="T">The type of the value to be compared</typeparam>
/// <param name="actual">The actual value to be evaluated</param>
/// <param name="low">The (inclusive) low value of the range</param>
/// <param name="high">The (inclusive) high value of the range</param>
/// <param name="comparer">The comparer used to evaluate the value's range</param>
/// <exception cref="InRangeException">Thrown when the value is not in the given range</exception>
public static void InRange<T>(T actual, T low, T high, IComparer<T> comparer)
{
Assert.GuardArgumentNotNull("comparer", comparer);
if (comparer.Compare(low, actual) > 0 || comparer.Compare(actual, high) > 0)
throw new InRangeException(actual, low, high);
}
/// <summary>
/// Verifies that a value is not within a given range, using the default comparer.
/// </summary>
/// <typeparam name="T">The type of the value to be compared</typeparam>
/// <param name="actual">The actual value to be evaluated</param>
/// <param name="low">The (inclusive) low value of the range</param>
/// <param name="high">The (inclusive) high value of the range</param>
/// <exception cref="NotInRangeException">Thrown when the value is in the given range</exception>
public static void NotInRange<T>(T actual, T low, T high) where T : IComparable
{
NotInRange(actual, low, high, GetComparer<T>());
}
/// <summary>
/// Verifies that a value is not within a given range, using a comparer.
/// </summary>
/// <typeparam name="T">The type of the value to be compared</typeparam>
/// <param name="actual">The actual value to be evaluated</param>
/// <param name="low">The (inclusive) low value of the range</param>
/// <param name="high">The (inclusive) high value of the range</param>
/// <param name="comparer">The comparer used to evaluate the value's range</param>
/// <exception cref="NotInRangeException">Thrown when the value is in the given range</exception>
public static void NotInRange<T>(T actual, T low, T high, IComparer<T> comparer)
{
Assert.GuardArgumentNotNull("comparer", comparer);
if (comparer.Compare(low, actual) <= 0 && comparer.Compare(actual, high) <= 0)
throw new NotInRangeException(actual, low, high);
}
}
}