Skip to content

Commit

Permalink
Code Butler clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Dec 10, 2024
1 parent 6272592 commit ab5f58b
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 191 deletions.
13 changes: 13 additions & 0 deletions codebutler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

dotnet tool update --global code-butler

find ./src/PhoneNumbers/ -type f -name '*.cs' | while read fname;
do
dotnet-code-butler "$fname"
done

find ./test/PhoneNumbers.Tests/ -type f -name '*.cs' | while read fname;
do
dotnet-code-butler "$fname"
done
54 changes: 27 additions & 27 deletions src/PhoneNumbers/CountryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ internal CountryInfo()
public bool IsNatoMember { get; init; }

/// <summary>
/// Gets the ISO 3166 Alpha-2 code for the country.
/// Gets a value indicating whether the country is a member of the Organisation for Economic Co-operation and Development.
/// </summary>
/// <remarks>See https://www.iso.org/iso-3166-country-codes.html</remarks>
public required string Iso3166Code { get; init; } = null!;
public bool IsOecdMember { get; init; }

/// <summary>
/// Gets a value indicating whether the country is a member of the Organisation for Economic Co-operation and Development.
/// Gets the ISO 3166 Alpha-2 code for the country.
/// </summary>
public bool IsOecdMember { get; init; }
/// <remarks>See https://www.iso.org/iso-3166-country-codes.html</remarks>
public required string Iso3166Code { get; init; } = null!;

/// <summary>
/// Gets the name of the country in English.
Expand Down Expand Up @@ -159,25 +159,6 @@ internal CountryInfo()
return countryInfo1.Equals(countryInfo2);
}

internal static IEnumerable<CountryInfo> GetCountries() =>
typeof(CountryInfo)
.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.PropertyType == typeof(CountryInfo))
.Select(x => x.GetValue(null))
.Cast<CountryInfo>();

internal static IEnumerable<CountryInfo> GetCountries(Func<CountryInfo, bool> predicate) =>
GetCountries()
.Where(predicate);

/// <summary>
/// Gets the <see cref="PhoneNumberFormatter"/> for the specified format.
/// </summary>
/// <exception cref="FormatException">Thrown if the format string is not valid.</exception>
/// <returns>The <see cref="PhoneNumberFormatter"/>.</returns>
internal static PhoneNumberFormatter GetFormatter(string format) =>
s_formatters.SingleOrDefault(x => x.CanFormat(format)) ?? throw new FormatException($"{format} is not a supported format");

/// <inheritdoc/>
public override bool Equals(object? obj) =>
Equals(obj as CountryInfo);
Expand All @@ -203,6 +184,25 @@ public bool Equals(CountryInfo? other)
public override int GetHashCode() =>
HashCode.Combine(Iso3166Code);

internal static IEnumerable<CountryInfo> GetCountries() =>
typeof(CountryInfo)
.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.PropertyType == typeof(CountryInfo))
.Select(x => x.GetValue(null))
.Cast<CountryInfo>();

internal static IEnumerable<CountryInfo> GetCountries(Func<CountryInfo, bool> predicate) =>
GetCountries()
.Where(predicate);

/// <summary>
/// Gets the <see cref="PhoneNumberFormatter"/> for the specified format.
/// </summary>
/// <exception cref="FormatException">Thrown if the format string is not valid.</exception>
/// <returns>The <see cref="PhoneNumberFormatter"/>.</returns>
internal static PhoneNumberFormatter GetFormatter(string format) =>
s_formatters.SingleOrDefault(x => x.CanFormat(format)) ?? throw new FormatException($"{format} is not a supported format");

/// <summary>
/// Gets a value indicating whether the specified value has the calling code for this country.
/// </summary>
Expand Down Expand Up @@ -262,13 +262,13 @@ internal string ReadNationalSignificantNumber(string value)
internal bool SharesCallingCodeWith(CountryInfo countryInfo) =>
CallingCode.Equals(countryInfo.CallingCode, StringComparison.Ordinal);

private static bool IsDelimiter(char charVal) =>
charVal == Chars.Comma || charVal == Chars.Semicolon;

/// <remarks>Char.IsDigit returns true for more than 0-9 so use a more restricted version.</remarks>
private static bool IsDigit(char charVal) =>
charVal is >= '0' and <= '9';

private static bool IsDelimiter(char charVal) =>
charVal == Chars.Comma || charVal == Chars.Semicolon;

private static bool IsSeparator(char charVal) =>
charVal == Chars.Hyphen || charVal == Chars.ForwardSlash;

Expand Down
48 changes: 24 additions & 24 deletions test/PhoneNumbers.Tests/CountryInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,6 @@ namespace PhoneNumbers.Tests;

public class CountryInfoTests
{
[Fact]
public void GetFormatter_E123_Returns_E123PhoneNumberFormatter() =>
Assert.IsType<E123PhoneNumberFormatter>(CountryInfo.GetFormatter("E.123"));

[Fact]
public void GetFormatter_E164_Returns_E164PhoneNumberFormatter() =>
Assert.IsType<E164PhoneNumberFormatter>(CountryInfo.GetFormatter("E.164"));

[Fact]
public void GetFormatter_N_Returns_NationalPhoneNumberFormatter() =>
Assert.IsType<NationalPhoneNumberFormatter>(CountryInfo.GetFormatter("N"));

[Fact]
public void GetFormatter_RFC3966_Returns_Rfc3966PhoneNumberFormatter() =>
Assert.IsType<Rfc3966PhoneNumberFormatter>(CountryInfo.GetFormatter("RFC3966"));

[Fact]
public void GetFormatter_U_Returns_NationalUnformattedPhoneNumberFormatter() =>
Assert.IsType<NationalUnformattedPhoneNumberFormatter>(CountryInfo.GetFormatter("U"));

[Fact]
public void GetFormatter_Throws_For_Invalid_Format() =>
Assert.Throws<FormatException>(() => CountryInfo.GetFormatter("X"));

[Fact]
public void Equality_Both_Null()
Expand Down Expand Up @@ -81,6 +58,29 @@ public void Equality_Same_Iso3166Code()
Assert.True(countryInfo1 == countryInfo2);
Assert.False(countryInfo1 != countryInfo2);
}
[Fact]
public void GetFormatter_E123_Returns_E123PhoneNumberFormatter() =>
Assert.IsType<E123PhoneNumberFormatter>(CountryInfo.GetFormatter("E.123"));

[Fact]
public void GetFormatter_E164_Returns_E164PhoneNumberFormatter() =>
Assert.IsType<E164PhoneNumberFormatter>(CountryInfo.GetFormatter("E.164"));

[Fact]
public void GetFormatter_N_Returns_NationalPhoneNumberFormatter() =>
Assert.IsType<NationalPhoneNumberFormatter>(CountryInfo.GetFormatter("N"));

[Fact]
public void GetFormatter_RFC3966_Returns_Rfc3966PhoneNumberFormatter() =>
Assert.IsType<Rfc3966PhoneNumberFormatter>(CountryInfo.GetFormatter("RFC3966"));

[Fact]
public void GetFormatter_Throws_For_Invalid_Format() =>
Assert.Throws<FormatException>(() => CountryInfo.GetFormatter("X"));

[Fact]
public void GetFormatter_U_Returns_NationalUnformattedPhoneNumberFormatter() =>
Assert.IsType<NationalUnformattedPhoneNumberFormatter>(CountryInfo.GetFormatter("U"));

[Theory]
[InlineData(default(string))]
Expand Down Expand Up @@ -270,7 +270,7 @@ public void ShareCallingCodeWith()
Assert.True(CountryInfo.UnitedKingdom.SharesCallingCodeWith(CountryInfo.IsleOfMan));
Assert.True(CountryInfo.UnitedKingdom.SharesCallingCodeWith(CountryInfo.Jersey));
}

