Skip to content

Commit

Permalink
Merge pull request #232 from wmundev/feature/enable-nullable-for-test…
Browse files Browse the repository at this point in the history
…-project

feat: enable nullable for PhoneNumbers.Test project
  • Loading branch information
twcclegg authored Feb 23, 2024
2 parents 5fba9a6 + 11d74b5 commit 9a375a0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

max_line_length = 99999

# Xml project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_size = 2
Expand Down
4 changes: 4 additions & 0 deletions csharp/PhoneNumbers.Test/PhoneNumbers.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0'">
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PhoneNumbers\PhoneNumbers.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions csharp/PhoneNumbers.Test/TestBuildMetadataFromXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static XElement ParseXmlString(string xmlString)
{
using (var reader = new StringReader(xmlString))
{
return XDocument.Load(reader).Root;
return XDocument.Load(reader).Root ?? throw new Exception("Failed to ParseXmlString");
}
}

Expand Down Expand Up @@ -489,4 +489,4 @@ public void TestLoadGeneralDescSetsAllDescriptions()
Assert.Equal("\\d{9}", metadata.Uan.NationalNumberPattern);
}
}
}
}
25 changes: 22 additions & 3 deletions csharp/PhoneNumbers.Test/TestPhoneNumberMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ public void TestNonPlusPrefixedNumbersNotFoundForInvalidRegion()
// Does not start with a "+", we won't match it.
var iterable = phoneUtil.FindNumbers("1 456 764 156", RegionCode.ZZ);
var iterator = iterable.GetEnumerator();

Assert.False(iterator.MoveNext());
Assert.False(iterator.MoveNext());
iterator.Dispose();
Expand Down Expand Up @@ -885,7 +885,11 @@ private void AssertEqualRange(string text, int index, int start, int end)
*
* @param number the number to test and the corresponding region code to use
*/
#if NET6_0_OR_GREATER
private void DoTestFindInContext(string number, string? defaultCountry)
#else
private void DoTestFindInContext(string number, string defaultCountry)
#endif
{
FindPossibleInContext(number, defaultCountry);

Expand All @@ -894,7 +898,11 @@ private void DoTestFindInContext(string number, string defaultCountry)
FindValidInContext(number, defaultCountry);
}

#if NET6_0_OR_GREATER
private void FindPossibleInContext(string number, string? defaultCountry)
#else
private void FindPossibleInContext(string number, string defaultCountry)
#endif
{
var contextPairs = new List<NumberContext>
{
Expand Down Expand Up @@ -933,7 +941,11 @@ private void FindPossibleInContext(string number, string defaultCountry)
/**
* Tests valid numbers in contexts that fail for {@link Leniency#POSSIBLE}.
*/
#if NET6_0_OR_GREATER
private void FindValidInContext(string number, string? defaultCountry)
#else
private void FindValidInContext(string number, string defaultCountry)
#endif
{
var contextPairs = new List<NumberContext> {
// With other small numbers.
Expand All @@ -947,8 +959,11 @@ private void FindValidInContext(string number, string defaultCountry)
DoTestInContext(number, defaultCountry, contextPairs, PhoneNumberUtil.Leniency.VALID);
}

private void DoTestInContext(string number, string defaultCountry,
List<NumberContext> contextPairs, PhoneNumberUtil.Leniency leniency)
#if NET6_0_OR_GREATER
private void DoTestInContext(string number, string? defaultCountry, List<NumberContext> contextPairs, PhoneNumberUtil.Leniency leniency)
#else
private void DoTestInContext(string number, string defaultCountry, List<NumberContext> contextPairs, PhoneNumberUtil.Leniency leniency)
#endif
{
foreach (var context in contextPairs)
{
Expand Down Expand Up @@ -976,7 +991,11 @@ private void DoTestInContext(string number, string defaultCountry,
* Exhaustively searches for phone numbers from each index within {@code text} to test that
* finding matches always terminates.
*/
#if NET6_0_OR_GREATER
private void EnsureTermination(string text, string? defaultCountry, PhoneNumberUtil.Leniency leniency)
#else
private void EnsureTermination(string text, string defaultCountry, PhoneNumberUtil.Leniency leniency)
#endif
{
for (var index = 0; index <= text.Length; index++)
{
Expand Down
11 changes: 9 additions & 2 deletions csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,13 @@ public void TestConvertAlphaCharactersInNumber()
[Fact]
public void TestNormaliseNull()
{
#if NET6_0_OR_GREATER
const string? inputNumber = null;
#else
const string inputNumber = null;
#endif
var expectedOutput = string.Empty;
Assert.Equal(expectedOutput,
PhoneNumberUtil.Normalize(inputNumber));
Assert.Equal(expectedOutput, PhoneNumberUtil.Normalize(inputNumber));
}

[Fact]
Expand Down Expand Up @@ -2032,7 +2035,11 @@ public void TestParseNumbersMexico()
Assert.Equal(mxNumber, phoneUtil.Parse("045 33 1234-5678", RegionCode.MX));
}

#if NET6_0_OR_GREATER
private void VerifyFailure(string? number, string? regionCode, ErrorType type)
#else
private void VerifyFailure(string number, string regionCode, ErrorType type)
#endif
{
try
{
Expand Down

0 comments on commit 9a375a0

Please sign in to comment.