/// <summary>
        /// Determines whether the format of the postal code is valid using on the postal code regex of that country
        /// </summary>
        public static void Validate(this Country.CountryViewModel country, string postalCode)
        {
            if (country == null)
            {
                throw new ArgumentNullException("country");
            }
            if (string.IsNullOrWhiteSpace(postalCode))
            {
                throw new ArgumentException("The postal code cannot be null or contain only whitespaces");
            }

            if (string.IsNullOrWhiteSpace(country.PostalCodeRegex))
            {
                throw new InvalidOperationException(string.Format("{0} does not have a regex for postal codes", country.CountryName));
            }

            var regex = new Regex(country.PostalCodeRegex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

            if (regex.IsMatch(postalCode))
            {
                return;
            }

            throw new InvalidOperationException(string.Format("The postal code {0} does not match the regex of {1}", postalCode, country.CountryName));
        }
Esempio n. 2
0
        private static Mock <ICountryService> CreateCountryServiceMock()
        {
            var country = new Country.CountryViewModel();

            var countryService = new Mock <ICountryService>(MockBehavior.Strict);

            countryService.Setup(c => c.RetrieveCountryAsync(It.IsAny <RetrieveCountryParam>()))
            .ReturnsAsync(country)
            .Verifiable();

            countryService.Setup(c => c.RetrieveRegionDisplayNameAsync(It.IsAny <RetrieveRegionDisplayNameParam>()))
            .ReturnsAsync(GetRandom.String(32))
            .Verifiable();
            return(countryService);
        }
        private void UseCountryServiceMock(string expectedPostalCodeRegex)
        {
            var country = new Country.CountryViewModel {
                PostalCodeRegex = expectedPostalCodeRegex
            };

            var countryService = new Mock <ICountryService>(MockBehavior.Strict);

            countryService.Setup(c => c.RetrieveCountryAsync(It.IsAny <RetrieveCountryParam>()))
            .ReturnsAsync(country)
            .Verifiable();

            countryService.Setup(c => c.RetrieveRegionDisplayNameAsync(It.IsAny <RetrieveRegionDisplayNameParam>()))
            .ReturnsAsync(GetRandom.String(32))
            .Verifiable();

            Container.Use(countryService);
        }
        /// <summary>
        /// Determines whether the format of the postal code is valid using on the postal code regex of that country
        /// </summary>
        public static void Validate(this Country.CountryViewModel country, string postalCode)
        {
            if (country == null)
            {
                throw new ArgumentNullException(nameof(country));
            }
            if (string.IsNullOrWhiteSpace(postalCode))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(), nameof(postalCode));
            }

            if (!string.IsNullOrWhiteSpace(country.PostalCodeRegex))
            {
                var regex = new Regex(country.PostalCodeRegex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                if (regex.IsMatch(postalCode))
                {
                    return;
                }
                else
                {
                    throw new InvalidOperationException(string.Format("The postal code {0} does not match the regex of {1}", postalCode, country.CountryName));
                }
            }
        }