Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions UnitsNet.Tests/Helpers/TypeDescriptorContext.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Is used to imitate e property with attributes
/// Is used to imitate a property with attributes
/// </summary>
public class TypeDescriptorContext : ITypeDescriptorContext
{
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 18 additions & 6 deletions UnitsNet.Tests/QuantityTypeConverterTest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -166,7 +166,7 @@ public void ConvertTo_GivenStringType_ReturnsQuantityString()
}

[Fact]
public void ConvertTo_GivenSomeQuantitysAndContextWithNoAttributes_ReturnsQuantityStringInUnitOfQuantity()
public void ConvertTo_GivenSomeQuantityAndContextWithNoAttributes_ReturnsQuantityStringInUnitOfQuantity()
{
var converter = new QuantityTypeConverter<Length>();
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] { });
Expand All @@ -178,7 +178,19 @@ public void ConvertTo_GivenSomeQuantitysAndContextWithNoAttributes_ReturnsQuanti
}

[Fact]
public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitDefaultFormating()
public void ConvertTo_GivenSomeQuantityAndContextWithoutProperty_ReturnsQuantityStringInUnitOfQuantity()
{
var converter = new QuantityTypeConverter<Length>();
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<Length>();
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]
Expand All @@ -193,7 +205,7 @@ public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUn
}

[Fact]
public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitFormateAsValueOnly()
public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUnitFormatAsValueOnly()
{
var converter = new QuantityTypeConverter<Length>();
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]
Expand All @@ -208,7 +220,7 @@ public void ConvertTo_TestDisplayAsFormatting_ReturnsQuantityStringWithDisplayUn
}

[Fact]
public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityStringWithQuantetiesUnitAndFormatedAsValueOnly()
public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityStringWithQuantityUnitAndFormattedAsValueOnly()
{
var converter = new QuantityTypeConverter<Length>();
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]
Expand All @@ -223,7 +235,7 @@ public void ConvertTo_TestDisplayAsFormattingWithoutDefinedUnit_ReturnsQuantityS
}

[Fact]
public void ConvertTo_GivenSomeQuantitysAndContextWithDisplayAsUnitAttributes_ReturnsQuantityStringInSpecifiedDisplayUnit()
public void ConvertTo_GivenSomeQuantityAndContextWithDisplayAsUnitAttributes_ReturnsQuantityStringInSpecifiedDisplayUnit()
{
var converter = new QuantityTypeConverter<Length>();
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[]
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet/QuantityTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
private static TAttribute GetAttribute<TAttribute>(ITypeDescriptorContext context) where TAttribute : UnitAttributeBase
{
TAttribute attribute = null;
AttributeCollection ua = context?.PropertyDescriptor.Attributes;
AttributeCollection ua = context?.PropertyDescriptor?.Attributes;

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

Expand Down