-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from MASSHUU12/add-random-extensions-for-enum
Add random extensions for enum
- Loading branch information
Showing
5 changed files
with
212 additions
and
16 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
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,80 @@ | ||
using System; | ||
using Confirma.Exceptions; | ||
|
||
namespace Confirma.Classes; | ||
|
||
public static class Confirm | ||
{ | ||
#region IsEnumValue | ||
public static int IsEnumValue<T>(int value, string? message = null) | ||
where T : struct, Enum | ||
{ | ||
foreach (int v in Enum.GetValues(typeof(T))) if (v == value) return value; | ||
|
||
throw new ConfirmAssertException( | ||
message ?? | ||
$"Expected {value} to be {typeof(T).Name} enum value." | ||
); | ||
} | ||
|
||
public static int IsNotEnumValue<T>(int value, string? message = null) | ||
where T : struct, Enum | ||
{ | ||
try | ||
{ | ||
IsEnumValue<T>(value); | ||
} | ||
catch (ConfirmAssertException) | ||
{ | ||
return value; | ||
} | ||
|
||
throw new ConfirmAssertException( | ||
message ?? | ||
$"Expected {value} not to be {typeof(T).Name} enum value." | ||
); | ||
} | ||
#endregion | ||
|
||
#region IsEnumName | ||
public static string IsEnumName<T>(string name, bool ignoreCase = false, string? message = null) | ||
where T : struct, Enum | ||
{ | ||
foreach (string v in Enum.GetNames(typeof(T))) | ||
{ | ||
var n = v; | ||
|
||
if (ignoreCase) | ||
{ | ||
n = n.ToLower(); | ||
name = name.ToLower(); | ||
} | ||
|
||
if (n == name) return name; | ||
} | ||
|
||
throw new ConfirmAssertException( | ||
message ?? | ||
$"Expected {name} to be {typeof(T).Name} enum name." | ||
); | ||
} | ||
|
||
public static string IsNotEnumName<T>(string name, string? message = null) | ||
where T : struct, Enum | ||
{ | ||
try | ||
{ | ||
IsEnumName<T>(name); | ||
} | ||
catch (ConfirmAssertException) | ||
{ | ||
return name; | ||
} | ||
|
||
throw new ConfirmAssertException( | ||
message ?? | ||
$"Expected {name} not to be {typeof(T).Name} enum name." | ||
); | ||
} | ||
#endregion | ||
} |
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,22 @@ | ||
using System; | ||
|
||
namespace Confirma.Extensions; | ||
|
||
public static class RandomEnumExtensions | ||
{ | ||
public static int NextEnumValue<T>(this Random rg) | ||
where T : struct, Enum | ||
{ | ||
var values = Enum.GetValues(typeof(T)); | ||
|
||
return (int)values.GetValue(rg.Next(0, values.Length))!; | ||
} | ||
|
||
public static string NextEnumName<T>(this Random rg) | ||
where T : struct, Enum | ||
{ | ||
var names = Enum.GetNames(typeof(T)); | ||
|
||
return names[rg.Next(0, names.Length)]; | ||
} | ||
} |
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
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,33 @@ | ||
using System; | ||
using Confirma.Attributes; | ||
using Confirma.Classes; | ||
using Confirma.Extensions; | ||
|
||
namespace Confirma.Tests; | ||
|
||
[TestClass] | ||
[Parallelizable] | ||
public static class RandomEnumTest | ||
{ | ||
private static readonly Random rg = new(); | ||
|
||
private enum TestEnum { A, B, C, D, E, F } | ||
|
||
[Repeat(6)] | ||
[TestCase] | ||
public static void NextEnumValue() | ||
{ | ||
var value = rg.NextEnumValue<TestEnum>(); | ||
|
||
Confirm.IsEnumValue<TestEnum>(value); | ||
} | ||
|
||
[Repeat(6)] | ||
[TestCase] | ||
public static void NextEnumName() | ||
{ | ||
var name = rg.NextEnumName<TestEnum>(); | ||
|
||
Confirm.IsEnumName<TestEnum>(name); | ||
} | ||
} |