forked from fluentassertions/fluentassertions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionAssertionHelpers.cs
64 lines (55 loc) · 2.02 KB
/
FunctionAssertionHelpers.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
using System;
using FluentAssertions.Common;
using FluentAssertions.Execution;
namespace FluentAssertions.Specialized
{
internal class FunctionAssertionHelpers
{
internal static T NotThrow<T>(Func<T> subject, string because, object[] becauseArgs)
{
try
{
return subject();
}
catch (Exception exception)
{
Execute.Assertion
.ForCondition(exception is null)
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect any exception{reason}, but found {0}.", exception);
return default;
}
}
internal static TResult NotThrowAfter<TResult>(Func<TResult> subject, IClock clock, TimeSpan waitTime, TimeSpan pollInterval, string because, object[] becauseArgs)
{
if (waitTime < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative.");
}
if (pollInterval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(pollInterval), $"The value of {nameof(pollInterval)} must be non-negative.");
}
TimeSpan? invocationEndTime = null;
Exception exception = null;
ITimer timer = clock.StartTimer();
while (invocationEndTime is null || invocationEndTime < waitTime)
{
try
{
return subject();
}
catch (Exception ex)
{
exception = ex;
}
clock.Delay(pollInterval);
invocationEndTime = timer.Elapsed;
}
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception);
return default;
}
}
}