public void Parse_ValidPostcodeAtStartOfLongerString_IsInvalid(string invalidPostcode,
                                                                PostcodeParseOptions options)
 {
     // Act
     Assert.Throws <FormatException>(() => Postcode.Parse(invalidPostcode, options),
                                     string.Format("No exception thrown when parsing {0}", invalidPostcode));
 }
Exemple #2
0
        private static bool TryParseBfpo(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchBfpo) == PostcodeParseOptions.None)
            {
                return(false);
            }

            return(TryParseRegex(sanitizedInput, options, ref result, RegexBfpoFull, RegexBfpoOuterStandalone));
        }
Exemple #3
0
        private static bool TryParseSanta(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchSanta) == PostcodeParseOptions.None)
            {
                return(false);
            }

            return(TryParseHardcoded(sanitizedInput, options, ref result, "SAN", "TA1"));
        }
Exemple #4
0
        private static bool TryParseGiroBank(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchGirobank) == PostcodeParseOptions.None)
            {
                return(false);
            }

            return(TryParseHardcoded(sanitizedInput, options, ref result, "GIR", "0AA"));
        }
Exemple #5
0
        /// <summary>
        /// Parses a string as a Postcode.
        /// </summary>
        /// <param name="value">String to be parsed</param>
        /// <param name="options"></param>
        /// <returns>Postcode object</returns>
        /// <exception cref="FormatException">
        /// If the passed string cannot be parsed as a UK postcode
        /// </exception>
        public static Postcode Parse(string value, PostcodeParseOptions options)
        {
            Postcode p;

            if (TryParse(value, out p, options))
            {
                return(p);
            }

            throw new FormatException();
        }
        public void ToString_ReturnsStringRepresentationOfPostcode(string input, PostcodeParseOptions options,
                                                                   string expectedToString)
        {
            // Arrange
            Postcode postcode = Postcode.Parse(input, options);

            // Act
            string actualToString = postcode.ToString();

            // Assert
            Assert.That(actualToString, Is.EqualTo(expectedToString));
        }
        public void Parse_CanCombineOptions(string input, string expectedOutcode, string expectedIncode)
        {
            // Arrange
            const PostcodeParseOptions options = PostcodeParseOptions.IncodeOptional ^
                                                 PostcodeParseOptions.MatchBfpo ^
                                                 PostcodeParseOptions.MatchGirobank ^
                                                 PostcodeParseOptions.MatchOverseasTerritories ^
                                                 PostcodeParseOptions.MatchSanta;

            // Act
            Postcode output = Postcode.Parse(input, options);

            // Assert
            Assert.That(output.InCode, Is.EqualTo(expectedIncode));
            Assert.That(output.OutCode, Is.EqualTo(expectedOutcode));
        }
Exemple #8
0
        private static bool TryParseHardcoded(string sanitizedInput, PostcodeParseOptions options, ref Postcode result, string outer, string inner)
        {
            if (sanitizedInput == outer + inner)
            {
                result.OutCode = outer;
                result.InCode  = inner;
                return(true);
            }

            if ((sanitizedInput == outer) && ((options & PostcodeParseOptions.IncodeOptional) != PostcodeParseOptions.None))
            {
                result.OutCode = outer;
                return(true);
            }

            return(false);
        }
Exemple #9
0
        /// <summary>
        /// Attempts to parse a string as a Postcode.
        /// </summary>
        /// <param name="value">String to be parsed</param>
        /// <param name="result">Postcode object</param>
        /// <param name="options"></param>
        /// <returns>
        /// Boolean indicating whether the string was successfully parsed as a UK Postcode
        /// </returns>
        public static bool TryParse(string value, out Postcode result, PostcodeParseOptions options)
        {
            // Set output to new Postcode
            result = new Postcode();

            // Guard clause - check for null or whitespace
            if (string.IsNullOrWhiteSpace(value))
            {
                return(false);
            }

            // uppercase input and strip undesirable characters
            value = Regex.Replace(value.ToUpperInvariant(), "[^A-Z0-9]", string.Empty, RegexOptions.Compiled);

            // Work through different options in turn until we have a match.
            return(TryParseBs7666(value, options, ref result) ||
                   TryParseBfpo(value, options, ref result) ||
                   TryParseOverseasTerritories(value, options, ref result) ||
                   TryParseGiroBank(value, options, ref result) ||
                   TryParseSanta(value, options, ref result));
        }
Exemple #10
0
        private static bool TryParseRegex(string sanitizedInput, PostcodeParseOptions options, ref Postcode result, string fullMatchPattern, string outerMatchPattern)
        {
            Match fullMatch = Regex.Match(sanitizedInput, fullMatchPattern, RegexOptions.Compiled);

            if (fullMatch.Success)
            {
                result.OutCode = fullMatch.Groups["outCode"].Value;
                result.InCode  = fullMatch.Groups["inCode"].Value;
                return(true);
            }

            if ((options & PostcodeParseOptions.IncodeOptional) != PostcodeParseOptions.None)
            {
                Match outerMatch = Regex.Match(sanitizedInput, outerMatchPattern, RegexOptions.Compiled);
                if (outerMatch.Success)
                {
                    result.OutCode = outerMatch.Groups["outCode"].Value;
                    return(true);
                }
            }
            return(false);
        }
