of
Helper function to create a KeyWithCreator based on the given reason and assertionCreator.
Samples
data class Person(val age: Int)
val customers = listOf(Person(21))
expect("filename") because of("? is not allowed in file names on Windows") {
it notToContain "?"
}
expect(customers) toHaveElementsAndAll (fun Expect<Person>.() {
it because of("the legal age of maturity in Switzerland is 18") {
feature { f(it::age) } toBeGreaterThanOrEqualTo 18
}
})Helper function to create a Feature based on a KFunction2 + arguments.
Return
The newly created Feature.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String) = "$title ${person.name}"
expect(person) feature of(
::f, "Dr."
) toStartWith "Dr. John" toEndWith "Smith"
// subject is now String (actually the return value of calling `f` with the given argument)
fails {
expect(person) feature of(
::f, "Dr."
) toStartWith "Kevin" toEndWith "Bacon"
// fails, not evaluated/reported because `toStartWith` already fails
}Helper function to create a Feature based on a KFunction3 + arguments.
Return
The newly created Feature.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String) = "$title ${person.name}, $suffix"
expect(person) feature of(
::f, "Dr.", "PMP"
) toStartWith "Dr. John" toEndWith "Smith, PMP"
// subject is now String (actually the return value of calling `f` with the given two arguments)
fails {
expect(person) feature of(
::f, "Dr.", "PMP"
) toStartWith "Kevin" toEndWith "Bacon"
// fails not evaluated/reported because `toStartWith` already fails
}Helper function to create a Feature based on a KFunction4 + arguments.
Return
The newly created Feature.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String, english: String) =
"$title ${person.name}, $suffix. English level: $english"
expect(person) feature of(
::f, "Dr.", "PMP", "Native"
) toStartWith "Dr. John Smith" toContain "PMP" toEndWith "English level: Native"
// subject is now String (actually the return value of calling `f` with the given three arguments)
fails {
expect(person) feature of(
::f, "Dr.", "PMP", "Native"
) toStartWith "Kevin" toEndWith "Bacon" // fails
// not evaluated/reported because `toStartWith` already fails
}Helper function to create a Feature based on a KFunction5 + arguments.
Return
The newly created Feature.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String, english: String, german: String) =
"$title ${person.name}, $suffix. English level: $english, German level: $german"
expect(person) feature of(
::f, "Dr.", "PMP", "Native", "C1"
) toStartWith "Dr. John Smith" toContain "PMP" toContain "English level: Native" toEndWith "German level: C1"
// subject is now String (actually the return value of calling `f` with the given four arguments)
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(
::f, "Dr.", "PMP", "Native", "C1"
) toStartWith "Kevin" toEndWith "Bacon" // fails
// not evaluated/reported because `toStartWith` already fails
}Helper function to create a Feature based on a KFunction6 + arguments.
Return
The newly created Feature.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String, english: String, german: String, spanish: String) =
"$title ${person.name}, $suffix. English level: $english, German level: $german, Spanish level: $spanish"
expect(person) feature of(
::f, "Dr.", "PMP", "Native", "C1", "B2"
) toStartWith "Dr. John Smith" toContain "PMP" toContain "English level: Native" toContain "German level: C1" toEndWith "Spanish level: B2"
// subject is now String (actually the return value of calling `f` with the given five arguments)
fails {
expect(person) feature of(
::f, "Dr.", "PMP", "Native", "C1", "B2"
) toStartWith "Kevin" toEndWith "Bacon" // fails
// not evaluated/reported because `toStartWith` already fails
}Helper function to create a FeatureWithCreator based on a KProperty1 + assertionCreator.
Return
The newly created FeatureWithCreator.
Samples
val person = Person(name = "John Smith", age = 25)
expect(person) feature of(Person::name) { // subject within this expectation-group is of type String
it toStartWith "John"
it toEndWith "Smith"
} // subject here is back to type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(Person::name) {
it toStartWith "Kevin" // fails
it toEndWith "Bacon" // still evaluated, use `feature Person::name ...` if you want fail fast behaviour
}
}Helper function to create a FeatureWithCreator based on a KFunction1 + assertionCreator.
Return
The newly created FeatureWithCreator.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person) = person.name
expect(person) feature of(::f) { // subject is now String, after function [f] is applied
it toStartWith "John"
it toEndWith "Smith"
} // subject here is back type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(::f) {
it toStartWith "Kevin" // fails
it toEndWith "Bacon" // still evaluated, use `feature ::f ...` if you want fail fast behaviour
}
}Helper function to create a FeatureWithCreator based on a KFunction2 + arguments + assertionCreator.
Return
The newly created FeatureWithCreator.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String) = "$title ${person.name}"
expect(person) feature of(
::f, "Dr."
) {// subject within this expectation-group is of type String (actually the return value of calling `f` with the given argument)
it toStartWith "Dr. John"
it toEndWith "Smith"
} // subject here is back type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(::f, "Dr.") {
it toStartWith "Kevin" // fails
it toEndWith "Bacon" // still evaluated, use `feature of(...) ...` if you want fail fast behaviour
}
}Helper function to create a FeatureWithCreator based on a KFunction3 + arguments + assertionCreator.
Return
The newly created FeatureWithCreator.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String) = "$title ${person.name}, $suffix"
expect(person) feature of(
::f, "Dr.", "PMP"
) { // subject within this expectation-group is of type String (actually the return value of calling `f` with the given two arguments)
it toStartWith "Dr. John"
it toEndWith "Smith, PMP"
} // subject here is back type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(::f, "Dr.", "PMP") {
it toStartWith "Kevin" // fails
it toEndWith "Bacon" // still evaluated, use `feature of(...) ...` if you want fail fast behaviour
}
}Helper function to create a FeatureWithCreator based on a KFunction4 + arguments + assertionCreator.
Return
The newly created FeatureWithCreator.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String, english: String) =
"$title ${person.name}, $suffix. English level: $english"
expect(person) feature of(
::f, "Dr.", "PMP", "Native"
) { // subject within this expectation-group is of type String (actually the return value of calling `f` with the given three arguments)
it toStartWith "Dr. John Smith"
it toContain "PMP"
it toEndWith "English level: Native"
} // subject here is back type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(::f, "Dr.", "PMP", "Native") {
it toStartWith "Kevin" // fails
it toEndWith "Bacon" // still evaluated, use `feature of(...) ...` if you want fail fast behaviour
}
}Helper function to create a FeatureWithCreator based on a KFunction5 + arguments + assertionCreator.
Return
The newly created FeatureWithCreator.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String, english: String, german: String) =
"$title ${person.name}, $suffix. English level: $english, German level: $german"
expect(person) feature of(
::f, "Dr.", "PMP", "Native", "C1"
) { // subject within this expectation-group is of type String (actually the return value of calling `f` with the given four arguments)
it toStartWith "Dr. John Smith"
it toContain "PMP"
it toContain "English level: Native"
it toEndWith "German level: C1"
} // subject here is back type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(::f, "Dr.", "PMP", "Native", "C1") {
it toStartWith "Kevin" // fails
it toEndWith "Bacon" // still evaluated, use `feature of(...) ...` if you want fail fast behaviour
}
}Helper function to create a FeatureWithCreator based on a KFunction6 + arguments + assertionCreator.
Return
The newly created FeatureWithCreator.
Samples
val person = Person(name = "John Smith", age = 25)
fun f(person: Person, title: String, suffix: String, english: String, german: String, spanish: String) =
"$title ${person.name}, $suffix. English level: $english, German level: $german, Spanish level: $spanish"
expect(person) feature of(
::f, "Dr.", "PMP", "Native", "C1", "B2"
) { // subject within this expectation-group is of type String (actually the return value of calling `f` with the given five arguments)
it toStartWith "Dr. John Smith"
it toContain "PMP"
it toContain "English level: Native"
it toContain "German level: C1"
it toEndWith "Spanish level: B2"
} // subject here is back type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of(::f, "Dr.", "PMP", "Native", "C1", "B2") {
it toStartWith "Kevin" // fails
it toEndWith "Bacon" // still evaluated, use `feature of(...) ...` if you want fail fast behaviour
}
}Helper function to create a MetaFeatureOptionWithCreator based on a lambda with MetaFeatureOption receiver (has to return a MetaFeature) and an assertionCreator.
Samples
val person = Person(name = "John Smith", age = 25)
expect(person) feature of({ f(it::age) }) { // `it` refers to `person`, subject within this expectation-group is of type Int (actually 25)
it toBeGreaterThan 20
it toBeLessThan 30
} // subject here is back to Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
expect(person) feature of({ f(it::age) }) {
it toBeGreaterThan 30 // fails
it toBeLessThan 20 // still evaluated, use `feature { ... } ...` if you want fail fast behaviour
}
}Creates a Feature using the given extractor and description.
This can be used to create complex features with a custom description or as workaround where Kotlin is not able to infer the types properly.
For instance:
expect(person) feature of("first underage child") { children.first { it < 18 } }Note, you need to enable the new type inference of Kotlin (or use Kotlin 1.4 and above) in order that Kotlin is able to infer the types. As workaround, you can use feature with the overload which expects MetaFeatureOption<T>.(T) -> MetaFeature<R>. For instance:
// use
expect(person) feature { f("first underage child", it.children.first { it < 18 }) }
// instead of (which causes problems with Kotlin < 1.4)
expect(person) feature of("first underage child") { children.first { it < 18 }Return
The newly created Feature.
Samples
val person = Person(name = "John Smith", age = 25)
expect(person) feature of(
"Actual age", Person::age
) toBeLessThan 30 toBeGreaterThan 20 // subject is now Int (actually 25)
fails {
// Reporting will include the description:
// expected that subject: Person(name=John Smith, age=25)
// * Actual age: 25
// - to be less than: 30
expect(person) feature of("Actual age", Person::age) toBeLessThan 20 toBeGreaterThan 30
// fails
// not evaluated/reported because `toBeLessThan` already fails
}Creates a Feature using the given extractor and description.
This can be used to create complex features with a custom description or as workaround where Kotlin is not able to infer the types properly.
For instance:
expect(person) feature of("first underage child", { children.first { it < 18 }) { name.toEqual("robert) }Note, you need to enable the new type inference of Kotlin (or use Kotlin 1.4 and above) in order that Kotlin is able to infer the types. As workaround, you can use feature with the overload which expects MetaFeatureOption<T>.(T) -> MetaFeature<R>. and use it after the call (import from the package workaround). For instance:
// use
import ch.tutteli.atrium.api.infix.en_GB.workaround.it
expect(person) feature { f(it::age) } it { this toEqual 20 }
// instead of (which causes problems with Kotlin < 1.4)
expect(person) feature of({ f(it::age) }) { this toEqual 20 }Return
The newly created Feature.
Samples
val person = Person(name = "John Smith", age = 25)
expect(person) feature of("Actual age", Person::age) {
// subject within this expectation-group is now of type Int (actually 25)
it toBeLessThan 30
it toBeGreaterThan 20
} // subject here is back type Person
fails {
// all expectations are evaluated inside an expectation-group block; for more details:
// https://github.com/robstoll/atrium#define-single-expectations-or-an-expectation-group
// Reporting will include the description:
// expected that subject: Person(name=John Smith, age=25)
// * Actual age: 25
// - to be less than: 30
// - to be greater than: 30
expect(person) feature of("Actual age", Person::age) {
it toBeLessThan 20 // fails
it toBeGreaterThan 30 // still evaluated, use `feature of(...) ...` if you want fail fast behaviour
}
}