Skip to content

Commit

Permalink
Add tests for Any.isNoneOf and Any.isNotIn assertions
Browse files Browse the repository at this point in the history
and update error message, reflect IterableLike
  • Loading branch information
Anes Abismail authored and robstoll committed Aug 25, 2020
1 parent 3a3f046 commit bdd894b
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ infix fun <T> Expect<T>.and(assertionCreator: Expect<T>.() -> Unit): Expect<T> =
*
* @since 0.13.0
*/
inline fun <reified T> Expect<T>.isNoneOf(expected: T, vararg otherValues: T): Expect<T> =
fun <T> Expect<T>.isNoneOf(expected: T, vararg otherValues: T): Expect<T> =
_logicAppend { isNotIn(expected glue otherValues) }

/**
Expand All @@ -194,7 +194,7 @@ inline fun <reified T> Expect<T>.isNoneOf(expected: T, vararg otherValues: T): E
*/
inline fun <reified T> Expect<T>.isNotIn(expected: IterableLike): Expect<T> {
val iterable = iterableLikeToIterable<T>(expected)
require(iterable.iterator().hasNext()) { "Iterable without elements are not allowed for this function." }
require(iterable.iterator().hasNext()) { "IterableLike without elements are not allowed for this function." }
return _logicAppend { isNotIn(iterable.toList()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ch.tutteli.atrium.api.fluent.en_GB
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.specs.feature0
import ch.tutteli.atrium.specs.fun1
import ch.tutteli.atrium.specs.fun2
import ch.tutteli.atrium.specs.withFeatureSuffix
import ch.tutteli.atrium.specs.withNullableSuffix
import kotlin.reflect.KFunction2
Expand All @@ -25,6 +26,14 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(
fun1(Expect<DataClass>::isNotSameAs),
fun1(Expect<Int?>::isNotSameAs).withNullableSuffix(),
fun1(Expect<DataClass?>::isNotSameAs).withNullableSuffix(),
fun2(Expect<Int>::isNoneOf),
fun2(Expect<DataClass>::isNoneOf),
fun2(Expect<Int?>::isNoneOf).withNullableSuffix(),
fun2(Expect<DataClass?>::isNoneOf).withNullableSuffix(),
fun1(Expect<Int>::isNotIn),
fun1(Expect<DataClass>::isNotIn),
fun1(Expect<Int?>::isNotIn).withNullableSuffix(),
fun1(Expect<DataClass?>::isNotIn).withNullableSuffix(),

"${Expect<Int?>::toBe.name}(null)" to Companion::toBeNull,
fun1(Expect<Int?>::toBeNullIfNullGivenElse),
Expand All @@ -39,7 +48,8 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(
"notToBeNull" to Companion::notToBeNull,

getAndImmediatePair(),
getAndLazyPair()
getAndLazyPair(),
""
) {

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ch.tutteli.atrium.api.infix.en_GB

import ch.tutteli.atrium.api.infix.en_GB.creating.Values
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.domain.builders.utils.iterableLikeToIterable
import ch.tutteli.atrium.domain.creating.typeutils.IterableLike
import ch.tutteli.atrium.logic.*
import ch.tutteli.atrium.reporting.Reporter
import ch.tutteli.kbox.glue

/**
* Expects that the subject of the assertion is (equal to) [expected].
Expand Down Expand Up @@ -229,15 +229,18 @@ inline val <T> Expect<T>.it: Expect<T> get() : Expect<T> = this
inline val <T> Expect<T>.its: Expect<T> get() : Expect<T> = this

/**
* Expects that the subject of the assertion is not (equal to) [expected] and [otherValues].
* Expects that the subject of the assertion is not (equal to) in [values].
*
* @param values The values which are not expected to be contained within the subject of the assertion
* -- use the function `values(t, ...)` to create a [Values].
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
inline fun <reified T> Expect<T>.isNoneOf(expected: T, vararg otherValues: T): Expect<T> =
_logicAppend { isNotIn(expected glue otherValues) }
infix fun <T> Expect<T>.isNoneOf(values: Values<T>): Expect<T> =
_logicAppend { isNotIn(values.toList()) }

/**
* Expects that the subject of the assertion is not (equal to) any value of [expected].
Expand All @@ -251,8 +254,8 @@ inline fun <reified T> Expect<T>.isNoneOf(expected: T, vararg otherValues: T): E
*
* @since 0.13.0
*/
inline fun <reified T> Expect<T>.isNotIn(expected: IterableLike): Expect<T> {
inline infix fun <reified T> Expect<T>.isNotIn(expected: IterableLike): Expect<T> {
val iterable = iterableLikeToIterable<T>(expected)
require(iterable.iterator().hasNext()) { "Iterable without elements are not allowed for this function." }
require(iterable.iterator().hasNext()) { "IterableLike without elements are not allowed for this function." }
return _logicAppend { isNotIn(iterable.toList()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.tutteli.atrium.api.infix.en_GB

import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.specs.fun1
import ch.tutteli.atrium.specs.fun2
import ch.tutteli.atrium.specs.notImplemented
import ch.tutteli.atrium.specs.testutils.WithAsciiReporter
import ch.tutteli.atrium.specs.withFeatureSuffix
Expand All @@ -25,6 +26,14 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(
fun1(Expect<DataClass>::isNotSameAs),
fun1(Expect<Int?>::isNotSameAs).withNullableSuffix(),
fun1(Expect<DataClass?>::isNotSameAs).withNullableSuffix(),
fun2(Companion::isNoneOfInt),
fun2(Companion::isNoneOfDataClass),
fun2(Companion::isNoneOfIntNullable).withNullableSuffix(),
fun2(Companion::isNoneOfDataClassNullable).withNullableSuffix(),
fun1(Expect<Int>::isNotIn),
fun1(Expect<DataClass>::isNotIn),
fun1(Expect<Int?>::isNotIn).withNullableSuffix(),
fun1(Expect<DataClass?>::isNotIn).withNullableSuffix(),

"${Expect<Int?>::toBe.name}(null)" to Companion::toBeNull,
fun1(Expect<Int?>::toBeNullIfNullGivenElse),
Expand All @@ -39,10 +48,11 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(
"notToBeNull" to Companion::notToBeNull,

getAndImmediatePair(),
getAndLazyPair()
getAndLazyPair(),
"- "
) {

companion object : WithAsciiReporter(){
companion object : WithAsciiReporter() {
private fun toBeNull(expect: Expect<Int?>) = expect toBe null

@Suppress("RemoveExplicitTypeArguments")
Expand Down Expand Up @@ -81,6 +91,18 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(

private fun notToBeNull(expect: Expect<Int?>, assertionCreator: Expect<Int>.() -> Unit) =
expect notToBeNull assertionCreator

private fun isNoneOfInt(expect: Expect<Int>, expected: Int, otherValues: Array<out Int>): Expect<Int> =
expect isNoneOf values(expected, *otherValues)

private fun isNoneOfIntNullable(expect: Expect<Int?>, expected: Int?, otherValues: Array<out Int?>): Expect<Int?> =
expect isNoneOf values(expected, *otherValues)

private fun isNoneOfDataClass(expect: Expect<DataClass>, expected: DataClass, otherValues: Array<out DataClass>): Expect<DataClass> =
expect isNoneOf values(expected, *otherValues)

private fun isNoneOfDataClassNullable(expect: Expect<DataClass?>, expected: DataClass?, otherValues: Array<out DataClass?>): Expect<DataClass?> =
expect isNoneOf values(expected, *otherValues)
}

@Suppress("unused")
Expand All @@ -98,6 +120,8 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(
a1 isNotSameAs 1.2
a1.isA<Int>()
a1.isA<Int> {}
a1 isNoneOf values(1, 2)
a1 isNotIn listOf(1, 1.2)

a1b toBe 1
a1b toBe 1.2
Expand All @@ -109,6 +133,8 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(
a1b isNotSameAs 1.2
a1b.isA<Int>()
a1b.isA<Int> {}
a1b isNoneOf values(1, 2)
a1b isNotIn listOf(1, 1.2)

a1b notToBeNull o toBe 1
a1b notToBeNull {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.tutteli.atrium.logic.impl

import ch.tutteli.atrium.assertions.Assertion
import ch.tutteli.atrium.assertions.builders.assertionBuilder
import ch.tutteli.atrium.core.trueProvider
import ch.tutteli.atrium.creating.AssertionContainer
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.domain.creating.changers.ChangedSubjectPostStep
Expand Down Expand Up @@ -54,8 +55,13 @@ class DefaultAnyAssertions : AnyAssertions {
.downCastTo(subType)
.build()

override fun <T> isNotIn(container: AssertionContainer<T>, expected: List<T>): Assertion {
val assertions = expected.map { assertionBuilder.representationOnly.failing.withRepresentation(it).build() }
override fun <T> isNotIn(container: AssertionContainer<T>, expected: Iterable<T>): Assertion {
val assertions = expected.map { value ->
assertionBuilder.representationOnly
.withTest(container) { it != value }
.withRepresentation(value)
.build()
}
return assertionBuilder.list
.withDescriptionAndEmptyRepresentation(IS_NONE_OF)
.withAssertions(assertions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ inline fun <reified T> iterableLikeToIterable(iterableLike: IterableLike): Itera
is FloatArray -> iterableLike.map { it as T }
is DoubleArray -> iterableLike.map { it as T }
is BooleanArray -> iterableLike.map { it as T }
else -> throw IllegalArgumentException("toVarArg accepts arguments of types Iterable, Sequence, Array")
else -> throw IllegalArgumentException("iterableLikeToIterable accepts arguments of types Iterable, Sequence, Array")
}

inline fun <reified T> iterableToPair(iterable: Iterable<T>): Pair<T, Array<out T>> {
Expand Down
Loading

0 comments on commit bdd894b

Please sign in to comment.