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

use case: configure list size #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/kotlin/me/yujinyan/faktory/Factory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class Factory<T : Any>(private val kClass: KClass<T>, block: (Config.() -
}
}

public fun make(range: IntRange): Iterable<T> = make(range.random())

/**
* Generate sample data.
*
Expand Down
14 changes: 14 additions & 0 deletions src/test/kotlin/me/yujinyan/faktory/FactoryTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.yujinyan.faktory

import io.kotest.matchers.collections.shouldHaveAtLeastSize
import io.kotest.matchers.collections.shouldHaveAtMostSize
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
Expand Down Expand Up @@ -67,4 +69,16 @@ class FactoryTest {
Post::author / Author::name by { "Peter Parker" }
}.author.name shouldBe "Peter Parker"
}

@Test
fun `can configure list length`() {
val postFactory = factory<Post> {
Post::images by { factory<Image>().make(5..10) }
}

postFactory.make().images.apply {
shouldHaveAtMostSize(10)
shouldHaveAtLeastSize(5)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ data class Post(
val title: String,
val body: String,
val author: Author,
val images: List<Image>
)

data class Author(
val name: String
)

data class Image(
val url: String
)