[Fact]
public void When_Constructed()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,62 +114,6 @@ public void GetFormat_National_No_TrunkPrefix_With_Ndc(string ndc, string sn, st
SimplePhoneNumberFormatProvider.Default.GetFormat(
TestHelper.CreateNonGeographicPhoneNumber(null, ndc, sn), false));

[Theory]
[InlineData("0", "1234", "0####")]
[InlineData("0", "12345", "0#####")]
[InlineData("0", "123456", "0######")]
[InlineData("0", "1234567", "0#######")]
[InlineData("0", "12345678", "0########")]
[InlineData("0", "123456789", "0#########")]
[InlineData("0", "1234567890", "0##########")]
public void GetFormat_National_With_TrunkPrefix_No_Ndc(string trunkPrefix, string sn, string expectedMask) =>
Assert.Equal(
expectedMask,
SimplePhoneNumberFormatProvider.Default.GetFormat(
TestHelper.CreateNonGeographicPhoneNumber(trunkPrefix, null, sn), false));

[Theory]
[InlineData("0", "1", "1234", "0# ####")]
[InlineData("0", "1", "12345", "0# #####")]
[InlineData("0", "1", "123456", "0# ######")]
[InlineData("0", "1", "1234567", "0# #######")]
[InlineData("0", "1", "12345678", "0# ########")]
[InlineData("0", "1", "123456789", "0# #########")]
[InlineData("0", "1", "1234567890", "0# ##########")]
[InlineData("0", "12", "1234", "0## ####")]
[InlineData("0", "12", "12345", "0## #####")]
[InlineData("0", "12", "123456", "0## ######")]
[InlineData("0", "12", "1234567", "0## #######")]
[InlineData("0", "12", "12345678", "0## ########")]
[InlineData("0", "12", "123456789", "0## #########")]
[InlineData("0", "12", "1234567890", "0## ##########")]
[InlineData("0", "123", "1234", "0### ####")]
[InlineData("0", "123", "12345", "0### #####")]
[InlineData("0", "123", "123456", "0### ######")]
[InlineData("0", "123", "1234567", "0### #######")]
[InlineData("0", "123", "12345678", "0### ########")]
[InlineData("0", "123", "123456789", "0### #########")]
[InlineData("0", "123", "1234567890", "0### ##########")]
[InlineData("0", "1234", "1234", "0#### ####")]
[InlineData("0", "1234", "12345", "0#### #####")]
[InlineData("0", "1234", "123456", "0#### ######")]
[InlineData("0", "1234", "1234567", "0#### #######")]
[InlineData("0", "1234", "12345678", "0#### ########")]
[InlineData("0", "1234", "123456789", "0#### #########")]
[InlineData("0", "1234", "1234567890", "0#### ##########")]
[InlineData("0", "12345", "1234", "0##### ####")]
[InlineData("0", "12345", "12345", "0##### #####")]
[InlineData("0", "12345", "123456", "0##### ######")]
[InlineData("0", "12345", "1234567", "0##### #######")]
[InlineData("0", "12345", "12345678", "0##### ########")]
[InlineData("0", "12345", "123456789", "0##### #########")]
[InlineData("0", "12345", "1234567890", "0##### ##########")]
public void GetFormat_National_With_TrunkPrefix_With_Ndc(string trunkPrefix, string ndc, string sn, string expectedMask) =>
Assert.Equal(
expectedMask,
SimplePhoneNumberFormatProvider.Default.GetFormat(
TestHelper.CreateNonGeographicPhoneNumber(trunkPrefix, ndc, sn), false));

[Theory]
[InlineData("1", "1234", "(#) ####")]
[InlineData("1", "12345", "(#) #####")]
Expand Down Expand Up @@ -254,6 +198,62 @@ public void GetFormat_National_No_TrunkPrefix_With_Ndc_Required(string ndc, stri
SimplePhoneNumberFormatProvider.Default.GetFormat(
TestHelper.CreateNonGeographicPhoneNumber(null, ndc, sn, true), false));

