Skip to content

Commit

Permalink
allow nullable string o TryParse methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Feb 10, 2023
1 parent 5f5b326 commit a428728
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Cnpj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static Cnpj Parse(string value)
/// contains a valid Cnpj. If the method returns false, result equals Empty.
/// </param>
/// <returns> true if the parse operation was successful; otherwise, false.</returns>
public static bool TryParse(string value, out Cnpj result)
public static bool TryParse(string? value, out Cnpj result)
{
var normalized = Format(value);
if (!Validate(normalized))
Expand Down
2 changes: 1 addition & 1 deletion src/Cpf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static Cpf Parse(string value)
/// contains a valid Cpf. If the method returns false, result equals Empty.
/// </param>
/// <returns> true if the parse operation was successful; otherwise, false.</returns>
public static bool TryParse(string value, out Cpf result)
public static bool TryParse(string? value, out Cpf result)
{
var normalized = Format(value, withMask: false);
if (!Validate(normalized))
Expand Down
2 changes: 1 addition & 1 deletion src/CpfCnpj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static CpfCnpj Parse(string value)
/// contains a valid CpfCnpj. If the method returns false, result equals Empty.
/// </param>
/// <returns> true if the parse operation was successful; otherwise, false.</returns>
public static bool TryParse(string value, out CpfCnpj result)
public static bool TryParse(string? value, out CpfCnpj result)
{
var type = Validate(value);
if (type is null)
Expand Down
13 changes: 11 additions & 2 deletions tests/BrazilModels.Tests/CnpjTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace BrazilModels.Tests;

[TestFixture]
public class CnpjTests
{
[Test]
Expand Down Expand Up @@ -49,7 +50,8 @@ public void ShouldCompareAsString(ValidCnpj first, ValidCnpj second)
{
var cnpj1 = new Cnpj(first);
var cnpj2 = new Cnpj(second);
var strCompare = string.Compare(first.Cleared, second.Cleared, StringComparison.OrdinalIgnoreCase);
var strCompare = string.Compare(first.Cleared, second.Cleared,
StringComparison.OrdinalIgnoreCase);
cnpj1.CompareTo(cnpj2).Should().Be(strCompare);
}

Expand Down Expand Up @@ -187,12 +189,19 @@ public void ShouldCreateUnformattedCnpj(CleanCnpj input)
}

[PropertyTest]
public void ShouldThrowInvalidFormattedCnpj(InvalidCnpj input)
public void ShouldReturnFalseForInvalidFormattedCnpj(InvalidCnpj input)
{
Cnpj.TryParse(input.Value, out var cnpj).Should().BeFalse();
cnpj.Value.Should().Be(Cnpj.Empty);
}

[Test]
public void ShouldReturnFalseForNullString()
{
Cnpj.TryParse(null, out var cnpj).Should().BeFalse();
cnpj.Value.Should().Be(Cnpj.Empty);
}

[PropertyTest]
public void ShouldThrowInvalidUnformattedCnpj(InvalidCnpj input)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace BrazilModels.Tests;

public class TaxIdTests
[TestFixture]
public class CpfCnpjTests
{
[Test]
public void ShouldHaveEmptyFormattedTaxId() =>
Expand All @@ -14,7 +15,7 @@ public void ShouldHaveEmptyTaxId() =>
public void NewTaxIdShouldBeEmpty() =>
new CpfCnpj().Should().Be(CpfCnpj.Empty);

public class TaxIdCnpjTests
public class CnpjTests
{
[PropertyTest]
public void ShouldHaveCnpjType(ValidCnpj cnpj) =>
Expand Down Expand Up @@ -52,7 +53,8 @@ public void ShouldCompareAsString(ValidCnpj first, ValidCnpj second)
{
var cnpj1 = new CpfCnpj(first);
var cnpj2 = new CpfCnpj(second);
var strCompare = string.Compare(first.Cleared, second.Cleared, StringComparison.OrdinalIgnoreCase);
var strCompare = string.Compare(first.Cleared, second.Cleared,
StringComparison.OrdinalIgnoreCase);
cnpj1.CompareTo(cnpj2).Should().Be(strCompare);
}

Expand Down Expand Up @@ -167,12 +169,19 @@ public void ShouldCreateUnformattedTaxId(CleanCnpj input)
}

[PropertyTest]
public void ShouldThrowInvalidFormattedTaxId(InvalidCnpj input)
public void ShouldReturnFalseForInvalidFormattedTaxId(InvalidCnpj input)
{
CpfCnpj.TryParse(input.Value, out var cnpj).Should().BeFalse();
cnpj.Value.Should().Be(CpfCnpj.Empty);
}

[Test]
public void ShouldReturnFalseForNullString()
{
CpfCnpj.TryParse(null, out var cnpj).Should().BeFalse();
cnpj.Value.Should().Be(CpfCnpj.Empty);
}

[PropertyTest]
public void ShouldThrowInvalidUnformattedTaxId(InvalidCnpj input)
{
Expand All @@ -182,9 +191,8 @@ public void ShouldThrowInvalidUnformattedTaxId(InvalidCnpj input)
}
}

public class TaxIdCpfTests
public class CpfTests
{

[PropertyTest]
public void ShouldHaveCnpjType(ValidCpf cpf) =>
new CpfCnpj(cpf).Type.Should().Be(DocumentType.CPF);
Expand Down Expand Up @@ -221,7 +229,8 @@ public void ShouldCompareAsString(ValidCpf first, ValidCpf second)
{
var cpf1 = new CpfCnpj(first);
var cpf2 = new CpfCnpj(second);
var strCompare = string.Compare(first.Cleared, second.Cleared, StringComparison.OrdinalIgnoreCase);
var strCompare = string.Compare(first.Cleared, second.Cleared,
StringComparison.OrdinalIgnoreCase);
cpf1.CompareTo(cpf2).Should().Be(strCompare);
}

Expand Down
13 changes: 11 additions & 2 deletions tests/BrazilModels.Tests/CpfTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace BrazilModels.Tests;

[TestFixture]
public class CpfTests
{
[Test]
Expand Down Expand Up @@ -46,7 +47,8 @@ public void ShouldCompareAsString(ValidCpf first, ValidCpf second)
{
var cpf1 = new Cpf(first);
var cpf2 = new Cpf(second);
var strCompare = string.Compare(first.Cleared, second.Cleared, StringComparison.OrdinalIgnoreCase);
var strCompare = string.Compare(first.Cleared, second.Cleared,
StringComparison.OrdinalIgnoreCase);
cpf1.CompareTo(cpf2).Should().Be(strCompare);
}

Expand Down Expand Up @@ -185,12 +187,19 @@ public void ShouldCreateUnformattedCpf(CleanCpf input)
}

[PropertyTest]
public void ShouldThrowInvalidFormattedCpf(InvalidCpf input)
public void ShouldReturnFalseForInvalidFormattedCpf(InvalidCpf input)
{
Cpf.TryParse(input.Value, out var cpf).Should().BeFalse();
cpf.Value.Should().Be(Cpf.Empty);
}

[Test]
public void ShouldReturnFalseForNullString()
{
Cpf.TryParse(null, out var cnpj).Should().BeFalse();
cnpj.Value.Should().Be(Cpf.Empty);
}

[PropertyTest]
public void ShouldThrowInvalidUnformattedCpf(InvalidCpf input)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/BrazilModels.Tests/TypeConvertersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using BrazilModels;
using BrazilModels.Tests.Utils;

#pragma warning disable S2187
namespace BrazilModels.Tests;

[TestFixture]
public class TypeConverterTests : BaseTest
{
public class CpfTests
Expand Down Expand Up @@ -102,7 +104,7 @@ public void ConvertToShouldWorkString(ValidCnpj input)
}
}

public class TaxIdTests
public class CpfCnpjTests
{
[Test]
public void ConverterIdValidShouldBeTrueToString()
Expand Down

0 comments on commit a428728

Please sign in to comment.