Skip to content

Commit

Permalink
Added null input handling to reverse.
Browse files Browse the repository at this point in the history
The reverse function previously didn't handle null values gracefully and would just throw null reference exceptions. By throwing an ArgumentNullException instead, the function will now alert consumers directly to the null input instead of misleading them.
  • Loading branch information
tacosontitan committed Aug 21, 2024
1 parent 515ba7a commit b3c61ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Hussy.Net/Display/Reverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public static partial class Hussy
/// </returns>
public static string Rev<T>(T input)
{
var stringValue = input.ToString();
var stringValue = input?.ToString();
if (stringValue is null)
throw new ArgumentNullException(nameof(input), message: "Unable to reverse a null input.");

var reversedSequence = stringValue.Reverse().ToArray();
return new string(reversedSequence);
}
Expand Down
14 changes: 14 additions & 0 deletions test/Hussy.Net.Tests/Display/ReverseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ You may obtain a copy of the License at
limitations under the License.
*/

using System.Security.Cryptography;

namespace Hussy.Net.Tests.Display;

public class ReverseTests
Expand All @@ -27,4 +29,16 @@ public void Reverse_ValidInput_ReversesInput()
const string expectation = "cba";
Assert.Equal(expectation, reversal);
}

[Fact]
public void Reverse_NullInput_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(TestInvocation);

static void TestInvocation()
{
const string? testValue = null;
_ = Rev(testValue!);
}
}
}

0 comments on commit b3c61ba

Please sign in to comment.