[Theory]
[InlineData("0", "1234", "0####")]
[InlineData("0", "12345", "0#####")]
[InlineData("0", "123456", "0######")]
[InlineData("0", "1234567", "0#######")]
[InlineData("0", "12345678", "0########")]
[InlineData("0", "123456789", "0#########")]
[InlineData("0", "1234567890", "0##########")]
public void GetFormat_National_With_TrunkPrefix_No_Ndc(string trunkPrefix, string sn, string expectedMask) =>
Assert.Equal(
expectedMask,
SimplePhoneNumberFormatProvider.Default.GetFormat(
TestHelper.CreateNonGeographicPhoneNumber(trunkPrefix, null, sn), false));

[Theory]
[InlineData("0", "1", "1234", "0# ####")]
[InlineData("0", "1", "12345", "0# #####")]
[InlineData("0", "1", "123456", "0# ######")]
[InlineData("0", "1", "1234567", "0# #######")]
[InlineData("0", "1", "12345678", "0# ########")]
[InlineData("0", "1", "123456789", "0# #########")]
[InlineData("0", "1", "1234567890", "0# ##########")]
[InlineData("0", "12", "1234", "0## ####")]
[InlineData("0", "12", "12345", "0## #####")]
[InlineData("0", "12", "123456", "0## ######")]
[InlineData("0", "12", "1234567", "0## #######")]
[InlineData("0", "12", "12345678", "0## ########")]
[InlineData("0", "12", "123456789", "0## #########")]
[InlineData("0", "12", "1234567890", "0## ##########")]
[InlineData("0", "123", "1234", "0### ####")]
[InlineData("0", "123", "12345", "0### #####")]
[InlineData("0", "123", "123456", "0### ######")]
[InlineData("0", "123", "1234567", "0### #######")]
[InlineData("0", "123", "12345678", "0### ########")]
[InlineData("0", "123", "123456789", "0### #########")]
[InlineData("0", "123", "1234567890", "0### ##########")]
[InlineData("0", "1234", "1234", "0#### ####")]
[InlineData("0", "1234", "12345", "0#### #####")]
[InlineData("0", "1234", "123456", "0#### ######")]
[InlineData("0", "1234", "1234567", "0#### #######")]
[InlineData("0", "1234", "12345678", "0#### ########")]
[InlineData("0", "1234", "123456789", "0#### #########")]
[InlineData("0", "1234", "1234567890", "0#### ##########")]
[InlineData("0", "12345", "1234", "0##### ####")]
[InlineData("0", "12345", "12345", "0##### #####")]
[InlineData("0", "12345", "123456", "0##### ######")]
[InlineData("0", "12345", "1234567", "0##### #######")]
[InlineData("0", "12345", "12345678", "0##### ########")]
[InlineData("0", "12345", "123456789", "0##### #########")]
[InlineData("0", "12345", "1234567890", "0##### ##########")]
public void GetFormat_National_With_TrunkPrefix_With_Ndc(string trunkPrefix, string ndc, string sn, string expectedMask) =>
Assert.Equal(
expectedMask,
SimplePhoneNumberFormatProvider.Default.GetFormat(
TestHelper.CreateNonGeographicPhoneNumber(trunkPrefix, ndc, sn), false));

[Theory]
[InlineData("0", "1", "1234", "(0#) ####")]
[InlineData("0", "1", "12345", "(0#) #####")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void Parse_NonGeographicPhoneNumber_SharedCost()
Assert.Equal("28000", nonGeographicPhoneNumber.SubscriberNumber);
Assert.Equal(PhoneNumberKind.NonGeographicPhoneNumber, nonGeographicPhoneNumber.Kind);
}

[Fact] // This scenario exists within Germany
public void Parse_When_Nsn_Shorter_Than_Some_Ndc_Lengths()
{
Expand Down
Loading

0 comments on commit ab5f58b

Please sign in to comment.