@@ -10,6 +10,7 @@ import ch.tutteli.atrium.translations.DescriptionBasic
1010import ch.tutteli.atrium.translations.DescriptionIterableLikeExpectation
1111
1212
13+ @Suppress(" FunctionName" )
1314abstract 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