Skip to content

Commit

Permalink
Improve Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
NimaAra committed Feb 4, 2024
1 parent 7edf93f commit 53084c0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
13 changes: 10 additions & 3 deletions Easy.Common.Tests.Unit/Enum/EnumTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Easy.Common.Tests.Unit.Enum;

using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Easy.Common.Interfaces;
using NUnit.Framework;
using Shouldly;
Expand Down Expand Up @@ -31,11 +32,17 @@ public void When_getting_enum_values()
values.ShouldBe(new[] { MyEnum.OptionA, MyEnum.OptionB }, ignoreOrder: true);
}

[Test]
public void When_calling_toString()
{
MyEnum.OptionB.ToString().ShouldBe("[1] OptionB");
}

sealed record class MyEnum : Enum<MyEnum>
{
public static readonly MyEnum OptionA = new(0, nameof(OptionA));
public static readonly MyEnum OptionB = new(1, nameof(OptionB));
public static readonly MyEnum OptionA = new(0);
public static readonly MyEnum OptionB = new(1);

private MyEnum(int id, string name) : base(id, name) { }
private MyEnum(int id, [CallerMemberName] string name = default!) : base(id, name!) { }
}
}
2 changes: 1 addition & 1 deletion Easy.Common/Easy.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PackageId>Easy.Common</PackageId>
<Description>A set of useful methods used across Easy.* projects.</Description>
<Authors>Nima Ara</Authors>
<Copyright>2023 Nima Ara</Copyright>
<Copyright>2024 Nima Ara</Copyright>
<Title>Easy Common</Title>
<PackageTags>Easy;Common</PackageTags>
<PackageProjectUrl>https://github.com/NimaAra/Easy.Common</PackageProjectUrl>
Expand Down
10 changes: 8 additions & 2 deletions Easy.Common/Enum.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
namespace Easy.Common;

using Easy.Common.Interfaces;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Easy.Common.Interfaces;
using System.Runtime.CompilerServices;

/// <summary>
/// An abstraction for representing a class based enum.
/// </summary>
public abstract record class Enum<T>(int Id, string Name) : IEnum where T : IEnum
public abstract record class Enum<T>(int Id, [CallerMemberName] string Name = default!) : IEnum where T : IEnum
{
/// <summary>
/// Retrieves a list of the values of the constants in a specified enumeration of type <typeparamref name="T"/>.
Expand All @@ -18,4 +19,9 @@ public abstract record class Enum<T>(int Id, string Name) : IEnum where T : IEnu
.Select(f => f.GetValue(null))
.Cast<T>()
.ToList();

/// <summary>
/// Returns the textual representation of the enum.
/// </summary>
public sealed override string ToString() => $"[{Id}] {Name}";
}
2 changes: 1 addition & 1 deletion Easy.Common/Interfaces/IEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IEnum
int Id { get; }

/// <summary>
/// Gets the name.
/// Gets the Name.
/// </summary>
string Name { get; }
}

0 comments on commit 53084c0

Please sign in to comment.