serpro69 / kotlin-faker

Port of a popular ruby faker gem written in kotlin. Generate realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data anonymization purposes.
https://serpro69.github.io/kotlin-faker/
MIT License
453 stars 42 forks source link

Allow randomClassInstance to directly use predefined generators #202

Closed serpro69 closed 8 months ago

serpro69 commented 8 months ago

Closes #201

Still not sure what the precedence should be, but I think it makes more sense to first try a default instance with public constructor. But I could probably as easy argue for the other way around , so I'm really just winging it here :D

serpro69 commented 8 months ago

OK, so one reason to consider returning "predefined instance" before "default instance" would be to allow generating abstract classes:

abstract class TestAbstractClass(
    val id: Int,
    val name: String,
)

val testAbsClass = faker{}.randomProvider.randomClassInstance<TestAbstractClass> {
    typeGenerator<TestAbstractClass> {
        object : TestAbstractClass(42, "Deep Thought") {}
    }
}

testAbsClass.id shouldBe 42
testAbsClass.name shouldBe "Deep Thought"

The question is - is it a good enough reason? Would anyone ever want to directly initialize an abstract class?