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 / dynamic-stack.c
Last active April 26, 2018 10:01
A dynamic stack data structure made in C
#include <stdlib.h>
#include <stdio.h>
#define DEFAULT_SIZE 16
int no_of_words = 0; //Number of elements in array
int array_size = DEFAULT_SIZE;
int *stack; //Stack pointer
void init_stack() {
stack = malloc(sizeof(uint32_t) * DEFAULT_SIZE);
@JohanneA
JohanneA / number_guessing_game.rb
Created September 20, 2019 17:38
A number guessing game in ruby, built during the Girl Code Amsterdam Meetup on the 18. September 2019
def play_game(start_range, end_range)
computer_guess = rand(start_range..end_range)
user_guesses = []
puts "Guess a number"
user_guess = gets.chomp.to_i
while user_guess != computer_guess
if user_guesses.include?(user_guess)
@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"))
)
)
@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 / 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 / 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 / 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 / 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 / override-shipment.kt
Created November 22, 2021 15:05
Kotlin test DSL
fun override(
buildShipment: ShipmentTestBuilder.() -> Unit
): ShipmentTestBuilder {
this.buildShipment()
return this
}
@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)