Пример #1
0
        private static IEnumerable <object> GetUnitTypesWithMissingAbbreviations <TUnit>(string cultureName,
                                                                                         IEnumerable <TUnit> unitValues)
            where TUnit : /*Enum constraint hack*/ struct, IComparable, IFormattable
        {
            UnitSystem unitSystem = UnitSystem.GetCached(GetCulture(cultureName));

            var unitsMissingAbbreviations = new List <TUnit>();

            foreach (TUnit unit in unitValues)
            {
                try
                {
#if WINDOWS_UWP
                    unitSystem.GetDefaultAbbreviation(unit.GetType(), Convert.ToInt32(unit));
#else
                    unitSystem.GetDefaultAbbreviation(unit);
#endif
                }
                catch
                {
                    unitsMissingAbbreviations.Add(unit);
                }
            }

            return(unitsMissingAbbreviations.Cast <object>());
        }
Пример #2
0
        public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified()
        {
            UnitSystem usUnits = UnitSystem.GetCached(GetCulture("en-US"));

#if WINDOWS_UWP
            string abbreviation = usUnits.GetDefaultAbbreviation(typeof(CustomUnit), (int)CustomUnit.Unit1);
            Assert.Equal("(no abbreviation for CustomUnit with numeric value 1)", abbreviation);
#else
            string abbreviation = usUnits.GetDefaultAbbreviation(CustomUnit.Unit1);
            Assert.Equal("(no abbreviation for CustomUnit.Unit1)", abbreviation);
#endif
        }
Пример #3
0
        public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
        {
            CultureInfo oldCurrentCulture   = CultureInfo.CurrentCulture;
            CultureInfo 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");
                UnitSystem zuluUnits   = UnitSystem.GetCached(zuluCulture);
                CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture;

                UnitSystem usUnits = UnitSystem.GetCached(AmericanCultureName);
                usUnits.MapUnitToAbbreviation(CustomUnit.Unit1, "US english abbreviation for Unit1");

                // Act
                string abbreviation = zuluUnits.GetDefaultAbbreviation(CustomUnit.Unit1);

                // Assert
                Assert.Equal("US english abbreviation for Unit1", abbreviation);
            }
            finally
            {
                CultureInfo.CurrentCulture   = oldCurrentCulture;
                CultureInfo.CurrentUICulture = oldCurrentUICulture;
            }
        }
Пример #4
0
        public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified()
        {
            UnitSystem usUnits      = new UnitSystem(AmericanCultureName);
            string     abbreviation = usUnits.GetDefaultAbbreviation(CustomUnit.Unit1);

            Assert.Equal("(no abbreviation for CustomUnit.Unit1)", abbreviation);
        }
Пример #5
0
        public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
        {
            UnitSystem unitSystem = UnitSystem.GetCached(AmericanCultureName);

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

            Assert.Equal("m²", unitSystem.GetDefaultAbbreviation(AreaUnit.SquareMeter));
        }
Пример #6
0
        public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified()
        {
            UnitSystem usUnits = UnitSystem.GetCached(CultureInfo.GetCultureInfo("en-US"));

            string abbreviation = usUnits.GetDefaultAbbreviation(CustomUnit.Unit1);

            Assert.AreEqual("(no abbreviation for CustomUnit.Unit1)", abbreviation);
        }
Пример #7
0
        public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
        {
            CultureInfo cultureInfo = CultureInfo.GetCultureInfo("en-US");
            UnitSystem  unitSystem  = UnitSystem.GetCached(cultureInfo);

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

            Assert.AreEqual("m²", unitSystem.GetDefaultAbbreviation(AreaUnit.SquareMeter));
        }
Пример #8
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            Length quantity = (Length)value;

            if (quantity < _minimum || quantity > _maximum)
            {
                string unitAbbrevation = UnitSystem.GetDefaultAbbreviation(_unit, null);
                return(new ValidationResult($"The field {validationContext.DisplayName} must be between {_minimum.Value} to {_maximum.Value} {unitAbbrevation}."));
            }

            return(ValidationResult.Success);
        }
Пример #9
0
        public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
        {
            // CurrentCulture affects number formatting, such as comma or dot as decimal separator.
            // CurrentUICulture affects localization, in this case the abbreviation.
            // Zulu (South Africa)
            UnitSystem zuluUnits = UnitSystem.GetCached(CultureInfo.GetCultureInfo("zu-ZA"));

            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = zuluUnits.Culture;

            UnitSystem usUnits = UnitSystem.GetCached(CultureInfo.GetCultureInfo("en-US"));

            usUnits.MapUnitToAbbreviation(CustomUnit.Unit1, "US english abbreviation for Unit1");

            // Act
            string abbreviation = zuluUnits.GetDefaultAbbreviation(CustomUnit.Unit1);

            // Assert
            Assert.AreEqual("US english abbreviation for Unit1", abbreviation);
        }
Пример #10
0
        private static IEnumerable <object> GetUnitTypesWithMissingAbbreviations <TUnit>(string cultureName, IEnumerable <TUnit> unitValues)
            where TUnit : /*Enum constraint hack*/ struct, IComparable, IFormattable
        {
            UnitSystem unitSystem = UnitSystem.GetCached(new CultureInfo(cultureName));

            var unitsMissingAbbreviations = new List <TUnit>();

            foreach (TUnit unit in unitValues)
            {
                try
                {
                    unitSystem.GetDefaultAbbreviation(unit);
                }
                catch
                {
                    unitsMissingAbbreviations.Add(unit);
                }
            }

            return(unitsMissingAbbreviations.Cast <object>());
        }
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     return(UnitSystem.GetDefaultAbbreviation((TUnit)value, culture));
 }