diff --git a/UnitsNet.Tests/Helpers/TypeDescriptorContext.cs b/UnitsNet.Tests/Helpers/TypeDescriptorContext.cs index 28e1979d88..1c15832f55 100644 --- a/UnitsNet.Tests/Helpers/TypeDescriptorContext.cs +++ b/UnitsNet.Tests/Helpers/TypeDescriptorContext.cs @@ -1,15 +1,13 @@ -// Licensed under MIT No Attribution, see LICENSE file at the root. +// Licensed under MIT No Attribution, see LICENSE file at the root. // Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Text; namespace UnitsNet.Tests.Helpers { /// - /// Is used to imitate e property with attributes + /// Is used to imitate a property with attributes /// public class TypeDescriptorContext : ITypeDescriptorContext { @@ -57,6 +55,10 @@ public TypeDescriptorContext(string name, Attribute[] attributes) PropertyDescriptor = new PropertyDescriptor_(name, attributes); } + public TypeDescriptorContext() + { + } + public IContainer Container => throw new NotImplementedException(); public object Instance => throw new NotImplementedException(); diff --git a/UnitsNet.Tests/QuantityTypeConverterTest.cs b/UnitsNet.Tests/QuantityTypeConverterTest.cs index 5de7b6d269..6d1b587aed 100644 --- a/UnitsNet.Tests/QuantityTypeConverterTest.cs +++ b/UnitsNet.Tests/QuantityTypeConverterTest.cs @@ -1,4 +1,4 @@ -// Licensed under MIT No Attribution, see LICENSE file at the root. +// Licensed under MIT No Attribution, see LICENSE file at the root. // Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. using System; @@ -166,7 +166,7 @@ public void ConvertTo_GivenStringType_ReturnsQuantityString() } [Fact] - public void ConvertTo_GivenSomeQuantitysAndContextWithNoAttributes_ReturnsQuantityStringInUnitOfQuantity() + public void ConvertTo_GivenSomeQuantityAndContextWithNoAttributes_ReturnsQuantityStringInUnitOfQuantity() { var converter = new QuantityTypeConverter(); ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] { }); @@ -178,7 +178,19 @@ public void ConvertTo_GivenSomeQuantitysAndContextWithNoAttributes_ReturnsQuanti } [Fact] - public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitDefaultFormating() + public void ConvertTo_GivenSomeQuantityAndContextWithoutProperty_ReturnsQuantityStringInUnitOfQuantity() + { + var converter = new QuantityTypeConverter(); + ITypeDescriptorContext context = new TypeDescriptorContext(); + Length length = Length.FromMeters(1); + + string convertedQuantity = (string)converter.ConvertTo(context, culture, length, typeof(string)); + + Assert.Equal("1 m", convertedQuantity); + } + + [Fact] + public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitDefaultFormatting() { var converter = new QuantityTypeConverter(); ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] @@ -193,7 +205,7 @@ public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUn } [Fact] - public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitFormateAsValueOnly() + public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitFormatAsValueOnly() { var converter = new QuantityTypeConverter(); ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] @@ -208,7 +220,7 @@ public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUn } [Fact] - public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityStringWithQuantetiesUnitAndFormatedAsValueOnly() + public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityStringWithQuantityUnitAndFormattedAsValueOnly() { var converter = new QuantityTypeConverter(); ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] @@ -223,7 +235,7 @@ public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityS } [Fact] - public void ConvertTo_GivenSomeQuantitysAndContextWithDisplayAsUnitAttributes_ReturnsQuantityStringInSpecifiedDisplayUnit() + public void ConvertTo_GivenSomeQuantityAndContextWithDisplayAsUnitAttributes_ReturnsQuantityStringInSpecifiedDisplayUnit() { var converter = new QuantityTypeConverter(); ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] diff --git a/UnitsNet/QuantityTypeConverter.cs b/UnitsNet/QuantityTypeConverter.cs index 63bb261d7e..712bd3fc79 100644 --- a/UnitsNet/QuantityTypeConverter.cs +++ b/UnitsNet/QuantityTypeConverter.cs @@ -141,7 +141,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT private static TAttribute GetAttribute(ITypeDescriptorContext context) where TAttribute : UnitAttributeBase { TAttribute attribute = null; - AttributeCollection ua = context?.PropertyDescriptor.Attributes; + AttributeCollection ua = context?.PropertyDescriptor?.Attributes; attribute = (TAttribute)ua?[typeof(TAttribute)];