public void ToStringTest() { Assert.AreEqual("Max", Enum <TestULongEnum> .ToString(TestULongEnum.Max)); Assert.AreEqual("0", Enum <EmptyEnum> .ToString(default(EmptyEnum))); Assert.AreEqual("None", Enum <TestLongEnum> .ToString(default(TestLongEnum))); Assert.AreEqual("Alpha", Enum <TestLongEnum> .ToString(TestLongEnum.Alpha)); Assert.AreEqual("-2147483648", Enum <EmptyEnum> .ToString((EmptyEnum)(1 << 31))); Assert.AreEqual("-2147483647", Enum <EmptyEnum> .ToString((EmptyEnum)((1 << 31) | 1))); Assert.AreEqual("1, -2147483648", Enum <EmptyEnum> .ToString((EmptyEnum)((1 << 31) | 1), EnumFormattingOptions.DistinctFlags)); Assert.AreNotEqual("-10", Enum <TestULongEnum> .ToString(unchecked ((TestULongEnum)(-10)))); Assert.AreEqual("-10", Enum <TestLongEnum> .ToString((TestLongEnum)(-10))); Assert.AreEqual("-10", Enum <TestIntEnum> .ToString((TestIntEnum)(-10))); TestLongEnum e = TestLongEnum.Gamma | TestLongEnum.Alphabet; Assert.AreEqual("Alphabet, Gamma", e.ToString(EnumFormattingOptions.Auto)); Assert.AreEqual("7", e.ToString(EnumFormattingOptions.NonFlags)); Assert.AreEqual("Alpha, Beta, Gamma", e.ToString(EnumFormattingOptions.DistinctFlags)); Assert.AreEqual("Alphabet, Gamma", e.ToString(EnumFormattingOptions.CompoundFlagsOrNumber)); Assert.AreEqual("Alphabet, Gamma", e.ToString(EnumFormattingOptions.CompoundFlagsAndNumber)); e += 16; Assert.AreEqual("23", e.ToString(EnumFormattingOptions.Auto)); Assert.AreEqual("23", e.ToString(EnumFormattingOptions.NonFlags)); Assert.AreEqual("Alpha, Beta, Gamma, 16", e.ToString(EnumFormattingOptions.DistinctFlags)); Assert.AreEqual("23", e.ToString(EnumFormattingOptions.CompoundFlagsOrNumber)); Assert.AreEqual("16, Alphabet, Gamma", e.ToString(EnumFormattingOptions.CompoundFlagsAndNumber)); TestIntEnum ie = TestIntEnum.Simple | TestIntEnum.Normal | TestIntEnum.Risky; Assert.AreEqual("Simple, Normal, Risky", ie.ToString(EnumFormattingOptions.Auto)); }
public void ParseTest() { Assert.AreEqual(default(EmptyEnum), Enum <EmptyEnum> .Parse("0")); Assert.AreEqual(default(EmptyEnum), Enum <EmptyEnum> .Parse("0".AsSegment())); #if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0) Assert.AreEqual(default(EmptyEnum), Enum <EmptyEnum> .Parse("0".AsSpan())); #endif Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse("Max")); Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse("Max".AsSegment())); #if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0) Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse("Max".AsSpan())); #endif Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse(UInt64.MaxValue.ToString())); Assert.AreEqual(TestLongEnum.Min, Enum <TestLongEnum> .Parse("Min")); Assert.AreEqual(TestLongEnum.Min, Enum <TestLongEnum> .Parse(Int64.MinValue.ToString())); Assert.AreEqual(TestLongEnum.Min, Enum <TestLongEnum> .Parse(" -9223372036854775808 ")); Assert.AreEqual(TestLongEnum.Max, Enum <TestLongEnum> .Parse("9223372036854775807")); Assert.AreEqual((EmptyEnum)(-10), Enum <EmptyEnum> .Parse("-10")); Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("Alpha")); Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("AlphaRedefined")); Assert.AreEqual(TestLongEnum.AlphaRedefined, Enum <TestLongEnum> .Parse("AlphaRedefined")); Assert.AreEqual(TestLongEnum.AlphaRedefined, Enum <TestLongEnum> .Parse("Alpha")); Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("alpha", true)); Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("ALPHAREDEFINED", true)); Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("ALPHAREDEFINED".AsSegment(), true)); #if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0) Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("ALPHAREDEFINED".AsSpan(), true)); #endif TestLongEnum e = TestLongEnum.Gamma | TestLongEnum.Alphabet; Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Gamma, Alphabet")); Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Gamma, Alphabet".AsSegment())); #if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0) Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Gamma, Alphabet".AsSpan())); #endif Assert.AreEqual(e, Enum <TestLongEnum> .Parse("7")); Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha, Beta, Gamma")); Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha Beta Gamma", " ")); Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha | Beta | Gamma", "|")); e += 16; Assert.AreEqual(e, Enum <TestLongEnum> .Parse("23")); Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha, Beta, Gamma, 16")); Assert.AreEqual(e, Enum <TestLongEnum> .Parse("16, Gamma, Alphabet")); Assert.AreEqual(e, Enum <TestLongEnum> .Parse("16, Gamma, Alphabet".AsSegment())); #if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0) Assert.AreEqual(e, Enum <TestLongEnum> .Parse("16, Gamma, Alphabet".AsSpan())); #endif Assert.IsFalse(Enum <TestLongEnum> .TryParse(UInt64.MaxValue.ToString(), out var _)); Assert.IsFalse(Enum <TestLongEnum> .TryParse("Beta, Gamma, , Delta, 16", out var _)); Assert.IsFalse(Enum <TestLongEnum> .TryParse(" ", out var _)); Assert.IsFalse(Enum <TestLongEnum> .TryParse("9223372036854775808", out var _)); Assert.IsFalse(Enum <TestLongEnum> .TryParse(" -9223372036854775809 ", out var _)); TestIntEnum ie = TestIntEnum.Simple | TestIntEnum.Normal | TestIntEnum.Risky; Assert.AreEqual(ie, Enum <TestIntEnum> .Parse(ie.ToString(EnumFormattingOptions.Auto))); }