diff --git a/Tuxedo.Tests/DigitTests.cs b/Tuxedo.Tests/DigitTests.cs new file mode 100644 index 0000000..bfeafba --- /dev/null +++ b/Tuxedo.Tests/DigitTests.cs @@ -0,0 +1,24 @@ +namespace Tuxedo.Tests; + +public static class DigitTests +{ + [Fact(DisplayName = "Characters can be refined as digits")] + public static void Case1() + { + Refined 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)'a'; + act.Should() + .Throw() + .WithMessage("Value must be a digit, instead found 'a'") + .And.StackTrace.Should() + .StartWith(" at Tuxedo.Tests.DigitTests"); + } +} diff --git a/Tuxedo/Refinements/Digit.cs b/Tuxedo/Refinements/Digit.cs new file mode 100644 index 0000000..75252ad --- /dev/null +++ b/Tuxedo/Refinements/Digit.cs @@ -0,0 +1,21 @@ +using System.Diagnostics.CodeAnalysis; + +namespace Tuxedo; + +/// +/// Enforces that a character is a digit +/// +public readonly struct Digit : IRefinement +{ + /// + 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; + } +}