Skip to content

Commit bfa0704

Browse files
committed
#2012 fix ambiguityTests use new testFactoryFeatureNonFeature
1 parent ded561e commit bfa0704

File tree

3 files changed

+131
-113
lines changed

3 files changed

+131
-113
lines changed
Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package ch.tutteli.atrium.api.fluent.en_GB
22

3-
import ch.tutteli.atrium.api.verbs.internal.expect
3+
import ch.tutteli.atrium.api.verbs.expect
44
import ch.tutteli.atrium.creating.Expect
5-
import ch.tutteli.atrium.specs.*
5+
import ch.tutteli.atrium.specs.feature0
6+
import ch.tutteli.atrium.specs.fun0
7+
import ch.tutteli.atrium.specs.fun1
8+
import ch.tutteli.atrium.specs.property
69
import kotlin.test.Test
710

8-
object IterableExpectationsTest : ch.tutteli.atrium.specs.integration.AbstractIterableExpectationsTest(
11+
class IterableExpectationsTest : ch.tutteli.atrium.specs.integration.AbstractIterableExpectationsTest(
912
fun0(Expect<Iterable<Int>>::toHaveElements),
1013
fun0(Expect<Iterable<Int>>::notToHaveElements),
1114
feature0(Expect<Iterable<Int>>::min),
@@ -18,32 +21,37 @@ object IterableExpectationsTest : ch.tutteli.atrium.specs.integration.AbstractIt
1821
) {
1922

2023
@Test
24+
@Suppress("AssignedValueIsNeverRead", "UNUSED_VARIABLE", "UNUSED_VALUE", "unused")
2125
fun ambiguityTest() {
22-
val a1 = expect(listOf(1.0, 2.0))
23-
val a1b = expect(setOf(1.0, 2.0, null))
24-
val star = expect(listOf(1, 2, 3) as Collection<*>)
26+
var list: Expect<List<Double>> = expect(listOf(1.2, 4.3))
27+
var listEmpty: Expect<List<Double>> = expect(emptyList())
28+
var nSet: Expect<Set<Double?>> = expect(setOf(1.2, null))
29+
var nSetEmpty: Expect<Set<Double?>> = expect(emptySet())
30+
var star: Expect<Collection<*>> = expect(listOf(1.2, 1.3))
31+
var starEmpty: Expect<Collection<*>> = expect(emptySet<Any?>())
2532

26-
a1.toHaveElements()
27-
a1.notToHaveElements()
28-
a1.toHaveElementsAndNoDuplicates()
33+
list = list.toHaveElements()
34+
list = list.toHaveElementsAndNoDuplicates()
35+
listEmpty = listEmpty.notToHaveElements()
2936

30-
a1b.toHaveElements()
31-
a1b.notToHaveElements()
32-
a1b.toHaveElementsAndNoDuplicates()
37+
nSet = nSet.toHaveElements()
38+
nSet = nSet.toHaveElementsAndNoDuplicates()
39+
nSetEmpty = nSetEmpty.notToHaveElements()
3340

34-
star.toHaveElements()
35-
star.notToHaveElements()
36-
star.toHaveElementsAndNoDuplicates()
3741

38-
//nullable not supported by min/max or rather T : Comparable<T> does not exist for T? (one cannot implement an interface for the nullable type)
39-
//same for Iterable<*>
40-
a1.min()
41-
a1.max()
42+
star = star.toHaveElements()
43+
star = star.toHaveElementsAndNoDuplicates()
44+
starEmpty = starEmpty.notToHaveElements()
4245

43-
a1.min { }
44-
a1.max { }
46+
// nullable not supported by min/max or rather T : Comparable<T> does not exist for T?
47+
// (one cannot implement an interface for the nullable type) same for Iterable<*>
48+
val l1: Expect<Double> = list.min()
49+
val l2: Expect<Double> = list.max()
4550

46-
a1.last
47-
a1.last { }
51+
list = list.min { toBeLessThan(1.3) }
52+
list = list.max { toBeGreaterThan(4.1) }
53+
54+
val l3: Expect<Double> = list.last
55+
list.last { toEqual(4.3) }
4856
}
4957
}
Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package ch.tutteli.atrium.api.infix.en_GB
22

3-
import ch.tutteli.atrium.api.verbs.internal.expect
43
import ch.tutteli.atrium.creating.Expect
5-
import ch.tutteli.atrium.specs.*
4+
import ch.tutteli.atrium.specs.feature1
5+
import ch.tutteli.atrium.specs.fun1
6+
import ch.tutteli.atrium.specs.name
67
import kotlin.reflect.KFunction2
78
import kotlin.test.Test
89

@@ -18,20 +19,26 @@ class IterableExpectationsTest : ch.tutteli.atrium.specs.integration.AbstractIte
1819
fun1(Expect<Iterable<Int>>::last),
1920
) {
2021
companion object {
21-
private val toHave: KFunction2<Expect<Iterable<Int>>, elements, Expect<Iterable<Int>>> = Expect<Iterable<Int>>::toHave
22-
private fun getToHaveElementsPair() = "${toHave.name} ${elements::class.simpleName}" to Companion::toHaveElements
22+
private val toHave: KFunction2<Expect<Iterable<Int>>, elements, Expect<Iterable<Int>>> =
23+
Expect<Iterable<Int>>::toHave
24+
25+
private fun getToHaveElementsPair() =
26+
"${toHave.name} ${elements::class.simpleName}" to Companion::toHaveElements
27+
2328
private fun toHaveElements(expect: Expect<Iterable<Int>>) = expect toHave elements
2429

2530
private val notToHave: KFunction2<Expect<Iterable<Int>>, elements, Expect<Iterable<Int>>> =
2631
Expect<Iterable<Int>>::notToHave
2732

28-
private fun getNotToHaveElementsPair() = "${notToHave.name} ${elements::class.simpleName}" to Companion::notToHaveElements
33+
private fun getNotToHaveElementsPair() =
34+
"${notToHave.name} ${elements::class.simpleName}" to Companion::notToHaveElements
35+
2936
private fun notToHaveElements(expect: Expect<Iterable<Int>>) = expect notToHave elements
3037

31-
private fun minFeaturePair() = feature1(Expect<Iterable<Int>>::min).name to ::minFeature
38+
private fun minFeaturePair() = feature1<Iterable<Int>, o, Int>(Expect<Iterable<Int>>::min).name to ::minFeature
3239
private fun minFeature(expect: Expect<Iterable<Int>>) = expect min o
3340

34-
private fun maxFeaturePair() = feature1(Expect<Iterable<Int>>::max).name to ::maxFeature
41+
private fun maxFeaturePair() = feature1<Iterable<Int>, o, Int>(Expect<Iterable<Int>>::max).name to ::maxFeature
3542
private fun maxFeature(expect: Expect<Iterable<Int>>) = expect max o
3643

3744
private val toHaveElementsAnd: KFunction2<Expect<Iterable<Int>>, noDuplicates, Expect<Iterable<Int>>> =
@@ -42,37 +49,44 @@ class IterableExpectationsTest : ch.tutteli.atrium.specs.integration.AbstractIte
4249

4350
private fun toHaveElementsAndNoDuplicates(expect: Expect<Iterable<Int>>) = expect toHaveElementsAnd noDuplicates
4451

45-
private fun lastFeaturePair() = feature1(Expect<Iterable<Int>>::last).name to ::lastFeature
52+
private fun lastFeaturePair() =
53+
feature1<Iterable<Int>, o, Int>(Expect<Iterable<Int>>::last).name to ::lastFeature
54+
4655
private fun lastFeature(expect: Expect<Iterable<Int>>) = expect last o
4756
}
4857

4958
@Test
59+
@Suppress("AssignedValueIsNeverRead", "UNUSED_VARIABLE", "UNUSED_VALUE", "unused")
5060
fun ambiguityTest() {
51-
val a1 = expect(listOf(1, 2))
52-
val a1b = expect(setOf(1.0, 2.0, null))
53-
val star = expect(listOf(1, 2, 3) as Collection<*>)
61+
var list: Expect<List<Double>> = ch.tutteli.atrium.api.verbs.expect(listOf(1.2, 4.3))
62+
var listEmpty: Expect<List<Double>> = ch.tutteli.atrium.api.verbs.expect(emptyList())
63+
var nSet: Expect<Set<Double?>> = ch.tutteli.atrium.api.verbs.expect(setOf(1.2, null))
64+
var nSetEmpty: Expect<Set<Double?>> = ch.tutteli.atrium.api.verbs.expect(emptySet())
65+
var star: Expect<Collection<*>> = ch.tutteli.atrium.api.verbs.expect(listOf(1.2, 1.3))
66+
var starEmpty: Expect<Collection<*>> = ch.tutteli.atrium.api.verbs.expect(emptySet<Any?>())
67+
68+
list = list toHave elements
69+
list = list toHaveElementsAnd noDuplicates
70+
listEmpty = listEmpty notToHave elements
5471

55-
a1 toHave elements
56-
a1 notToHave elements
57-
a1 toHaveElementsAnd noDuplicates
72+
nSet = nSet toHave elements
73+
nSet = nSet toHaveElementsAnd noDuplicates
74+
nSetEmpty = nSetEmpty notToHave elements
5875

59-
a1b toHave elements
60-
a1b notToHave elements
61-
a1b toHaveElementsAnd noDuplicates
6276

63-
star toHave elements
64-
star notToHave elements
65-
star toHaveElementsAnd noDuplicates
77+
star = star toHave elements
78+
star = star toHaveElementsAnd noDuplicates
79+
starEmpty = starEmpty notToHave elements
6680

67-
//nullable not supported by min/max or rather T : Comparable<T> does not exist for T? (one cannot implement an interface for the nullable type)
68-
//same for Iterable<*>
69-
a1 min o toEqual 2
70-
a1 max o toEqual 3
81+
// nullable not supported by min/max or rather T : Comparable<T> does not exist for T?
82+
// (one cannot implement an interface for the nullable type) same for Iterable<*>
83+
val l1: Expect<Double> = list min o
84+
val l2: Expect<Double> = list max o
7185

72-
a1 min { }
73-
a1 max { }
86+
list = list min { toBeLessThan(1.3) }
87+
list = list max { toBeGreaterThan(4.1) }
7488

75-
a1 last o
76-
a1 last { }
89+
val l3: Expect<Double> = list last o
90+
list last { toEqual(4.3) }
7791
}
7892
}

