Created
November 22, 2021 15:04
-
-
Save JohanneA/80edf07f7976566fcb102db7638b418c to your computer and use it in GitHub Desktop.
Kotlin test DSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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