-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 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
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"); | ||
} | ||
} |
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,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; | ||
} | ||
} |