Created
December 4, 2013 23:23
-
-
Save bradwilson/7797444 to your computer and use it in GitHub Desktop.
New custom assertions for xUnit.net v2, for developers using the source-based (extensible) assert library via the xunit.assert.source NuGet package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Xunit.Sdk; | |
namespace Xunit | |
{ | |
public partial class Assert | |
{ | |
/// <summary> | |
/// Verifies that two dictionaries are equivalent, using default comparers. | |
/// </summary> | |
/// <typeparam name="TKey">The type of the key</typeparam> | |
/// <typeparam name="TValue">The type of the value</typeparam> | |
/// <param name="expected">The expected dictionary values</param> | |
/// <param name="actual">The actual dictionary values</param> | |
/// <exception cref="EqualException">Thrown when the dictionaries are not equivalent</exception> | |
public static void Equal<TKey, TValue>(IDictionary<TKey, TValue> expected, IDictionary<TKey, TValue> actual) | |
{ | |
Equal<TKey, TValue>(expected, actual, GetEqualityComparer<TKey>(), GetEqualityComparer<TValue>()); | |
} | |
/// <summary> | |
/// Verifies that two dictionaries are equivalent, using the provided comparers. | |
/// </summary> | |
/// <typeparam name="TKey">The type of the key</typeparam> | |
/// <typeparam name="TValue">The type of the value</typeparam> | |
/// <param name="expected">The expected dictionary values</param> | |
/// <param name="actual">The actual dictionary values</param> | |
/// <param name="keyComparer">The comparer for comparing keys</param> | |
/// <param name="valueComparer">The comparer for comparing values</param> | |
/// <exception cref="EqualException">Thrown when the dictionaries are not equivalent</exception> | |
public static void Equal<TKey, TValue>( | |
IDictionary<TKey, TValue> expected, | |
IDictionary<TKey, TValue> actual, | |
IEqualityComparer<TKey> keyComparer = null, | |
IEqualityComparer<TValue> valueComparer = null) | |
{ | |
keyComparer = keyComparer ?? GetEqualityComparer<TKey>(); | |
valueComparer = valueComparer ?? GetEqualityComparer<TValue>(); | |
// TODO: The message here is going to be a little... insufficient. Hopefully | |
// for now, the developer will realize that the equality of the keys is what's | |
// failing. | |
var expectedKeys = expected.Keys.OrderBy(k => k).ToList(); | |
Equal(expectedKeys, actual.Keys.OrderBy(k => k), keyComparer); | |
expectedKeys.ForEach(key => | |
{ | |
var expectedValue = expected[key]; | |
var actualValue = actual[key]; | |
if (!valueComparer.Equals(expectedValue, actualValue)) | |
throw new EqualException(String.Format("{{ {0} = {1} }}", key, expectedValue), String.Format("{{ {0} = {1} }}", key, actualValue)); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Web.Http; | |
using System.Web.Http.Results; | |
using System.Web.Routing; | |
namespace Xunit | |
{ | |
public partial class Assert | |
{ | |
/// <summary> | |
/// Verifies that the given Web API action result is a bad request result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static void HttpBadRequest(IHttpActionResult actionResult) | |
{ | |
IsAssignableFrom<BadRequestResult>(actionResult); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a bad request result | |
/// with the given message. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="message">The error message.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the error message does not match.</exception> | |
public static void HttpBadRequest(IHttpActionResult actionResult, string message) | |
{ | |
var bremResult = IsAssignableFrom<BadRequestErrorMessageResult>(actionResult); | |
Equal(message, bremResult.Message); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a conflict result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static void HttpConflict(IHttpActionResult actionResult) | |
{ | |
IsAssignableFrom<ConflictResult>(actionResult); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a negotiated content result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="statusCode">The status code of the response. (optional)</param> | |
/// <typeparam name="T">The type of the content.</typeparam> | |
/// <returns>Returns the content from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the status codes are not equal.</exception> | |
public static T HttpContent<T>(IHttpActionResult actionResult, HttpStatusCode? statusCode = null) | |
{ | |
var ncResult = IsAssignableFrom<NegotiatedContentResult<T>>(actionResult); | |
if (statusCode.HasValue) | |
Equal(statusCode.GetValueOrDefault(), ncResult.StatusCode); | |
return ncResult.Content; | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a created result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="location">The location where the created content resides.</param> | |
/// <typeparam name="T">The type of the content.</typeparam> | |
/// <returns>Returns the content from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the locations are not equal.</exception> | |
public static T HttpCreated<T>(IHttpActionResult actionResult, string location) | |
{ | |
return HttpCreated<T>(actionResult, new Uri(location)); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a created result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="location">The location where the created content resides.</param> | |
/// <typeparam name="T">The type of the content.</typeparam> | |
/// <returns>Returns the content from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the locations are not equal.</exception> | |
public static T HttpCreated<T>(IHttpActionResult actionResult, Uri location) | |
{ | |
var cResult = IsAssignableFrom<CreatedNegotiatedContentResult<T>>(actionResult); | |
Equal(location, cResult.Location); | |
return cResult.Content; | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a created result from a named route. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="routeName">The route name.</param> | |
/// <typeparam name="T">The type of the content.</typeparam> | |
/// <returns>Returns the content from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the route names are not equal.</exception> | |
public static T HttpCreatedAtRoute<T>(IHttpActionResult actionResult, string routeName) | |
{ | |
return HttpCreatedAtRoute<T>(actionResult, routeName, (IDictionary<string, object>)null); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a created result from a named route | |
/// with the given route values. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="routeName">The route name.</param> | |
/// <param name="routeValues">The route values.</param> | |
/// <typeparam name="T">The type of the content.</typeparam> | |
/// <returns>Returns the content from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the route names or route values are not equal.</exception> | |
public static T HttpCreatedAtRoute<T>(IHttpActionResult actionResult, string routeName, object routeValues) | |
{ | |
return HttpCreatedAtRoute<T>(actionResult, routeName, new RouteValueDictionary(routeValues)); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a created result from a named route | |
/// with the given route values. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="routeName">The route name.</param> | |
/// <param name="routeValues">The route values.</param> | |
/// <typeparam name="T">The type of the content.</typeparam> | |
/// <returns>Returns the content from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the route names or route values are not equal.</exception> | |
public static T HttpCreatedAtRoute<T>(IHttpActionResult actionResult, string routeName, IDictionary<string, object> routeValues) | |
{ | |
var carncResult = IsAssignableFrom<CreatedAtRouteNegotiatedContentResult<T>>(actionResult); | |
Equal(routeName, carncResult.RouteName); | |
if (routeValues != null) | |
Equal(routeValues, carncResult.RouteValues); | |
return carncResult.Content; | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is an internal server error result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static void HttpInternalServerError(IHttpActionResult actionResult) | |
{ | |
IsAssignableFrom<InternalServerErrorResult>(actionResult); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is an internal server error result | |
/// with an exception. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <returns>Returns the exception from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static Exception HttpInternalServerErrorWithException(IHttpActionResult actionResult) | |
{ | |
var eResult = IsAssignableFrom<ExceptionResult>(actionResult); | |
return eResult.Exception; | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a not found result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static void HttpNotFound(IHttpActionResult actionResult) | |
{ | |
IsAssignableFrom<NotFoundResult>(actionResult); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is an ok result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static void HttpOk(IHttpActionResult actionResult) | |
{ | |
IsAssignableFrom<OkResult>(actionResult); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is an ok result with content. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <typeparam name="T">The type of the content.</typeparam> | |
/// <returns>Returns the content from the result.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static TContent HttpOk<TContent>(IHttpActionResult actionResult) | |
{ | |
var oncResult = IsAssignableFrom<OkNegotiatedContentResult<TContent>>(actionResult); | |
return oncResult.Content; | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a redirect result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="location">The redirect location.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the locations are not equal.</exception> | |
public static void HttpRedirect(IHttpActionResult actionResult, string location) | |
{ | |
HttpRedirect(actionResult, new Uri(location)); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a redirect result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="location">The redirect location.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the locations are not equal.</exception> | |
public static void HttpRedirect(IHttpActionResult actionResult, Uri location) | |
{ | |
var rResult = IsAssignableFrom<RedirectResult>(actionResult); | |
Equal(location, rResult.Location); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a redirect to route result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="routeName">The route name.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the route names are not equal.</exception> | |
public static void HttpRedirectToRoute(IHttpActionResult actionResult, string routeName) | |
{ | |
HttpRedirectToRoute(actionResult, routeName, (IDictionary<string, object>)null); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a redirect to route result | |
/// with the given route values. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="routeName">The route name.</param> | |
/// <param name="routeValues">The route values.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the route names or route values are not equal.</exception> | |
public static void HttpRedirectToRoute(IHttpActionResult actionResult, string routeName, object routeValues) | |
{ | |
HttpRedirectToRoute(actionResult, routeName, new RouteValueDictionary(routeValues)); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a redirect to route result | |
/// with the given route values. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="routeName">The route name.</param> | |
/// <param name="routeValues">The route values.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the route names or route values are not equal.</exception> | |
public static void HttpRedirectToRoute(IHttpActionResult actionResult, string routeName, IDictionary<string, object> routeValues) | |
{ | |
var rtrResult = IsAssignableFrom<RedirectToRouteResult>(actionResult); | |
Equal(routeName, rtrResult.RouteName); | |
if (routeValues != null) | |
Equal(routeValues, rtrResult.RouteValues); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a response message result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <returns>Returns the response message.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static HttpResponseMessage HttpResponseMessage(IHttpActionResult actionResult) | |
{ | |
var rmResult = IsAssignableFrom<ResponseMessageResult>(actionResult); | |
return rmResult.Response; | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is a status code result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <param name="statusCode">The status code.</param> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
/// <exception cref="EqualException">Thrown when the status codes don't match.</exception> | |
public static void HttpStatusCode(IHttpActionResult actionResult, HttpStatusCode statusCode) | |
{ | |
var scResult = IsAssignableFrom<StatusCodeResult>(actionResult); | |
Equal(statusCode, scResult.StatusCode); | |
} | |
/// <summary> | |
/// Verifies that the given Web API action result is an unauthorized result. | |
/// </summary> | |
/// <param name="actionResult">The Web API action result.</param> | |
/// <returns>Returns the authentication challenge header values.</returns> | |
/// <exception cref="IsAssignableFromException">Thrown when the action result is not the correct type.</exception> | |
public static IEnumerable<AuthenticationHeaderValue> HttpUnauthorized(IHttpActionResult actionResult) | |
{ | |
var uResult = IsAssignableFrom<UnauthorizedResult>(actionResult); | |
return uResult.Challenges; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment