Skip to content

Commit

Permalink
♻️ Add isValid property to CPF and CNPJ
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucien committed Jul 8, 2024
1 parent 96f08ac commit fe387f8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
11 changes: 10 additions & 1 deletion Sources/NadaADeclarar/CNPJ.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,22 @@ public struct CNPJ: NadaADeclararNumberProtocol, Generatable, NumberParsedInfoIn
return numberParsedInfo.parts.dropLast().last! // swiftlint:disable:this force_unwrapping
}

/**
Indicates whether the CNPJ is valid.
- Returns: `true` if the CNPJ is valid, `false` otherwise.
*/
public var isValid: Bool {
isValid(allSameDigitsAreValid: false)
}

/**
Validates the CNPJ using the specified algorithm.
- Parameter allSameDigitsAreValid: A flag indicating whether a CNPJ with all same digits is considered valid. Defaults to `false`.
- Returns: `true` if the CNPJ is valid; otherwise, `false`.
*/
public func isValid(allSameDigitsAreValid: Bool = false) -> Bool {
func isValid(allSameDigitsAreValid: Bool = false) -> Bool {
let algorithm = Validator.ValidationAlgorythm.fazenda { (basicNumber: String) -> (Int) in
NumberParsedType.calcWeightSum(basicNumber: basicNumber)
}
Expand Down
21 changes: 15 additions & 6 deletions Sources/NadaADeclarar/CPF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ public struct CPF: NadaADeclararNumberProtocol, Generatable, NumberParsedInfoInt
return cpf
}

/**
Indicates whether the CPF is valid.
- Returns: `true` if the CPF is valid, `false` otherwise.
*/
public var isValid: Bool {
isValid(validationAlgorythm: .simple, allSameDigitsAreValid: false)
}

public static let fazendaAlgorythm: Validator.ValidationAlgorythm =
Validator.ValidationAlgorythm.fazenda { (basicNumber: String) -> (Int) in
NumberParsedType.calcWeightSum(basicNumber: basicNumber)
}

/**
Validates the CPF.
Expand All @@ -88,14 +102,9 @@ public struct CPF: NadaADeclararNumberProtocol, Generatable, NumberParsedInfoInt
- allSameDigitsAreValid: A flag indicating whether a CPF with all same digits is valid. Defaults to `false`.
- Returns: `true` if the CPF is valid, `false` otherwise.
*/
public func isValid(validationAlgorythm: Validator.ValidationAlgorythm = .simple,
func isValid(validationAlgorythm: Validator.ValidationAlgorythm = .simple,
allSameDigitsAreValid: Bool = false) -> Bool {
return validator.isValid(validationAlgorythm: validationAlgorythm,
allSameDigitsAreValid: allSameDigitsAreValid)
}

public static let fazendaAlgorythm: Validator.ValidationAlgorythm =
Validator.ValidationAlgorythm.fazenda { (basicNumber: String) -> (Int) in
NumberParsedType.calcWeightSum(basicNumber: basicNumber)
}
}
4 changes: 2 additions & 2 deletions Tests/NadaADeclararTests/CNPJTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class CNPJTests: XCTestCase, ListImporter {

func testValidCNPJ() throws {
for cnpj in self.cnpjList {
XCTAssertTrue(cnpj.isValid())
XCTAssertTrue(cnpj.isValid)
}
}

func testGenerateCNPJ() {

for _ in 0...1000 {
XCTAssertTrue(CNPJ.generate().isValid())
XCTAssertTrue(CNPJ.generate().isValid)
}
}

Expand Down
58 changes: 29 additions & 29 deletions Tests/NadaADeclararTests/CPFTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,43 @@ class CPFTests: XCTestCase, ListImporter {

func testInvalidCPFsUsingFazendaAlgorythm() throws {

XCTAssertFalse(try CPF(number: "000.000.000-00").isValid())
XCTAssertFalse(try CPF(number: "111.111.111-11").isValid())
XCTAssertFalse(try CPF(number: "222.222.222-22").isValid())
XCTAssertFalse(try CPF(number: "333.333.333-33").isValid())
XCTAssertFalse(try CPF(number: "444.444.444-44").isValid())
XCTAssertFalse(try CPF(number: "555.555.555-55").isValid())
XCTAssertFalse(try CPF(number: "666.666.666-66").isValid())
XCTAssertFalse(try CPF(number: "777.777.777-77").isValid())
XCTAssertFalse(try CPF(number: "888.888.888-88").isValid())
XCTAssertFalse(try CPF(number: "999.999.999-99").isValid())
XCTAssertFalse(try CPF(number: "000.000.000-00").isValid)
XCTAssertFalse(try CPF(number: "111.111.111-11").isValid)
XCTAssertFalse(try CPF(number: "222.222.222-22").isValid)
XCTAssertFalse(try CPF(number: "333.333.333-33").isValid)
XCTAssertFalse(try CPF(number: "444.444.444-44").isValid)
XCTAssertFalse(try CPF(number: "555.555.555-55").isValid)
XCTAssertFalse(try CPF(number: "666.666.666-66").isValid)
XCTAssertFalse(try CPF(number: "777.777.777-77").isValid)
XCTAssertFalse(try CPF(number: "888.888.888-88").isValid)
XCTAssertFalse(try CPF(number: "999.999.999-99").isValid)
}

func testValidFazenda() {
XCTAssertTrue(try CPF(number: "60258671432").isValid())
XCTAssertTrue(try CPF(number: "60258671432").isValid)
}

func testValidCPFsUsingFazendaAlgorythm() throws {

XCTAssertTrue(try CPF(number: "100.000.987-44").isValid())
XCTAssertTrue(try CPF(number: "520.852.930-00").isValid())
XCTAssertTrue(try CPF(number: "000.000.001-91").isValid())
XCTAssertTrue(try CPF(number: "544.780.212-10").isValid())
XCTAssertTrue(try CPF(number: "813.219.583-31").isValid())
XCTAssertTrue(try CPF(number: "602.586.714-32").isValid())
XCTAssertTrue(try CPF(number: "334.028.435-01").isValid())
XCTAssertTrue(try CPF(number: "545.462.246-05").isValid())
XCTAssertTrue(try CPF(number: "135.991.648-27").isValid())
XCTAssertTrue(try CPF(number: "286.714.209-14").isValid())
XCTAssertTrue(try CPF(number: "100.000.987-44").isValid)
XCTAssertTrue(try CPF(number: "520.852.930-00").isValid)
XCTAssertTrue(try CPF(number: "000.000.001-91").isValid)
XCTAssertTrue(try CPF(number: "544.780.212-10").isValid)
XCTAssertTrue(try CPF(number: "813.219.583-31").isValid)
XCTAssertTrue(try CPF(number: "602.586.714-32").isValid)
XCTAssertTrue(try CPF(number: "334.028.435-01").isValid)
XCTAssertTrue(try CPF(number: "545.462.246-05").isValid)
XCTAssertTrue(try CPF(number: "135.991.648-27").isValid)
XCTAssertTrue(try CPF(number: "286.714.209-14").isValid)

XCTAssertTrue(try CPF(number: "728.793.773-58").isValid())
XCTAssertTrue(try CPF(number: "704.881.802-60").isValid())
XCTAssertTrue(try CPF(number: "508.584.317-77").isValid())
XCTAssertTrue(try CPF(number: "517.531.566-88").isValid())
XCTAssertTrue(try CPF(number: "728.793.773-58").isValid)
XCTAssertTrue(try CPF(number: "704.881.802-60").isValid)
XCTAssertTrue(try CPF(number: "508.584.317-77").isValid)
XCTAssertTrue(try CPF(number: "517.531.566-88").isValid)

XCTAssertTrue(try CPF(number: "050.505.834-03").isValid())
XCTAssertTrue(try CPF(number: "337.718.534-86").isValid())
XCTAssertTrue(try CPF(number: "827.525.778-69").isValid())
XCTAssertTrue(try CPF(number: "050.505.834-03").isValid)
XCTAssertTrue(try CPF(number: "337.718.534-86").isValid)
XCTAssertTrue(try CPF(number: "827.525.778-69").isValid)
}

func testValidCPFsUsingSimpleAlgorythm() throws {
Expand Down Expand Up @@ -163,7 +163,7 @@ class CPFTests: XCTestCase, ListImporter {
func testCPFGeneration() {
for _ in 1...1000 {
let cpf = CPF.generate()
XCTAssertTrue(cpf.isValid())
XCTAssertTrue(cpf.isValid)
}
}

Expand Down

0 comments on commit fe387f8

Please sign in to comment.