-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShould.cs
137 lines (126 loc) · 4.65 KB
/
Should.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
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using NUnit.Framework;
namespace ShouldBe
{
/// <summary>
/// Extension class for testing exceptions
/// </summary>
[DebuggerStepThrough]
[ShouldBeMethods]
public static class Should
{
/// <summary>
/// Test that an action throws an exception of type <typeparamref name="TException"/>.
/// </summary>
/// <typeparam name="TException">Expected exception type</typeparam>
/// <param name="action">Action that should throw exception</param>
/// <returns>Exception thrown</returns>
public static TException ShouldThrowException<TException>(Action action)
where TException : Exception
{
try
{
action();
}
catch (TException e)
{
return e;
}
catch (Exception e)
{
ShouldBeMessage.Fail(e.GetType(), typeof(TException));
}
throw new AssertionException("Should throw " + typeof(TException) + " but failed to throw an exception.");
}
/// <summary>
/// Test that an action throws an exception of
/// type <typeparamref name="TException"/> that
/// contains a message substring.
/// </summary>
/// <typeparam name="TException">Expected exception type</typeparam>
/// <param name="action">Action that should throw exception</param>
/// <param name="containsMessage"></param>
/// <returns>Exception thrown</returns>
public static TException ShouldThrowExceptionContaining<TException>(Action action, string containsMessage)
where TException : Exception
{
try
{
action();
}
catch (TException e)
{
if (e.Message.Contains(containsMessage))
{
return e;
}
ShouldBeMessage.Fail(e.Message, containsMessage);
}
catch (Exception e)
{
ShouldBeMessage.Fail(e.GetType(), typeof(TException));
}
throw new AssertionException("Should throw " + typeof(TException) + " but failed to throw an exception.");
}
#if NET_45_OR_GREATER
/// <summary>
/// Test that an action throws an exception of type <typeparamref name="TException"/>.
/// </summary>
/// <typeparam name="TException">Expected exception type</typeparam>
/// <param name="action">Action that should throw exception</param>
/// <returns>Exception thrown</returns>
public static async Task<TException> ShouldThrowExceptionAsync<TException>(Func<Task> action)
where TException : Exception
{
try
{
await action();
}
catch (TException e)
{
return e;
}
catch (Exception e)
{
ShouldBeMessage.Fail(e.GetType(), typeof(TException), "Should throw exception");
}
throw new AssertionException("Should throw " + typeof(TException) + " but failed to throw an exception.");
}
/// <summary>
/// Test that an action throws an exception of
/// type <typeparamref name="TException"/> that
/// contains a message substring.
/// </summary>
/// <typeparam name="TException">Expected exception type</typeparam>
/// <param name="action">Action that should throw exception</param>
/// <param name="containsMessage"></param>
/// <returns>Exception thrown</returns>
public static async Task<TException> ShouldThrowExceptionContainingAsync<TException>(Func<Task> action, string containsMessage)
where TException : Exception
{
try
{
await action();
}
catch (TException e)
{
if (e.Message.Contains(containsMessage))
{
return e;
}
ShouldBeMessage.Fail(e.Message, containsMessage, "Should throw " + typeof(TException) + " containing");
}
catch (Exception e)
{
ShouldBeMessage.Fail(e.GetType(), typeof(TException), "Should throw exception");
}
throw new AssertionException("Should throw " + typeof(TException) + " but failed to throw an exception.");
}
#endif
}
}