Skip to content

Commit

Permalink
feat: digit refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
bmazzarol committed Apr 5, 2024
1 parent f8d012a commit 9bb0818
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Tuxedo.Tests/DigitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Tuxedo.Tests;

public static class DigitTests
{
[Fact(DisplayName = "Characters can be refined as digits")]
public static void Case1()
{
Refined<char, Digit> digit = '1';
digit.Value.Should().Be('1');
char raw = digit;
raw.Should().Be('1');
}

[Fact(DisplayName = "Characters that are not digits cannot be refined")]
public static void Case2()
{
Action act = () => _ = (Refined<char, Digit>)'a';
act.Should()
.Throw<RefinementFailureException>()
.WithMessage("Value must be a digit, instead found 'a'")
.And.StackTrace.Should()
.StartWith(" at Tuxedo.Tests.DigitTests");
}
}
21 changes: 21 additions & 0 deletions Tuxedo/Refinements/Digit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Diagnostics.CodeAnalysis;

namespace Tuxedo;

/// <summary>
/// Enforces that a character is a digit
/// </summary>
public readonly struct Digit : IRefinement<Digit, char>
{
/// <inheritdoc />
public bool CanBeRefined(char value, [NotNullWhen(false)] out string? failureMessage)
{
if (char.IsDigit(value))
{
failureMessage = null;
return true;
}
failureMessage = $"Value must be a digit, instead found '{value}'";
return false;
}
}

0 comments on commit 9bb0818

Please sign in to comment.