Ejemplo n.º 1
0
        public void EmptyStringIsDeserializedIntoNullValue()
        {
            NullableEnumConverter <HttpStatusCode> converter = new NullableEnumConverter <HttpStatusCode>();
            string         text = "";
            HttpStatusCode?code = converter.Deserialize(text.AsMemory(), CultureInfo.InvariantCulture, null);

            code.Should().BeNull();
        }
Ejemplo n.º 2
0
        public void NullableEnumDeserializerIsValid()
        {
            NullableEnumConverter <HttpStatusCode> converter = new NullableEnumConverter <HttpStatusCode>();
            string         text = "NotFound";
            HttpStatusCode?code = converter.Deserialize(text.AsMemory(), CultureInfo.InvariantCulture, null);

            code.Should().Be(HttpStatusCode.NotFound);
        }
Ejemplo n.º 3
0
        public void NullValueIsSerializedIntoEmptyString()
        {
            NullableEnumConverter <HttpStatusCode> converter = new NullableEnumConverter <HttpStatusCode>();
            HttpStatusCode?code          = null;
            StringBuilder  stringBuilder = new StringBuilder();

            converter.AppendToStringBuilder(stringBuilder, CultureInfo.InvariantCulture, code, null, ',');
            string serialized = stringBuilder.ToString();

            serialized.Should().Be("");
        }
Ejemplo n.º 4
0
        public void CanConvertTo_DestinationTypeIsString_ReturnsTrue()
        {
            // Setup
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            bool canConvert = converter.CanConvertTo(typeof(string));

            // Assert
            Assert.IsTrue(canConvert);
        }
Ejemplo n.º 5
0
        public void CanConvertTo_DestinationTypeIsInvalid_ReturnsFalse()
        {
            // Setup
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            bool canConvert = converter.CanConvertTo(typeof(NotSupportedType));

            // Assert
            Assert.IsFalse(canConvert);
        }
Ejemplo n.º 6
0
        public void DefaultConstructor_ExpectedValues()
        {
            // Call
            Type nullableType = typeof(SimpleEnum?);
            var  converter    = new NullableEnumConverter(nullableType);

            // Assert
            Assert.IsInstanceOf <NullableConverter>(converter);
            Assert.AreEqual(nullableType, converter.NullableType);
            Assert.AreEqual(typeof(SimpleEnum), converter.UnderlyingType);
        }
Ejemplo n.º 7
0
        public void ConvertFrom_ValueIsNull_ThrowsNotSupportedException()
        {
            // Setup
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            TestDelegate test = () => converter.ConvertFrom(null);

            // Assert
            Assert.Throws <NotSupportedException>(test);
        }
Ejemplo n.º 8
0
        public void NullableEnumSerializerIsValid()
        {
            NullableEnumConverter <HttpStatusCode> converter = new NullableEnumConverter <HttpStatusCode>();
            HttpStatusCode?code          = HttpStatusCode.NotFound;
            StringBuilder  stringBuilder = new StringBuilder();

            converter.AppendToStringBuilder(stringBuilder, CultureInfo.InvariantCulture, code, null, ',');
            string serialized = stringBuilder.ToString();

            serialized.Should().Be("NotFound");
        }
Ejemplo n.º 9
0
        public void ConvertTo_DestinationTypeIsInvalid_ThrowsNotSupportedException()
        {
            // Setup
            const SimpleEnum enumValue = SimpleEnum.FirstValue;
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            TestDelegate test = () => converter.ConvertTo(enumValue, typeof(NotSupportedType));

            // Assert
            Assert.Throws <NotSupportedException>(test);
        }
Ejemplo n.º 10
0
        public void ConvertTo_DestinationTypeIsNull_ThrowsArgumentNullException()
        {
            // Setup
            const SimpleEnum enumValue = SimpleEnum.FirstValue;
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            TestDelegate test = () => converter.ConvertTo(enumValue, null);

            // Assert
            Assert.Throws <ArgumentNullException>(test);
        }
Ejemplo n.º 11
0
        public void ConvertTo_ValueIsOfInvalidType_ThrowsNotSupportedException()
        {
            // Setup
            var notSupportedValue = new NotSupportedType();
            var converter         = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            TestDelegate test = () => converter.ConvertTo(notSupportedValue, typeof(SimpleEnum));

            // Assert
            Assert.Throws <NotSupportedException>(test);
        }
Ejemplo n.º 12
0
        public void ConvertTo_ValueIsNull_DoesNotThrowException()
        {
            // Setup
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            var          result = new object();
            TestDelegate test   = () => result = converter.ConvertTo(null, typeof(string));

            // Assert
            Assert.DoesNotThrow(test);
            Assert.AreEqual(string.Empty, result);
        }
Ejemplo n.º 13
0
        public void GetStandardValues_ITypeDescriptorContextIsNull_ReturnEnumValues()
        {
            // Setup
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            TypeConverter.StandardValuesCollection result = converter.GetStandardValues(null);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(SimpleEnum.FirstValue, result[0]);
            Assert.AreEqual(SimpleEnum.SecondValue, result[1]);
        }
Ejemplo n.º 14
0
        public void ConvertFrom_ValueIsString_ReturnsExpectedEnum()
        {
            // Setup
            const string second    = "<second>";
            var          converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            object result = converter.ConvertFrom(second);

            // Assert
            const SimpleEnum expectedEnumValue = SimpleEnum.SecondValue;

            Assert.AreEqual(expectedEnumValue, result);
        }
Ejemplo n.º 15
0
        public void ConvertTo_DestinationTypeIsString_ReturnsExpectedEnumDisplayName()
        {
            // Setup
            const SimpleEnum enumValue = SimpleEnum.FirstValue;
            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            object result = converter.ConvertTo(enumValue, typeof(string));

            // Assert
            const string expectedText = "<first>";

            Assert.AreEqual(expectedText, result);
        }
