Skip to content

Commit

Permalink
ADD: samples of iterableSubjectChangers in api-infix and api-fluent (#…
Browse files Browse the repository at this point in the history
…1155)

* ADD: samples for iterableSubjectChangers in api-infix and api-fluent

* MOD: rename sample files and add comments

* MOD: rename method paths of @sample tag

* MOD: align spaces

* MOD: align spaces

* move the comment
  • Loading branch information
simonNozaki authored Jun 18, 2022
1 parent d9a6fd3 commit 0797d3b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import ch.tutteli.atrium.logic.changeSubject
*
* @return The newly created [Expect] for the transformed subject.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.IterableSubjectChangerSamples.asListFeature
*
* @since 0.14.0
*/
fun <E, T : Iterable<E>> Expect<T>.asList(): Expect<List<E>> = _logic.changeSubject.unreported { it.toList() }
Expand All @@ -25,6 +27,8 @@ fun <E, T : Iterable<E>> Expect<T>.asList(): Expect<List<E>> = _logic.changeSubj
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.IterableSubjectChangerSamples.asList
*
* @since 0.14.0
*/
fun <E, T : Iterable<E>> Expect<T>.asList(assertionCreator: Expect<List<E>>.() -> Unit): Expect<T> =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ch.tutteli.atrium.api.fluent.en_GB.samples

import ch.tutteli.atrium.api.fluent.en_GB.asList
import ch.tutteli.atrium.api.fluent.en_GB.toContain
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.internal.expect
import kotlin.test.Test

class IterableSubjectChangerSamples {
@Test
fun asListFeature() {
expect(0..2)
.asList() // subject is now of type List<Int>
.toEqual(listOf(0, 1, 2))

fails {
expect(0..2)
.asList() // subject is now of type List<Int>
.toContain(3) // fails
.toContain(4) // not evaluated/reported because above `toContain` already fails
// use `.asList { ... }` if you want that all expectations are evaluated
}
}

@Test
fun asList() {
expect(0..2) // subject within this expectation-group is of type List<Int>
.asList {
toEqual(listOf(0, 1, 2))
} // subject here is back to type IntRange

fails {
// all expectations inside an expectation-group are evaluated together; for more details see:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group

expect(0..2)
.asList {
toContain(3) // fails
toContain(4) // still evaluated even though above `toContain` already fails
// use `.asList().` if you want a fail fast behaviour
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlin.jvm.JvmName
*
* @return The newly created [Expect] for the transformed subject.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.IterableSubjectChangerSamples.asListFeature
*
* @since 0.14.0
*/
infix fun <E, T : Iterable<E>> Expect<T>.asList(
Expand All @@ -28,6 +30,8 @@ infix fun <E, T : Iterable<E>> Expect<T>.asList(
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.IterableSubjectChangerSamples.asList
*
* @since 0.14.0
*/
infix fun <E, T : Iterable<E>> Expect<T>.asList(assertionCreator: Expect<List<E>>.() -> Unit): Expect<T> =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ch.tutteli.atrium.api.infix.en_GB.samples

import ch.tutteli.atrium.api.infix.en_GB.asList
import ch.tutteli.atrium.api.infix.en_GB.it
import ch.tutteli.atrium.api.infix.en_GB.o
import ch.tutteli.atrium.api.infix.en_GB.toContain
import ch.tutteli.atrium.api.infix.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.internal.expect
import kotlin.test.Test

class IterableSubjectChangerSamples {
@Test
fun asListFeature() {
expect(0..2) asList o toEqual listOf(0, 1, 2)
// | subject is now of type List<Int>

fails {
expect(0..2) asList o toContain 3 toContain 4
}
}

@Test
fun asList() {
expect(0..2)
.asList { // subject within this expectation-group is of type List<Int>
it toEqual listOf(0, 1, 2)
} // subject here is back to type IntRange

fails {
// all expectations inside an expectation-group are evaluated together; for more details see:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(0..2)
.asList {
it toContain 3 // fails
it toContain 4 // still evaluated even though above `toContain` already fails
// use `asList o` if you want a fail fast behaviour
}
}
}
}

0 comments on commit 0797d3b

Please sign in to comment.