From 5dbc9f4e5d2c8e39b85a05dcf90b28507bac1f7e Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Thu, 24 Dec 2020 10:53:29 +0000 Subject: [PATCH] Normalize line endings --- .editorconfig | 53 +-- .gitignore | 3 + PhoneNumbers.sln | 2 +- build.sh | 0 nuget.config | 2 +- src/PhoneNumbers/CountryInfo.cs | 302 +++++++++--------- .../DataFiles/uk_geographic_area_codes.csv | 2 +- .../DataFiles/uk_mobile_area_codes.csv | 2 +- .../uk_non_geographic_area_codes.csv | 2 +- .../Formatters/PhoneNumberFormatter.cs | 2 +- .../Formatters/UkPhoneNumberFormatter.cs | 56 ++-- src/PhoneNumbers/GeographicPhoneNumber.cs | 2 +- src/PhoneNumbers/MobilePhoneNumber.cs | 2 +- src/PhoneNumbers/NonGeographicPhoneNumber.cs | 2 +- src/PhoneNumbers/Parsers/PhoneNumberParser.cs | 98 +++--- .../Parsers/UkPhoneNumberParser.cs | 196 ++++++------ src/PhoneNumbers/PhoneNumber.cs | 6 +- src/PhoneNumbers/PhoneNumberKind.cs | 42 +-- src/PhoneNumbers/PhoneNumbers.csproj | 125 ++++---- src/PhoneNumbers/Properties/AssemblyInfo.cs | 2 +- src/PhoneNumbers/ResourceUtility.cs | 10 +- test/PhoneNumbers.Tests/CountryInfoTests.cs | 2 +- test/PhoneNumbers.Tests/CountryInfoUKTests.cs | 4 +- .../Formatters/PhoneNumberFormatterTests.cs | 2 +- .../Formatters/UkPhoneNumberFormatterTests.cs | 2 +- .../GeographicPhoneNumberTests.cs | 4 +- .../MobilePhoneNumberTests.cs | 4 +- .../NonGeographicPhoneNumberTests.cs | 4 +- .../Parsers/PhoneNumberParserTests.cs | 56 ++-- ...NumberParserTests_GeographicPhoneNumber.cs | 4 +- ...honeNumberParserTests_MobilePhoneNumber.cs | 10 +- ...berParserTests_NonGeographicPhoneNumber.cs | 4 +- .../Parsers/UkPhoneNumberParserTests.cs | 208 ++++++------ test/PhoneNumbers.Tests/PhoneNumberTests.cs | 72 ++--- .../PhoneNumbers.Tests.csproj | 1 - 35 files changed, 652 insertions(+), 636 deletions(-) mode change 100755 => 100644 build.sh diff --git a/.editorconfig b/.editorconfig index 23713591..e7131d02 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,6 @@ -# editorconfig.org +# EditorConfig to support per-solution formatting. +# Use the EditorConfig VS add-in to make this work. +# http://editorconfig.org/ # top-most EditorConfig file root = true @@ -11,10 +13,11 @@ insert_final_newline = true indent_style = space indent_size = 4 trim_trailing_whitespace = true +charset = utf-8 -# JSON files -[*.json] -indent_size = 2 +# Generated code +[*{_AssemblyInfo.cs,.notsupported.cs}] +generated_code = true # C# files [*.cs] @@ -45,9 +48,9 @@ dotnet_style_qualification_for_method = false:suggestion dotnet_style_qualification_for_event = false:suggestion # Types: use keywords instead of BCL types, and permit var only when the type is clear -csharp_style_var_for_built_in_types = false:suggestion -csharp_style_var_when_type_is_apparent = false:none -csharp_style_var_elsewhere = false:suggestion +csharp_style_var_for_built_in_types = true:suggestion +csharp_style_var_when_type_is_apparent = true:suggestion +csharp_style_var_elsewhere = true:suggestion dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion dotnet_style_predefined_type_for_member_access = true:suggestion @@ -160,6 +163,15 @@ dotnet_code_quality.CA1802.api_surface = private, internal # CA1303: Do not pass literals as localized parameters dotnet_diagnostic.CA1303.severity = none +# CA1304: Specify CultureInfo +dotnet_diagnostic.CA1304.severity = error + +# CA1305: Specify IFormatProvider +dotnet_diagnostic.CA1305.severity = error + +# CA1310: Specify StringComparison for correctness +dotnet_diagnostic.CA1310.severity = error + # SA1101: Prefix local calls with this dotnet_diagnostic.SA1101.severity = none @@ -172,27 +184,30 @@ dotnet_diagnostic.SA1309.severity = none # SA1311: Static readonly fields should begin with upper-case letter dotnet_diagnostic.SA1311.severity = none -# C++ Files -[*.{cpp,h,in}] -curly_bracket_next_line = true -indent_brace_style = Allman +# Xml files +[*.{xml,stylecop,resx,ruleset}] +indent_size = 2 -# Xml project files -[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}] +# Xml config files +[*.{props,targets,config,nuspec}] indent_size = 2 -# Xml build files -[*.builds] +# Xml project files +[*.csproj] indent_size = 2 -# Xml files -[*.{xml,stylecop,resx,ruleset}] +# YAML config files +[*.{yml,yaml}] indent_size = 2 -# Xml config files -[*.{props,targets,config,nuspec}] +# JSON files +[*.json] indent_size = 2 +# PowerShell scripts +[*.{ps1,psm1}] +end_of_line = crlf + # Shell scripts [*.sh] end_of_line = lf diff --git a/.gitignore b/.gitignore index d8186856..adca565d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,9 @@ *.userosscache *.sln.docstates +# macOS +*.DS_Store + # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/PhoneNumbers.sln b/PhoneNumbers.sln index ac30d515..03053f91 100644 --- a/PhoneNumbers.sln +++ b/PhoneNumbers.sln @@ -1,4 +1,4 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30711.63 diff --git a/build.sh b/build.sh old mode 100755 new mode 100644 diff --git a/nuget.config b/nuget.config index 35696f81..95e879ef 100644 --- a/nuget.config +++ b/nuget.config @@ -1,4 +1,4 @@ - + diff --git a/src/PhoneNumbers/CountryInfo.cs b/src/PhoneNumbers/CountryInfo.cs index 66632a4b..598338e4 100644 --- a/src/PhoneNumbers/CountryInfo.cs +++ b/src/PhoneNumbers/CountryInfo.cs @@ -1,66 +1,66 @@ -using System; -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; -using PhoneNumbers.Formatters; -using PhoneNumbers.Parsers; - -namespace PhoneNumbers -{ - /// - /// A class which contains country information related to phone numbers. - /// - public sealed class CountryInfo - { - /// - /// Initialises a new instance of the class. - /// - /// The ISO 3166 Aplha-2 code for the country. - /// The country calling code. - /// The international call prefix. - /// The trunk prefix. - /// The . - /// The . - /// The permitted lengths for the national significant number. - private CountryInfo( - string countryCode, - string callingCode, - string internationalCallPrefix, - string trunkPrefix, - PhoneNumberFormatter formatter, - PhoneNumberParser parser, - int[] nsnLengths) => - (CountryCode, CallingCode, Formatter, InternationalCallPrefix, Parser, TrunkPrefix, NsnLengths) = - (countryCode, callingCode, formatter, internationalCallPrefix, parser, trunkPrefix, new ReadOnlyCollection(nsnLengths)); - - /// - /// Gets the for the United Kingdom. - /// - /// Covers England, Scotland, Wales and Northern Ireland. - public static CountryInfo UK { get; } = new CountryInfo("GB", "+44", "00", "0", new UKPhoneNumberFormatter(), new UKPhoneNumberParser(), new[] { 7, 9, 10 }); - - /// - /// Gets the country calling code. - /// +using PhoneNumbers.Formatters; +using PhoneNumbers.Parsers; + +namespace PhoneNumbers +{ + /// + /// A class which contains country information related to phone numbers. + /// + public sealed class CountryInfo + { + /// + /// Initialises a new instance of the class. + /// + /// The ISO 3166 Aplha-2 code for the country. + /// The country calling code. + /// The international call prefix. + /// The trunk prefix. + /// The . + /// The . + /// The permitted lengths for the national significant number. + private CountryInfo( + string countryCode, + string callingCode, + string internationalCallPrefix, + string trunkPrefix, + PhoneNumberFormatter formatter, + PhoneNumberParser parser, + int[] nsnLengths) => + (CountryCode, CallingCode, Formatter, InternationalCallPrefix, Parser, TrunkPrefix, NsnLengths) = + (countryCode, callingCode, formatter, internationalCallPrefix, parser, trunkPrefix, new ReadOnlyCollection(nsnLengths)); + + /// + /// Gets the for the United Kingdom. + /// + /// Covers England, Scotland, Wales and Northern Ireland. + public static CountryInfo UK { get; } = new CountryInfo("GB", "+44", "00", "0", new UKPhoneNumberFormatter(), new UKPhoneNumberParser(), new[] { 7, 9, 10 }); + + /// + /// Gets the country calling code. + /// public string CallingCode { get; } - /// - /// Gets the ISO 3166 Aplha-2 code for the country. - /// + /// + /// Gets the ISO 3166 Aplha-2 code for the country. + /// public string CountryCode { get; } - /// - /// Gets the international call prefix. - /// + /// + /// Gets the international call prefix. + /// public string InternationalCallPrefix { get; } - /// - /// Gets the trunk prefix. - /// + /// + /// Gets the trunk prefix. + /// public string TrunkPrefix { get; } - /// - /// Gets the for the country. - /// + /// + /// Gets the for the country. + /// internal PhoneNumberFormatter Formatter { get; } /// @@ -68,52 +68,52 @@ private CountryInfo( /// internal ReadOnlyCollection NsnLengths { get; } - /// - /// Gets the for the country. - /// - internal PhoneNumberParser Parser { get; } - - /// - /// Gets all supported s as an enumerable. - /// - /// All supported s as an enumerable. - internal static IEnumerable AllSupported() - { - yield return UK; - } - - /// - /// Finds the based upon the ISO 3166 Aplha-2 code for the country. - /// - /// The ISO 3166 Aplha-2 code for the country. - /// - internal static CountryInfo Find(string countryCode) => - countryCode switch - { - "GB" => UK, - _ => throw new NotSupportedException(countryCode), - }; - + /// + /// Gets the for the country. + /// + internal PhoneNumberParser Parser { get; } + + /// + /// Gets all supported s as an enumerable. + /// + /// All supported s as an enumerable. + internal static IEnumerable AllSupported() + { + yield return UK; + } + + /// + /// Finds the based upon the ISO 3166 Aplha-2 code for the country. + /// + /// The ISO 3166 Aplha-2 code for the country. + /// + internal static CountryInfo Find(string countryCode) => + countryCode switch + { + "GB" => UK, + _ => throw new NotSupportedException(countryCode), + }; + /// /// Gets the national significant number (NSN) from the specified phone number value. - /// + /// /// A string containing a phone number. - /// The national significant number (NSN). - /// This excludes the CallingCode and TrunkPrefix. - internal string GetNationalSignificantNumber(string value) - { - if (string.IsNullOrWhiteSpace(value)) - { - throw new ArgumentException("The value must not be blank", nameof(value)); - } - - int startPos = IsInternationalNumber(value) - ? CallingCode.Length - : value.IndexOf(TrunkPrefix, StringComparison.Ordinal) + 1; - - int digits = 0; - - for (int i = startPos; i < value.Length; i++) + /// The national significant number (NSN). + /// This excludes the CallingCode and TrunkPrefix. + internal string GetNationalSignificantNumber(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("The value must not be blank", nameof(value)); + } + + var startPos = IsInternationalNumber(value) + ? CallingCode.Length + : value.IndexOf(TrunkPrefix, StringComparison.Ordinal) + 1; + + var digits = 0; + + for (var i = startPos; i < value.Length; i++) { if (IsDigit(value[i])) { @@ -126,12 +126,12 @@ internal string GetNationalSignificantNumber(string value) return value.Substring(startPos); } - char[] chars = new char[digits]; - int charPos = 0; + var chars = new char[digits]; + var charPos = 0; - for (int i = startPos; i < value.Length; i++) + for (var i = startPos; i < value.Length; i++) { - char charVal = value[i]; + var charVal = value[i]; if (IsDigit(charVal)) { @@ -139,53 +139,53 @@ internal string GetNationalSignificantNumber(string value) } } - return new string(chars); - } - - internal bool IsInternationalNumber(string value) => - value?.StartsWith(CallingCode, StringComparison.Ordinal) == true; - - internal bool IsNumber(string value) - { - if (string.IsNullOrWhiteSpace(value)) - { - return false; - } - - if (value.StartsWith(TrunkPrefix, StringComparison.Ordinal)) - { - return true; - } - - if (value.StartsWith(CallingCode, StringComparison.Ordinal)) - { - return true; - } - - int firstDigit = 0; - - for (int i = 0; i < value.Length; i++) - { - if (IsDigit(value[i])) - { - firstDigit = i; - break; - } - } - - for (int i = 0; i < TrunkPrefix.Length; i++) - { - if (value[firstDigit++] != TrunkPrefix[i]) - { - return false; - } - } - - return true; - } - - /// Char.IsDigit returns true for more than 0-9 so use a more restricted version. - private static bool IsDigit(char charVal) => - charVal >= '0' && charVal <= '9'; - } -} + return new string(chars); + } + + internal bool IsInternationalNumber(string value) => + value?.StartsWith(CallingCode, StringComparison.Ordinal) == true; + + internal bool IsNumber(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + if (value.StartsWith(TrunkPrefix, StringComparison.Ordinal)) + { + return true; + } + + if (value.StartsWith(CallingCode, StringComparison.Ordinal)) + { + return true; + } + + var firstDigit = 0; + + for (var i = 0; i < value.Length; i++) + { + if (IsDigit(value[i])) + { + firstDigit = i; + break; + } + } + + for (var i = 0; i < TrunkPrefix.Length; i++) + { + if (value[firstDigit++] != TrunkPrefix[i]) + { + return false; + } + } + + return true; + } + + /// Char.IsDigit returns true for more than 0-9 so use a more restricted version. + private static bool IsDigit(char charVal) => + charVal >= '0' && charVal <= '9'; + } +} diff --git a/src/PhoneNumbers/DataFiles/uk_geographic_area_codes.csv b/src/PhoneNumbers/DataFiles/uk_geographic_area_codes.csv index 6ea97776..b60baf19 100644 --- a/src/PhoneNumbers/DataFiles/uk_geographic_area_codes.csv +++ b/src/PhoneNumbers/DataFiles/uk_geographic_area_codes.csv @@ -1,4 +1,4 @@ -113|Leeds +113|Leeds 114|Sheffield 115|Nottingham 116|Leicester diff --git a/src/PhoneNumbers/DataFiles/uk_mobile_area_codes.csv b/src/PhoneNumbers/DataFiles/uk_mobile_area_codes.csv index 857cc66f..362251e9 100644 --- a/src/PhoneNumbers/DataFiles/uk_mobile_area_codes.csv +++ b/src/PhoneNumbers/DataFiles/uk_mobile_area_codes.csv @@ -1,4 +1,4 @@ -7106 +7106 7107 7300 7301 diff --git a/src/PhoneNumbers/DataFiles/uk_non_geographic_area_codes.csv b/src/PhoneNumbers/DataFiles/uk_non_geographic_area_codes.csv index 604182ae..26cae157 100644 --- a/src/PhoneNumbers/DataFiles/uk_non_geographic_area_codes.csv +++ b/src/PhoneNumbers/DataFiles/uk_non_geographic_area_codes.csv @@ -1,4 +1,4 @@ -300 +300 302 303 306 diff --git a/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs b/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs index 62390a3c..bc5b922b 100644 --- a/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs +++ b/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace PhoneNumbers.Formatters { diff --git a/src/PhoneNumbers/Formatters/UkPhoneNumberFormatter.cs b/src/PhoneNumbers/Formatters/UkPhoneNumberFormatter.cs index 35237d09..2404d39b 100644 --- a/src/PhoneNumbers/Formatters/UkPhoneNumberFormatter.cs +++ b/src/PhoneNumbers/Formatters/UkPhoneNumberFormatter.cs @@ -1,31 +1,31 @@ -namespace PhoneNumbers.Formatters -{ - /// - /// A for UK phone numbers. - /// - /// See https://www.area-codes.org.uk/formatting.php for the rules this class implements. - internal sealed class UKPhoneNumberFormatter : PhoneNumberFormatter - { - /// - protected override string FormatDisplay(PhoneNumber phoneNumber) - { - if (phoneNumber.AreaCode.Length == 2) - { - return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber.Substring(0, 4)} {phoneNumber.LocalNumber.Substring(4)}"; - } - - if (phoneNumber.AreaCode.Length == 3) - { - return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber.Substring(0, 3)} {phoneNumber.LocalNumber.Substring(3)}"; +namespace PhoneNumbers.Formatters +{ + /// + /// A for UK phone numbers. + /// + /// See https://www.area-codes.org.uk/formatting.php for the rules this class implements. + internal sealed class UKPhoneNumberFormatter : PhoneNumberFormatter + { + /// + protected override string FormatDisplay(PhoneNumber phoneNumber) + { + if (phoneNumber.AreaCode.Length == 2) + { + return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber.Substring(0, 4)} {phoneNumber.LocalNumber.Substring(4)}"; + } + + if (phoneNumber.AreaCode.Length == 3) + { + return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber.Substring(0, 3)} {phoneNumber.LocalNumber.Substring(3)}"; } // Geographic and Mobiles can both have a 4 digit area code, however the preference is to only split the local number for geographical - if (phoneNumber.AreaCode.Length == 4 && phoneNumber.PhoneNumberKind == PhoneNumberKind.GeographicPhoneNumber) - { - return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber.Substring(0, 3)} {phoneNumber.LocalNumber.Substring(3)}"; - } - - return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber}"; - } - } -} + if (phoneNumber.AreaCode.Length == 4 && phoneNumber.PhoneNumberKind == PhoneNumberKind.GeographicPhoneNumber) + { + return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber.Substring(0, 3)} {phoneNumber.LocalNumber.Substring(3)}"; + } + + return $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.AreaCode} {phoneNumber.LocalNumber}"; + } + } +} diff --git a/src/PhoneNumbers/GeographicPhoneNumber.cs b/src/PhoneNumbers/GeographicPhoneNumber.cs index 4e1715cd..2e5cfef0 100644 --- a/src/PhoneNumbers/GeographicPhoneNumber.cs +++ b/src/PhoneNumbers/GeographicPhoneNumber.cs @@ -1,4 +1,4 @@ -namespace PhoneNumbers +namespace PhoneNumbers { /// /// An implementation of which represents a geographicly assigned phone number. diff --git a/src/PhoneNumbers/MobilePhoneNumber.cs b/src/PhoneNumbers/MobilePhoneNumber.cs index 18c67bb5..ccac23c4 100644 --- a/src/PhoneNumbers/MobilePhoneNumber.cs +++ b/src/PhoneNumbers/MobilePhoneNumber.cs @@ -1,4 +1,4 @@ -namespace PhoneNumbers +namespace PhoneNumbers { /// /// An implementation of which represents a mobile phone number. diff --git a/src/PhoneNumbers/NonGeographicPhoneNumber.cs b/src/PhoneNumbers/NonGeographicPhoneNumber.cs index b2ea5c4f..f933aa8b 100644 --- a/src/PhoneNumbers/NonGeographicPhoneNumber.cs +++ b/src/PhoneNumbers/NonGeographicPhoneNumber.cs @@ -1,4 +1,4 @@ -namespace PhoneNumbers +namespace PhoneNumbers { /// /// An implementation of which represents a phone number which is not assigned to a geographic area. diff --git a/src/PhoneNumbers/Parsers/PhoneNumberParser.cs b/src/PhoneNumbers/Parsers/PhoneNumberParser.cs index f3c5f082..3c3a4355 100644 --- a/src/PhoneNumbers/Parsers/PhoneNumberParser.cs +++ b/src/PhoneNumbers/Parsers/PhoneNumberParser.cs @@ -1,52 +1,52 @@ -using System; - -namespace PhoneNumbers.Parsers -{ - /// - /// The base class for a class which parses a string containing a phone number into a instance. - /// - public abstract class PhoneNumberParser - { - /// - /// Parses the phone number represented in the specified string into a instance. - /// - /// A string containing a phone number. - /// The of the country for the phone number. - /// A instance representing the specified string. - public PhoneNumber Parse(string value, CountryInfo countryInfo) - { - if (string.IsNullOrWhiteSpace(value)) - { - throw new ArgumentException("A value must be specified.", nameof(value)); - } - - if (countryInfo is null) - { - throw new ArgumentNullException(nameof(countryInfo)); - } - - if (!countryInfo.IsNumber(value)) - { - throw new ArgumentException($"The value must be a {countryInfo.CountryCode} phone number starting {countryInfo.TrunkPrefix} or {countryInfo.CallingCode}."); - } - - string nsnValue = countryInfo.GetNationalSignificantNumber(value); - +using System; + +namespace PhoneNumbers.Parsers +{ + /// + /// The base class for a class which parses a string containing a phone number into a instance. + /// + public abstract class PhoneNumberParser + { + /// + /// Parses the phone number represented in the specified string into a instance. + /// + /// A string containing a phone number. + /// The of the country for the phone number. + /// A instance representing the specified string. + public PhoneNumber Parse(string value, CountryInfo countryInfo) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("A value must be specified.", nameof(value)); + } + + if (countryInfo is null) + { + throw new ArgumentNullException(nameof(countryInfo)); + } + + if (!countryInfo.IsNumber(value)) + { + throw new ArgumentException($"The value must be a {countryInfo.CountryCode} phone number starting {countryInfo.TrunkPrefix} or {countryInfo.CallingCode}."); + } + + var nsnValue = countryInfo.GetNationalSignificantNumber(value); + if (!countryInfo.NsnLengths.Contains(nsnValue.Length)) { throw new ArgumentException($"The national significant number of the phone number must be {string.Join(",", countryInfo.NsnLengths)} in length."); - } - - return ParseNationalSignificantNumber(nsnValue, countryInfo); - } - - /// - /// Parses the phone number represented in the specified string into a instance. - /// - /// A string containing the national significant number. - /// - /// A instance representing the specified string. - /// By the time this method is called, nsnValue will have been validated against the .NsnLengths and contain digits only. - protected abstract PhoneNumber ParseNationalSignificantNumber(string nsnValue, CountryInfo countryInfo); - } -} + } + + return ParseNationalSignificantNumber(nsnValue, countryInfo); + } + + /// + /// Parses the phone number represented in the specified string into a instance. + /// + /// A string containing the national significant number. + /// + /// A instance representing the specified string. + /// By the time this method is called, nsnValue will have been validated against the .NsnLengths and contain digits only. + protected abstract PhoneNumber ParseNationalSignificantNumber(string nsnValue, CountryInfo countryInfo); + } +} diff --git a/src/PhoneNumbers/Parsers/UkPhoneNumberParser.cs b/src/PhoneNumbers/Parsers/UkPhoneNumberParser.cs index 384a697c..28735481 100644 --- a/src/PhoneNumbers/Parsers/UkPhoneNumberParser.cs +++ b/src/PhoneNumbers/Parsers/UkPhoneNumberParser.cs @@ -1,21 +1,21 @@ -using System; -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; -namespace PhoneNumbers.Parsers -{ - /// - /// A for UK phone numbers. - /// - /// - /// https://en.m.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom - /// https://en.wikipedia.org/wiki/List_of_dialling_codes_in_the_United_Kingdom - /// https://www.area-codes.org.uk/full-uk-area-code-list.php - /// - internal sealed class UKPhoneNumberParser : PhoneNumberParser - { - private static readonly IReadOnlyDictionary s_geographicAreas = - ResourceUtility.ReadLines("uk_geographic_area_codes.csv").Select(x => x.Split('|')).ToDictionary(x => x[0], x => x[1]); +namespace PhoneNumbers.Parsers +{ + /// + /// A for UK phone numbers. + /// + /// + /// https://en.m.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom + /// https://en.wikipedia.org/wiki/List_of_dialling_codes_in_the_United_Kingdom + /// https://www.area-codes.org.uk/full-uk-area-code-list.php + /// + internal sealed class UKPhoneNumberParser : PhoneNumberParser + { + private static readonly IReadOnlyDictionary s_geographicAreas = + ResourceUtility.ReadLines("uk_geographic_area_codes.csv").Select(x => x.Split('|')).ToDictionary(x => x[0], x => x[1]); private static readonly HashSet s_mobileAreaCodes = new HashSet(ResourceUtility.ReadLines("uk_mobile_area_codes.csv")); @@ -23,106 +23,106 @@ internal sealed class UKPhoneNumberParser : PhoneNumberParser private static readonly HashSet s_nonGeographicAreaCodes = new HashSet(ResourceUtility.ReadLines("uk_non_geographic_area_codes.csv")); - /// - /// By the time this method is called, nsnValue will have been validated against the .NsnLengths and contain digits only. - protected override PhoneNumber ParseNationalSignificantNumber(string nsnValue, CountryInfo countryInfo) - { - switch (nsnValue[0]) - { - case '1': - case '2': + /// + /// By the time this method is called, nsnValue will have been validated against the .NsnLengths and contain digits only. + protected override PhoneNumber ParseNationalSignificantNumber(string nsnValue, CountryInfo countryInfo) + { + switch (nsnValue[0]) + { + case '1': + case '2': return ParseGeographicPhoneNumber(nsnValue, countryInfo); - case '3': - case '8': - return ParseNonGeographicPhoneNumber(nsnValue, countryInfo); - - case '7': - return ParseMobilePhoneNumber(nsnValue, countryInfo); - - default: - throw new NotSupportedException(nsnValue); - } - } - - private static PhoneNumber ParseGeographicPhoneNumber(string nsnValue, CountryInfo countryInfo) - { - int areaCodeLength = 4; - - // 11X or 1X1 - if (nsnValue[0] == '1' && (nsnValue[1] == '1' || nsnValue[2] == '1')) - { - areaCodeLength = 3; - } - else if (nsnValue[0] == '2') // 2X - { - areaCodeLength = 2; - } - - string areaCode = nsnValue.Substring(0, areaCodeLength); - string localNumber = nsnValue.Substring(areaCodeLength); - - if (areaCode.Length == 2 && localNumber.Length != 8 - || areaCode.Length == 3 && localNumber.Length != 7) - { - throw new ArgumentException($"The for the area code {areaCode}, the local number must be {10 - areaCode.Length} digits."); + case '3': + case '8': + return ParseNonGeographicPhoneNumber(nsnValue, countryInfo); + + case '7': + return ParseMobilePhoneNumber(nsnValue, countryInfo); + + default: + throw new NotSupportedException(nsnValue); } + } - if (!s_geographicAreas.TryGetValue(areaCode, out string? geographicArea)) + private static PhoneNumber ParseGeographicPhoneNumber(string nsnValue, CountryInfo countryInfo) + { + var areaCodeLength = 4; + + // 11X or 1X1 + if (nsnValue[0] == '1' && (nsnValue[1] == '1' || nsnValue[2] == '1')) + { + areaCodeLength = 3; + } + else if (nsnValue[0] == '2') // 2X { - throw new ArgumentException($"The area code {areaCode} is invalid."); - } - - return new GeographicPhoneNumber(countryInfo, areaCode, localNumber, geographicArea); + areaCodeLength = 2; + } + + var areaCode = nsnValue.Substring(0, areaCodeLength); + var localNumber = nsnValue.Substring(areaCodeLength); + + if (areaCode.Length == 2 && localNumber.Length != 8 + || areaCode.Length == 3 && localNumber.Length != 7) + { + throw new ArgumentException($"The for the area code {areaCode}, the local number must be {10 - areaCode.Length} digits."); + } + + if (!s_geographicAreas.TryGetValue(areaCode, out var geographicArea)) + { + throw new ArgumentException($"The area code {areaCode} is invalid."); + } + + return new GeographicPhoneNumber(countryInfo, areaCode, localNumber, geographicArea); } - private static PhoneNumber ParseMobilePhoneNumber(string nsnValue, CountryInfo countryInfo) - { - // All UK mobile numbers have a 10 digit NSN. - if (nsnValue.Length != 10) - { - throw new ArgumentException("For a UK mobile phone, the national significant number of the phone number must be 10 digits."); - } - - string areaCode = nsnValue.Substring(0, 4); - string localNumber = nsnValue.Substring(4); - - // 70XX are personal numbers but won't be in the phone area codes list. - if (!s_mobileAreaCodes.Contains(areaCode) && areaCode[1] != '0') - { - throw new ArgumentException($"The area code {areaCode} is invalid."); + private static PhoneNumber ParseMobilePhoneNumber(string nsnValue, CountryInfo countryInfo) + { + // All UK mobile numbers have a 10 digit NSN. + if (nsnValue.Length != 10) + { + throw new ArgumentException("For a UK mobile phone, the national significant number of the phone number must be 10 digits."); + } + + var areaCode = nsnValue.Substring(0, 4); + var localNumber = nsnValue.Substring(4); + + // 70XX are personal numbers but won't be in the phone area codes list. + if (!s_mobileAreaCodes.Contains(areaCode) && areaCode[1] != '0') + { + throw new ArgumentException($"The area code {areaCode} is invalid."); } // UK Wifi (data only plans) start 7911 2xx or 8xx - bool isDataOnly = areaCode == "7911" && (localNumber[0] == '2' || localNumber[0] == '8'); - - // UK pagers start 76 (except 7624 which is used by mobile phones in Isle of Man) - bool isPager = areaCode[1] == '6' && areaCode != "7624"; - - // UK virtual (personal numbers) start 70 - bool isVirtual = areaCode[1] == '0'; - - return new MobilePhoneNumber(countryInfo, areaCode, localNumber, isDataOnly, isPager, isVirtual); + var isDataOnly = areaCode == "7911" && (localNumber[0] == '2' || localNumber[0] == '8'); + + // UK pagers start 76 (except 7624 which is used by mobile phones in Isle of Man) + var isPager = areaCode[1] == '6' && areaCode != "7624"; + + // UK virtual (personal numbers) start 70 + var isVirtual = areaCode[1] == '0'; + + return new MobilePhoneNumber(countryInfo, areaCode, localNumber, isDataOnly, isPager, isVirtual); } private static PhoneNumber ParseNonGeographicPhoneNumber(string nsnValue, CountryInfo countryInfo) - { - // All UK non geographic numbers have a 10 digit NSN. - if (nsnValue.Length != 10) - { - throw new ArgumentException("For a UK non-geographic number, the national significant number of the phone number must be 10 digits."); + { + // All UK non geographic numbers have a 10 digit NSN. + if (nsnValue.Length != 10) + { + throw new ArgumentException("For a UK non-geographic number, the national significant number of the phone number must be 10 digits."); } - string areaCode = nsnValue.Substring(0, 3); + var areaCode = nsnValue.Substring(0, 3); if (!s_nonGeographicAreaCodes.Contains(areaCode)) - { + { throw new ArgumentException($"The area code {areaCode} is invalid."); - } - - string localNumber = nsnValue.Substring(3); + } + + var localNumber = nsnValue.Substring(3); return new NonGeographicPhoneNumber(countryInfo, areaCode, localNumber); } - } -} + } +} diff --git a/src/PhoneNumbers/PhoneNumber.cs b/src/PhoneNumbers/PhoneNumber.cs index 4f049ee6..65aea37b 100644 --- a/src/PhoneNumbers/PhoneNumber.cs +++ b/src/PhoneNumbers/PhoneNumber.cs @@ -1,4 +1,4 @@ -using System; +using System; using PhoneNumbers.Formatters; namespace PhoneNumbers @@ -48,7 +48,7 @@ protected PhoneNumber(CountryInfo countryInfo, string areaCode, string localNumb /// A instance representing the specified string. public static PhoneNumber Parse(string value) { - foreach (CountryInfo countryInfo in CountryInfo.AllSupported()) + foreach (var countryInfo in CountryInfo.AllSupported()) { if (countryInfo.IsInternationalNumber(value)) { @@ -67,7 +67,7 @@ public static PhoneNumber Parse(string value) /// A instance representing the specified string. public static PhoneNumber Parse(string value, string countryCode) { - CountryInfo countryInfo = CountryInfo.Find(countryCode); + var countryInfo = CountryInfo.Find(countryCode); return countryInfo.Parser.Parse(value, countryInfo); } diff --git a/src/PhoneNumbers/PhoneNumberKind.cs b/src/PhoneNumbers/PhoneNumberKind.cs index 9881ed47..1ce96a78 100644 --- a/src/PhoneNumbers/PhoneNumberKind.cs +++ b/src/PhoneNumbers/PhoneNumberKind.cs @@ -1,23 +1,23 @@ -namespace PhoneNumbers -{ - /// - /// The kinds of . - /// - public enum PhoneNumberKind - { - /// - /// The phone number represents a geographicly assigned phone number. - /// - GeographicPhoneNumber, - - /// - /// The phone number represents a mobile phone. - /// - MobilePhoneNumber, - +namespace PhoneNumbers +{ + /// + /// The kinds of . + /// + public enum PhoneNumberKind + { + /// + /// The phone number represents a geographicly assigned phone number. + /// + GeographicPhoneNumber, + + /// + /// The phone number represents a mobile phone. + /// + MobilePhoneNumber, + /// /// The phone number is not assigned to a geographic area. - /// - NonGeographicPhoneNumber, - } -} + /// + NonGeographicPhoneNumber, + } +} diff --git a/src/PhoneNumbers/PhoneNumbers.csproj b/src/PhoneNumbers/PhoneNumbers.csproj index 963f6f41..22b9003e 100644 --- a/src/PhoneNumbers/PhoneNumbers.csproj +++ b/src/PhoneNumbers/PhoneNumbers.csproj @@ -1,71 +1,70 @@ - - - - PhoneNumbers - PhoneNumbers - net5.0;netstandard2.1;netstandard2.0 - 9.0 - enable - AllEnabledByDefault - true - ..\..\PhoneNumbers.snk - 1.0.0 - Trevor Pilley and Project Contributors - Copyright 2020 Project Contributors - MIT - https://github.com/TrevorPilley/PhoneNumbers - https://github.com/TrevorPilley/PhoneNumbers - git - en-GB - Phone Number Parser - See https://github.com/TrevorPilley/PhoneNumbers/releases for a summary of changes made in this release of the package. - - - - bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml - - - - bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml - - - - bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml - - - - bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml - - - - bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml - - - - bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml - - + + + + PhoneNumbers + PhoneNumbers + net5.0;netstandard2.1;netstandard2.0 + 9.0 + enable + AllEnabledByDefault + true + ..\..\PhoneNumbers.snk + 1.0.0 + Trevor Pilley and Project Contributors + Copyright 2020 Project Contributors + MIT + https://github.com/TrevorPilley/PhoneNumbers + https://github.com/TrevorPilley/PhoneNumbers + git + en-GB + Phone Number Parser + See https://github.com/TrevorPilley/PhoneNumbers/releases for a summary of changes made in this release of the package. + + + + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + + + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + + + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + + + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + + + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + + + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + - - + + - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + diff --git a/src/PhoneNumbers/Properties/AssemblyInfo.cs b/src/PhoneNumbers/Properties/AssemblyInfo.cs index 66cf16dd..552320fc 100644 --- a/src/PhoneNumbers/Properties/AssemblyInfo.cs +++ b/src/PhoneNumbers/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Runtime.CompilerServices; [assembly: CLSCompliant(true)] diff --git a/src/PhoneNumbers/ResourceUtility.cs b/src/PhoneNumbers/ResourceUtility.cs index 2aa25866..b11acae1 100644 --- a/src/PhoneNumbers/ResourceUtility.cs +++ b/src/PhoneNumbers/ResourceUtility.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -10,11 +10,11 @@ internal static class ResourceUtility { internal static IEnumerable ReadLines(string name) { - Assembly assembly = Assembly.GetExecutingAssembly(); - string resourceName = assembly.GetManifestResourceNames().Single(x => x.EndsWith(name, StringComparison.Ordinal)); + var assembly = Assembly.GetExecutingAssembly(); + var resourceName = assembly.GetManifestResourceNames().Single(x => x.EndsWith(name, StringComparison.Ordinal)); - using Stream stream = assembly.GetManifestResourceStream(resourceName)!; - using StreamReader reader = new StreamReader(stream); + using var stream = assembly.GetManifestResourceStream(resourceName)!; + using var reader = new StreamReader(stream); string? line; diff --git a/test/PhoneNumbers.Tests/CountryInfoTests.cs b/test/PhoneNumbers.Tests/CountryInfoTests.cs index e416ba64..d7ef9fb9 100644 --- a/test/PhoneNumbers.Tests/CountryInfoTests.cs +++ b/test/PhoneNumbers.Tests/CountryInfoTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using Xunit; namespace PhoneNumbers.Tests diff --git a/test/PhoneNumbers.Tests/CountryInfoUKTests.cs b/test/PhoneNumbers.Tests/CountryInfoUKTests.cs index 99d08e36..26d4044c 100644 --- a/test/PhoneNumbers.Tests/CountryInfoUKTests.cs +++ b/test/PhoneNumbers.Tests/CountryInfoUKTests.cs @@ -1,4 +1,4 @@ -using PhoneNumbers.Formatters; +using PhoneNumbers.Formatters; using PhoneNumbers.Parsers; using Xunit; @@ -16,7 +16,7 @@ public void UK() Assert.Same(CountryInfo.UK, CountryInfo.UK); Assert.Same(CountryInfo.UK, CountryInfo.Find("GB")); - CountryInfo countryInfo = CountryInfo.UK; + var countryInfo = CountryInfo.UK; Assert.Equal("+44", countryInfo.CallingCode); Assert.IsType(countryInfo.Formatter); diff --git a/test/PhoneNumbers.Tests/Formatters/PhoneNumberFormatterTests.cs b/test/PhoneNumbers.Tests/Formatters/PhoneNumberFormatterTests.cs index 7b9089ff..e652652f 100644 --- a/test/PhoneNumbers.Tests/Formatters/PhoneNumberFormatterTests.cs +++ b/test/PhoneNumbers.Tests/Formatters/PhoneNumberFormatterTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using Moq; using PhoneNumbers.Formatters; using Xunit; diff --git a/test/PhoneNumbers.Tests/Formatters/UkPhoneNumberFormatterTests.cs b/test/PhoneNumbers.Tests/Formatters/UkPhoneNumberFormatterTests.cs index 11841b57..a76515a5 100644 --- a/test/PhoneNumbers.Tests/Formatters/UkPhoneNumberFormatterTests.cs +++ b/test/PhoneNumbers.Tests/Formatters/UkPhoneNumberFormatterTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using PhoneNumbers.Formatters; using Xunit; diff --git a/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs b/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs index 9982bfb0..0bb946ef 100644 --- a/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs @@ -1,4 +1,4 @@ -using Xunit; +using Xunit; namespace PhoneNumbers.Tests { @@ -7,7 +7,7 @@ public class GeographicPhoneNumberTests [Fact] public void Constructor_Sets_Properties() { - CountryInfo countryInfo = CountryInfo.UK; + var countryInfo = CountryInfo.UK; var phoneNumber = new GeographicPhoneNumber(countryInfo, "12345", "667788", "N/A"); Assert.Equal("12345", phoneNumber.AreaCode); diff --git a/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs b/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs index 8f46b12f..7fcfd525 100644 --- a/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs @@ -1,4 +1,4 @@ -using Xunit; +using Xunit; namespace PhoneNumbers.Tests { @@ -7,7 +7,7 @@ public class MobilePhoneNumberTests [Fact] public void Constructor_Sets_Properties() { - CountryInfo countryInfo = CountryInfo.UK; + var countryInfo = CountryInfo.UK; var phoneNumber = new MobilePhoneNumber(countryInfo, "7654", "112233", true, true, true); Assert.Equal("7654", phoneNumber.AreaCode); diff --git a/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs b/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs index 7fb7bd40..86c62458 100644 --- a/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs @@ -1,4 +1,4 @@ -using Xunit; +using Xunit; namespace PhoneNumbers.Tests { @@ -7,7 +7,7 @@ public class NonGeographicPhoneNumberTests [Fact] public void Constructor_Sets_Properties() { - CountryInfo countryInfo = CountryInfo.UK; + var countryInfo = CountryInfo.UK; var phoneNumber = new NonGeographicPhoneNumber(countryInfo, "7654", "112233"); Assert.Equal("7654", phoneNumber.AreaCode); diff --git a/test/PhoneNumbers.Tests/Parsers/PhoneNumberParserTests.cs b/test/PhoneNumbers.Tests/Parsers/PhoneNumberParserTests.cs index 76a5bc84..6aefd984 100644 --- a/test/PhoneNumbers.Tests/Parsers/PhoneNumberParserTests.cs +++ b/test/PhoneNumbers.Tests/Parsers/PhoneNumberParserTests.cs @@ -1,31 +1,31 @@ -using System; -using Moq; -using PhoneNumbers.Parsers; -using Xunit; - -namespace PhoneNumbers.Tests.Formatters -{ - /// - /// Contains unit tests for the class. - /// - /// - /// All tests use unused calling codes and fake numbers. - /// - public class PhoneNumberParserTests - { - [Fact] - public void Parse_Throws_Exception_For_Empty_Value() => +using System; +using Moq; +using PhoneNumbers.Parsers; +using Xunit; + +namespace PhoneNumbers.Tests.Formatters +{ + /// + /// Contains unit tests for the class. + /// + /// + /// All tests use unused calling codes and fake numbers. + /// + public class PhoneNumberParserTests + { + [Fact] + public void Parse_Throws_Exception_For_Empty_Value() => Assert.Throws(() => GetParser().Parse(" ", CountryInfo.UK)); - [Fact] - public void Parse_Throws_Exception_For_Null_CountryInfo() => + [Fact] + public void Parse_Throws_Exception_For_Null_CountryInfo() => Assert.Throws(() => GetParser().Parse("012345667788", null)); - - [Fact] - public void Parse_Throws_Exception_For_Null_Value() => - Assert.Throws(() => GetParser().Parse(null, CountryInfo.UK)); - - private static PhoneNumberParser GetParser() => - new Mock { CallBase = true }.Object; - } -} + + [Fact] + public void Parse_Throws_Exception_For_Null_Value() => + Assert.Throws(() => GetParser().Parse(null, CountryInfo.UK)); + + private static PhoneNumberParser GetParser() => + new Mock { CallBase = true }.Object; + } +} diff --git a/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_GeographicPhoneNumber.cs b/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_GeographicPhoneNumber.cs index a60da3c7..831e843e 100644 --- a/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_GeographicPhoneNumber.cs +++ b/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_GeographicPhoneNumber.cs @@ -1,4 +1,4 @@ -using PhoneNumbers.Parsers; +using PhoneNumbers.Parsers; using Xunit; namespace PhoneNumbers.Tests.Parsers @@ -27,7 +27,7 @@ public partial class UKPhoneNumberParserTests public void Parse_Known_GeographicPhoneNumber(string value, string areaCode, string localNumber, string geographicArea) { var parser = new UKPhoneNumberParser(); - PhoneNumber phoneNumber = parser.Parse(value, CountryInfo.UK); + var phoneNumber = parser.Parse(value, CountryInfo.UK); Assert.NotNull(phoneNumber); Assert.IsType(phoneNumber); diff --git a/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_MobilePhoneNumber.cs b/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_MobilePhoneNumber.cs index 87575a32..68bda28b 100644 --- a/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_MobilePhoneNumber.cs +++ b/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_MobilePhoneNumber.cs @@ -1,4 +1,4 @@ -using PhoneNumbers.Parsers; +using PhoneNumbers.Parsers; using Xunit; namespace PhoneNumbers.Tests.Parsers @@ -25,7 +25,7 @@ public partial class UKPhoneNumberParserTests public void Parse_Known_MobilePhoneNumber(string value, string areaCode, string localNumber) { var parser = new UKPhoneNumberParser(); - PhoneNumber phoneNumber = parser.Parse(value, CountryInfo.UK); + var phoneNumber = parser.Parse(value, CountryInfo.UK); Assert.NotNull(phoneNumber); Assert.IsType(phoneNumber); @@ -45,7 +45,7 @@ public void Parse_Known_MobilePhoneNumber(string value, string areaCode, string public void Parse_Known_MobilePhoneNumber_DataOnly(string value, string areaCode, string localNumber) { var parser = new UKPhoneNumberParser(); - PhoneNumber phoneNumber = parser.Parse(value, CountryInfo.UK); + var phoneNumber = parser.Parse(value, CountryInfo.UK); Assert.NotNull(phoneNumber); Assert.IsType(phoneNumber); @@ -64,7 +64,7 @@ public void Parse_Known_MobilePhoneNumber_DataOnly(string value, string areaCode public void Parse_Known_MobilePhoneNumber_Pager(string value, string areaCode, string localNumber) { var parser = new UKPhoneNumberParser(); - PhoneNumber phoneNumber = parser.Parse(value, CountryInfo.UK); + var phoneNumber = parser.Parse(value, CountryInfo.UK); Assert.NotNull(phoneNumber); Assert.IsType(phoneNumber); @@ -83,7 +83,7 @@ public void Parse_Known_MobilePhoneNumber_Pager(string value, string areaCode, s public void Parse_Known_MobilePhoneNumber_Virtual(string value, string areaCode, string localNumber) { var parser = new UKPhoneNumberParser(); - PhoneNumber phoneNumber = parser.Parse(value, CountryInfo.UK); + var phoneNumber = parser.Parse(value, CountryInfo.UK); Assert.NotNull(phoneNumber); Assert.IsType(phoneNumber); diff --git a/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_NonGeographicPhoneNumber.cs b/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_NonGeographicPhoneNumber.cs index 020016f5..24368c40 100644 --- a/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_NonGeographicPhoneNumber.cs +++ b/test/PhoneNumbers.Tests/Parsers/UKPhoneNumberParserTests_NonGeographicPhoneNumber.cs @@ -1,4 +1,4 @@ -using PhoneNumbers.Parsers; +using PhoneNumbers.Parsers; using Xunit; namespace PhoneNumbers.Tests.Parsers @@ -32,7 +32,7 @@ public partial class UKPhoneNumberParserTests public void Parse_Known_NonGeographicPhoneNumber(string value, string areaCode, string localNumber) { var parser = new UKPhoneNumberParser(); - PhoneNumber phoneNumber = parser.Parse(value, CountryInfo.UK); + var phoneNumber = parser.Parse(value, CountryInfo.UK); Assert.NotNull(phoneNumber); Assert.IsType(phoneNumber); diff --git a/test/PhoneNumbers.Tests/Parsers/UkPhoneNumberParserTests.cs b/test/PhoneNumbers.Tests/Parsers/UkPhoneNumberParserTests.cs index 41dc538c..2d77fb48 100644 --- a/test/PhoneNumbers.Tests/Parsers/UkPhoneNumberParserTests.cs +++ b/test/PhoneNumbers.Tests/Parsers/UkPhoneNumberParserTests.cs @@ -1,121 +1,121 @@ -using System; -using PhoneNumbers.Parsers; -using Xunit; - -namespace PhoneNumbers.Tests.Parsers -{ - /// - /// Contains unit tests for the class. - /// - /// - /// All valid number tests use the local council number for the area code, or a public company/charity number for non geographic numbers. - /// - public partial class UKPhoneNumberParserTests +using System; +using PhoneNumbers.Parsers; +using Xunit; + +namespace PhoneNumbers.Tests.Parsers +{ + /// + /// Contains unit tests for the class. + /// + /// + /// All valid number tests use the local council number for the area code, or a public company/charity number for non geographic numbers. + /// + public partial class UKPhoneNumberParserTests { - [Fact] - public void Parse_Throws_Exception_For_1XX_AreaCode_And_Local_Number_Not_7_Digits() - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse("0113111222", CountryInfo.UK)); - Assert.Equal("The for the area code 113, the local number must be 7 digits.", exception.Message); + [Fact] + public void Parse_Throws_Exception_For_1XX_AreaCode_And_Local_Number_Not_7_Digits() + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse("0113111222", CountryInfo.UK)); + Assert.Equal("The for the area code 113, the local number must be 7 digits.", exception.Message); + } + + [Fact] + public void Parse_Throws_Exception_For_2X_AreaCode_And_Local_Number_Not_8_Digits() + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse("0201112222", CountryInfo.UK)); + Assert.Equal("The for the area code 20, the local number must be 8 digits.", exception.Message); } - [Fact] - public void Parse_Throws_Exception_For_2X_AreaCode_And_Local_Number_Not_8_Digits() - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse("0201112222", CountryInfo.UK)); - Assert.Equal("The for the area code 20, the local number must be 8 digits.", exception.Message); + [Fact] + public void Parse_Throws_Exception_For_3XX_AreaCode_And_Local_Number_Not_7_Digits() + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse("0300111111", CountryInfo.UK)); + Assert.Equal("For a UK non-geographic number, the national significant number of the phone number must be 10 digits.", exception.Message); } - [Fact] - public void Parse_Throws_Exception_For_3XX_AreaCode_And_Local_Number_Not_7_Digits() - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse("0300111111", CountryInfo.UK)); - Assert.Equal("For a UK non-geographic number, the national significant number of the phone number must be 10 digits.", exception.Message); + [Fact] + public void Parse_Throws_Exception_For_7XXX_AreaCode_And_Local_Number_Not_6_Digits() + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse("0700012345", CountryInfo.UK)); + Assert.Equal("For a UK mobile phone, the national significant number of the phone number must be 10 digits.", exception.Message); } - [Fact] - public void Parse_Throws_Exception_For_7XXX_AreaCode_And_Local_Number_Not_6_Digits() - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse("0700012345", CountryInfo.UK)); - Assert.Equal("For a UK mobile phone, the national significant number of the phone number must be 10 digits.", exception.Message); + [Theory] + [InlineData("01100000000", "110")] + [InlineData("01110000000", "111")] + [InlineData("01120000000", "112")] + [InlineData("01190000000", "119")] + [InlineData("01710000000", "171")] + [InlineData("01810000000", "181")] + [InlineData("02100000000", "21")] + [InlineData("02200000000", "22")] + [InlineData("02500000000", "25")] + [InlineData("02600000000", "26")] + [InlineData("02700000000", "27")] + [InlineData("03100000000", "310")] + [InlineData("03200000000", "320")] + [InlineData("03500000000", "350")] + [InlineData("03600000000", "360")] + [InlineData("03800000000", "380")] + [InlineData("03900000000", "390")] + [InlineData("07200000000", "7200")] + public void Parse_Throws_Exception_If_AreaCode_Invalid(string value, string areaCode) + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse(value, CountryInfo.UK)); + Assert.Equal($"The area code {areaCode} is invalid.", exception.Message); } - [Theory] - [InlineData("01100000000", "110")] - [InlineData("01110000000", "111")] - [InlineData("01120000000", "112")] - [InlineData("01190000000", "119")] - [InlineData("01710000000", "171")] - [InlineData("01810000000", "181")] - [InlineData("02100000000", "21")] - [InlineData("02200000000", "22")] - [InlineData("02500000000", "25")] - [InlineData("02600000000", "26")] - [InlineData("02700000000", "27")] - [InlineData("03100000000", "310")] - [InlineData("03200000000", "320")] - [InlineData("03500000000", "350")] - [InlineData("03600000000", "360")] - [InlineData("03800000000", "380")] - [InlineData("03900000000", "390")] - [InlineData("07200000000", "7200")] - public void Parse_Throws_Exception_If_AreaCode_Invalid(string value, string areaCode) - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse(value, CountryInfo.UK)); - Assert.Equal($"The area code {areaCode} is invalid.", exception.Message); + [Fact] + public void Parse_Throws_Exception_If_CallingCode_Invalid() + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse("+1111111111", CountryInfo.UK)); + Assert.Equal($"The value must be a GB phone number starting {CountryInfo.UK.TrunkPrefix} or {CountryInfo.UK.CallingCode}.", exception.Message); } - [Fact] - public void Parse_Throws_Exception_If_CallingCode_Invalid() - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse("+1111111111", CountryInfo.UK)); - Assert.Equal($"The value must be a GB phone number starting {CountryInfo.UK.TrunkPrefix} or {CountryInfo.UK.CallingCode}.", exception.Message); + [Theory] + [InlineData("07101111111")] + public void Parse_Throws_Exception_If_LocalNumber_Invalid_For_AreaCode(string value) + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse(value, CountryInfo.UK)); + Assert.Equal($"The area code {value.Substring(1, 4)} is invalid.", exception.Message); } - [Theory] - [InlineData("07101111111")] - public void Parse_Throws_Exception_If_LocalNumber_Invalid_For_AreaCode(string value) - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse(value, CountryInfo.UK)); - Assert.Equal($"The area code {value.Substring(1, 4)} is invalid.", exception.Message); - } - - [Theory] - [InlineData("02")] - [InlineData("020")] - [InlineData("0201")] - [InlineData("02011")] - [InlineData("020111")] - [InlineData("0201111")] - [InlineData("020111111")] // 8 - [InlineData("020111111111")] // 11 + [Theory] + [InlineData("02")] + [InlineData("020")] + [InlineData("0201")] + [InlineData("02011")] + [InlineData("020111")] + [InlineData("0201111")] + [InlineData("020111111")] // 8 + [InlineData("020111111111")] // 11 public void Parse_Throws_Exception_If_Nsn_Incorrect_Length(string value) - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse(value, CountryInfo.UK)); + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse(value, CountryInfo.UK)); Assert.Equal("The national significant number of the phone number must be 7,9,10 in length.", exception.Message); } - [Fact] - public void Parse_Throws_Exception_If_ServiceType_Invalid() - { - var parser = new UKPhoneNumberParser(); - NotSupportedException exception = Assert.Throws(() => parser.Parse("0411111111", CountryInfo.UK)); - } - - [Fact] - public void Parse_Throws_Exception_If_TrunkPrefix_Invalid() - { - var parser = new UKPhoneNumberParser(); - ArgumentException exception = Assert.Throws(() => parser.Parse("1111111111", CountryInfo.UK)); - Assert.Equal($"The value must be a GB phone number starting {CountryInfo.UK.TrunkPrefix} or {CountryInfo.UK.CallingCode}.", exception.Message); - } - } -} + [Fact] + public void Parse_Throws_Exception_If_ServiceType_Invalid() + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse("0411111111", CountryInfo.UK)); + } + + [Fact] + public void Parse_Throws_Exception_If_TrunkPrefix_Invalid() + { + var parser = new UKPhoneNumberParser(); + var exception = Assert.Throws(() => parser.Parse("1111111111", CountryInfo.UK)); + Assert.Equal($"The value must be a GB phone number starting {CountryInfo.UK.TrunkPrefix} or {CountryInfo.UK.CallingCode}.", exception.Message); + } + } +} diff --git a/test/PhoneNumbers.Tests/PhoneNumberTests.cs b/test/PhoneNumbers.Tests/PhoneNumberTests.cs index a94d6714..347d6cc2 100644 --- a/test/PhoneNumbers.Tests/PhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/PhoneNumberTests.cs @@ -1,43 +1,43 @@ -using System; -using PhoneNumbers.Formatters; -using Xunit; - -namespace PhoneNumbers.Tests -{ - public class PhoneNumberTests - { - [Fact] - public void Parse_Ignores_Parenthesis() => - Assert.NotNull(PhoneNumber.Parse("(0114) 123 4567", "GB")); - - [Fact] - public void Parse_Ignores_Spaces() => - Assert.NotNull(PhoneNumber.Parse("0114 123 4567", "GB")); - - [Fact] - public void Parse_Throws_If_Value_Does_Not_Start_With_Plus() => +using System; +using PhoneNumbers.Formatters; +using Xunit; + +namespace PhoneNumbers.Tests +{ + public class PhoneNumberTests + { + [Fact] + public void Parse_Ignores_Parenthesis() => + Assert.NotNull(PhoneNumber.Parse("(0114) 123 4567", "GB")); + + [Fact] + public void Parse_Ignores_Spaces() => + Assert.NotNull(PhoneNumber.Parse("0114 123 4567", "GB")); + + [Fact] + public void Parse_Throws_If_Value_Does_Not_Start_With_Plus() => Assert.Throws(() => PhoneNumber.Parse("441141234567")); - [Fact] - public void Parse_Throws_If_Value_Empty() => + [Fact] + public void Parse_Throws_If_Value_Empty() => Assert.Throws(() => PhoneNumber.Parse(" ")); - [Fact] - public void Parse_Throws_If_Value_Null() => + [Fact] + public void Parse_Throws_If_Value_Null() => Assert.Throws(() => PhoneNumber.Parse(null)); - [Fact] - public void Parse_UK_CallingCode() => + [Fact] + public void Parse_UK_CallingCode() => Assert.NotNull(PhoneNumber.Parse("+441141234567")); - - [Fact] - public void ToString_Returns_Default_Formatted_PhoneNumber() - { - var phoneNumber = PhoneNumber.Parse("+441141234567"); - - Assert.Equal( - phoneNumber.Country.Formatter.Format(phoneNumber, PhoneNumberFormatter.DefaultFormat), - phoneNumber.ToString()); - } - } -} + + [Fact] + public void ToString_Returns_Default_Formatted_PhoneNumber() + { + var phoneNumber = PhoneNumber.Parse("+441141234567"); + + Assert.Equal( + phoneNumber.Country.Formatter.Format(phoneNumber, PhoneNumberFormatter.DefaultFormat), + phoneNumber.ToString()); + } + } +} diff --git a/test/PhoneNumbers.Tests/PhoneNumbers.Tests.csproj b/test/PhoneNumbers.Tests/PhoneNumbers.Tests.csproj index 780fe00e..c386f656 100644 --- a/test/PhoneNumbers.Tests/PhoneNumbers.Tests.csproj +++ b/test/PhoneNumbers.Tests/PhoneNumbers.Tests.csproj @@ -34,5 +34,4 @@ -