diff --git a/Supernova.Enum.Generators/EnumSourceGenerator.cs b/Supernova.Enum.Generators/EnumSourceGenerator.cs index b6de88b..a567f3e 100644 --- a/Supernova.Enum.Generators/EnumSourceGenerator.cs +++ b/Supernova.Enum.Generators/EnumSourceGenerator.cs @@ -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 diff --git a/test/UnitTests/InternalEnumGeneratorTest.cs b/test/UnitTests/InternalEnumGeneratorTest.cs new file mode 100644 index 0000000..84a5a5a --- /dev/null +++ b/test/UnitTests/InternalEnumGeneratorTest.cs @@ -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(); + } + + [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(); + } + + [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(); + } + + [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(); + } + + [TestMethod] + public void TestEnumDisplayNamesDictionary() + { + var names = InternalUserTypeTestEnumExtensions.DisplayNamesDictionary; + Assert.IsNotNull(names); + names.Should().NotBeEmpty() + .And.HaveCount(3) + .And.ContainInOrder( + new KeyValuePair(InternalUserTypeTest.Men, "مرد"), + new KeyValuePair(InternalUserTypeTest.Women, "زن"), + new KeyValuePair(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.Men, "Descمرد"), + new KeyValuePair(InternalUserTypeTest.Women, "Descزن"), + new KeyValuePair(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(); + } + + [TestMethod] + public void TestEnumGetLength() + { + var length = InternalUserTypeTestEnumExtensions.GetLengthFast(); + + Assert.AreEqual(Enum.GetValues().Length, length); + } + + private InternalUserTypeTest GetUndefinedEnumValue() => (InternalUserTypeTest)(-1); +}