All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.spinnaker.spek.SpekExtensions.kt Maven / Gradle / Ivy

There is a newer version: 8.57.0
Show newest version
/*
 * Copyright 2018 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.spek

import arrow.core.partially2
import org.jetbrains.spek.api.dsl.SpecBody

/**
 * Grammar for nesting inside [given].
 */
fun SpecBody.and(description: String, body: SpecBody.() -> Unit) {
  group("and $description", body = body)
}

fun  SpecBody.where(description: String, vararg parameterSets: Single, block: SpecBody.(A) -> Unit) {
  parameterSets.forEach {
    val body = block.partially2(it.first)
    group(com.netflix.spinnaker.spek.describe(description, it.first), body = body)
  }
}

fun  SpecBody.where(description: String, vararg parameterSets: Pair, block: SpecBody.(A, B) -> Unit) {
  parameterSets.forEach {
    val body = block.partially2(it.first).partially2(it.second)
    group(com.netflix.spinnaker.spek.describe(description, it.first, it.second), body = body)
  }
}

fun  SpecBody.where(description: String, vararg parameterSets: Triple, block: SpecBody.(A, B, C) -> Unit) {
  parameterSets.forEach {
    val body = block.partially2(it.first).partially2(it.second).partially2(it.third)
    group(com.netflix.spinnaker.spek.describe(description, it.first, it.second, it.third), body = body)
  }
}

fun  SpecBody.where(description: String, vararg parameterSets: Quad, block: SpecBody.(A, B, C, D) -> Unit) {
  parameterSets.forEach {
    val body = block.partially2(it.first).partially2(it.second).partially2(it.third).partially2(it.fourth)
    group(com.netflix.spinnaker.spek.describe(description, it.first, it.second, it.third, it.fourth), body = body)
  }
}

private fun describe(description: String, vararg arguments: Any?) =
  String.format("where $description", *arguments)

data class Single(val first: A) {
  override fun toString() = "($first)"
}

data class Quad(val first: A, val second: B, val third: C, val fourth: D) {
  override fun toString() = "($first, $second, $third, $fourth)"
}

fun  values(first: A) =
  Single(first)

fun  values(first: A, second: B) =
  Pair(first, second)

fun  values(first: A, second: B, third: C) =
  Triple(first, second, third)

fun  values(first: A, second: B, third: C, fourth: D) =
  Quad(first, second, third, fourth)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy