diff --git a/Sources/NadaADeclarar/CNPJ.swift b/Sources/NadaADeclarar/CNPJ.swift index 5adea7e..da96fc7 100644 --- a/Sources/NadaADeclarar/CNPJ.swift +++ b/Sources/NadaADeclarar/CNPJ.swift @@ -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) } diff --git a/Sources/NadaADeclarar/CPF.swift b/Sources/NadaADeclarar/CPF.swift index e0520c4..9a90963 100644 --- a/Sources/NadaADeclarar/CPF.swift +++ b/Sources/NadaADeclarar/CPF.swift @@ -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. @@ -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) - } } diff --git a/Tests/NadaADeclararTests/CNPJTests.swift b/Tests/NadaADeclararTests/CNPJTests.swift index 1db5afc..ad80452 100644 --- a/Tests/NadaADeclararTests/CNPJTests.swift +++ b/Tests/NadaADeclararTests/CNPJTests.swift @@ -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) } } diff --git a/Tests/NadaADeclararTests/CPFTests.swift b/Tests/NadaADeclararTests/CPFTests.swift index 2f4c36c..de705d6 100644 --- a/Tests/NadaADeclararTests/CPFTests.swift +++ b/Tests/NadaADeclararTests/CPFTests.swift @@ -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 { @@ -163,7 +163,7 @@ class CPFTests: XCTestCase, ListImporter { func testCPFGeneration() { for _ in 1...1000 { let cpf = CPF.generate() - XCTAssertTrue(cpf.isValid()) + XCTAssertTrue(cpf.isValid) } }