Skip to content

Commit

Permalink
- Address feedback
Browse files Browse the repository at this point in the history
- Add new assertion handler for asserting an action does not throw an exception
  • Loading branch information
nwithan8 committed Sep 17, 2024
1 parent f0ec5ea commit 85cb8a3
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 23 deletions.
6 changes: 2 additions & 4 deletions EasyPost.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
Expand All @@ -9,6 +8,7 @@
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
using Xunit;
using CustomAssertions = EasyPost.Tests._Utilities.Assertions.Assert;

namespace EasyPost.Tests
{
Expand Down Expand Up @@ -74,9 +74,7 @@ public void TestThreadSafety()
public void TestClientDisposal()
{
Client client = new(new ClientConfiguration(FakeApikey));
client.Dispose();

// As long as this test doesn't throw an exception, it passes
CustomAssertions.DoesNotThrow(() => client.Dispose());
}

[Fact]
Expand Down
6 changes: 2 additions & 4 deletions EasyPost.Tests/HttpTests/ClientConfigurationTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using EasyPost.Tests._Utilities.Attributes;
using Xunit;
using CustomAssertions = EasyPost.Tests._Utilities.Assertions.Assert;

namespace EasyPost.Tests.HttpTests
{
Expand All @@ -12,10 +13,7 @@ public class ClientConfigurationTest
public void TestClientConfigurationDisposal()
{
ClientConfiguration configuration = new("not_a_real_api_key");

configuration.Dispose();

// As long as this test doesn't throw an exception, it passes
CustomAssertions.DoesNotThrow(() => configuration.Dispose());
}

[Fact]
Expand Down
6 changes: 2 additions & 4 deletions EasyPost.Tests/HttpTests/RequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using EasyPost._base;
using EasyPost.Http;
using Xunit;
using CustomAssertions = EasyPost.Tests._Utilities.Assertions.Assert;

namespace EasyPost.Tests.HttpTests;

Expand All @@ -13,10 +14,7 @@ public class RequestTest
public void TestRequestDisposal()
{
Request request = new("https://google.com", "not_a_real_endpoint", Method.Get, ApiVersion.V2, new Dictionary<string, object> { { "key", "value" } }, new Dictionary<string, string> { { "header_key", "header_value" } });

request.Dispose();

// As long as this test doesn't throw an exception, it passes
CustomAssertions.DoesNotThrow(() => request.Dispose());
}

#endregion
Expand Down
5 changes: 2 additions & 3 deletions EasyPost.Tests/ServicesTests/ServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using EasyPost.Tests._Utilities.Attributes;
using EasyPost.Utilities.Internal.Attributes;
using Xunit;
using CustomAssertions = EasyPost.Tests._Utilities.Assertions.Assert;

namespace EasyPost.Tests.ServicesTests
{
Expand All @@ -28,9 +29,7 @@ public void TestServiceDisposal()
Client client = new(new ClientConfiguration("not_a_real_api_key"));

AddressService addressService = client.Address;
addressService.Dispose();

// As long as this test doesn't throw an exception, it passes
CustomAssertions.DoesNotThrow(() => addressService.Dispose());
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion EasyPost.Tests/_Utilities/Assertions/AnyException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace EasyPost.Tests._Utilities.Assertions
{
/// <summary>
/// Exception thrown when an Any assertion has one or more items fail an assertion.
/// Exception thrown when an Any assertion has one or more items fail an assertion.
/// </summary>
public class AnyException : XunitException
{
Expand Down
6 changes: 0 additions & 6 deletions EasyPost.Tests/_Utilities/Assertions/CollectionAsserts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ 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);
}

/// <summary>
/// Verifies that any items in the collection pass when executed against
/// action.
Expand Down
50 changes: 50 additions & 0 deletions EasyPost.Tests/_Utilities/Assertions/DoesNotThrowAssert.cs
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 EasyPost.Tests/_Utilities/Assertions/DoesNotThrowException.cs
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 EasyPost.Tests/_Utilities/Assertions/GuardArgumentNotNull.cs
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);
}
}
}
1 change: 0 additions & 1 deletion EasyPost/_base/EasyPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ protected virtual void Dispose(bool disposing)
// Dispose managed state (managed objects)

// Don't dispose of the associated client here, as it may be shared among multiple services
// Client.Dispose();
}

/// <summary>
Expand Down

0 comments on commit 85cb8a3

Please sign in to comment.