Giter Club home page Giter Club logo

Comments (4)

serpro69 avatar serpro69 commented on May 24, 2024

Hi @artdk ,

I'm always interested in something that brings value :) but would like to first understand your use-case.
Do you actually want to generate an instance of Instant via randomClassInstance function? Or is instant a property in another class?

Because if it's the former, then it's not really going to be "random instance" of Instant as such, it will always return Instant.now() (or whatever you configure via typeGenerator). So why not just call Instant.now() wherever you need an instance of it, instead of faker.randomClassInstance<Instant>()? The result will be the same.

PS: I do get that you're using instant as an example here, but I'm not really seeing the use-case. So maybe you could elaborate on what you're trying to achieve.

from kotlin-faker.

serpro69 avatar serpro69 commented on May 24, 2024

This change is now in master and should be available in latest snapshot version after the build completes.
I want to give it a bit more thought before getting this into an RC version, but feel free to try out the snapshot and share your feedback, if you have any.
Thanks for suggesting this feature :)

from kotlin-faker.

artdk avatar artdk commented on May 24, 2024

Sorry for the delay,
The most pressing use case is when testing generic functions. For example we have an inline fun <reified T: Any> myFun<T>(p0: T): Boolean which contains logic using the values of member variables which may or may not exist in p0; if we define this highly simplified test

@Test
fun myTest() {
      testClass(Foo::class, true)
      testClass(Instant::class, true)
      testClass(Bar::class, false)
      ...
}

private inline fun <reified T: Any> testClass(klass: KClass<T>, expectedMyFunValue: Boolean) {
    val testVal = faker.randomProvider.randomClassInstance<T>()
    myFun(testVal) shouldBe expectedMyFunValue
}

This allows us to test myFun against a number of different types, but if one of those types has no public constructors then it cannot be tested using this generic method and instead we must provide an alternative generator for those specific cases

The other benefit here is consistency and control; for example in a given domain we have constraints on Instant, so we introduce a function randomConstrainedInstant() to generate Instants that fit these constraints; if we then want to test a function myFun(p0: Foo, p1: Instant, p2: Foo): Bar we can define the test variables as:

val p0 = faker.randomClassInstance<Foo>()
val p1 = randomConstrainedInstant()
val p2 = randomConstrainedInstant()
val result = myFun(p0, p1, p2) 
// assert on result comparing to p0, p1, and p2

This is much more consistent if we can just use faker.randomClassInstance<Instant>() for p1 and p2, and doesnt require that every developer has to remember all the cases where a a random object cannot be generated and all the exceptional random object generation functions that fill those gaps

I hope this helps elaborate on some of the motivation and cases that inspired this, and thanks for being so receptive to such a corner-case feature!

from kotlin-faker.

serpro69 avatar serpro69 commented on May 24, 2024

Thanks for the examples. I think this is a pretty valid use-case.

from kotlin-faker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.