1
// -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
5 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
6 using System
.Runtime
.Serialization
;
8 namespace System
.UnitTesting
10 public static class ExceptionAssert
12 // NOTE: To catch state corrupting exceptions, it by design that
13 // the ThrowsXXX methods retry by default. To prevent this in a
14 // test, simply use one of the overloads that takes a RetryMode.
17 /// Verifies that the exception has the default message generated by the base Exception class.
19 public static void HasDefaultMessage(Exception exception
)
21 Assert
.IsNotNull(exception
);
23 // Exception of type '[typename]' was thrown
24 StringAssert
.Contains(exception
.Message
, exception
.GetType().FullName
);
28 /// Verifies that the specified action throws a SerializationException.
30 public static SerializationException
ThrowsSerialization(string memberName
, Action action
)
32 var exception
= Throws
<SerializationException
>(RetryMode
.Retry
, action
, (actual
, retryCount
) =>
34 AssertSerializationMemberName(memberName
, actual
, retryCount
);
41 /// Verifies that the specified action throws an ObjectDisposedException.
43 public static ObjectDisposedException
ThrowsDisposed(object instance
, Action action
)
45 var exception
= Throws
<ObjectDisposedException
>(RetryMode
.Retry
, action
, (actual
, retryCount
) =>
47 AssertObjectDisposed(instance
, actual
, retryCount
);
54 /// Verifies that the specified action throws an ArgumentNullException.
56 public static ArgumentNullException
ThrowsArgumentNull(string parameterName
, Action action
)
58 return ThrowsArgument
<ArgumentNullException
>(parameterName
, action
);
62 /// Verifies that the specified action throws an ArgumentException.
64 public static ArgumentException
ThrowsArgument(string parameterName
, Action action
)
66 return ThrowsArgument
<ArgumentException
>(parameterName
, action
);
70 /// Verifies that the specified action throws an ArgumentException of type <typeparam name="T"/>.
72 public static T ThrowsArgument
<T
>(string parameterName
, Action action
)
73 where T
: ArgumentException
75 var exception
= Throws
<T
>(RetryMode
.Retry
, action
, (actual
, retryCount
) =>
77 AssertSameParameterName(parameterName
, actual
, retryCount
);
84 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
85 /// with the specified inner exception of type <typeparam name="TInner"/>.
87 public static T Throws
<T
, TInner
>(Action action
)
89 where TInner
: Exception
91 return Throws
<T
, TInner
>(RetryMode
.Retry
, action
);
95 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
96 /// with the specified inner exception of type <typeparam name="TInner"/>, and indicating
99 public static T Throws
<T
, TInner
>(RetryMode retry
, Action action
)
101 where TInner
: Exception
103 return Throws
<T
, TInner
>(retry
, action
, (Action
<T
, int>)null);
107 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
108 /// with the specified inner exception of type <typeparam name="TInner"/>, indicating
109 /// whether to retry and running the specified validator.
111 public static T Throws
<T
, TInner
>(RetryMode retry
, Action action
, Action
<T
, int> validator
)
113 where TInner
: Exception
115 var exception
= Throws
<T
>(retry
, action
, (actual
, retryCount
) =>
117 AssertIsExactInstanceOfInner(typeof(TInner
), actual
, retryCount
);
119 if (validator
!= null)
121 validator(actual
, retryCount
);
129 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
130 /// with the specified inner exception.
132 public static T Throws
<T
>(Exception innerException
, Action action
)
135 return Throws
<T
>(innerException
, RetryMode
.Retry
, action
, (Action
<T
, int>)null);
139 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
140 /// with the specified inner exception, and indicating whether to retry.
142 public static T Throws
<T
>(Exception innerException
, RetryMode retry
, Action action
)
145 return Throws
<T
>(innerException
, RetryMode
.Retry
, action
, (Action
<T
, int>)null);
149 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
150 /// with the specified inner exception, indicating whether to retry and running the
151 /// specified validator.
153 public static T Throws
<T
>(Exception innerException
, RetryMode retry
, Action action
, Action
<T
, int> validator
)
156 T exception
= Throws
<T
>(retry
, action
, (actual
, retryCount
) =>
158 AssertSameInner(innerException
, actual
, retryCount
);
160 if (validator
!= null)
162 validator(actual
, retryCount
);
170 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>.
172 public static T Throws
<T
>(Action action
)
175 return Throws
<T
>(RetryMode
.Retry
, action
, (Action
<T
, int>)null);
179 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
180 /// indicating whether to retry.
182 public static T Throws
<T
>(RetryMode retry
, Action action
)
185 return Throws
<T
>(retry
, action
, (Action
<T
, int>)null);
189 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
190 /// indicating whether to retry and running the specified validator.
192 public static T Throws
<T
>(RetryMode retry
, Action action
, Action
<T
, int> validator
)
195 var exception
= (T
)Run(retry
, action
, (actual
, retryCount
) =>
197 AssertIsExactInstanceOf(typeof(T
), actual
, retryCount
);
199 if (validator
!= null)
201 validator((T
)actual
, retryCount
);
209 /// Verifies that the specified action throws the specified exception.
211 public static void Throws(Exception expected
, Action action
)
213 Throws(expected
, RetryMode
.Retry
, action
);
217 /// Verifies that the specified action throws the specified exception,
218 /// indicating whether to retry.
220 public static void Throws(Exception expected
, RetryMode retry
, Action action
)
222 Throws(expected
, retry
, action
, (Action
<Exception
, int>)null);
226 /// Verifies that the specified action throws the specified exception,
227 /// indicating whether to retry and running the specified validator.
229 public static void Throws(Exception expected
, RetryMode retry
, Action action
, Action
<Exception
, int> validator
)
231 Run(retry
, action
, (actual
, retryCount
) =>
233 AssertSame(expected
, actual
, retryCount
);
235 if (validator
!= null)
237 validator(actual
, retryCount
);
242 private static Exception
Run(RetryMode retry
, Action action
, Action
<Exception
, int> validator
)
244 Exception exception
= null;
246 for (int i
= -1; i
< (int)retry
; i
++)
248 exception
= Run(action
);
250 validator(exception
, i
+ 2);
256 private static Exception
Run(Action action
)
269 private static void AssertSerializationMemberName(string memberName
, SerializationException actual
, int retryCount
)
271 // Unfortunately, SerializationException does not provide a way to get our hands on the
272 // the actual member that was missing from the SerializationInfo, so we need to grok the
275 // Member '[memberName]' was not found.
276 StringAssert
.Contains(actual
.Message
, "'" + memberName
+ "'", "Retry Count {0}: Expected SerializationException MemberName to be '{1}'", retryCount
, memberName
);
279 private static void AssertObjectDisposed(object instance
, ObjectDisposedException actual
, int retryCount
)
281 string objectName
= instance
.GetType().FullName
;
283 Assert
.AreEqual(objectName
, actual
.ObjectName
, "Retry Count {0}: Expected {1}.ObjectName to be '{2}', however, '{3}' is.", retryCount
, actual
.GetType().Name
, objectName
, actual
.ObjectName
);
286 private static void AssertSameParameterName(string parameterName
, ArgumentException actual
, int retryCount
)
289 Assert
.AreEqual(parameterName
, actual
.ParamName
, "Retry Count {0}: Expected {1}.ParamName to be '{2}', however, '{3}' is.", retryCount
, actual
.GetType().Name
, parameterName
, actual
.ParamName
);
291 // Silverlight doesn't have ArgumentException.ParamName
292 StringAssert
.Contains(actual
.Message
, parameterName
, "Retry Count {0}: Expected {1}.ParamName to be '{2}'", retryCount
, actual
.GetType().Name
, parameterName
);
296 private static void AssertSame(Exception expected
, Exception actual
, int retryCount
)
298 Assert
.AreSame(expected
, actual
, "Retry Count {0}: Expected '{1}' to be thrown, however, '{2}' was thrown.", retryCount
, expected
, actual
);
301 private static void AssertSameInner(Exception innerException
, Exception actual
, int retryCount
)
303 Assert
.AreSame(innerException
, actual
.InnerException
, "Retry Count {0}: Expected '{1}' to be the inner exception, however, '{2}' is.", retryCount
, innerException
, actual
.InnerException
);
306 private static void AssertIsExactInstanceOf(Type expectedType
, Exception actual
, int retryCount
)
309 Assert
.Fail("Retry Count {0}: Expected '{1}' to be thrown", retryCount
, expectedType
);
311 Type actualType
= actual
.GetType();
313 Assert
.AreSame(expectedType
, actualType
, "Retry Count {0}: Expected '{1}' to be thrown, however, '{2}' was thrown.", retryCount
, expectedType
, actualType
);
316 private static void AssertIsExactInstanceOfInner(Type expectedType
, Exception actual
, int retryCount
)
318 if (actual
.InnerException
== null)
319 Assert
.Fail("Retry Count {0}: Expected '{1}' be the inner exception, however, it is null.", retryCount
, expectedType
);
321 Type actualType
= actual
.InnerException
.GetType();
323 Assert
.AreSame(expectedType
, actualType
, "Retry Count {0}: Expected '{1}' to be the inner exception, however, '{2}' is.", retryCount
, expectedType
, actualType
);