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 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);
        }
        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 MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
        {
            var cache = new UnitAbbreviationsCache();

            cache.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");

            Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter));
        }
Example #5
0
        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);
        }
Example #7
0
        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);
        }