Exemple #11
0
        private static bool TryParseOverseasTerritories(string sanitizedInput, PostcodeParseOptions options,
                                                        ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchOverseasTerritories) == PostcodeParseOptions.None)
            {
                return(false);
            }

            // Loop through overseas territories
            for (var i = 0; i < OverseasTerritories.GetLength(0); i++)
            {
                var match = TryParseHardcoded(sanitizedInput, options, ref result, OverseasTerritories[i, 0],
                                              OverseasTerritories[i, 1]);

                if (match)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #12
0
        /// <summary>
        /// Parses a string as a Postcode.
        /// </summary>
        /// <param name="value">String to be parsed</param>
        /// <param name="options"></param>
        /// <returns>Postcode object</returns>
        /// <exception cref="FormatException">
        /// If the passed string cannot be parsed as a UK postcode
        /// </exception>
        public static Postcode Parse(string value, PostcodeParseOptions options)
        {
            Postcode p;
            if (TryParse(value, out p, options))
                return p;

            throw new FormatException();
        }
Exemple #13
0
        private static bool TryParseSanta(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchSanta) == PostcodeParseOptions.None) return false;

            return TryParseHardcoded(sanitizedInput, options, ref result, "SAN", "TA1");
        }
Exemple #14
0
        private static bool TryParseRegex(string sanitizedInput, PostcodeParseOptions options, ref Postcode result, string fullMatchPattern, string outerMatchPattern)
        {
            Match fullMatch = Regex.Match(sanitizedInput, fullMatchPattern, RegexOptions.Compiled);
            if (fullMatch.Success)
            {
                result.OutCode = fullMatch.Groups["outCode"].Value;
                result.InCode = fullMatch.Groups["inCode"].Value;
                return true;
            }

            if ((options & PostcodeParseOptions.IncodeOptional) != PostcodeParseOptions.None)
            {
                Match outerMatch = Regex.Match(sanitizedInput, outerMatchPattern, RegexOptions.Compiled);
                if (outerMatch.Success)
                {
                    result.OutCode = outerMatch.Groups["outCode"].Value;
                    return true;
                }
            }
            return false;
        }
Exemple #15
0
        private static bool TryParseOverseasTerritories(string sanitizedInput, PostcodeParseOptions options,
                                                        ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchOverseasTerritories) == PostcodeParseOptions.None) return false;

            // Loop through overseas territories
            for (var i = 0; i < OverseasTerritories.GetLength(0); i++)
            {
                var match = TryParseHardcoded(sanitizedInput, options, ref result, OverseasTerritories[i, 0],
                                              OverseasTerritories[i, 1]);

                if (match) return true;
            }

            return false;
        }
Exemple #16
0
        private static bool TryParseHardcoded(string sanitizedInput, PostcodeParseOptions options, ref Postcode result, string outer, string inner)
        {
            if (sanitizedInput == outer + inner)
            {
                result.OutCode = outer;
                result.InCode = inner;
                return true;
            }

            if ((sanitizedInput == outer) && ((options & PostcodeParseOptions.IncodeOptional) != PostcodeParseOptions.None))
            {
                result.OutCode = outer;
                return true;
            }

            return false;
        }
Exemple #17
0
        private static bool TryParseGiroBank(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchGirobank) == PostcodeParseOptions.None) return false;

            return TryParseHardcoded(sanitizedInput, options, ref result, "GIR", "0AA");
        }
Exemple #18
0
 private static bool TryParseBs7666(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
 {
     return TryParseRegex(sanitizedInput, options, ref result, RegexBs7666Full, RegexBs7666OuterStandAlone);
 }
Exemple #19
0
        private static bool TryParseBfpo(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
        {
            if ((options & PostcodeParseOptions.MatchBfpo) == PostcodeParseOptions.None) return false;

            return TryParseRegex(sanitizedInput, options, ref result, RegexBfpoFull, RegexBfpoOuterStandalone);
        }
Exemple #20
0
        /// <summary>
        /// Attempts to parse a string as a Postcode.
        /// </summary>
        /// <param name="value">String to be parsed</param>
        /// <param name="result">Postcode object</param>
        /// <param name="options"></param>
        /// <returns>
        /// Boolean indicating whether the string was successfully parsed as a UK Postcode
        /// </returns>
        public static bool TryParse(string value, out Postcode result, PostcodeParseOptions options)
        {
            // Set output to new Postcode
            result = new Postcode();

            // Guard clause - check for null or whitespace
            if (string.IsNullOrWhiteSpace(value)) return false;

            // uppercase input and strip undesirable characters
            value = Regex.Replace(value.ToUpperInvariant(), "[^A-Z0-9]", string.Empty, RegexOptions.Compiled);

            // Work through different options in turn until we have a match.
            return (TryParseBs7666(value, options, ref result) ||
                    TryParseBfpo(value, options, ref result) ||
                    TryParseOverseasTerritories(value, options, ref result) ||
                    TryParseGiroBank(value, options, ref result) ||
                    TryParseSanta(value, options, ref result));
        }
        public void ToString_ReturnsStringRepresentationOfPostcode(string input, PostcodeParseOptions options,
                                                                   string expectedToString)
        {
            // Arrange
            Postcode postcode = Postcode.Parse(input, options);

            // Act
            string actualToString = postcode.ToString();

            // Assert
            Assert.That(actualToString, Is.EqualTo(expectedToString));
        }
 public void Parse_ValidPostcodeAtStartOfLongerString_IsInvalid(string invalidPostcode,
                                                                PostcodeParseOptions options)
 {
     // Act
     Assert.Throws<FormatException>(() => Postcode.Parse(invalidPostcode, options),
                                    string.Format("No exception thrown when parsing {0}", invalidPostcode));
 }
Exemple #23
0
 private static bool TryParseBs7666(string sanitizedInput, PostcodeParseOptions options, ref Postcode result)
 {
     return(TryParseRegex(sanitizedInput, options, ref result, RegexBs7666Full, RegexBs7666OuterStandAlone));
 }