Skip to content

Commit 071c88d

Browse files
Fix NullReferenceException in QuantityTypeConverter (#755) (#756)
1 parent 74f2999 commit 071c88d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

UnitsNet.Tests/Helpers/TypeDescriptorContext.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
// Licensed under MIT No Attribution, see LICENSE file at the root.
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.ComponentModel;
7-
using System.Text;
86

97
namespace UnitsNet.Tests.Helpers
108
{
119
/// <summary>
12-
/// Is used to imitate e property with attributes
10+
/// Is used to imitate a property with attributes
1311
/// </summary>
1412
public class TypeDescriptorContext : ITypeDescriptorContext
1513
{
@@ -57,6 +55,10 @@ public TypeDescriptorContext(string name, Attribute[] attributes)
5755
PropertyDescriptor = new PropertyDescriptor_(name, attributes);
5856
}
5957

58+
public TypeDescriptorContext()
59+
{
60+
}
61+
6062
public IContainer Container => throw new NotImplementedException();
6163

6264
public object Instance => throw new NotImplementedException();

UnitsNet.Tests/QuantityTypeConverterTest.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed under MIT No Attribution, see LICENSE file at the root.
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
@@ -166,7 +166,7 @@ public void ConvertTo_GivenStringType_ReturnsQuantityString()
166166
}
167167

168168
[Fact]
169-
public void ConvertTo_GivenSomeQuantitysAndContextWithNoAttributes_ReturnsQuantityStringInUnitOfQuantity()
169+
public void ConvertTo_GivenSomeQuantityAndContextWithNoAttributes_ReturnsQuantityStringInUnitOfQuantity()
170170
{
171171
var converter = new QuantityTypeConverter<Length>();
172172
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] { });
@@ -178,7 +178,19 @@ public void ConvertTo_GivenSomeQuantitysAndContextWithNoAttributes_ReturnsQuanti
178178
}
179179

180180
[Fact]
181-
public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitDefaultFormating()
181+
public void ConvertTo_GivenSomeQuantityAndContextWithoutProperty_ReturnsQuantityStringInUnitOfQuantity()
182+
{
183+
var converter = new QuantityTypeConverter<Length>();
184+
ITypeDescriptorContext context = new TypeDescriptorContext();
185+
Length length = Length.FromMeters(1);
186+
187+
string convertedQuantity = (string)converter.ConvertTo(context, culture, length, typeof(string));
188+
189+
Assert.Equal("1 m", convertedQuantity);
190+
}
191+
192+
[Fact]
193+
public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitDefaultFormatting()
182194
{
183195
var converter = new QuantityTypeConverter<Length>();
184196
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]
@@ -193,7 +205,7 @@ public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUn
193205
}
194206

195207
[Fact]
196-
public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitFormateAsValueOnly()
208+
public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitFormatAsValueOnly()
197209
{
198210
var converter = new QuantityTypeConverter<Length>();
199211
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]
@@ -208,7 +220,7 @@ public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUn
208220
}
209221

210222
[Fact]
211-
public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityStringWithQuantetiesUnitAndFormatedAsValueOnly()
223+
public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityStringWithQuantityUnitAndFormattedAsValueOnly()
212224
{
213225
var converter = new QuantityTypeConverter<Length>();
214226
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]
@@ -223,7 +235,7 @@ public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityS
223235
}
224236

225237
[Fact]
226-
public void ConvertTo_GivenSomeQuantitysAndContextWithDisplayAsUnitAttributes_ReturnsQuantityStringInSpecifiedDisplayUnit()
238+
public void ConvertTo_GivenSomeQuantityAndContextWithDisplayAsUnitAttributes_ReturnsQuantityStringInSpecifiedDisplayUnit()
227239
{
228240
var converter = new QuantityTypeConverter<Length>();
229241
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]

UnitsNet/QuantityTypeConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
141141
private static TAttribute GetAttribute<TAttribute>(ITypeDescriptorContext context) where TAttribute : UnitAttributeBase
142142
{
143143
TAttribute attribute = null;
144-
AttributeCollection ua = context?.PropertyDescriptor.Attributes;
144+
AttributeCollection ua = context?.PropertyDescriptor?.Attributes;
145145

146146
attribute = (TAttribute)ua?[typeof(TAttribute)];
147147

0 commit comments

Comments
 (0)