Skip to content

Commit

Permalink
Develop (#22)
Browse files Browse the repository at this point in the history
* Testes (#20)

* CNH validation

* teste

* Fix cnh validation
  • Loading branch information
lira92 authored Nov 26, 2019
1 parent 25f7d70 commit 069be88
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ bld/
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.vs/
.history
7 changes: 7 additions & 0 deletions src/Flunt.Br/Extensions/PersonContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ public static Contract IsVoterDocument(this Contract contract, string value, str
contract.AddNotification(property, message);
return contract;
}

public static Contract IsCnh(this Contract contract, string value, string property, string message)
{
if(string.IsNullOrEmpty(value) || !new Cnh().Validate(value))
contract.AddNotification(property,message);
return contract;
}
}
}
52 changes: 52 additions & 0 deletions src/Flunt.Br/Validations/Cnh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Flunt.Br.Document.Interfaces;

namespace Flunt.Br.Validations
{
internal class Cnh : IValidate
{
public bool Validate(string valor)
{
bool isValid = false;

var Cnh = valor;

var FirstChar = Cnh[0];

if (Cnh.Length == 11 && Cnh != new string('1', 11))
{
var dsc = 0;
var v = 0;

for (int i = 0, j = 9; i < 9; i++, j--)
{
v += (Convert.ToInt32(Cnh[i].ToString()) * j);
}

var vl1 = v % 11;

if (vl1 >= 10)
{
vl1 = 0;
dsc = 2;
}

v = 0;

for (int i = 0, j = 1; i < 9; ++i, ++j)
{
v += (Convert.ToInt32(Cnh[i].ToString()) * j);
}

var x = v % 11;
var vl2 = (x >= 10) ? 0 : x - dsc;

isValid = vl1.ToString() + vl2.ToString() == Cnh.Substring(Cnh.Length - 2, 2);

}

return isValid;

}
}
}
24 changes: 24 additions & 0 deletions tests/Flunt.Br.Tests/PersonContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,29 @@ public void IsVoterDocument_Valid(string value)
.IsVoterDocument(value, "document", "Invalid document");
Assert.IsTrue(right.Valid);
}


[TestMethod]
[DataRow("668247690132")]
[DataRow("333438450601")]
[DataRow("6568351232550")]
[DataRow(null)]
public void IsCnh_Invalid(string value)
{
var wrong = new Contract()
.IsCnh(value, "document", "Invalid document");
Assert.IsFalse(wrong.Valid);
}

[TestMethod]
[DataRow("09974348506")]
[DataRow("32983022807")]
[DataRow("93642298453")]
public void IsCnh_Valid(string value)
{
var right = new Contract()
.IsCnh(value, "document", "Invalid document");
Assert.IsTrue(right.Valid);
}
}
}

0 comments on commit 069be88

Please sign in to comment.