Skip to content

Commit

Permalink
Merge pull request #69 from OhFlowi/fix/InternalVisibility
Browse files Browse the repository at this point in the history
fix: Fixes the usage with internal enums
  • Loading branch information
EngRajabi authored Mar 10, 2024
2 parents 402a6bb + 69022ab commit 6fed25a
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Supernova.Enum.Generators/EnumSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public sealed class {SourceGeneratorHelper.AttributeName}Attribute : Attribute
using System.Collections.Immutable;
namespace {SourceGeneratorHelper.NameSpace}
{{
public static class {symbol.Name}EnumExtensions
{symbol.DeclaredAccessibility.ToString("G").ToLower()} static class {symbol.Name}EnumExtensions
{{");

//DisplayNames Dictionary
Expand Down
181 changes: 181 additions & 0 deletions test/UnitTests/InternalEnumGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using EnumFastToStringGenerated;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace UnitTests;

[EnumGenerator]
internal enum InternalUserTypeTest
{
[Display(Name = "مرد", Description = "Descمرد")] Men = 3,

[Display(Name = "زن", Description = "Descزن")] Women = 4,

//[Display(Name = "نامشخص")]
None
}

[TestClass]
public class InternalEnumGeneratorTest
{
[TestMethod]
public void TestEnumDefined()
{
var defined = InternalUserTypeTestEnumExtensions.IsDefinedFast("Men");

Assert.IsTrue(defined);
}

[TestMethod]
public void TestEnumDefined_Undefined()
{
var defined = InternalUserTypeTestEnumExtensions.IsDefinedFast("DoesNotExist");

Assert.IsFalse(defined);
}

[TestMethod]
public void TestEnumToString()
{
var menString = InternalUserTypeTest.Men.ToStringFast();

Assert.AreEqual("Men", menString);
}

[TestMethod]
public void TestEnumToString_Undefined()
{
var action = () => GetUndefinedEnumValue().ToStringFast();

action.Should().Throw<ArgumentOutOfRangeException>();
}

[TestMethod]
public void TestEnumToString_Undefined_DefaultValue()
{
var value = GetUndefinedEnumValue().ToStringFast("DefaultValue");

value.Should().Be("DefaultValue");
}

[TestMethod]
public void TestEnumToDescription()
{
var menString = InternalUserTypeTest.Men.ToDescriptionFast();

menString.Should().Be("Descمرد");
}

[TestMethod]
public void TestEnumToDescription_Undefined()
{
var action = () => GetUndefinedEnumValue().ToDescriptionFast();

action.Should().Throw<ArgumentOutOfRangeException>();
}

[TestMethod]
public void TestEnumToDescription_Undefined_DefaultValue()
{
var value = GetUndefinedEnumValue().ToDescriptionFast("DefaultValue");

value.Should().Be("DefaultValue");
}

[TestMethod]
public void TestEnumToDisplay()
{
var menString = InternalUserTypeTest.Men.ToDisplayFast();

Assert.AreEqual("مرد", menString);
}

[TestMethod]
public void TestEnumToDisplayNone()
{
var menString = InternalUserTypeTest.None.ToDisplayFast();

Assert.AreEqual("None", menString);
}

[TestMethod]
public void TestEnumToDisplay_Undefined()
{
var action = () => GetUndefinedEnumValue().ToDisplayFast();

action.Should().Throw<ArgumentOutOfRangeException>();
}

[TestMethod]
public void TestEnumToDisplay_Undefined_DefaultValue()
{
var value = GetUndefinedEnumValue().ToDisplayFast("DefaultValue");

value.Should().Be("DefaultValue");
}

[TestMethod]
public void TestEnumGetNames()
{
var names = InternalUserTypeTestEnumExtensions.GetNamesFast();

Assert.IsNotNull(names);
names.Should().NotBeEmpty()
.And.HaveCount(3)
.And.ContainInOrder(nameof(InternalUserTypeTest.Men), nameof(InternalUserTypeTest.Women), nameof(InternalUserTypeTest.None))
.And.ContainItemsAssignableTo<string>();
}

[TestMethod]
public void TestEnumDisplayNamesDictionary()
{
var names = InternalUserTypeTestEnumExtensions.DisplayNamesDictionary;
Assert.IsNotNull(names);
names.Should().NotBeEmpty()
.And.HaveCount(3)
.And.ContainInOrder(
new KeyValuePair<InternalUserTypeTest, string>(InternalUserTypeTest.Men, "مرد"),
new KeyValuePair<InternalUserTypeTest, string>(InternalUserTypeTest.Women, "زن"),
new KeyValuePair<InternalUserTypeTest, string>(InternalUserTypeTest.None, "None")
);
}

[TestMethod]
public void TestEnumDisplayDescriptionsDictionary()
{
var names = InternalUserTypeTestEnumExtensions.DisplayDescriptionsDictionary;
Assert.IsNotNull(names);
names.Should().NotBeEmpty()
.And.HaveCount(3)
.And.ContainInOrder(
new KeyValuePair<InternalUserTypeTest, string>(InternalUserTypeTest.Men, "Descمرد"),
new KeyValuePair<InternalUserTypeTest, string>(InternalUserTypeTest.Women, "Descزن"),
new KeyValuePair<InternalUserTypeTest, string>(InternalUserTypeTest.None, "None")
);
}

[TestMethod]
public void TestEnumGetValues()
{
var values = InternalUserTypeTestEnumExtensions.GetValuesFast();

Assert.IsNotNull(values);
values.Should().NotBeEmpty()
.And.HaveCount(3)
.And.ContainInOrder(InternalUserTypeTest.Men, InternalUserTypeTest.Women, InternalUserTypeTest.None)
.And.ContainItemsAssignableTo<InternalUserTypeTest>();
}

[TestMethod]
public void TestEnumGetLength()
{
var length = InternalUserTypeTestEnumExtensions.GetLengthFast();

Assert.AreEqual(Enum.GetValues<InternalUserTypeTest>().Length, length);
}

private InternalUserTypeTest GetUndefinedEnumValue() => (InternalUserTypeTest)(-1);
}

0 comments on commit 6fed25a

Please sign in to comment.