/// <summary> /// Construct a converter using the provided map of {name : quantity} /// </summary> /// <param name="quantities">The dictionary of quantity names</param> /// <param name="abbreviations">The unit abbreviations used for the serialization </param> /// <param name="propertyComparer">The comparer used to compare the property names (e.g. StringComparer.OrdinalIgnoreCase) </param> public AbbreviatedUnitsConverter(IDictionary <string, QuantityInfo> quantities, UnitAbbreviationsCache abbreviations, IEqualityComparer <string> propertyComparer) { _quantities = quantities; _abbreviations = abbreviations; _propertyComparer = propertyComparer; _unitParser = new UnitParser(abbreviations); }
public void GetDefaultAbbreviationFallsBackToUsEnglishCulture() { var oldCurrentCulture = CultureInfo.CurrentCulture; var oldCurrentUICulture = CultureInfo.CurrentUICulture; try { // CurrentCulture affects number formatting, such as comma or dot as decimal separator. // CurrentUICulture affects localization, in this case the abbreviation. // Zulu (South Africa) var zuluCulture = new CultureInfo("zu-ZA"); CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture; var abbreviationsCache = new UnitAbbreviationsCache(); abbreviationsCache.MapUnitToAbbreviation(CustomUnit.Unit1, AmericanCulture, "US english abbreviation for Unit1"); // Act string abbreviation = abbreviationsCache.GetDefaultAbbreviation(CustomUnit.Unit1, zuluCulture); // Assert Assert.Equal("US english abbreviation for Unit1", abbreviation); } finally { CultureInfo.CurrentCulture = oldCurrentCulture; CultureInfo.CurrentUICulture = oldCurrentUICulture; } }
public void MapUnitToDefaultAbbreviation_GivenUnitAndCulture_SetsDefaultAbbreviationForUnitAndCulture() { var cache = new UnitAbbreviationsCache(); cache.MapUnitToDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2"); Assert.Equal("m^2", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture)); }
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits() { var cache = new UnitAbbreviationsCache(); cache.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2"); Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter)); }
public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviation, AreaUnit expected) { var abbrevCache = new UnitAbbreviationsCache(); abbrevCache.MapUnitToAbbreviation(expected, customAbbreviation); var parser = new UnitParser(abbrevCache); var actual = parser.Parse <AreaUnit>(customAbbreviation); Assert.Equal(expected, actual); }
public void Parse_MappedCustomUnit() { var unitAbbreviationsCache = new UnitAbbreviationsCache(); unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "fooh"); var unitParser = new UnitParser(unitAbbreviationsCache); var parsedUnit = unitParser.Parse <HowMuchUnit>("fooh"); Assert.Equal(HowMuchUnit.Some, parsedUnit); }
public void Parse_MappedCustomUnit() { var unitAbbreviationsCache = new UnitAbbreviationsCache(); unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "fooh"); var quantityParser = new QuantityParser(unitAbbreviationsCache); HowMuch q = quantityParser.Parse <HowMuch, HowMuchUnit>("1 fooh", null, (value, unit) => new HowMuch((double)value, unit)); Assert.Equal(HowMuchUnit.Some, q.Unit); Assert.Equal(1, q.Value); }
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); }
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); }
public void GetDefaultAbbreviationThrowsNotImplementedExceptionIfNoneExist() { var unitAbbreviationCache = new UnitAbbreviationsCache(); Assert.Throws <NotImplementedException>(() => unitAbbreviationCache.GetDefaultAbbreviation(CustomUnit.Unit1)); }