Skip to content

Commit d3bdd17

Browse files
Insensitive case comparison.
1 parent 2d757e5 commit d3bdd17

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

UnitsNet.Tests/UnitConverterTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace UnitsNet.Tests
2626
public class UnitConverterTest
2727
{
2828
[Theory]
29+
[InlineData(0, 0, "length", "meter", "centimeter")]
2930
[InlineData(0, 0, "Length", "Meter", "Centimeter")]
3031
[InlineData(100, 1, "Length", "Meter", "Centimeter")]
3132
[InlineData(1, 1000, "Mass", "Gram", "Kilogram")]
@@ -35,6 +36,18 @@ public void ConvertByName_ConvertsTheValueToGivenUnit(double expectedValue, doub
3536
Assert.Equal(expectedValue, UnitConverter.ConvertByName(inputValue, quantityTypeName, fromUnit, toUnit));
3637
}
3738

39+
[Fact]
40+
public void ConvertByName__QuantityCaseInsensitive()
41+
{
42+
Assert.Equal(0, UnitConverter.ConvertByName(0, "length", "Meter", "Centimeter"));
43+
}
44+
45+
[Fact]
46+
public void ConvertByName__UnitTypeCaseInsensitive()
47+
{
48+
Assert.Equal(0, UnitConverter.ConvertByName(0, "Length", "meter", "Centimeter"));
49+
}
50+
3851
[Theory]
3952
[InlineData(1, "UnknownQuantity", "Meter", "Centimeter")]
4053
public void ConvertByName_ThrowsQuantityNotFoundExceptionOnUnknownQuantity(double inputValue, string quantityTypeName, string fromUnit, string toUnit)

UnitsNet.Tests/UnitParserTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviatio
4141
Assert.Equal(expected, actual);
4242
}
4343

44+
[Fact]
45+
public void Parse_AbbreviationCaseInsensitive()
46+
{
47+
var abbreviation = "years";
48+
var expected = DurationUnit.Year365;
49+
var parser = UnitParser.Default;
50+
51+
var actual = parser.Parse<DurationUnit>(abbreviation);
52+
53+
Assert.Equal(expected, actual);
54+
55+
abbreviation = "Years";
56+
actual = parser.Parse<DurationUnit>(abbreviation);
57+
58+
Assert.Equal(expected, actual);
59+
}
60+
4461
[Fact]
4562
public void Parse_UnknownAbbreviationThrowsUnitNotFoundException()
4663
{

UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ internal List<string> GetAbbreviationsForUnit(int unit)
6363

6464
internal List<int> GetUnitsForAbbreviation(string abbreviation)
6565
{
66-
if(!abbreviationToUnitMap.TryGetValue(abbreviation, out var units))
67-
abbreviationToUnitMap[abbreviation] = units = new List<int>();
68-
69-
return units.Distinct().ToList();
66+
return abbreviationToUnitMap
67+
.Where(x => x.Key.Equals(abbreviation, StringComparison.InvariantCultureIgnoreCase))
68+
.SelectMany(x => x.Value).Distinct().ToList();
7069
}
7170

7271
internal void Add(int unit, string abbreviation, bool setAsDefault = false)

UnitsNet/UnitConverter.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// THE SOFTWARE.
2121

2222
using System;
23+
using System.Collections.Generic;
2324
using System.Globalization;
2425
using System.Linq;
2526
using System.Reflection;
@@ -384,8 +385,9 @@ private static bool HasParameterTypes(MethodInfo methodInfo, params Type[] expec
384385
private static bool TryParseUnit(Type unitType, string unitName, out object unitValue)
385386
{
386387
unitValue = null;
387-
388-
if(!Enum.IsDefined(unitType, unitName))
388+
var eNames = Enum.GetNames(unitType);
389+
unitName = eNames.FirstOrDefault(x => x.Equals(unitName, StringComparison.InvariantCultureIgnoreCase));
390+
if(unitName is null)
389391
return false;
390392

391393
unitValue = Enum.Parse(unitType, unitName);
@@ -395,22 +397,30 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit
395397
return true;
396398
}
397399

400+
private static List<Type> UnitTypes = UnitsNetAssembly.ExportedTypes
401+
.Where(x => x.FullName.StartsWith(UnitTypeNamespace))
402+
.ToList();
403+
398404
private static bool TryGetUnitType(string quantityName, out Type unitType)
399405
{
400-
string unitTypeName = $"{UnitTypeNamespace}.{quantityName}Unit";
406+
quantityName += "Unit";
407+
unitType = QuantityTypes.FirstOrDefault(x =>
408+
x.Name.Equals(quantityName, StringComparison.InvariantCultureIgnoreCase));
401409

402-
unitType = UnitsNetAssembly.GetType(unitTypeName); // ex: UnitsNet.Units.LengthUnit enum
403410
if(unitType == null)
404411
return false;
405412

406413
return true;
407414
}
408415

416+
private static List<Type> QuantityTypes = UnitsNetAssembly.ExportedTypes
417+
.Where(x => x.FullName.StartsWith(QuantityNamespace))
418+
.ToList();
419+
409420
private static bool TryGetQuantityType(string quantityName, out Type quantityType)
410421
{
411-
string quantityTypeName = $"{QuantityNamespace}.{quantityName}";
412-
413-
quantityType = UnitsNetAssembly.GetType(quantityTypeName); // ex: UnitsNet.Length struct
422+
quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.InvariantCultureIgnoreCase));
423+
414424
if(quantityType == null)
415425
return false;
416426

0 commit comments

Comments
 (0)