Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating non-scalar values #35

Closed
russel opened this issue Jun 17, 2016 · 5 comments
Closed

Generating non-scalar values #35

russel opened this issue Jun 17, 2016 · 5 comments

Comments

@russel
Copy link

russel commented Jun 17, 2016

The property-based testing seems to be able to automatically generate integers (and I assume floats, etc), but I need to be able to generate sets of strings. I am guessing this is handled as yet as I am getting lots of incomprehensible messages when I try it.

Is there an idiomatic way of handling this immediately?

@sksamuel
Copy link
Member

Strings should work , can you paste up some sample code with errosr?

@russel
Copy link
Author

russel commented Jun 17, 2016

     property("loading a file correctly creates the dictionary").
          forAll{s: Set<String> ->
            val w = s
            var result = true
            w.forEach{item ->
              if (!(item in s)) {
                result = false
              }
            }
            result
          }
    }

seems to generate:

Error:(22, 1) Kotlin: Expecting a top level declaration

@sksamuel
Copy link
Member

Oh I see, a Set of Strings. Yes that's not supported natively (but should be), but you can make your own instance of Gen and then pass it in manually.

@russel
Copy link
Author

russel commented Jun 18, 2016

For generating List<String> I have done:

fun generateWordlikeString(max:Int):Gen<String> = object : Gen<String> {
  override fun generate(): String {
    val data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtsuvwxyz-"
    return (1..Gen.choose(1, max).generate()).map{data[Gen.choose(1, data.length).generate()]}.joinToString("")
  }
}

fun generateListOfWordlikeStrings(max:Int):Gen<List<String>> = object : Gen<List<String>> {
  override fun generate(): List<String> = (1..Gen.choose(1, max).generate()).map{generateWordlikeString(10).generate()}
}

would this be seen as idiomatic KotlinTest?

@sksamuel
Copy link
Member

Yes it looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants