public void DeserializeEnum_ValidEnums_HelperMethodFasterThanEnumMethod()
        {
            var cache = new ConcurrentDictionary <string, FakeTestingEnum>();

            const int testIterations = 1000000;

            var swDeserializeEnumMember = new Stopwatch();

            swDeserializeEnumMember.Start();

            for (int i = 0; i < testIterations; i++)
            {
                FakeTestingEnum value = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum("YourValue", cache);
            }

            swDeserializeEnumMember.Stop();

            var swDeserializeToString = new Stopwatch();

            swDeserializeToString.Start();

            for (int i = 0; i < testIterations; i++)
            {
                object value = Enum.Parse(typeof(FakeTestingEnum), "YourValue");
            }

            swDeserializeToString.Stop();

            _output.WriteLine($"Helper: {swDeserializeEnumMember.Elapsed}");
            _output.WriteLine($"ToString: {swDeserializeToString.Elapsed}");
            _output.WriteLine($"Ratio: {swDeserializeToString.Elapsed.TotalMilliseconds / swDeserializeEnumMember.Elapsed.TotalMilliseconds}");

            Assert.True(swDeserializeEnumMember.Elapsed <= swDeserializeToString.Elapsed);
        }
        public void GetEnumFromDescription_EmptyStringValue_ReturnsDefaultEnumValue()
        {
            FakeTestingEnum deserializedEnum =
                PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(string.Empty);

            Assert.Equal(default(FakeTestingEnum), deserializedEnum);
        }
        public void GetEnumFromDescription_WhiteSpacePaddedEnumMemberAttrName_ReturnsCorrectEnum()
        {
            FakeTestingEnum deserializedEnum =
                PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum("\t" + YourValueEnumMemberValue + "  ");

            Assert.Equal(FakeTestingEnum.YourValue, deserializedEnum);
        }
        public void GetEnumFromDescription_FromEnumMemberAttributeName_ReturnsCorrectEnum()
        {
            FakeTestingEnum deserializedEnum =
                PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(YourValueEnumMemberValue);

            Assert.Equal(FakeTestingEnum.YourValue, deserializedEnum);
        }
        public void GetEnumFromDescription_WhitespacePaddedEnum_ResturnsCorrectEnum()
        {
            FakeTestingEnum deserializedEnum =
                PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum("\tYourValue  ");

            Assert.Equal(FakeTestingEnum.YourValue, deserializedEnum);
        }
        public void GetEnumFromDescription_FromEnumNameMixedCase_ResturnsCorrectEnum()
        {
            FakeTestingEnum deserializedEnum = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(
                "YoURvaLuE");

            Assert.Equal(FakeTestingEnum.YourValue, deserializedEnum);
        }
        public void GetEnumFromDescription_InvalidEnumString_ReturnsDefaultEnum()
        {
            FakeTestingEnum deserializedEnum =
                PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum("Invalid Value");

            Assert.Equal(default(FakeTestingEnum), deserializedEnum);
        }
        public void DeserializeEnum_ValidEnums_HelperMethodFasterThanEnumMethod()
        {
            var cache = new ConcurrentDictionary <string, FakeTestingEnum>();

            const int testIterations = 100000;

            var swDeserializeEnumMember = new Stopwatch();

            swDeserializeEnumMember.Start();

            for (int i = 0; i < testIterations; i++)
            {
                FakeTestingEnum value = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum("YourValue", cache);
            }

            swDeserializeEnumMember.Stop();

            var swDeserializeStringTo = new Stopwatch();

            swDeserializeStringTo.Start();

            for (int i = 0; i < testIterations; i++)
            {
                object value = Enum.Parse(typeof(FakeTestingEnum), "YourValue");
            }

            swDeserializeStringTo.Stop();
            Assert.True(swDeserializeEnumMember.ElapsedTicks <= swDeserializeStringTo.ElapsedTicks);
        }
        public void DeserializeEnum_WithCacheAndNullString_DefaultEnumAndNothingAddedToCache()
        {
            var             cache            = new ConcurrentDictionary <string, FakeTestingEnum>();
            FakeTestingEnum deserializedEnum = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(null, cache);

            Assert.Equal(default(FakeTestingEnum), deserializedEnum);
            Assert.Equal(0, cache.Count);
        }
        public void DeserializeEnum_WithCacheAndEmptyString_DefaultEnumAndAddedToCache()
        {
            var             cache            = new ConcurrentDictionary <string, FakeTestingEnum>();
            FakeTestingEnum deserializedEnum = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(
                string.Empty, cache);

            Assert.Equal(default(FakeTestingEnum), deserializedEnum);
            Assert.Equal(1, cache.Count);
            Assert.True(cache.ContainsKey(string.Empty));
        }
        public void DeserializeEnum_WithCacheAndWhiteSpaceString_DefaultEnumAndAddedToCache()
        {
            const string Whitespace = "  \r  \t  ";

            var             cache            = new ConcurrentDictionary <string, FakeTestingEnum>();
            FakeTestingEnum deserializedEnum = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(Whitespace, cache);

            Assert.Equal(default(FakeTestingEnum), deserializedEnum);
            Assert.Equal(1, cache.Count);
            Assert.True(cache.ContainsKey(Whitespace));
        }
        public void DeserializeEnum_WithCache_EnumsAddedToCache()
        {
            const int YourvalueInt = 123;
            const int MyValueInt   = 99;

            var cache = new ConcurrentDictionary <string, FakeTestingEnum>();

            FakeTestingEnum yourvalueEnum =
                PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(YourvalueInt.ToString(), cache);

            FakeTestingEnum myvalueEnum = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum(
                MyValueInt.ToString(), cache);

            Assert.Equal(FakeTestingEnum.YourValue, yourvalueEnum);
            Assert.Equal(FakeTestingEnum.MyValue, myvalueEnum);
            Assert.Equal(2, cache.Count);
            Assert.True(cache.ContainsKey(YourvalueInt.ToString()));
            Assert.True(cache.ContainsKey(MyValueInt.ToString()));
        }
        public void GetEnumFromDescription_FromInt_ReturnsCorrectEnum()
        {
            FakeTestingEnum deserializedEnum = PrettyEnumHelpers <FakeTestingEnum> .DeserializeEnum("123");

            Assert.Equal(FakeTestingEnum.YourValue, deserializedEnum);
        }
 public void GetEnumFromDescription_NonEnumType_ThrowsException()
 {
     Assert.Throws <InvalidOperationException>(
         () =>
         PrettyEnumHelpers <DateTime> .DeserializeEnum("yup"));
 }