-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathRangeAsserts.cs
103 lines (95 loc) · 3.79 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
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
#pragma warning disable CA1052 // Static holder types should be static
#pragma warning disable IDE0058 // Expression value is never used
#pragma warning disable IDE0161 // Convert to file-scoped namespace
#if XUNIT_NULLABLE
#nullable enable
#else
// In case this is source-imported with global nullable enabled but no XUNIT_NULLABLE
#pragma warning disable CS8604
#endif
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, GetRangeComparer<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)
{
GuardArgumentNotNull(nameof(actual), actual);
GuardArgumentNotNull(nameof(low), low);
GuardArgumentNotNull(nameof(high), high);
GuardArgumentNotNull(nameof(comparer), comparer);
if (comparer.Compare(low, actual) > 0 || comparer.Compare(actual, high) > 0)
throw InRangeException.ForValueNotInRange(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, GetRangeComparer<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)
{
GuardArgumentNotNull(nameof(actual), actual);
GuardArgumentNotNull(nameof(low), low);
GuardArgumentNotNull(nameof(high), high);
GuardArgumentNotNull(nameof(comparer), comparer);
if (comparer.Compare(low, actual) <= 0 && comparer.Compare(actual, high) <= 0)
throw NotInRangeException.ForValueInRange(actual, low, high);
}
}
}