public void Cast_Vs_ToObject() { Assert.That(Enumeration.Cast <StringComparison>(4), Is.EqualTo(StringComparison.Ordinal)); Assert.That(Enum.ToObject(typeof(StringComparison), 4), Is.EqualTo(StringComparison.Ordinal)); Assert.That(() => Enumeration.Cast <StringComparison>(100), Throws.InstanceOf <InvalidEnumArgumentException>()); Assert.That(Enum.ToObject(typeof(StringComparison), 100), Is.EqualTo((StringComparison)100)); Assert.That(() => Enumeration.Cast <StringComparison>(4L), Throws.ArgumentException); Assert.That(Enum.ToObject(typeof(StringComparison), 4L), Is.EqualTo(StringComparison.Ordinal)); }
public void Cast_DefinedUnderlyingValue_EnumValue() { Assert.That(Enumeration.Cast <ByteEnum>((byte)1), Is.EqualTo(ByteEnum.Two)); Assert.That(Enumeration.Cast <SByteEnum>((sbyte)1), Is.EqualTo(SByteEnum.Two)); Assert.That(Enumeration.Cast <ShortEnum>((short)1), Is.EqualTo(ShortEnum.Two)); Assert.That(Enumeration.Cast <UShortEnum>((ushort)1), Is.EqualTo(UShortEnum.Two)); Assert.That(Enumeration.Cast <IntEnum>(1), Is.EqualTo(IntEnum.Two)); Assert.That(Enumeration.Cast <UIntEnum>(1u), Is.EqualTo(UIntEnum.Two)); Assert.That(Enumeration.Cast <LongEnum>(1L), Is.EqualTo(LongEnum.Two)); Assert.That(Enumeration.Cast <ULongEnum>(1UL), Is.EqualTo(ULongEnum.Two)); }
public void Cast_UndefinedValue_Exception() { Assert.That(() => Enumeration.Cast <StringComparison>(100), Throws.InstanceOf <InvalidEnumArgumentException>()); }
public void Cast_NotUnderLying_Exception() { Assert.That(() => Enumeration.Cast <ByteEnum>(1L), Throws.ArgumentException); Assert.That(() => Enumeration.Cast <ByteEnum>(100L), Throws.ArgumentException); }
public void Explore() { Assert.That(Enumeration.IsEnum <ActionTargets>(), Is.True); Assert.That(Enumeration.IsEnum <int>(), Is.False); Assert.That(Enumeration.AssertEnum <ActionTargets>, Throws.Nothing); Assert.That(Enumeration.AssertEnum <int>, Throws .InstanceOf <ArgumentException>() .With.Message.Contains("Int32")); Assert.That(Enumeration.IsFlags <ZeroFlags>(), Is.True); Assert.That(Enumeration.IsFlags <IntEnum>(), Is.False); Assert.That(Enumeration.AssertFlags <ZeroFlags>, Throws.Nothing); Assert.That(Enumeration.AssertFlags <IntEnum>, Throws .ArgumentException .With.Message.Contains("IntEnum")); Assert.That(Enumeration.IsDefined(StringComparison.Ordinal), Is.True); Assert.That(Enumeration.IsDefined((StringComparison)100), Is.False); Assert.That(Enumeration.IsDefined <LongEnum>(1L), Is.True); Assert.That(Enumeration.IsDefined <StringComparison>("undefined"), Is.False); Assert.That(Enumeration.IsDefined <StringComparison>("oRDinaL", ignoreCase: true), Is.True); Assert.That(() => Enumeration.AssertDefined(StringComparison.Ordinal), Throws.Nothing); Assert.That(() => Enumeration.AssertDefined <StringComparison>("ordinal"), Throws .InstanceOf <InvalidEnumArgumentException>() .With.Message.Contains("ordinal") .And.With.Message.Contains("StringComparison")); Assert.That(Enumeration.GetName(StringComparison.Ordinal), Is.EqualTo("Ordinal")); Assert.That(Enumeration.GetName <ULongEnum>(1UL), Is.EqualTo("Two")); Assert.That(() => Enumeration.GetName <IntEnum>(100), Throws.InstanceOf <InvalidEnumArgumentException>()); Assert.That(Enumeration.GetValues <StringSplitOptions>(), Is.EquivalentTo( new[] { StringSplitOptions.None, StringSplitOptions.RemoveEmptyEntries })); Assert.That(Enumeration.GetValue <ULongEnum, ulong>(ULongEnum.Two), Is.EqualTo(1UL)); Assert.That(Enumeration.Cast <ByteEnum>((byte)1), Is.EqualTo(ByteEnum.Two)); StringComparison?value; Assert.That(Enumeration.TryCast(100, out value), Is.False); Assert.That(Enumeration.Parse <ActionTargets>("Suite"), Is.EqualTo(ActionTargets.Suite)); Assert.That(() => Enumeration.Parse <ActionTargets>("SUIte", ignoreCase: false), Throws.InstanceOf <InvalidEnumArgumentException>()); Assert.That(Enumeration.Parse <StringComparison>("4"), Is.EqualTo(StringComparison.Ordinal)); ActionTargets?parsed; Assert.That(() => Enumeration.TryParse("100", out parsed), Is.False); Assert.That(Enumeration.TryParse("suiTE", true, out parsed), Is.True); var fourNotSet = NoZeroFlags.Three; NoZeroFlags fourSet = fourNotSet.SetFlag(NoZeroFlags.Four); Assert.That(fourSet.HasFlag(NoZeroFlags.Four), Is.True); NoZeroFlags fourUnset = fourSet.UnsetFlag(NoZeroFlags.Four); Assert.That(fourUnset.HasFlag(NoZeroFlags.Four), Is.False); fourSet = fourUnset.ToggleFlag(NoZeroFlags.Four); Assert.That(fourSet.HasFlag(NoZeroFlags.Four), Is.True); fourNotSet = fourSet.ToggleFlag(NoZeroFlags.Four); Assert.That(fourNotSet.HasFlag(NoZeroFlags.Four), Is.False); Assert.That(fourSet.GetFlags(), Is.EquivalentTo(new[] { NoZeroFlags.Three, NoZeroFlags.Four })); }
/// <summary> /// Converts the specified 16-bit signed integer to a <see cref="CurrencyIsoCode"/>. /// </summary> /// <para>The conversion is safe, in the sense that the value has to be defined within the values of the enumeration to be converted, but throws an exception when it cannot.</para> /// <param name="numericCode">The value to be converted.</param> /// <returns>An instance of the enumeration set to <paramref name="numericCode"/>.</returns> /// <exception cref="ArgumentException"><paramref name="numericCode"/> is not defined within the values of <see cref="CurrencyIsoCode"/>.</exception> public static CurrencyIsoCode Cast(short numericCode) { return(Enumeration.Cast <CurrencyIsoCode>(numericCode)); }