Skip to content

Instantly share code, notes, and snippets.

View JohanneA's full-sized avatar

Johanne Andersen JohanneA

  • Kambr
  • Amsterdam
View GitHub Profile
@JohanneA
JohanneA / shipment-test-w-dsl.kt
Created November 22, 2021 15:09
Kotlin test DSL
fun getShipmentWithOneBox(): ShipmentBuilder {
return buildShipment {
id = 1L
buildBoxes {
id = 1L
buildProduct {
id = 1L
name = "Pink Fussy Jacket"
}
}
@JohanneA
JohanneA / shipment-test-wo-dsl.kt
Created November 22, 2021 15:08
Kotlin test DSL
fun getShipmentWithOneBox(): Shipment {
return Shipment(
id = 1L,
boxes = listOf(
Box(
id = 1L,
contents = listOf(
Product(id = 1L, name = "Pink Fuzzy Jacket"))
)
)
@JohanneA
JohanneA / nested-override-logic.kt
Last active November 22, 2021 15:06
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)
@JohanneA
JohanneA / override-shipment.kt
Created November 22, 2021 15:05
Kotlin test DSL
fun override(
buildShipment: ShipmentTestBuilder.() -> Unit
): ShipmentTestBuilder {
this.buildShipment()
return this
}
@JohanneA
JohanneA / shipment-builder.kt
Created November 22, 2021 15:04
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()
@JohanneA
JohanneA / dsl-shipment-ex.kt
Created November 22, 2021 15:04
Kotlin test DSL
fun shipmentWithOneBoxWithOneProduct(): Shipment = buildShipment {
id = 1L
buildBox {
id = 1L
buildProduct {
id = 1L
name = "Pink Fuzzy Jacket"
}
}
}.build()
@JohanneA
JohanneA / lambda-w-receiver.kt
Created November 22, 2021 15:01
Kotlin test DSL
// Lambda with receiver
fun buildString (
builderAction: StringBuilder.() -> Unit // Note the location of the parentheses
) : String {
val stringBuilder = StringBuilder()
stringBuilder.builderAction() // This is now a method on the string builder
return stringBuilder.toString()
}
val string = buildString {
append("Hello, ") // Note we no longer need the qualifyer in front
@JohanneA
JohanneA / lambda-wo-receiver.kt
Created November 22, 2021 15:00
Kotlin test DSL
//Lambda without receiver
fun buildString (
builderAction: (StringBuilder) -> Unit
) : String {
val stringBuilder = StringBuilder()
builderAction(stringBuilder)
return stringBuilder.toString()
}
val string = buildString {
it.append("Hello, ") // Note the need for adding 'it' in front
@JohanneA
JohanneA / builder-pattern.kt
Created November 22, 2021 14:59
Kotlin test DSL
fun getShipmentWithOneBox(): Shipment {
return ShipmentBuilder()
.withId(1L)
.withBoxes(
BoxBuilder()
.withId(1L)
.withContents(
ProductBuilder()
.withId(1L)
.withName("Pink Fuzzy Jacket")
@JohanneA
JohanneA / object-mother.kt
Last active November 22, 2021 14:57
Kotlin Test DSL
fun getShipmentWithOneBox(): Shipment {
return Shipment(
id = 1L,
boxes = listOf(
Box(
id = 1L,
contents = listOf(
Product(id = 1L, name = "Pink Fuzzy Jacket"))
)
)