120 lines
2.9 KiB
C#
120 lines
2.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using AMWD.Common.Tests.Utils;
|
|
using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
|
|
|
|
namespace AMWD.Common.Tests.Extensions
|
|
{
|
|
[TestClass]
|
|
public class EnumExtensionsTest
|
|
{
|
|
[TestMethod]
|
|
public void ShouldReturnEmptyList()
|
|
{
|
|
// arrange
|
|
var enumValue = TestEnum.Two;
|
|
|
|
// act
|
|
var customList = enumValue.GetAttributes<CustomMultipleAttribute>();
|
|
var descriptionList = enumValue.GetAttributes<DescriptionAttribute>();
|
|
|
|
// assert
|
|
Assert.IsNotNull(customList);
|
|
Assert.IsFalse(customList.Any());
|
|
Assert.IsNotNull(descriptionList);
|
|
Assert.IsFalse(descriptionList.Any());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnList()
|
|
{
|
|
// arrange
|
|
var enumValue = TestEnum.Zero;
|
|
|
|
// act
|
|
var customList = enumValue.GetAttributes<CustomMultipleAttribute>();
|
|
var descriptionList = enumValue.GetAttributes<DescriptionAttribute>();
|
|
|
|
// assert
|
|
Assert.IsNotNull(customList);
|
|
Assert.IsTrue(customList.Any());
|
|
Assert.AreEqual(2, customList.Count());
|
|
Assert.IsNotNull(descriptionList);
|
|
Assert.IsTrue(descriptionList.Any());
|
|
Assert.AreEqual(1, descriptionList.Count());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnNothing()
|
|
{
|
|
// arrange
|
|
var enumValue = TestEnum.Two;
|
|
|
|
// act
|
|
var customAttribute = enumValue.GetAttribute<CustomMultipleAttribute>();
|
|
var descriptionAttribute = enumValue.GetAttribute<DescriptionAttribute>();
|
|
|
|
// assert
|
|
Assert.IsNull(customAttribute);
|
|
Assert.IsNull(descriptionAttribute);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnFirstAttribute()
|
|
{
|
|
// arrange
|
|
var enumValue = TestEnum.Zero;
|
|
|
|
// act
|
|
var customAttribute = enumValue.GetAttribute<CustomMultipleAttribute>();
|
|
var descriptionAttribute = enumValue.GetAttribute<DescriptionAttribute>();
|
|
|
|
// assert
|
|
Assert.IsNotNull(customAttribute);
|
|
Assert.AreEqual("nix", customAttribute.Name);
|
|
Assert.IsNotNull(descriptionAttribute);
|
|
Assert.AreEqual("Null", descriptionAttribute.Description);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnDescriptionOrStringRepresentation()
|
|
{
|
|
// arrange
|
|
var enumWithDescription = TestEnum.One;
|
|
var enumWithoutDescripton = TestEnum.Two;
|
|
|
|
// act
|
|
string description = enumWithDescription.GetDescription();
|
|
string noDescription = enumWithoutDescripton.GetDescription();
|
|
|
|
// assert
|
|
Assert.AreEqual("Eins", description);
|
|
Assert.AreEqual(enumWithoutDescripton.ToString(), noDescription);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnEmptyListOnNotDefinedEnumValue()
|
|
{
|
|
// arrange
|
|
var notDefinedEnum = (TestEnum)10;
|
|
|
|
// act
|
|
var list = notDefinedEnum.GetAttributes<DescriptionAttribute>();
|
|
|
|
// assert
|
|
Assert.IsFalse(list.Any());
|
|
}
|
|
|
|
internal enum TestEnum
|
|
{
|
|
[CustomMultiple("nix")]
|
|
[CustomMultiple("Null")]
|
|
[Description("Null")]
|
|
Zero,
|
|
[Description("Eins")]
|
|
One,
|
|
Two,
|
|
}
|
|
}
|
|
}
|