Skip to content

Commit

Permalink
Merge pull request #98 from MASSHUU12/add-random-extensions-for-enum
Browse files Browse the repository at this point in the history
Add random extensions for enum
  • Loading branch information
MASSHUU12 authored Jul 1, 2024
2 parents acdbf6e + 9310787 commit 629f6bf
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ All notable changes to this project will be documented in this file.
- Extensions:
- ConfirmIsOdd
- ConfirmIsEven
- Extension classes:
- RandomEnumExtensions
- Confirm class with assertions:
- IsEnumValue
- IsNotEnumValue
- IsEnumName
- IsNotEnumName

## [0.5.0-beta 2024-06-29]

Expand Down
80 changes: 80 additions & 0 deletions addons/confirma/src/classes/Confirm.cs
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
}
22 changes: 22 additions & 0 deletions addons/confirma/src/extensions/RandomEnumExtensions.cs
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)];
}
}
86 changes: 70 additions & 16 deletions tests/ConfirmTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Confirma.Attributes;
using Confirma.Classes;
using Confirma.Exceptions;
using Confirma.Extensions;

Expand All @@ -9,35 +10,88 @@ namespace Confirma.Tests;
[Parallelizable]
public static class ConfirmTest
{
[TestCase]
public static void ConfirmThrows_Action()
private enum TestEnum
{
static void Test() => throw new ConfirmAssertException("Lorem ipsum dolor sit amet.");
A, B, C, D, E
}

#region IsEnumValue
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
public static void IsEnumValue_WhenIsEnumValue(int value)
{
Confirm.IsEnumValue<TestEnum>(value);
}

[TestCase(-1)]
[TestCase(5)]
public static void IsEnumValue_WhenISNotEnumValue(int value)
{
Action action = () => Confirm.IsEnumValue<TestEnum>(value);

ConfirmExceptionExtensions.ConfirmThrows<ConfirmAssertException>(Test);
action.ConfirmThrows<ConfirmAssertException>();
}
#endregion

[TestCase]
public static void ConfirmThrows_Func()
#region IsNotEnumValue
[TestCase(-1)]
[TestCase(5)]
public static void IsNotEnumValue_WhenIsNotEnumValue(int value)
{
Func<object> Test = () => throw new ConfirmAssertException("Lorem ipsum dolor sit amet.");
Confirm.IsNotEnumValue<TestEnum>(value);
}

[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
public static void IsNotEnumValue_WhenIsEnumValue(int value)
{
Action action = () => Confirm.IsNotEnumValue<TestEnum>(value);

Test.ConfirmThrows<ConfirmAssertException>();
action.ConfirmThrows<ConfirmAssertException>();
}
#endregion

#region IsEnumName
[TestCase("A", false)]
[TestCase("a", true)]
[TestCase("B", false)]
[TestCase("b", true)]
[TestCase("C", false)]
[TestCase("c", true)]
[TestCase("D", false)]
[TestCase("d", true)]
[TestCase("E", false)]
[TestCase("e", true)]
public static void IsEnumName_WhenIsEnumName(string name, bool ignoreCase)
{
Confirm.IsEnumName<TestEnum>(name, ignoreCase);
}

[TestCase]
public static void ConfirmNotThrows_Action()
[TestCase("a")]
[TestCase("b")]
[TestCase("c")]
public static void IsEnumName_WhenIsEnumNameIncorrectCase(string name)
{
static void Test() => new object().ToString();
Action action = () => Confirm.IsEnumName<TestEnum>(name);

ConfirmExceptionExtensions.ConfirmNotThrows<ConfirmAssertException>(Test);
action.ConfirmThrows<ConfirmAssertException>();
}

[TestCase]
public static void ConfirmNotThrows_Func()
[TestCase("0", false)]
[TestCase("F", false)]
[TestCase("0", true)]
[TestCase("f", true)]
public static void IsEnumName_WhenIsNotEnumName(string name, bool ignoreCase)
{
Func<object> Test = () => new object();
Action action = () => Confirm.IsEnumName<TestEnum>(name, ignoreCase);

Test.ConfirmNotThrows<ConfirmAssertException>();
action.ConfirmThrows<ConfirmAssertException>();
}
#endregion
}
33 changes: 33 additions & 0 deletions tests/RandomEnumTest.cs
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);
}
}

0 comments on commit 629f6bf

Please sign in to comment.