mirror of
https://github.com/jlengrand/atrium.git
synced 2026-03-10 08:01:19 +00:00
reduce code duplication for Comparable and CollectionAssertionsSpec
This commit is contained in:
committed by
Robert Stoll
parent
9ad81eb9ca
commit
1688d855b0
@@ -131,7 +131,7 @@ class AdjustStackSpec : Spek({
|
||||
coreFactory.newRemoveRunnerAtriumErrorAdjuster(),
|
||||
listOf()
|
||||
),
|
||||
"combine noop ajdust, remove atrium adjuster and remove runner adjuster" to
|
||||
"combine noop adjust, remove atrium adjuster and remove runner adjuster" to
|
||||
coreFactory.newMultiAtriumErrorAdjuster(
|
||||
coreFactory.newNoOpAtriumErrorAdjuster(),
|
||||
coreFactory.newRemoveAtriumFromAtriumErrorAdjuster(),
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
package ch.tutteli.atrium.specs
|
||||
|
||||
import ch.tutteli.atrium.creating.Expect
|
||||
import ch.tutteli.kbox.joinToString
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.dsl.GroupBody
|
||||
import org.spekframework.spek2.dsl.Root
|
||||
import org.spekframework.spek2.style.specification.Suite
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
|
||||
|
||||
@@ -21,39 +21,37 @@ abstract class CollectionAssertionsSpec(
|
||||
isNotEmpty.forSubjectLess()
|
||||
) {})
|
||||
|
||||
fun describeFun(vararg funName: String, body: Suite.() -> Unit) =
|
||||
describeFunTemplate(describePrefix, funName, body = body)
|
||||
|
||||
fun describeFun(vararg pairs: SpecPair<*>, body: Suite.() -> Unit) =
|
||||
describeFunTemplate(describePrefix, pairs.map { it.name }.toTypedArray(), body = body)
|
||||
|
||||
val isDescr = DescriptionBasic.IS.getDefault()
|
||||
val isNotDescr = DescriptionBasic.IS_NOT.getDefault()
|
||||
val empty = DescriptionCollectionAssertion.EMPTY.getDefault()
|
||||
|
||||
describeFun(isEmpty.name) {
|
||||
describeFun(isEmpty, isNotEmpty) {
|
||||
val isEmptyFun = isEmpty.lambda
|
||||
|
||||
it("does not throw if a collection is empty") {
|
||||
expect(listOf<Int>() as Collection<Int>).isEmptyFun()
|
||||
}
|
||||
|
||||
it("throws an AssertionError if a collection is not empty") {
|
||||
expect {
|
||||
expect(listOf(1, 2) as Collection<Int>).isEmptyFun()
|
||||
}.toThrow<AssertionError> { messageContains("$isDescr: $empty") }
|
||||
}
|
||||
}
|
||||
|
||||
describeFun(isNotEmpty.name) {
|
||||
val isNotEmptyFun = isNotEmpty.lambda
|
||||
|
||||
it("does not throw if a collection is not empty") {
|
||||
expect(listOf(1) as Collection<Int>).isNotEmptyFun()
|
||||
context("collection is empty") {
|
||||
it("${isEmpty.name} - does not throw") {
|
||||
expect(listOf<Int>() as Collection<Int>).isEmptyFun()
|
||||
}
|
||||
it("${isNotEmpty.name} - throws an AssertionError") {
|
||||
expect {
|
||||
expect(listOf<Int>() as Collection<Int>).isNotEmptyFun()
|
||||
}.toThrow<AssertionError> { messageContains("$isNotDescr: $empty") }
|
||||
}
|
||||
}
|
||||
|
||||
it("throws an AssertionError if a collection is empty") {
|
||||
expect {
|
||||
expect(listOf<Int>() as Collection<Int>).isNotEmptyFun()
|
||||
}.toThrow<AssertionError> { messageContains("$isNotDescr: $empty") }
|
||||
context("collection is not empty") {
|
||||
it("${isEmpty.name} - throws an AssertionError") {
|
||||
expect {
|
||||
expect(listOf(1, 2) as Collection<Int>).isEmptyFun()
|
||||
}.toThrow<AssertionError> { messageContains("$isDescr: $empty") }
|
||||
}
|
||||
it("${isNotEmpty.name} - does not throw") {
|
||||
expect(listOf(1) as Collection<Int>).isNotEmptyFun()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -24,41 +24,26 @@ abstract class CollectionFeatureAssertionsSpec(
|
||||
size.forAssertionCreatorSpec("$toBeDescr: 1") { toBe(1) }
|
||||
) {})
|
||||
|
||||
fun describeFun(vararg funName: String, body: Suite.() -> Unit) =
|
||||
describeFunTemplate(describePrefix, funName, body = body)
|
||||
fun describeFun(vararg pairs: SpecPair<*>, body: Suite.() -> Unit) =
|
||||
describeFunTemplate(describePrefix, pairs.map { it.name }.toTypedArray(), body = body)
|
||||
|
||||
val fluent = expect(listOf("a", "b") as Collection<String>)
|
||||
val sizeDescr = DescriptionCollectionAssertion.SIZE.getDefault()
|
||||
|
||||
describeFun("val ${sizeFeature.name}") {
|
||||
val sizeVal = sizeFeature.lambda
|
||||
describeFun(sizeFeature, size) {
|
||||
val sizeFunctions = unifySignatures(sizeFeature, size)
|
||||
|
||||
context("list with two entries") {
|
||||
it("toBe(2) holds") {
|
||||
fluent.sizeVal().toBe(2)
|
||||
}
|
||||
it("toBe(1) fails") {
|
||||
expect {
|
||||
fluent.sizeVal().toBe(1)
|
||||
}.toThrow<AssertionError> {
|
||||
messageContains("$sizeDescr: 2")
|
||||
sizeFunctions.forEach { (name, sizeFun, _) ->
|
||||
it("$name - is greater than 1 holds") {
|
||||
fluent.sizeFun { isGreaterThan(1) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describeFun("fun ${size.name}") {
|
||||
val sizeFun = size.lambda
|
||||
|
||||
context("map with two entries") {
|
||||
it("is greater than 1 holds") {
|
||||
fluent.sizeFun { isGreaterThan(1) }
|
||||
}
|
||||
it("is less than 1 fails") {
|
||||
expect {
|
||||
fluent.sizeFun { isLessThan(1) }
|
||||
}.toThrow<AssertionError> {
|
||||
messageContains("$sizeDescr: 2")
|
||||
it("$name - is less than 1 fails") {
|
||||
expect {
|
||||
fluent.sizeFun { isLessThan(1) }
|
||||
}.toThrow<AssertionError> {
|
||||
messageContains("$sizeDescr: 2")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import ch.tutteli.atrium.api.verbs.internal.expect
|
||||
import ch.tutteli.atrium.specs.*
|
||||
import ch.tutteli.atrium.translations.DescriptionComparableAssertion
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.Suite
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
|
||||
abstract class ComparableAssertionsSpec(
|
||||
@@ -24,7 +25,6 @@ abstract class ComparableAssertionsSpec(
|
||||
isGreaterOrEquals.forSubjectLess(1)
|
||||
) {})
|
||||
|
||||
|
||||
val isLessThanDescr = DescriptionComparableAssertion.IS_LESS_THAN.getDefault()
|
||||
val isLessOrEqualsDescr = DescriptionComparableAssertion.IS_LESS_OR_EQUALS.getDefault()
|
||||
val isGreaterThanDescr = DescriptionComparableAssertion.IS_GREATER_THAN.getDefault()
|
||||
|
||||
@@ -50,9 +50,8 @@ abstract class IterableContainsInOrderOnlyGroupedEntriesAssertionsSpec(
|
||||
) {})
|
||||
//@formatter:on
|
||||
|
||||
fun describeFun(vararg funName: String, body: Suite.() -> Unit) =
|
||||
describeFunTemplate(describePrefix, funName, body = body)
|
||||
|
||||
fun describeFun(vararg pairs: SpecPair<*>, body: Suite.() -> Unit) =
|
||||
describeFunTemplate(describePrefix, pairs.map { it.name }.toTypedArray(), body = body)
|
||||
|
||||
fun Expect<Iterable<Double?>>.containsInOrderOnlyGroupedEntriesFun(
|
||||
t1: Group<(Expect<Double>.() -> Unit)?>,
|
||||
@@ -156,11 +155,8 @@ abstract class IterableContainsInOrderOnlyGroupedEntriesAssertionsSpec(
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
describeFun(containsInOrderOnlyGroupedEntries.name) {
|
||||
context("$describePrefix describe non-nullable cases") {
|
||||
describeFun(containsInOrderOnlyGroupedEntries) {
|
||||
context("describe non-nullable cases") {
|
||||
|
||||
context("throws an $illegalArgumentException") {
|
||||
it("if an empty group is given as first parameter") {
|
||||
@@ -410,7 +406,7 @@ abstract class IterableContainsInOrderOnlyGroupedEntriesAssertionsSpec(
|
||||
}
|
||||
}
|
||||
nullableCases(describePrefix) {
|
||||
describeFun("$containsInOrderOnlyGroupedEntries for nullable") {
|
||||
describeFun(containsInOrderOnlyGroupedEntries) {
|
||||
val null1null3 = { sequenceOf(null, 1.0, null, 3.0).constrainOnce().asIterable() }
|
||||
|
||||
context("iterable ${null1null3().toList()}") {
|
||||
|
||||
Reference in New Issue
Block a user