Skip to content

Commit dafbb99

Browse files
angularsentmilnthorp
authored andcommitted
Fix #652 - Parse fails with custom formatprovider (#655)
* Add failing tests * Fix failing tests * Fix test case
1 parent e4731b7 commit dafbb99

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

UnitsNet.Tests/CustomCode/ParseTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,71 @@ public void TryParseLengthUnitUsEnglish(string s, bool expected)
143143
bool actual = Length.TryParse(s, usEnglish, out Length _);
144144
Assert.Equal(expected, actual);
145145
}
146+
147+
[Theory]
148+
[InlineData("1 ng", "en-US", 1, MassUnit.Nanogram)]
149+
[InlineData("1 нг", "ru-RU", 1, MassUnit.Nanogram)]
150+
[InlineData("1 g", "en-US", 1, MassUnit.Gram)]
151+
[InlineData("1 г", "ru-RU", 1, MassUnit.Gram)]
152+
[InlineData("1 kg", "en-US", 1, MassUnit.Kilogram)]
153+
[InlineData("1 кг", "ru-RU", 1, MassUnit.Kilogram)]
154+
public void ParseMassWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, double expectedValue, Enum expectedUnit)
155+
{
156+
var actual = Mass.Parse(str, new CultureInfo(cultureName));
157+
158+
Assert.Equal(expectedUnit, actual.Unit);
159+
Assert.Equal(expectedValue, actual.Value);
160+
}
161+
162+
[Theory]
163+
[InlineData("1 nm", "en-US", 1, LengthUnit.Nanometer)]
164+
[InlineData("1 нм", "ru-RU", 1, LengthUnit.Nanometer)]
165+
[InlineData("1 m", "en-US", 1, LengthUnit.Meter)]
166+
[InlineData("1 м", "ru-RU", 1, LengthUnit.Meter)]
167+
[InlineData("1 km", "en-US", 1, LengthUnit.Kilometer)]
168+
[InlineData("1 км", "ru-RU", 1, LengthUnit.Kilometer)]
169+
public void ParseLengthWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, double expectedValue, Enum expectedUnit)
170+
{
171+
var actual = Length.Parse(str, new CultureInfo(cultureName));
172+
173+
Assert.Equal(expectedUnit, actual.Unit);
174+
Assert.Equal(expectedValue, actual.Value);
175+
}
176+
177+
[Theory]
178+
[InlineData("1 µN", "en-US", 1, ForceUnit.Micronewton)]
179+
[InlineData("1 мкН", "ru-RU", 1, ForceUnit.Micronewton)]
180+
[InlineData("1 N", "en-US", 1, ForceUnit.Newton)]
181+
[InlineData("1 Н", "ru-RU", 1, ForceUnit.Newton)]
182+
[InlineData("1 kN", "en-US", 1, ForceUnit.Kilonewton)]
183+
[InlineData("1 кН", "ru-RU", 1, ForceUnit.Kilonewton)]
184+
public void ParseForceWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, double expectedValue, Enum expectedUnit)
185+
{
186+
var actual = Force.Parse(str, new CultureInfo(cultureName));
187+
188+
Assert.Equal(expectedUnit, actual.Unit);
189+
Assert.Equal(expectedValue, actual.Value);
190+
}
191+
192+
[Theory]
193+
[InlineData("1 b", "en-US", 1, InformationUnit.Bit)]
194+
[InlineData("1 b", "ru-RU", 1, InformationUnit.Bit)]
195+
[InlineData("1 B", "en-US", 1, InformationUnit.Byte)]
196+
[InlineData("1 B", "ru-RU", 1, InformationUnit.Byte)]
197+
[InlineData("1 Mb", "en-US", 1, InformationUnit.Megabit)]
198+
[InlineData("1 Mb", "ru-RU", 1, InformationUnit.Megabit)]
199+
[InlineData("1 Mib", "en-US", 1, InformationUnit.Mebibit)]
200+
[InlineData("1 Mib", "ru-RU", 1, InformationUnit.Mebibit)]
201+
[InlineData("1 MB", "en-US", 1, InformationUnit.Megabyte)]
202+
[InlineData("1 MB", "ru-RU", 1, InformationUnit.Megabyte)]
203+
[InlineData("1 MiB", "en-US", 1, InformationUnit.Mebibyte)]
204+
[InlineData("1 MiB", "ru-RU", 1, InformationUnit.Mebibyte)]
205+
public void ParseInformationWithPrefixUnits_GivenCulture_ReturnsQuantityWithSameUnitAndValue(string str, string cultureName, decimal expectedValue, Enum expectedUnit)
206+
{
207+
var actual = Information.Parse(str, new CultureInfo(cultureName));
208+
209+
Assert.Equal(expectedUnit, actual.Unit);
210+
Assert.Equal(expectedValue, actual.Value);
211+
}
146212
}
147213
}

UnitsNet.Tests/UnitParserTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// 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

4+
using System;
5+
using System.Globalization;
46
using UnitsNet.Units;
57
using Xunit;
68

@@ -131,5 +133,17 @@ public void Parse_AmbiguousUnitsThrowsException()
131133
Assert.Equal("Cannot parse \"pt\" since it could be either of these: DtpPoint, PrinterPoint", exception1.Message);
132134
Assert.Equal("Cannot parse \"pt\" since it could be either of these: DtpPoint, PrinterPoint", exception2.Message);
133135
}
136+
137+
[Theory]
138+
[InlineData("ng", "en-US", MassUnit.Nanogram)]
139+
[InlineData("нг", "ru-RU", MassUnit.Nanogram)]
140+
[InlineData("g", "en-US", MassUnit.Gram)]
141+
[InlineData("г", "ru-RU", MassUnit.Gram)]
142+
[InlineData("kg", "en-US", MassUnit.Kilogram)]
143+
[InlineData("кг", "ru-RU", MassUnit.Kilogram)]
144+
public void ParseMassUnit_GivenCulture(string str, string cultureName, Enum expectedUnit)
145+
{
146+
Assert.Equal(expectedUnit, UnitParser.Default.Parse<MassUnit>(str, new CultureInfo(cultureName)));
147+
}
134148
}
135149
}

UnitsNet/CustomCode/UnitParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static UnitParser()
5050
[PublicAPI]
5151
public TUnitType Parse<TUnitType>(string unitAbbreviation, [CanBeNull] IFormatProvider formatProvider = null) where TUnitType : Enum
5252
{
53-
return (TUnitType)Parse(unitAbbreviation, typeof(TUnitType));
53+
return (TUnitType)Parse(unitAbbreviation, typeof(TUnitType), formatProvider);
5454
}
5555

5656
/// <summary>

0 commit comments

Comments
 (0)