Skip to content

Commit 7d3f110

Browse files
Case comparison - code review response
1 parent 3317a49 commit 7d3f110

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

UnitsNet.Tests/UnitParserTests.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,32 @@ public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviatio
4242
}
4343

4444
[Fact]
45-
public void Parse_AbbreviationCaseInsensitive()
45+
public void Parse_AbbreviationCaseInsensitive_Lowercase_years()
4646
{
47+
// Arrange
4748
var abbreviation = "years";
4849
var expected = DurationUnit.Year365;
4950
var parser = UnitParser.Default;
5051

52+
// Act
5153
var actual = parser.Parse<DurationUnit>(abbreviation);
5254

55+
// Assert
5356
Assert.Equal(expected, actual);
57+
}
5458

55-
abbreviation = "Years";
56-
actual = parser.Parse<DurationUnit>(abbreviation);
59+
[Fact]
60+
public void Parse_AbbreviationCaseInsensitive_Uppercase_Years()
61+
{
62+
// Arrange
63+
var abbreviation = "Years";
64+
var expected = DurationUnit.Year365;
65+
var parser = UnitParser.Default;
66+
67+
// Act
68+
var actual = parser.Parse<DurationUnit>(abbreviation);
5769

70+
// Assert
5871
Assert.Equal(expected, actual);
5972
}
6073

UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
22
// https://github.com/angularsen/UnitsNet
33
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -65,7 +65,9 @@ internal List<int> GetUnitsForAbbreviation(string abbreviation)
6565
{
6666
return abbreviationToUnitMap
6767
.Where(x => x.Key.Equals(abbreviation, StringComparison.OrdinalIgnoreCase))
68-
.SelectMany(x => x.Value).Distinct().ToList();
68+
.SelectMany(x => x.Value)
69+
.Distinct()
70+
.ToList();
6971
}
7072

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

UnitsNet/UnitConverter.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public static class UnitConverter
4545
private static readonly string UnitTypeNamespace = typeof(LengthUnit).Namespace;
4646
private static readonly Assembly UnitsNetAssembly = typeof(Length).GetAssembly();
4747

48+
private static readonly List<Type> QuantityTypes = UnitsNetAssembly.GetTypes()
49+
.Where(x => x.FullName.StartsWith(QuantityNamespace))
50+
.ToList();
51+
52+
private static readonly List<Type> UnitTypes = UnitsNetAssembly.GetTypes()
53+
.Where(x => x.FullName.StartsWith(UnitTypeNamespace))
54+
.ToList();
55+
4856
/// <summary>
4957
/// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter".
5058
/// This is particularly useful for creating things like a generated unit conversion UI,
@@ -387,7 +395,7 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit
387395
unitValue = null;
388396
var eNames = Enum.GetNames(unitType);
389397
unitName = eNames.FirstOrDefault(x => x.Equals(unitName, StringComparison.OrdinalIgnoreCase));
390-
if(unitName is null)
398+
if(unitName == null)
391399
return false;
392400

393401
unitValue = Enum.Parse(unitType, unitName);
@@ -397,14 +405,10 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit
397405
return true;
398406
}
399407

400-
private static List<Type> UnitTypes = UnitsNetAssembly.GetTypes()
401-
.Where(x => x.FullName.StartsWith(UnitTypeNamespace))
402-
.ToList();
403-
404408
private static bool TryGetUnitType(string quantityName, out Type unitType)
405409
{
406410
quantityName += "Unit";
407-
unitType = QuantityTypes.FirstOrDefault(x =>
411+
unitType = UnitTypes.FirstOrDefault(x =>
408412
x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase));
409413

410414
if(unitType == null)
@@ -413,10 +417,6 @@ private static bool TryGetUnitType(string quantityName, out Type unitType)
413417
return true;
414418
}
415419

416-
private static List<Type> QuantityTypes = UnitsNetAssembly.GetTypes()
417-
.Where(x => x.FullName.StartsWith(QuantityNamespace))
418-
.ToList();
419-
420420
private static bool TryGetQuantityType(string quantityName, out Type quantityType)
421421
{
422422
quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase));

0 commit comments

Comments
 (0)