Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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.atrium.specs.integration.AbstractIterableNotToContainEntriesExpectationsTest
import ch.tutteli.atrium.specs.withNullableSuffix
import kotlin.test.Test

class IterableNotToContainEntriesExpectationsTest :
AbstractIterableNotToContainEntriesExpectationsTest(
functionDescription to Companion::notToContainFun,
(functionDescription to Companion::notToContainNullableFun).withNullableSuffix(),
Expect<List<Int>>::notToHaveElementsOrNone.name,
) {
Comment on lines +9 to +14

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: can you put it on the same line please

Suggested change
class IterableNotToContainEntriesExpectationsTest :
AbstractIterableNotToContainEntriesExpectationsTest(
functionDescription to Companion::notToContainFun,
(functionDescription to Companion::notToContainNullableFun).withNullableSuffix(),
Expect<List<Int>>::notToHaveElementsOrNone.name,
) {
class IterableNotToContainEntriesExpectationsTest : AbstractIterableNotToContainEntriesExpectationsTest(
functionDescription to Companion::notToContainFun,
(functionDescription to Companion::notToContainNullableFun).withNullableSuffix(),
Expect<List<Int>>::notToHaveElementsOrNone.name,
) {

companion object : IterableToContainSpecBase() {
private val functionDescription = "$notToContain.$entry/$entries"

private fun notToContainFun(
expect: Expect<Iterable<Double>>,
a: Expect<Double>.() -> Unit,
aX: Array<out Expect<Double>.() -> Unit>
): Expect<Iterable<Double>> =
if (aX.isEmpty()) expect.notToContain.entry(a)
else expect.notToContain.entries(a, *aX)

private fun notToContainNullableFun(
expect: Expect<Iterable<Double?>>,
a: (Expect<Double>.() -> Unit)?,
aX: Array<out (Expect<Double>.() -> Unit)?>
): Expect<Iterable<Double?>> =
if (aX.isEmpty()) expect.notToContain.entry(a)
else expect.notToContain.entries(a, *aX)
}


@Test
@Suppress("AssignedValueIsNeverRead")
fun ambiguityTest() {
var list: Expect<List<Number>> = expect(listOf(1, 2, 3))
var nList: Expect<Set<Number?>> = expect(setOf(1, 2))
var subList: Expect<ArrayList<Number>> = expect(arrayListOf(1, 2, 3))
var star: Expect<Collection<*>> = expect(listOf(1, 2))


list = list.notToContain.value(4)
nList = nList.notToContain.value(4)
subList = subList.notToContain.value(4)
star = star.notToContain.value(4)

nList = nList.notToContain.value(null)
star = star.notToContain.value(null)

list = list.notToContain.values(5, 7)
nList = nList.notToContain.values(3, 4)
subList = subList.notToContain.values(5, 6)
star = star.notToContain.values(7, 8)

nList = nList.notToContain.values(null, 8)
star = star.notToContain.values(null, {})
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ch.tutteli.atrium.api.infix.en_GB

import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.specs.integration.AbstractIterableNotToContainEntriesExpectationsTest
import ch.tutteli.atrium.specs.withNullableSuffix

class IterableNotToContainEntriesExpectationsSpec :
ch.tutteli.atrium.specs.integration.IterableNotToContainEntriesExpectationsSpec(
class IterableNotToContainEntriesExpectationsTest :
AbstractIterableNotToContainEntriesExpectationsTest(
getNotToContainPair(),
getNotToContainNullablePair().withNullableSuffix(),
Expect<List<Int>>::notToHaveElementsOrNone.name,
"[Atrium][Builder] "
) {

companion object : IterableToContainSpecBase() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
package ch.tutteli.atrium.specs.integration

import ch.tutteli.atrium.api.fluent.en_GB.message
import ch.tutteli.atrium.api.fluent.en_GB.notToContain
import ch.tutteli.atrium.api.fluent.en_GB.regex
import ch.tutteli.atrium.api.fluent.en_GB.toBeGreaterThan
import ch.tutteli.atrium.api.fluent.en_GB.toBeLessThan
import ch.tutteli.atrium.api.fluent.en_GB.toContainRegex
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.fluent.en_GB.toThrow
import ch.tutteli.atrium.api.verbs.internal.expect
import ch.tutteli.atrium.core.polyfills.format
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.logic.utils.expectLambda
import ch.tutteli.atrium.specs.Fun2
import ch.tutteli.atrium.specs.explanatoryBulletPoint
import ch.tutteli.atrium.specs.forExpectationCreatorTest
import ch.tutteli.atrium.specs.forSubjectLessTest
import ch.tutteli.atrium.specs.hintBulletPoint
import ch.tutteli.atrium.specs.indentListBulletPoint
import ch.tutteli.atrium.specs.indentRootBulletPoint
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.afterExplanatory
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.afterExplanatoryIndent
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.afterMismatchedWarning
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.anElementWhichNeedsDescr
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.hasANextElement
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.index
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.mismatchedIndex
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.toBeGreaterThanFun
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.toBeLessThanFun
import ch.tutteli.atrium.specs.integration.IterableToContainEntriesSpecBase.Companion.toEqualFun
import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.mismatches
import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.oneToSeven
import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.oneToSevenNullable
import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.separator
import ch.tutteli.atrium.specs.integration.utils.ExpectationCreatorTestData
import ch.tutteli.atrium.specs.integration.utils.ExpectationCreatorTriple
import ch.tutteli.atrium.specs.integration.utils.SubjectLessTestData
import ch.tutteli.atrium.specs.invoke
import ch.tutteli.atrium.specs.listBulletPoint
import ch.tutteli.atrium.specs.rootBulletPoint
import ch.tutteli.atrium.specs.toEqualDescr
import ch.tutteli.atrium.specs.warningBulletPoint
import ch.tutteli.atrium.testfactories.TestFactory
import ch.tutteli.atrium.translations.DescriptionComparableExpectation
import ch.tutteli.atrium.translations.DescriptionIterableLikeExpectation
import ch.tutteli.kbox.toVararg


@Suppress("FunctionName")
abstract class AbstractIterableNotToContainEntriesExpectationsTest(
private val notToContainEntriesSpec: Fun2<Iterable<Double>, Expect<Double>.() -> Unit, Array<out Expect<Double>.() -> Unit>>,
private val notToContainNullableEntriesSpec: Fun2<Iterable<Double?>, (Expect<Double>.() -> Unit)?, Array<out (Expect<Double>.() -> Unit)?>>,
private val notToHaveElementsOrNoneFunName: String,
) : ExpectationFunctionBaseTest() {

@TestFactory
fun subjectLessTest() = subjectLessTestFactory(
SubjectLessTestData(
notToContainEntriesSpec.forSubjectLessTest({ toEqual(2.3) }, arrayOf())
),
SubjectLessTestData(
notToContainNullableEntriesSpec.forSubjectLessTest({ toEqual(2.3) }, arrayOf())
)
)

private val notToContainDescr = DescriptionIterableLikeExpectation.NOT_TO_CONTAIN.getDefault()
private val toBeLessThanDescr = DescriptionComparableExpectation.TO_BE_LESS_THAN.getDefault()
private val toBeGreaterThanDescr = DescriptionComparableExpectation.TO_BE_GREATER_THAN.getDefault()

@TestFactory
fun expectationCreatorTest() = expectationCreatorTestFactory(

notToContainEntriesSpec.forExpectationCreatorTest(
"$toBeGreaterThanDescr: 8.0",
"$toBeGreaterThanDescr: 10.0",
{ toBeGreaterThan(8.0) }, arrayOf(expectLambda { toBeGreaterThan(10.0) })
).toVararg().let { (first, rest) ->
ExpectationCreatorTestData(
oneToSeven().toList().asIterable(),
first as ExpectationCreatorTriple<Iterable<Double>>,
*(rest as Array<ExpectationCreatorTriple<Iterable<Double>>>)
)
},

notToContainNullableEntriesSpec.forExpectationCreatorTest(
"$toBeGreaterThanDescr: 8.0",
"$toBeGreaterThanDescr: 10.0",
{ toBeGreaterThan(8.0) }, arrayOf(expectLambda { toBeGreaterThan(10.0) })
).toVararg().let { (first, rest) ->
ExpectationCreatorTestData(
oneToSeven().toList().asIterable(),
first as ExpectationCreatorTriple<Iterable<Double?>>,
*(rest as Array<ExpectationCreatorTriple<Iterable<Double?>>>),
groupPrefix = "[nullable Element] "
)
},
)
Comment on lines +71 to +98

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see before that you use casts here, something is odd, this shouldn't be necessary. I took a closer look, seems like kotlin apiVersion 1.4 has problems inferring the types correctly for toVarArg(). You can use the following, which doesn't use casts but requires additional type hints for the compiler

Suggested change
@TestFactory
fun expectationCreatorTest() = expectationCreatorTestFactory(
notToContainEntriesSpec.forExpectationCreatorTest(
"$toBeGreaterThanDescr: 8.0",
"$toBeGreaterThanDescr: 10.0",
{ toBeGreaterThan(8.0) }, arrayOf(expectLambda { toBeGreaterThan(10.0) })
).toVararg().let { (first, rest) ->
ExpectationCreatorTestData(
oneToSeven().toList().asIterable(),
first as ExpectationCreatorTriple<Iterable<Double>>,
*(rest as Array<ExpectationCreatorTriple<Iterable<Double>>>)
)
},
notToContainNullableEntriesSpec.forExpectationCreatorTest(
"$toBeGreaterThanDescr: 8.0",
"$toBeGreaterThanDescr: 10.0",
{ toBeGreaterThan(8.0) }, arrayOf(expectLambda { toBeGreaterThan(10.0) })
).toVararg().let { (first, rest) ->
ExpectationCreatorTestData(
oneToSeven().toList().asIterable(),
first as ExpectationCreatorTriple<Iterable<Double?>>,
*(rest as Array<ExpectationCreatorTriple<Iterable<Double?>>>),
groupPrefix = "[nullable Element] "
)
},
)
@TestFactory
fun expectationCreatorTest(): Any {
//TODO 2.0.0 try to inline again in case we require kotlin 1.6 at least
val doubleCase: Pair<ExpectationCreatorTriple<Iterable<Double>>, Array<out ExpectationCreatorTriple<Iterable<Double>>>> =
notToContainEntriesSpec.forExpectationCreatorTest(
"${toBeGreaterThanDescr}: 8.0",
"${toBeGreaterThanDescr}: 10.0",
{ toBeGreaterThan(8.0) }, arrayOf(expectLambda { toBeGreaterThan(10.0) })
).toVararg()
val nullableDoubleCase: Pair<ExpectationCreatorTriple<Iterable<Double?>>, Array<out ExpectationCreatorTriple<Iterable<Double?>>>> =
notToContainNullableEntriesSpec.forExpectationCreatorTest(
"${toBeGreaterThanDescr}: 8.0",
"${toBeGreaterThanDescr}: 10.0",
{ toBeGreaterThan(8.0) }, arrayOf(expectLambda { toBeGreaterThan(10.0) })
).toVararg()
return expectationCreatorTestFactory(
ExpectationCreatorTestData(oneToSeven().toList().asIterable(), doubleCase.first, *doubleCase.second),
ExpectationCreatorTestData(
oneToSeven().toList().asIterable(),
nullableDoubleCase.first,
*nullableDoubleCase.second,
groupPrefix = "[nullable Element] "
)
)
}




private fun Expect<Iterable<Double?>>.notToContainNullableFun(
a: (Expect<Double>.() -> Unit)?,
vararg aX: (Expect<Double>.() -> Unit)?,
) = notToContainNullableEntriesSpec(this, a, aX)

@TestFactory
fun empty_iterable__throws_AssertionError() =
nonNullableCases(notToContainEntriesSpec, notToContainNullableEntriesSpec) { notToContainFunArr ->
fun Expect<Iterable<Double>>.notToContainFun(
a: Expect<Double>.() -> Unit,
vararg aX: Expect<Double>.() -> Unit,
) = notToContainFunArr(a, aX)

expect {
expect(setOf<Double>().asIterable()).notToContainFun({ toEqual(4.0) })
}.toThrow<AssertionError> {
message {
toContainRegex(
"$hasANextElement$separator" +
"$indentRootBulletPoint\\Q$explanatoryBulletPoint\\E$notToContainDescr: $separator" +
"$indentRootBulletPoint$indentListBulletPoint\\Q$listBulletPoint\\E$anElementWhichNeedsDescr: $separator" +
"$indentListBulletPoint$afterExplanatory$toEqualDescr: 4.0.*",
"$hintBulletPoint${
DescriptionIterableLikeExpectation.USE_NOT_TO_HAVE_ELEMENTS_OR_NONE.getDefault()
.format(notToHaveElementsOrNoneFunName)
}"
)
}
}
}

@TestFactory
fun iterable__happy_case() =
nonNullableCases(notToContainEntriesSpec, notToContainNullableEntriesSpec) { notToContainFunArr ->
fun Expect<Iterable<Double>>.notToContainFun(
a: Expect<Double>.() -> Unit,
vararg aX: Expect<Double>.() -> Unit,
) = notToContainFunArr(a, aX)
testFactory(notToContainEntriesSpec) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that I took a closer look, that won't work as it should, nonNullableCases is already a testFactory behind the scene, you cannot nest them. I suggest you remove the it(..) in the following lines and just keep expect(...) for now

it("$toBeGreaterThanFun(1.0) and $toBeLessThanFun(2.0) does not throw") {
expect(oneToSeven()).notToContainFun({ toBeGreaterThan(1.0); toBeLessThan(2.0) })
}
it("$toEqualFun(1.1), $toEqualFun(2.2), $toEqualFun(3.3) does not throw") {
expect(oneToSeven()).notToContainFun({ toEqual(1.1) }, { toEqual(2.2) }, { toEqual(3.3) })
}
it("$toEqualFun(3.3), $toEqualFun(1.1), $toEqualFun(2.2) does not throw") {
expect(oneToSeven()).notToContainFun({ toEqual(3.3) }, { toEqual(1.1) }, { toEqual(2.2) })
}
}
expect(oneToSeven()).notToContainFun({ toBeGreaterThan(1.0); toBeLessThan(2.0) })

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't spot before but you need to remove this one (is the first case in testFactory):

Suggested change
expect(oneToSeven()).notToContainFun({ toBeGreaterThan(1.0); toBeLessThan(2.0) })

}

@TestFactory
fun iterable__failing_cases_search_string_at_different_positions() {
nonNullableCases(notToContainEntriesSpec, notToContainNullableEntriesSpec) { notToContainFunArr ->
fun Expect<Iterable<Double>>.notToContainFun(
a: Expect<Double>.() -> Unit,
vararg aX: Expect<Double>.() -> Unit,
) = notToContainFunArr(a, aX)

testFactory(notToContainEntriesSpec) {
it("$toBeLessThanFun(4.0) throws AssertionError") {
expect {
expect(oneToSeven()).notToContainFun({ toBeLessThan(4.0) })
}.toThrow<AssertionError> {
message {
toContainRegex(
"\\Q$rootBulletPoint\\E$notToContainDescr: $separator" +
"$indentRootBulletPoint\\Q$listBulletPoint\\E$anElementWhichNeedsDescr: $separator" +
"$afterExplanatory$toBeLessThanDescr: 4.0.*$separator" +
"$afterExplanatoryIndent\\Q$warningBulletPoint$mismatches:\\E $separator" +
"$afterMismatchedWarning${mismatchedIndex(0, "1.0")}.*$separator" +
"$afterMismatchedWarning${mismatchedIndex(1, "2.0")}.*$separator" +
"$afterMismatchedWarning${mismatchedIndex(5, "3.0")}.*"
)
}
}
}
it("$toEqualFun(1.0), $toEqualFun(4.0) throws AssertionError") {
expect {
expect(oneToSeven()).notToContainFun({ toEqual(1.0) }, { toEqual(4.0) })
}.toThrow<AssertionError> {
message {
toContainRegex(
"\\Q$rootBulletPoint\\E$notToContainDescr: $separator" +
"$indentRootBulletPoint\\Q$listBulletPoint\\E$anElementWhichNeedsDescr: $separator" +
"$afterExplanatory$toEqualDescr: 1.0.*$separator" +
"$afterExplanatoryIndent\\Q$warningBulletPoint$mismatches:\\E $separator" +
"$afterMismatchedWarning${mismatchedIndex(0, "1.0")}.*$separator" +
"$indentRootBulletPoint\\Q$listBulletPoint\\E$anElementWhichNeedsDescr: $separator" +
"$afterExplanatory$toEqualDescr: 4.0.*$separator" +
"$afterExplanatoryIndent\\Q$warningBulletPoint$mismatches:\\E $separator" +
"$afterMismatchedWarning${mismatchedIndex(2, "4.0")}.*$separator" +
"$afterMismatchedWarning${mismatchedIndex(3, "4.0")}.*$separator" +
"$afterMismatchedWarning${mismatchedIndex(8, "4.0")}.*"
)
}
}
}
it("$toEqualFun(4.0), $toEqualFun(1.1) throws AssertionError mentioning only 4.0") {
expect {
expect(oneToSeven()).notToContainFun({ toEqual(4.0) }, { toEqual(1.0) })
}.toThrow<AssertionError> {
message {
toContainRegex("$anElementWhichNeedsDescr: $separator.*$toEqualDescr: 4.0")
notToContain.regex("$anElementWhichNeedsDescr: $separator.*$toEqualDescr: 1.1")
}
}
}
}
}
}

@TestFactory
fun iterable__null_does_not_throw() {
expect(oneToSeven() as Iterable<Double?>).notToContainNullableFun(null)
}

@TestFactory
fun iterable__null() {
testFactory(notToContainNullableEntriesSpec) {
Comment on lines +221 to +222

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise no tests are executed

Suggested change
fun iterable__null() {
testFactory(notToContainNullableEntriesSpec) {
fun iterable__null() = testFactory(notToContainNullableEntriesSpec) {

it("null throws AssertionError") {
expect {
expect(oneToSevenNullable()).notToContainNullableFun(null)
}.toThrow<AssertionError> {
message {
toContainRegex(
"\\Q$rootBulletPoint\\E$notToContainDescr: $separator" +
"$indentRootBulletPoint\\Q$listBulletPoint\\E$anElementWhichNeedsDescr: $separator" +
"$afterExplanatory$toEqualDescr: null$separator" +
"$afterExplanatoryIndent\\Q$warningBulletPoint$mismatches:\\E $separator" +
"$afterMismatchedWarning${mismatchedIndex(1, "null")}.*$separator" +
"$afterMismatchedWarning${mismatchedIndex(5, "null")}.*"
)
}
}
}
it("1.1, null throws AssertionError mentioning only null") {
expect {
expect(oneToSevenNullable()).notToContainNullableFun({ toEqual(1.1) }, null)
}.toThrow<AssertionError> {
message {
toContainRegex(
"\\Q$rootBulletPoint\\E$notToContainDescr: $separator" +
"$indentRootBulletPoint\\Q$listBulletPoint\\E$anElementWhichNeedsDescr: $separator" +
"$afterExplanatory$toEqualDescr: null$separator" +
"$afterExplanatoryIndent\\Q$warningBulletPoint$mismatches:\\E $separator" +
"$afterMismatchedWarning${index(1)}: null.*$separator" +
"$afterMismatchedWarning${index(5)}: null.*"
)
this.notToContain("$notToContainDescr: 1.1")
}
}
}
}
}
}
Loading
Loading