Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Xunit;

namespace PhoneNumbers.Extensions.Test
Expand All @@ -15,6 +16,14 @@ public void ConvertFrom_ParsesString()
Assert.Equal(6192987704UL, number.NationalNumber);
}

[Fact]
public void ConvertFrom_InvalidString_ThrowsFormatException()
{
var ex = Assert.Throws<FormatException>(() => Converter.ConvertFrom("not-a-number"));

Assert.IsType<NumberParseException>(ex.InnerException);
}

[Fact]
public void ConvertFrom_MatchesTryParse_ForEqualityAndHashing()
{
Expand Down
18 changes: 15 additions & 3 deletions csharp/PhoneNumbers.Extensions/PhoneNumberTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
=> sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
=> value is string stringValue
? Util.Parse(stringValue, null)
: base.ConvertFrom(context, culture, value);
{
if (value is not string stringValue)
{
return base.ConvertFrom(context, culture, value);
}

try
{
return Util.Parse(stringValue, null);
}
catch (NumberParseException ex)
{
throw new FormatException($"'{stringValue}' is not a valid phone number.", ex);
}
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
Expand Down
2 changes: 1 addition & 1 deletion csharp/PhoneNumbers.MetadataBuilder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private static int BuildLocaleNames(string inputFile, string outputDir)
var byCountry = ParseLocaleText(inputFile);
foreach (var country in byCountry)
{
var outPath = Path.Join(outputDir, country.Key);
var outPath = Path.Join(outputDir, Path.GetFileName(country.Key));
using var gz = new GZipStream(File.Create(outPath), CompressionLevel.SmallestSize);
BuildPrefixMapFromBin.WriteLocaleNames(gz, country.Value);
}
Expand Down