misc/atrium-specs/src/commonMain/kotlin/ch/tutteli/atrium/specs/integration/AbstractIterableExpectationsTest.kt

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ch.tutteli.atrium.translations.DescriptionBasic
1010
import ch.tutteli.atrium.translations.DescriptionIterableLikeExpectation
1111

1212

13+
@Suppress("FunctionName")
1314
abstract class AbstractIterableExpectationsTest(
1415
private val toHaveElementsSpec: Fun0<Iterable<Int>>,
1516
private val notToHaveElementsSpec: Fun0<Iterable<Int>>,
@@ -20,7 +21,6 @@ abstract class AbstractIterableExpectationsTest(
2021
private val toHaveElementsAndNoDuplicatesSpec: Fun0<Iterable<Int>>,
2122
private val lastFeatureSpec: Feature0<Iterable<Int>, Int>,
2223
private val lastSpec: Fun1<Iterable<Int>, Expect<Int>.() -> Unit>,
23-
describePrefix: String = "[Atrium] "
2424
) : ExpectationFunctionBaseTest() {
2525

2626
@TestFactory
@@ -76,66 +76,67 @@ abstract class AbstractIterableExpectationsTest(
7676
}
7777
}
7878

79+
val iterableWith4And3 = { listOf(4, 3) as Iterable<Int> }
80+
7981
@TestFactory
80-
fun minAndMax() = testFactory(minFeatureSpec, minSpec, maxFeatureSpec, maxSpec) { spec ->
81-
val minFunctions = unifySignatures(minFeatureSpec, minSpec)
82-
val maxFunctions = unifySignatures(maxFeatureSpec, maxSpec)
83-
84-
describe("list with 4 and 3") {
85-
val iterableWith4And3 = listOf(4, 3) as Iterable<Int>
86-
minFunctions.forEach { (name, minFun, _) ->
87-
it("$name - is greater than 2 holds") {
88-
expect(iterableWith4And3).minFun { toBeGreaterThan(2) }
89-
}
90-
it("$name - is less than 2 fails") {
91-
expect {
92-
expect(iterableWith4And3).minFun { toBeLessThan(2) }
93-
}.toThrow<AssertionError> {
94-
messageToContain("min(): 3")
95-
}
96-
}
82+
fun min() = testFactoryForFeatureNonFeature(minFeatureSpec, minSpec) { name, minFun, _ ->
83+
describeIterable(::iterableWith4And3) {
84+
it("$name - ${toBeGreaterThanDescr}(2) holds") {
85+
expect(iterableWith4And3()).minFun { toBeGreaterThan(2) }
9786
}
98-
maxFunctions.forEach { (name, maxFun, _) ->
99-
it("$name - toBe(4) holds") {
100-
expect(iterableWith4And3).maxFun { toEqual(4) }
87+
it("$name - ${toBeLessThanDescr}(2) throws") {
88+
expect {
89+
expect(iterableWith4And3()).minFun { toBeLessThan(2) }
90+
}.toThrow<AssertionError> {
91+
messageToContain("min(): 3")
10192
}
102-
it("$name - $toEqualDescr(3) fails") {
103-
expect {
104-
expect(iterableWith4And3).maxFun { toEqual(3) }
105-
}.toThrow<AssertionError> {
106-
messageToContain("max(): 4")
107-
}
93+
}
94+
}
95+
}
96+
97+
@TestFactory
98+
fun max() = testFactoryForFeatureNonFeature(maxFeatureSpec, maxSpec) { name, maxFun, _ ->
99+
describeIterable(::iterableWith4And3) {
100+
it("$name - $toEqualDescr(4) holds") {
101+
expect(iterableWith4And3()).maxFun { toEqual(4) }
102+
}
103+
it("$name - $toEqualDescr(3) fails") {
104+
expect {
105+
expect(iterableWith4And3()).maxFun { toEqual(3) }
106+
}.toThrow<AssertionError> {
107+
messageToContain("max(): 4")
108108
}
109109
}
110110
}
111+
}
112+
113+
@TestFactory
114+
fun min_max__empty_list() = testFactory(minFeatureSpec, minSpec, maxFeatureSpec, maxSpec) {
115+
val functions = unifySignatures(minFeatureSpec, minSpec) + unifySignatures(maxFeatureSpec, maxSpec)
111116

112117
describe("empty list") {
113118
val emptyIterable = expect(emptyList<Int>() as Iterable<Int>)
114119
val noElementsDescr = DescriptionIterableLikeExpectation.NO_ELEMENTS.getDefault()
115120

116-
minFunctions.forEach { (name, minFun, _) ->
117-
it("$name - fails warning about empty iterable") {
121+
functions.forEach { (name, minFun, hasExtraHint) ->
122+
it("$name - fails warning about empty iterable" + showsSubExpectationIf(hasExtraHint)) {
118123
expect {
119124
emptyIterable.minFun { toEqual(1) }
120125
}.toThrow<AssertionError> {
121-
messageToContain(noElementsDescr)
122-
}
123-
}
124-
}
125-
maxFunctions.forEach { (name, maxFun, _) ->
126-
it("$name - fails warning about empty iterable") {
127-
expect {
128-
emptyIterable.maxFun { toEqual(1) }
129-
}.toThrow<AssertionError> {
130-
messageToContain(noElementsDescr)
126+
message {
127+
toContain(noElementsDescr)
128+
if (hasExtraHint) toContain("$toEqualDescr: 1")
129+
}
131130
}
132131
}
133132
}
134133
}
135134
}
136135

137136
@TestFactory
138-
fun toHaveElementsAndNoDuplicates() = testFactory(toHaveElementsAndNoDuplicatesSpec) { toHaveElementsAndNoDuplicatesFun ->
137+
fun toHaveElementsAndNoDuplicates() = testFactory(
138+
toHaveElementsAndNoDuplicatesSpec
139+
) { toHaveElementsAndNoDuplicatesFun ->
139140
describe("empty collection") {
140141
it("throws AssertionError as there needs to be at least one element") {
141142
expect {
@@ -159,7 +160,9 @@ abstract class AbstractIterableExpectationsTest(
159160

160161
describe("list with duplicates") {
161162
it("throws AssertionError with details of duplicate indices") {
162-
fun index(i: Int, element: Int) = DescriptionIterableLikeExpectation.INDEX.getDefault().format("$i: $element")
163+
fun index(i: Int, element: Int) =
164+
DescriptionIterableLikeExpectation.INDEX.getDefault().format("$i: $element")
165+
163166
fun duplicatedBy(vararg elements: Int) =
164167
DescriptionIterableLikeExpectation.DUPLICATED_BY.getDefault().format(elements.joinToString(", "))
165168

@@ -178,33 +181,26 @@ abstract class AbstractIterableExpectationsTest(
178181
}
179182
}
180183

184+
val oneThreeFour = { listOf(1, 3, 4) as Iterable<Int> }
185+
181186
@TestFactory
182-
fun last() = testFactory(lastFeatureSpec, lastSpec) { spec ->
183-
val lastFunctions = unifySignatures(lastFeatureSpec, lastSpec)
184-
val listNullable = listOf(1, 3, 4) as Iterable<Int>
185-
val fluentNullable = expect(listNullable)
186-
187-
describe("list $listNullable") {
188-
lastFunctions.forEach { (name, lastFun, _) ->
189-
it("$name - can perform sub-assertion on last element with value") {
190-
fluentNullable.lastFun { toEqual(4) }
191-
}
187+
fun last() = testFactoryForFeatureNonFeature(lastFeatureSpec, lastSpec) { name, lastFun, hasExtraHint ->
188+
189+
describeIterable(::oneThreeFour) {
190+
it("$name - can perform sub-assertion on last element with value") {
191+
expect(oneThreeFour()).lastFun { toEqual(4) }
192192
}
193193
}
194194

195-
val emptyList = emptyList<Int>() as Iterable<Int>
196-
val fluentEmptyList = expect(emptyList)
197195
val listIsEmptyDescr = DescriptionIterableLikeExpectation.NO_ELEMENTS.getDefault()
198196

199-
describe("list $emptyList") {
200-
lastFunctions.forEach { (name, lastFun, hasExtraHint) ->
201-
it("$name - empty list throws" + showsSubExpectationIf(hasExtraHint)) {
202-
expect {
203-
fluentEmptyList.lastFun { toEqual(3) }
204-
}.toThrow<AssertionError> {
205-
messageToContain("last(): $listIsEmptyDescr")
206-
if (hasExtraHint) messageToContain("$toEqualDescr: 3")
207-
}
197+
describe("empty list") {
198+
it("$name - empty list throws" + showsSubExpectationIf(hasExtraHint)) {
199+
expect {
200+
expect(emptyList<Int>() as Iterable<Int>).lastFun { toEqual(3) }
201+
}.toThrow<AssertionError> {
202+
messageToContain("last(): $listIsEmptyDescr")
203+
if (hasExtraHint) messageToContain("$toEqualDescr: 3")
208204
}
209205
}
210206
}

0 commit comments

Comments
 (0)