Skip to content

Instantly share code, notes, and snippets.

@JohanneA
Created November 22, 2021 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohanneA/80edf07f7976566fcb102db7638b418c to your computer and use it in GitHub Desktop.
Save JohanneA/80edf07f7976566fcb102db7638b418c to your computer and use it in GitHub Desktop.
Kotlin test DSL
fun buildShipment(
buildShipment: ShipmentTestDataBuilder.() -> Unit
): Shipment {
val shipmentBuilder = ShipmentTestDataBuilder()
shipmentBuilder.buildShipment()
return shipmentBuilder.build()
}
class ShipmentTestDataBuilder(
var id: Long = 1L,
var boxes: List<Box> = emptyList()
) {
// This is the builder for the nested boxes
inline fun buildBoxes(buildBoxes: BoxTestDataBuilder.() -> Unit) {
val boxBuilder = BoxTestDataBuilder()
boxBuilder.buildBoxes()
addBox(boxBuilder.build())
}
fun addBox(box: Box) {
this.boxes = listOf(box) + boxes
}
fun build(): Shipment {
return Shipment(
id = id,
boxes = boxes
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment