Skip to content

Commit

Permalink
Merge branch 'master' into robstoll#245-samples-build
Browse files Browse the repository at this point in the history
  • Loading branch information
assaflei committed Apr 15, 2020
2 parents f6e09c6 + c444526 commit b030d3b
Show file tree
Hide file tree
Showing 29 changed files with 682 additions and 478 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ import kotlin.jvm.JvmName
* @return This plant to support a fluent API.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
@Deprecated(
"Switch from Assert to Expect; will be removed with 1.0.0",
ReplaceWith(
"this.asExpect().toBe(expected).asAssert()",
"ch.tutteli.atrium.domain.builders.migration.asExpect",
"ch.tutteli.atrium.domain.builders.migration.asAssert",
"ch.tutteli.atrium.api.infix.en_GB.toBe"
)
)
infix fun <T : Any> Assert<T>.toBe(expected: T)
= addAssertion(AssertImpl.any.toBe(this, expected))

Expand All @@ -41,6 +50,15 @@ infix fun <T: Any> Assert<T>.toBe(keyword: Keyword): Nothing
* @return This plant to support a fluent API.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
@Deprecated(
"Switch from Assert to Expect; will be removed with 1.0.0",
ReplaceWith(
"this.asExpect().notToBe(expected).asAssert()",
"ch.tutteli.atrium.domain.builders.migration.asExpect",
"ch.tutteli.atrium.domain.builders.migration.asAssert",
"ch.tutteli.atrium.api.infix.en_GB.notToBe"
)
)
infix fun <T : Any> Assert<T>.notToBe(expected: T)
= addAssertion(AssertImpl.any.notToBe(this, expected))

Expand All @@ -58,6 +76,15 @@ infix fun <T: Any> Assert<T>.notToBe(keyword: Keyword): Nothing
* @return This plant to support a fluent API.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
@Deprecated(
"Switch from Assert to Expect; will be removed with 1.0.0",
ReplaceWith(
"this.asExpect().isSameAs(expected).asAssert()",
"ch.tutteli.atrium.domain.builders.migration.asExpect",
"ch.tutteli.atrium.domain.builders.migration.asAssert",
"ch.tutteli.atrium.api.infix-en_GB.isSameAs"
)
)
infix fun <T : Any> Assert<T>.isSameAs(expected: T)
= addAssertion(AssertImpl.any.isSame(this, expected))

Expand All @@ -70,6 +97,15 @@ infix fun <T : Any> Assert<T>.isSameAs(expected: T)
* @return This plant to support a fluent API.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
@Deprecated(
"Switch from Assert to Expect; will be removed with 1.0.0",
ReplaceWith(
"this.asExpect().isNotSameAs(expected).asAssert()",
"ch.tutteli.atrium.domain.builders.migration.asExpect",
"ch.tutteli.atrium.domain.builders.migration.asAssert",
"ch.tutteli.atrium.api.infix-en_GB.isNotSameAs"
)
)
infix fun <T : Any> Assert<T>.isNotSameAs(expected: T)
= addAssertion(AssertImpl.any.isNotSame(this, expected))

Expand All @@ -80,6 +116,15 @@ infix fun <T : Any> Assert<T>.isNotSameAs(expected: T)
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
@Suppress("DEPRECATION")
@Deprecated(
"Switch from Assert to Expect; will be removed with 1.0.0",
ReplaceWith(
"this.asExpect().toBe(expected).asAssert()",
"ch.tutteli.atrium.domain.builders.migration.asExpect",
"ch.tutteli.atrium.domain.builders.migration.asAssert",
"ch.tutteli.atrium.api.infix.en_GB.toBe"
)
)
inline infix fun <reified T : Any> AssertionPlantNullable<T?>.toBe(expected: T?) {
addAssertion(AssertImpl.any.isNullable(this, T::class, expected))
}
Expand All @@ -100,6 +145,15 @@ inline infix fun <reified T : Any> AssertionPlantNullable<T?>.toBe(expected: T?)
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
@Suppress("DEPRECATION")
@Deprecated(
"Switch from Assert to Expect; will be removed with 1.0.0",
ReplaceWith(
"this.asExpect().toBeNullIfNullGivenElse(assertionCreatorOrNull).asAssert()",
"ch.tutteli.atrium.domain.builders.migration.asExpect",
"ch.tutteli.atrium.domain.builders.migration.asAssert",
"ch.tutteli.atrium.api.infix.en_GB.toBeNullIfNullGivenElse"
)
)
inline infix fun <reified T : Any> AssertionPlantNullable<T?>.toBeNullIfNullGivenElse(noinline assertionCreatorOrNull: (Assert<T>.() -> Unit)?) {
addAssertion(AssertImpl.any.isNullIfNullGivenElse(this, T::class, assertionCreatorOrNull))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
@file:Suppress("DEPRECATION" /* will be removed with 1.0.0 */)

package ch.tutteli.atrium.api.cc.infix.en_GB

import ch.tutteli.atrium.api.infix.en_GB.isNotSameAs
import ch.tutteli.atrium.api.infix.en_GB.isSameAs
import ch.tutteli.atrium.api.infix.en_GB.notToBe
import ch.tutteli.atrium.api.infix.en_GB.toBe
import ch.tutteli.atrium.verbs.internal.AssertionVerbFactory
import ch.tutteli.atrium.creating.Assert
import ch.tutteli.atrium.creating.AssertionPlantNullable
import ch.tutteli.atrium.domain.builders.migration.asAssert
import ch.tutteli.atrium.domain.builders.migration.asExpect
import kotlin.reflect.KFunction2

//TODO remove with 1.0.0, no need to migrate to Spek 2
Expand All @@ -22,10 +29,10 @@ class AnyAssertionsSpec : ch.tutteli.atrium.spec.integration.AnyAssertionsSpec(
getAndLazyPair()
) {
class AnyAssertionsSpecFunFactory<T : Any> : ch.tutteli.atrium.spec.integration.AnyAssertionsSpec.AnyAssertionsSpecFunFactory<T> {
override val toBeFun: Assert<T>.(T) -> Assert<T> = { o toBe it }
override val notToBeFun: Assert<T>.(T) -> Assert<T> = { o notToBe it }
override val isSameFun: Assert<T>.(T) -> Assert<T> = { o isSameAs it }
override val isNotSameFun: Assert<T>.(T) -> Assert<T> = { o isNotSameAs it }
override val toBeFun: Assert<T>.(T) -> Assert<T> = { o.asExpect().toBe(it).asAssert() }
override val notToBeFun: Assert<T>.(T) -> Assert<T> = { o.asExpect().notToBe(it).asAssert() }
override val isSameFun: Assert<T>.(T) -> Assert<T> = { o.asExpect().isSameAs(it).asAssert() }
override val isNotSameFun: Assert<T>.(T) -> Assert<T> = { o.asExpect().isNotSameAs(it).asAssert() }
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ fun <E : Any, T : Iterable<E?>> Expect<T>.containsExactly(
vararg otherAssertionCreatorsOrNulls: (Expect<E>.() -> Unit)?
): Expect<T> = contains.inOrder.only.entries(assertionCreatorOrNull, *otherAssertionCreatorsOrNulls)

/**
* Expects that the subject of the assertion (an [Iterable]) contains only elements of [expectedIterable]
* in same order
*
* It is a shortcut for 'contains.inOrder.only.elementsOf(anotherList)'
*
* @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.11.0
*/
inline fun <reified E, T : Iterable<E>> Expect<T>.containsExactlyElementsOf(
expectedIterable: Iterable<E>
): Expect<T> = contains.inOrder.only.elementsOf(expectedIterable)

/** Expects that the subject of the assertion (an [Iterable]) contains all elements of [expectedIterable].
*
* It is a shortcut for `contains.inAnyOrder.atLeast(1).elementsOf(expectedIterable)`
*
* @param expectedIterable The [Iterable] whose elements are expected to be contained within this [Iterable].
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
* @throws IllegalArgumentException in case the given [expectedIterable] does not have elements (is empty).
*
* @since 0.11.0
*/
inline fun <reified E, T : Iterable<E>> Expect<T>.containsElementsOf(expectedIterable: Iterable<E>): Expect<T> =
contains.inAnyOrder.atLeast(1).elementsOf(expectedIterable)

/**
* Expects that the subject of the assertion (an [Iterable]) does not contain the [expected] value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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.withFeatureSuffix
import ch.tutteli.atrium.specs.withNullableSuffix
import kotlin.reflect.KFunction2
import kotlin.reflect.KProperty1
Expand All @@ -26,16 +28,15 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(

"${Expect<Int?>::toBe.name}(null)" to Companion::toBeNull,
fun1(Expect<Int?>::toBeNullIfNullGivenElse),
"isA" to Companion::isAFeature,
"isA" to Companion::isAStringToInt,
"isA" to Companion::isAStringToInt,
"isA" to Companion::isAString,
"isA" to Companion::isACharSequence,
("isA" to Companion::isAIntFeature).withFeatureSuffix(),
"isA" to Companion::isAInt,
("isA" to Companion::isASuperTypeFeature).withFeatureSuffix(),
"isA" to Companion::isASuperType,
("isA" to Companion::isASubTypeFeature).withFeatureSuffix(),
"isA" to Companion::isASubType,
"isA" to Companion::isAIntLess,

feature0<Int?, Int>(Expect<Int?>::notToBeNull),
"notToBeNull" to Companion::notToBeNull,
Companion::notToBeNullLess,
Companion::notToBeNullGreaterAndLess,

getAndImmediatePair(),
getAndLazyPair()
Expand All @@ -45,46 +46,41 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec(
private fun toBeNull(expect: Expect<Int?>) = expect.toBe(null)

@Suppress("RemoveExplicitTypeArguments")
private fun isAFeature(expect: Expect<Int?>): Expect<Int> = expect.isA<Int>()
private fun isAIntFeature(expect: Expect<out Any?>): Expect<Int> =
expect.isA<Int>()

private val andImmediate: KProperty1<Expect<Int>, Expect<Int>> = Expect<Int>::and
fun getAndImmediatePair(): Pair<String, Expect<Int>.() -> Expect<Int>> = andImmediate.name to Expect<Int>::and
@Suppress("RemoveExplicitTypeArguments")
private fun isAInt(expect: Expect<out Any?>, assertionCreator: Expect<Int>.() -> Unit): Expect<Int> =
expect.isA<Int> { assertionCreator() }

private val andLazyName: KFunction2<Expect<Int>, Expect<Int>.() -> Unit, Expect<Int>> = Expect<Int>::and
fun getAndLazyPair(): Pair<String, Expect<Int>.(Expect<Int>.() -> Unit) -> Expect<Int>> =
andLazyName.name to Expect<Int>::and
@Suppress("RemoveExplicitTypeArguments")
private fun isASuperTypeFeature(expect: Expect<out Any?>): Expect<SuperType> =
expect.isA<SuperType>()

private inline fun <reified TSub : Any> isA(
expect: Expect<*>,
noinline assertionCreator: Expect<TSub>.() -> Unit
) = expect.isA(assertionCreator)
@Suppress("RemoveExplicitTypeArguments")
private fun isASuperType(expect: Expect<out Any?>, assertionCreator: Expect<SuperType>.() -> Unit): Expect<SuperType> =
expect.isA<SuperType> { assertionCreator() }

//TODO get rid of different overloads as soon as https://youtrack.jetbrains.com/issue/KT-19884 is fixed
private fun isAStringToInt(expect: Expect<*>, assertionCreator: Expect<Int>.() -> Unit) =
isA(expect, assertionCreator)
@Suppress("RemoveExplicitTypeArguments")
private fun isASubTypeFeature(expect: Expect<out Any?>): Expect<SubType> =
expect.isA<SubType>()

private fun isAString(expect: Expect<*>, assertionCreator: Expect<String>.() -> Unit) =
isA(expect, assertionCreator)
@Suppress("RemoveExplicitTypeArguments")
private fun isASubType(expect: Expect<out Any?>, assertionCreator: Expect<SubType>.() -> Unit): Expect<SubType> =
expect.isA<SubType> { assertionCreator() }

private fun isACharSequence(expect: Expect<*>, assertionCreator: Expect<CharSequence>.() -> Unit) =
isA(expect, assertionCreator)
private val andImmediate: KProperty1<Expect<Int>, Expect<Int>> = Expect<Int>::and
fun getAndImmediatePair(): Pair<String, Expect<Int>.() -> Expect<Int>> = andImmediate.name to Expect<Int>::and

private fun isASubType(expect: Expect<*>, assertionCreator: Expect<SubType>.() -> Unit) =
isA(expect, assertionCreator)
private val andLazyName: KFunction2<Expect<Int>, Expect<Int>.() -> Unit, Expect<Int>> = Expect<Int>::and
fun getAndLazyPair(): Pair<String, Expect<Int>.(Expect<Int>.() -> Unit) -> Expect<Int>> =
andLazyName.name to Expect<Int>::and

private fun isAIntLess(expect: Expect<Number>, number: Int) = expect.isA<Int> { isLessThan(number) }
private fun notToBeNullFeature(expect: Expect<Int?>) =
expect.notToBeNull()

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

private fun notToBeNullLess(expect: Expect<Int?>, number: Int) =
expect.notToBeNull { isLessThan(number) }

private fun notToBeNullGreaterAndLess(expect: Expect<Int?>, lowerBound: Int, upperBound: Int) =
expect.notToBeNull {
isGreaterThan(lowerBound)
isLessThan(upperBound)
}
}

//regression for #298, should compile without the need for E : Any or List<E?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ package ch.tutteli.atrium.api.fluent.en_GB

import ch.tutteli.atrium.api.verbs.internal.expect
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.kbox.glue
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.reflect.KFunction2

class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
include(BuilderSpec)
include(ShortcutSpec)

describe("elementsOf") {
describe("contains.inAnyOrder.atLeast(1).elementsOf") {
it("passing an empty iterable throws an IllegalArgumentException") {
expect {
expect(listOf(1, 2)).contains.inAnyOrder.atLeast(1).elementsOf(listOf())
}.toThrow<IllegalArgumentException>()
}
}

describe("containsElementsOf") {
it("passing an empty iterable throws an IllegalArgumentException") {
expect {
expect(listOf(1, 2)).containsElementsOf(listOf())
}.toThrow<IllegalArgumentException>()
}
}
}) {
object BuilderSpec : ch.tutteli.atrium.specs.integration.IterableContainsInAnyOrderAtLeast1ValuesAssertionsSpec(
getContainsPair(),
Expand All @@ -23,6 +34,13 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
"[Atrium][Builder] "
)

object ShortcutSpec : ch.tutteli.atrium.specs.integration.IterableContainsInAnyOrderAtLeast1ValuesAssertionsSpec(
getContainsShortcutPair(),
getContainsNullableShortcutPair(),
"◆ ",
"[Atrium][Shortcut] "
)

companion object : IterableContainsSpecBase() {
fun getContainsPair() = "$contains.$inAnyOrder.$atLeast(1).$inAnyOrderElementsOf" to Companion::containsValues

Expand All @@ -41,5 +59,26 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
aX: Array<out Double?>
): Expect<Iterable<Double?>> = expect.contains.inAnyOrder.atLeast(1).elementsOf(listOf(a, *aX))

private val containsElementsOfShortcutFun: KFunction2<Expect<Iterable<Double>>, Iterable<Double>, Expect<Iterable<Double>>> =
Expect<Iterable<Double>>::containsElementsOf

private fun getContainsShortcutPair() = containsElementsOfShortcutFun.name to Companion::containsInAnyOrderShortcut

private fun containsInAnyOrderShortcut(
expect: Expect<Iterable<Double>>,
a: Double,
aX: Array<out Double>
): Expect<Iterable<Double>> = expect.containsElementsOf(listOf(a, *aX))

private val containsElementsOfNullableShortcutFun: KFunction2<Expect<Iterable<Double?>>, Iterable<Double?>, Expect<Iterable<Double?>>> =
Expect<Iterable<Double?>>::containsElementsOf

private fun getContainsNullableShortcutPair() = containsElementsOfNullableShortcutFun.name to Companion::containsInAnyOrderNullableShortcut;

private fun containsInAnyOrderNullableShortcut(
expect: Expect<Iterable<Double?>>,
a: Double?,
aX: Array<out Double?>
): Expect<Iterable<Double?>> = expect.containsElementsOf(listOf(a, *aX))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.spekframework.spek2.style.specification.describe
class IterableContainsInAnyOrderOnlyElementsOfAssertionSpec : Spek({
include(BuilderSpec)

describe("elementsOf") {
describe("contains.inAnyOrder.only.elementsOf") {
it("passing an empty iterable throws an IllegalArgumentException") {
expect {
expect(listOf(1, 2)).contains.inAnyOrder.only.elementsOf(listOf())
Expand Down
Loading

0 comments on commit b030d3b

Please sign in to comment.