public void EmptyStringIsDeserializedIntoNullValue() { NullableEnumConverter <HttpStatusCode> converter = new NullableEnumConverter <HttpStatusCode>(); string text = ""; HttpStatusCode?code = converter.Deserialize(text.AsMemory(), CultureInfo.InvariantCulture, null); code.Should().BeNull(); }
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); }
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(""); }
public void CanConvertTo_DestinationTypeIsString_ReturnsTrue() { // Setup var converter = new NullableEnumConverter(typeof(SimpleEnum?)); // Call bool canConvert = converter.CanConvertTo(typeof(string)); // Assert Assert.IsTrue(canConvert); }
public void CanConvertTo_DestinationTypeIsInvalid_ReturnsFalse() { // Setup var converter = new NullableEnumConverter(typeof(SimpleEnum?)); // Call bool canConvert = converter.CanConvertTo(typeof(NotSupportedType)); // Assert Assert.IsFalse(canConvert); }
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); }
public void ConvertFrom_ValueIsNull_ThrowsNotSupportedException() { // Setup var converter = new NullableEnumConverter(typeof(SimpleEnum?)); // Call TestDelegate test = () => converter.ConvertFrom(null); // Assert Assert.Throws <NotSupportedException>(test); }
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"); }
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); }
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); }
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); }
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); }
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]); }
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); }
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); }
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); }
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); }
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(""); }
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; } }
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(); }
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(); }