Ejemplo n.º 16
0
        public void ConvertNullableEnumUsingDefaultRepresent()
        {
            Assert.IsTrue(NullableEnumConverter.IsNullableEnum(typeof(TestEnum?)));

            TestEnum?origin    = TestEnum.Enum2;
            var      converted = ConvertNullable(origin);

            Assert.IsNotNull(converted);
            Assert.AreEqual(origin, converted);

            var nil = ConvertNullable <TestEnum?>(null);

            Assert.IsNull(nil);
        }
Ejemplo n.º 17
0
        public void EmittedDeserializerIsValid()
        {
            NullableEnumConverter <HttpStatusCode> converter = new NullableEnumConverter <HttpStatusCode>();
            string        text        = "NotFound";
            DynamicMethod deserialize = new DynamicMethod("Deserialize", typeof(HttpStatusCode?), new Type[] { typeof(ReadOnlyMemory <char>), typeof(IFormatProvider) }, typeof(NullableEnumConverterTests));

            deserialize.GetILGenerator()
            .DeclareLocal <HttpStatusCode?>(out LocalBuilder nullableLocal)
            .DeclareLocal <string>(out LocalBuilder secondaryLocal)
            .Ldarga_S(0)
            .Emit(gen => converter.EmitDeserialize(gen, nullableLocal, secondaryLocal, null))
            .Ret();
            HttpStatusCode?code = (HttpStatusCode?)deserialize.Invoke(this, new object?[] { text.AsMemory(), CultureInfo.InvariantCulture }) !;

            code.Should().Be(HttpStatusCode.NotFound);
        }
Ejemplo n.º 18
0
        public void EmittedSerializerSerializesNullValueIntoEmptyString()
        {
            NullableEnumConverter <HttpStatusCode> converter = new NullableEnumConverter <HttpStatusCode>();
            HttpStatusCode?code      = null;
            DynamicMethod  serialize = new DynamicMethod("Serialize", typeof(string), new Type[] { typeof(HttpStatusCode?), typeof(IFormatProvider), typeof(char) }, typeof(NullableEnumConverterTests));

            serialize.GetILGenerator()
            .DeclareLocal <HttpStatusCode?>(out LocalBuilder nullableLocal)
            .Newobj <StringBuilder>()
            .Ldarg_0()
            .Emit(gen => converter.EmitAppendToStringBuilder(gen, nullableLocal, null, null))
            .Callvirt <StringBuilder>("ToString")
            .Ret();
            string serialized = (string)serialize.Invoke(null, new object?[] { code, CultureInfo.InvariantCulture, ',' }) !;

            serialized.Should().Be("");
        }
Ejemplo n.º 19
0
        protected override void Parse(EnumConvertionAttribute attribute, TypeMetadata typeMetadata, MemberMetadata memberMetadata)
        {
            if (attribute != null)
            {
                if ((memberMetadata.Attributes & MemberAttributes.Unmappable) == MemberAttributes.Unmappable)
                    throw new OhmSharpInvalidSchemaException(typeMetadata.Type, memberMetadata.Name,
                        string.Format("Member {0} of {1} cannot be marked with EnumConvertion.", memberMetadata.Name, typeMetadata.Type.FullName));
                if ((memberMetadata.Attributes & MemberAttributes.Ignored) == MemberAttributes.Ignored)
                    throw new OhmSharpInvalidSchemaException(typeMetadata.Type, memberMetadata.Name,
                        string.Format("Member {0} of {1} cannot be marked with both EnumConvertion and MappingIgnore.", memberMetadata.Name, typeMetadata.Type.FullName));

                if (!EnumConverter.IsEnum(memberMetadata.Type) && !NullableEnumConverter.IsNullableEnum(memberMetadata.Type))
                    throw new OhmSharpInvalidSchemaException(typeMetadata.Type,
                        string.Format("Member {0} of {1} not of type Enum cannot be marked with EnumConvertion.", memberMetadata.Name, typeMetadata.Type.FullName));

                memberMetadata.Attributes |= MemberAttributes.Mapped;
                memberMetadata.FormatProvider = attribute.ConvertionInfo;
            }
        }
Ejemplo n.º 20
0
        public void GetStandardValues_ITypeDescriptorContext_ReturnEnumValues()
        {
            // Setup
            var mockRepository        = new MockRepository();
            var typeDescriptorContext = mockRepository.Stub <ITypeDescriptorContext>();

            mockRepository.ReplayAll();

            var converter = new NullableEnumConverter(typeof(SimpleEnum?));

            // Call
            TypeConverter.StandardValuesCollection result = converter.GetStandardValues(typeDescriptorContext);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(SimpleEnum.FirstValue, result[0]);
            Assert.AreEqual(SimpleEnum.SecondValue, result[1]);
            mockRepository.VerifyAll();
        }
Ejemplo n.º 21
0
        private T ConvertNullable <T>(T origin)
        {
            var redisVal = NullableEnumConverter.ConvertTo(origin, typeof(T), null);

            return((T)NullableEnumConverter.ConvertFrom(redisVal, typeof(T), null));
        }
 public void TryCreateReturnsInstanceForEnumType()
 {
     NullableEnumConverter <DayOfWeek?> .TryCreate(out _).ShouldBeTrue();
 }
 public void TryCreateReturnsFalseForNonNullableTypeReturnsFalse()
 {
     NullableEnumConverter <int> .TryCreate(out _).ShouldBeFalse();
 }
 public void TryCreateReturnsFalseForNonEnumUnderlyingType()
 {
     NullableEnumConverter <int?> .TryCreate(out _).ShouldBeFalse();
 }