run.qontract.core.pattern.Examples.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qontract-core Show documentation
Show all versions of qontract-core Show documentation
A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner
package run.qontract.core.pattern
import io.cucumber.messages.Messages
import io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario.Examples
import java.util.*
data class Examples(val columnNames: List = emptyList(), val rows: List = listOf()) {
val isEmpty: Boolean
get() = rows.isEmpty()
companion object {
fun examplesFrom(examplesList: List): List = examplesList.map { examplesFrom(it) }
fun examplesFrom(examples: Examples): run.qontract.core.pattern.Examples {
val columns = getColumnNames(examples)
val rows = examples.tableBodyList.map { Row(columns, getValues(it)) }
return Examples(columns, rows)
}
private fun getColumnNames(examples: Examples) = getValues(examples.tableHeader)
private fun getValues(row: Messages.GherkinDocument.Feature.TableRow): ArrayList = ArrayList(row.cellsList.map { it.value })
private fun getValues(line: String): List {
val values = " $line ".split("\\|".toRegex()).map { value -> value.trim()}
return values.drop(1).dropLast(1)
}
}
}