Skip to content

Commit

Permalink
fix(cep): formato esperado
Browse files Browse the repository at this point in the history
Added tests to validate cep
  • Loading branch information
brunobritodev committed Aug 14, 2021
1 parent 8b39ac6 commit 127149c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/NetDevPack.Brasil/Documentos/CNPJ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Cnpj

public Cnpj(string numero)
{
Numero = numero.OnlyNumbers(numero);
Numero = numero.OnlyNumbers();
if (!EstaValido()) throw new DomainException("CNPJ Invalido");
}

Expand Down
2 changes: 1 addition & 1 deletion src/NetDevPack.Brasil/Documentos/Cpf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Cpf

public Cpf(string numero)
{
Numero = numero.OnlyNumbers(numero);
Numero = numero.OnlyNumbers();
if (!EstaValido()) throw new DomainException("CPF Invalido");
}

Expand Down
6 changes: 3 additions & 3 deletions src/NetDevPack.Brasil/Documentos/Validacao/CnpjValidador.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using NetDevPack.Utilities;
using NetDevPack.Utilities;
using System.Collections.Generic;

namespace NetDevPack.Brasil.Documentos.Validacao
{
Expand All @@ -21,7 +21,7 @@ public class CnpjValidador
"99999999999999"
};

public CnpjValidador(string cnpj) => _cpnjTratado = cnpj.OnlyNumbers(cnpj);
public CnpjValidador(string cnpj) => _cpnjTratado = cnpj.OnlyNumbers();

public bool EstaValido()
{
Expand Down
6 changes: 3 additions & 3 deletions src/NetDevPack.Brasil/Documentos/Validacao/CpfValidador.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using NetDevPack.Utilities;
using NetDevPack.Utilities;
using System.Collections.Generic;

namespace NetDevPack.Brasil.Documentos.Validacao
{
Expand All @@ -21,7 +21,7 @@ public class CpfValidador
"99999999999"
};

public CpfValidador(string numero) => _cpfTratado = numero.OnlyNumbers(numero);
public CpfValidador(string numero) => _cpfTratado = numero.OnlyNumbers();

public bool EstaValido()
{
Expand Down
16 changes: 6 additions & 10 deletions src/NetDevPack.Brasil/Localizacao/Cep.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using NetDevPack.Brasil.Localizacao.Validacao;
using NetDevPack.Brasil.Localizacao.Validacao;
using NetDevPack.Domain;
using NetDevPack.Utilities;
using System;

namespace NetDevPack.Brasil.Localizacao
{
Expand Down Expand Up @@ -63,13 +62,10 @@ public class Cep : IEquatable<Cep>
/// <exception cref="DomainException">CEP (Código de Endereçamento Postal) inválido.</exception>
public Cep(string codigo)
{
Codigo = codigo?.OnlyNumbers(codigo);
_validador = new CepValidador(Codigo);
if (!EstaValido())
{
throw new DomainException("CEP Inválido");
}
PreencherEstrutura();
Codigo = codigo;
_validador = new CepValidador(codigo);
if (EstaValido())
PreencherEstrutura();
}

/// <inheritdoc/>
Expand Down
13 changes: 11 additions & 2 deletions src/NetDevPack.Brasil/Localizacao/Validacao/CepValidador.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetDevPack.Utilities;
using System.Text.RegularExpressions;

namespace NetDevPack.Brasil.Localizacao.Validacao
{
Expand All @@ -16,19 +17,27 @@ public class CepValidador
/// </summary>
private readonly string _cepTratado;

private readonly string _cepOriginal;

private readonly Regex _formatoEsperado = new Regex(@"^(\d{5}-\d{3})|(\d{8})$", RegexOptions.Compiled);

/// <summary>
/// Inicializa uma instância da classe <see cref="CepValidador"/>.
/// </summary>
/// <param name="codigo">O CEP (Código de Endereçamento Postal).</param>
public CepValidador(string codigo) => _cepTratado = codigo.OnlyNumbers(codigo);
public CepValidador(string codigo)
{
_cepTratado = codigo.OnlyNumbers();
_cepOriginal = codigo;
}

/// <summary>
/// Verifica se o CEP atende o critério de tamanho definido.
/// </summary>
/// <returns><see cref="true"/> se o CEP atende o critério de tamanho definido; caso contrário, <see cref="false"/>.</returns>
public bool EstaValido()
{
return PossuiTamanhoValido();
return PossuiTamanhoValido() && _formatoEsperado.IsMatch(_cepOriginal);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NetDevPack.Brasil/NetDevPack.Brasil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NetDevPack" Version="5.0.3" />
<PackageReference Include="NetDevPack" Version="5.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
36 changes: 36 additions & 0 deletions tests/NetDevPack.Brasil.Tests/Localizacao/CepTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using FluentAssertions;
using NetDevPack.Brasil.Localizacao;
using Xunit;

namespace NetDevPack.Brasil.Tests.Localizacao
{
public class CepTests
{

[Theory]
[InlineData("87970-000")]
[InlineData("86039-215")]
[InlineData("01000-000")]
[InlineData("01000000")]
[InlineData("87970000")]
[InlineData("86039215")]
public void Should_Validate_Ceps(string codigo)
{
var cep = new Cep(codigo);
cep.EstaValido().Should().BeTrue();
}

[Theory]
[InlineData("87970O00")]
[InlineData("860321o5")]
[InlineData("0100000")]
[InlineData("01-00000")]
[InlineData("879aoe700^0-0")]
[InlineData("8603a9215")]
public void Should_Not_Validate_Ceps(string codigo)
{
var cep = new Cep(codigo);
cep.EstaValido().Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Expand Down

0 comments on commit 127149c

Please sign in to comment.