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
47 changes: 47 additions & 0 deletions UnitsNet.Tests/QuantityParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,53 @@ namespace UnitsNet.Tests
{
public class QuantityParserTests
{
[Fact]
public void Parse_WithSingleCaseInsensitiveMatch_ParsesWithMatchedUnit()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "foo");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>("1 FOO",
null,
(value, unit) => new HowMuch((double) value, unit));

Assert.Equal(HowMuchUnit.Some, q.Unit);
Assert.Equal(1, q.Value);
}

[Fact]
public void Parse_WithOneCaseInsensitiveMatchAndOneExactMatch_ParsesWithTheExactMatchUnit()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "foo");
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "FOO");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>("1 FOO",
null,
(value, unit) => new HowMuch((double) value, unit));

Assert.Equal(HowMuchUnit.ATon, q.Unit);
Assert.Equal(1, q.Value);
}

[Fact]
public void Parse_WithMultipleCaseInsensitiveMatchesButNoExactMatches_ThrowsUnitNotFoundException()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "foo");
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "FOO");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

void Act()
{
quantityParser.Parse<HowMuch, HowMuchUnit>("1 Foo", null, (value, unit) => new HowMuch((double) value, unit));
}

Assert.Throws<UnitNotFoundException>(Act);
}

[Fact]
public void Parse_MappedCustomUnit()
{
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet/CustomCode/QuantityParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private string CreateRegexPatternForQuantity<TUnitType>(IFormatProvider formatPr
private Regex CreateRegexForQuantity<TUnitType>([CanBeNull] IFormatProvider formatProvider) where TUnitType : Enum
{
var pattern = CreateRegexPatternForQuantity<TUnitType>(formatProvider);
return new Regex(pattern, RegexOptions.Singleline);
return new Regex(pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
}
}
}