-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add new assertion handler for asserting an action does not throw an exception
- Loading branch information
Showing
10 changed files
with
92 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
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
50 changes: 50 additions & 0 deletions
50
EasyPost.Tests/_Utilities/Assertions/DoesNotThrowAssert.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
|
||
namespace EasyPost.Tests._Utilities.Assertions | ||
{ | ||
// ReSharper disable once PartialTypeWithSinglePart | ||
public abstract partial class Assert | ||
{ | ||
/// <summary> | ||
/// Verifies that an action does not throw an exception. | ||
/// </summary> | ||
/// <param name="action">The action to test</param> | ||
/// <exception cref="DoesNotThrowException">Thrown when the action throws an exception</exception> | ||
public static void DoesNotThrow(Action action) | ||
{ | ||
GuardArgumentNotNull(nameof(action), action); | ||
|
||
try | ||
{ | ||
action(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new DoesNotThrowException(ex); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that an action does not throw a specific exception. | ||
/// </summary> | ||
/// <param name="action">The action to test</param> | ||
/// <typeparam name="T">The type of the exception to not throw</typeparam> | ||
/// <exception cref="DoesNotThrowException">Thrown when the action throws an exception of type T</exception> | ||
public static void DoesNotThrow<T>(Action action) where T : Exception | ||
{ | ||
GuardArgumentNotNull(nameof(action), action); | ||
|
||
try | ||
{ | ||
action(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (ex.GetType() == typeof(T)) | ||
{ | ||
throw new DoesNotThrowException(ex); | ||
} | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
EasyPost.Tests/_Utilities/Assertions/DoesNotThrowException.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using Xunit.Sdk; | ||
|
||
namespace EasyPost.Tests._Utilities.Assertions | ||
{ | ||
/// <summary> | ||
/// Exception thrown when a DoesNotThrow assertion fails. | ||
/// </summary> | ||
public class DoesNotThrowException : XunitException | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the <see cref="DoesNotThrowException"/> class. | ||
/// </summary> | ||
public DoesNotThrowException(Exception ex) | ||
: base("Assert.DoesNotThrow() Failure", ex) | ||
{ | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
EasyPost.Tests/_Utilities/Assertions/GuardArgumentNotNull.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
namespace EasyPost.Tests._Utilities.Assertions | ||
{ | ||
// ReSharper disable once PartialTypeWithSinglePart | ||
public abstract partial class Assert | ||
{ | ||
private static void GuardArgumentNotNull(string argName, object argValue) | ||
{ | ||
if (argValue == null) | ||
throw new ArgumentNullException(argName); | ||
} | ||
} | ||
} |
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