Skip to content

Commit

Permalink
Migrate from KotlinTest to Kotest (#14)
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
LeoColman authored Aug 18, 2020
1 parent 7851b1a commit abc34b8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ repositories {

dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation(group = "io.kotlintest", name = "kotlintest-runner-junit5", version = "3.4.2")
testImplementation(group = "io.kotest", name = "kotest-runner-junit5-jvm", version = "4.2.0.RC2")
testImplementation(group = "io.kotest", name = "kotest-property-jvm", version = "4.2.0.RC2")
}

tasks.withType<KotlinCompile> {
Expand Down
76 changes: 38 additions & 38 deletions src/test/kotlin/top/colman/simplecpfvalidator/CpfValidatorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,56 @@
*/
package top.colman.simplecpfvalidator

import io.kotlintest.inspectors.forNone
import io.kotlintest.matchers.boolean.shouldBeFalse
import io.kotlintest.matchers.boolean.shouldBeTrue
import io.kotlintest.properties.Gen
import io.kotlintest.properties.forAll
import io.kotlintest.properties.forNone
import io.kotlintest.specs.FunSpec
import io.kotest.core.spec.style.FunSpec
import io.kotest.inspectors.forNone
import io.kotest.matchers.booleans.shouldBeFalse
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.property.Arb
import io.kotest.property.arbitrary.map
import io.kotest.property.arbitrary.string
import io.kotest.property.forAll

class CpfValidatorTest : FunSpec() {

init {
test("Should return false on blacklisted CPFs") {
blacklistedCpfs.forNone { it.shouldBeCpf() }
}
class CpfValidatorTest : FunSpec({
test("Should return false on blacklisted CPFs") {
blacklistedCpfs.forNone { it.shouldBeCpf() }
}

test("Should return true on valid CPFs") {
ValidCpfGenerator.forAll { cpf -> cpf.isCpf() }
}
test("Should return true on valid CPFs") {
ValidCpfGenerator.forAll { cpf -> cpf.isCpf() }
}

test("Should return false on random strings") {
Gen.string().forNone { cpf -> cpf.isCpf() }
}
test("Should return false on random strings") {
Arb.string().forAll { cpf -> !cpf.isCpf() }
}

test("Should return false on invalid but non-blacklisted CPF") {
knownInvalidCpfs.forNone { it.shouldBeCpf() }
}
test("Should return false on invalid but non-blacklisted CPF") {
knownInvalidCpfs.forNone { it.shouldBeCpf() }
}

test("Should sanitize the String given replaceable characters and still return true on valid cpfs") {
ValidCpfGenerator.map { "$it..--." }.forAll { cpf -> cpf.isCpf(listOf('.', '-')) }
}
test("Should sanitize the String given replaceable characters and still return true on valid cpfs") {
ValidCpfGenerator.map { "$it..--." }.forAll { cpf -> cpf.isCpf(listOf('.', '-')) }
}

test("Shouldn't sanitize unspecified characters") {
ValidCpfGenerator.map { "$it++" }.forNone { cpf -> cpf.isCpf(listOf('.', '-')) }
}
test("Shouldn't sanitize unspecified characters") {
ValidCpfGenerator.map { "$it++" }.forAll { cpf -> !cpf.isCpf(listOf('.', '-')) }
}

test("Should return true on valid Long typed CPF input") {
24865482385.isCpf().shouldBeTrue()
}
test("Should return true on valid Long typed CPF input") {
24865482385.isCpf().shouldBeTrue()
}

test("Should return false on blacklisted Long typed CPF input") {
11111111111.isCpf().shouldBeFalse()
}
test("Should return false on blacklisted Long typed CPF input") {
11111111111.isCpf().shouldBeFalse()
}

test("Should return false on invalid length of Long typed CPF input") {
999L.isCpf().shouldBeFalse()
}
test("Should return false on invalid length of Long typed CPF input") {
999L.isCpf().shouldBeFalse()
}
})

private fun String.shouldBeCpf() = this.isCpf().shouldBeTrue()

private fun String.shouldBeCpf() = this.isCpf().shouldBeTrue()
}

private val blacklistedCpfs = listOf(
"00000000000",
Expand Down
21 changes: 14 additions & 7 deletions src/test/kotlin/top/colman/simplecpfvalidator/ValidCpfGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/
package top.colman.simplecpfvalidator

import io.kotlintest.properties.Gen
import io.kotlintest.properties.generateInfiniteSequence
import io.kotest.property.Arb
import io.kotest.property.RandomSource
import io.kotest.property.Sample
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.next

object ValidCpfGenerator : Gen<String> {

override fun constants() = listOf(
object ValidCpfGenerator : Arb<String>() {

override fun edgecases() = listOf(
"10147788080",
"98503877007",
"57773940002",
Expand All @@ -30,14 +34,17 @@ object ValidCpfGenerator : Gen<String> {

// https://pt.wikipedia.org/wiki/D%C3%ADgito_verificador
// https://www.geradorcpf.com/algoritmo_do_cpf.htm
override fun random() = generateInfiniteSequence {
override fun values(rs: RandomSource) = generateSequence {
val digits = List(9) { randomDigit() }
val firstVerifierDigit = digits.firstVerifierDigit()
val secondVerifierDigit = digits.secondVerifierDigit(firstVerifierDigit)

digits.joinToString(separator = "") + "$firstVerifierDigit" + "$secondVerifierDigit"
Sample(
digits.joinToString(separator = "") + "$firstVerifierDigit" + "$secondVerifierDigit"
)
}


private fun List<Int>.firstVerifierDigit(): Int {
val weights = (10 downTo 2).toList()
return calculateVerifierDigit(weights, this)
Expand All @@ -59,4 +66,4 @@ object ValidCpfGenerator : Gen<String> {
}
}

private fun randomDigit() = Gen.from("0123456789".toList()).next().toString().toInt()
private fun randomDigit() = Arb.int(0, 9).next()

0 comments on commit abc34b8

Please sign in to comment.