From 7cc0f8dbe204dcf205bdd8eba3b2e143fb9b5a16 Mon Sep 17 00:00:00 2001 From: M3LiNdRu Date: Wed, 26 Oct 2016 00:24:01 +0200 Subject: [PATCH] Added tests for EnumExtension --- .../EnumExtensions/GetDescriptionTests.cs | 69 ++++++++++++++++++- .../EnumExtensions/GetDisplayNameTests.cs | 28 ++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDescriptionTests.cs b/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDescriptionTests.cs index d405213..4d0de5b 100644 --- a/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDescriptionTests.cs +++ b/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDescriptionTests.cs @@ -1,13 +1,80 @@ namespace MoreDotNet.Tests.Extensions.Common.EnumExtensions { + using System; + using System.ComponentModel; using MoreDotNet.Extensions.Common; using Xunit; public class GetDescriptionTests { + private enum TestEnumWithDescription + { + [Description("This is the default value.")] + Default = 0, + [Description("This is the second value")] + One = 1, + [Description("This is the third value")] + Two = 2 + } + + private enum TestEnumWithoutDescription + { + Default = 0, + One = 1, + Two = 2 + } + [Fact] - public void IsLike_CompareBasicPatterns_ShouldPass() + public void GetDescription_GivenTestEnumWithDescription_ShouldReturnEnumDescriptionString() { + string expected = "This is the default value."; + TestEnumWithDescription testEnum = TestEnumWithDescription.Default; + + string enumDescription = testEnum.GetDescription(); + + Assert.Equal(enumDescription, expected); + } + + [Fact] + public void GetDescription_GivenTestEnumWithoutDescription_ShouldReturnToStringMethodValue() + { + string expected = TestEnumWithoutDescription.Default.ToString(); + TestEnumWithoutDescription testEnum = TestEnumWithoutDescription.Default; + + string enumDescription = testEnum.GetDescription(); + + Assert.Equal(enumDescription, expected); + } + + [Fact] + public void GetDescription_GivenInvalidTestEnumValue_ShouldReturnToStringMethodValue() + { + string expected = "4"; + int value = 4; + + string result = ((TestEnumWithoutDescription)value).GetDescription(); + + Assert.Equal(expected, result); + } + + [Fact] + public void GetDescription_GivenInvalidEnumType_ShouldThrowException() + { + TestStruct structTest = default(TestStruct); + + Assert.Throws(() => structTest.GetDescription()); + } + + private struct TestStruct + { + private int value; + private string name; + + public TestStruct(int number, string key) + { + this.value = number; + this.name = key; + } } } } diff --git a/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDisplayNameTests.cs b/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDisplayNameTests.cs index bb42038..4eb3ae2 100644 --- a/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDisplayNameTests.cs +++ b/Source/MoreDotNet.Test/Extensions/Common/EnumExtensions/GetDisplayNameTests.cs @@ -5,5 +5,33 @@ public class GetDisplayNameTests { + private enum TestEnum + { + Default = 0, + One = 1, + Two = 2 + } + + [Fact] + public void GetDisplayName__GivenTestEnum_ShouldReturnEnumNameString() + { + string expected = "Default"; + TestEnum testEnum = TestEnum.Default; + + string enumName = testEnum.GetDisplayName(); + + Assert.Equal(enumName, expected); + } + + [Fact] + public void GetDescription_GivenInvalidTestEnumValue_ShouldReturnStringValue() + { + string expected = "4"; + int value = 4; + + string result = ((TestEnum)value).GetDescription(); + + Assert.Equal(expected, result); + } } }