Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin test DSL
inline fun overrideBox(
id: Long,
buildBox: BoxTestDataBuilder.() -> Unit
) {
if (!boxIdExists(id)) {
throw IllegalArgumentException("Box with id $id does not exist")
}
val boxBuilder = BoxTestDataBuilder()
val existingBox = getBox(id)
boxBuilder.merge(existingBox)
boxBuilder.buildBox()
removeBoxes(id)
addBox(boxBuilder.build())
}
fun boxIdExists(boxId: Long): Boolean {
return this.boxes.find { it.id == boxId } != null
}
fun getBox(boxId: Long): Box {
return this.boxes.find { it.id == boxId }!!
}
fun removeBox(boxId: Long) {
val mutableBoxes = this.boxes.toMutableList()
mutableBoxes.removeIf { it.id == boxId }
this.boxes = mutableBoxes.toList()
}
// Merge function inside the box test data builder
class BoxTestDataBuilder(
var id: Long = 1L,
var contents: List<Product> = emptyList()
) {
//...
fun merge(box: Box): BoxTestDataBuilder {
this.id = box.id
this.contents = box.contents
return this
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment