|
| 1 | +package ch.tutteli.atrium.specs.integration |
| 2 | + |
| 3 | +import ch.tutteli.atrium.api.fluent.en_GB.* |
| 4 | +import ch.tutteli.atrium.core.polyfills.format |
| 5 | +import ch.tutteli.atrium.creating.Expect |
| 6 | +import ch.tutteli.atrium.logic.utils.expectLambda |
| 7 | +import ch.tutteli.atrium.specs.* |
| 8 | +import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.fluentEmpty |
| 9 | +import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.mismatches |
| 10 | +import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.oneToSeven |
| 11 | +import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.oneToSevenNullable |
| 12 | +import ch.tutteli.atrium.specs.integration.IterableToContainSpecBase.Companion.separator |
| 13 | +import ch.tutteli.atrium.specs.integration.utils.ExpectationCreatorTestData |
| 14 | +import ch.tutteli.atrium.specs.integration.utils.SubjectLessTestData |
| 15 | +import ch.tutteli.atrium.testfactories.TestFactory |
| 16 | +import ch.tutteli.atrium.translations.DescriptionIterableLikeExpectation |
| 17 | + |
| 18 | +@Suppress("FunctionName") |
| 19 | +abstract class AbstractIterableNotToHaveElementsOrAllExpectationsTest( |
| 20 | + private val notToHaveElementsOrAllSpec: Fun1<Iterable<Double>, Expect<Double>.() -> Unit>, |
| 21 | + private val notToHaveElementsOrAllNullableSpec: Fun1<Iterable<Double?>, (Expect<Double>.() -> Unit)?>, |
| 22 | +) : ExpectationFunctionBaseTest() { |
| 23 | + |
| 24 | + @TestFactory |
| 25 | + fun subjectLessTest() = subjectLessTestFactory( |
| 26 | + SubjectLessTestData<Iterable<Double>>( |
| 27 | + notToHaveElementsOrAllSpec.first to expectLambda { |
| 28 | + notToHaveElementsOrAllSpec.second(this) { |
| 29 | + toEqual(2.5) |
| 30 | + } |
| 31 | + } |
| 32 | + ), |
| 33 | + SubjectLessTestData<Iterable<Double?>>( |
| 34 | + "${notToHaveElementsOrAllNullableSpec.first} for nullable" to expectLambda { |
| 35 | + notToHaveElementsOrAllNullableSpec.second(this, null) |
| 36 | + } |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + @TestFactory |
| 41 | + fun expectationCreatorTest() = expectationCreatorTestFactory( |
| 42 | + ExpectationCreatorTestData( |
| 43 | + oneToSeven().toList().asIterable(), |
| 44 | + notToHaveElementsOrAllSpec.forExpectationCreatorTest("$toBeGreaterThanDescr: 0.0") { toBeGreaterThan(0.0) } |
| 45 | + ), |
| 46 | + ExpectationCreatorTestData( |
| 47 | + oneToSeven().toList().asIterable(), |
| 48 | + notToHaveElementsOrAllNullableSpec.forExpectationCreatorTest("$toBeGreaterThanDescr: 0.0") { |
| 49 | + toBeGreaterThan( |
| 50 | + 0.0 |
| 51 | + ) |
| 52 | + } |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + val allElementsDescr = DescriptionIterableLikeExpectation.NOT_TO_HAVE_ELEMENTS_OR_ALL.getDefault() |
| 57 | + |
| 58 | + val explanatoryPointWithIndent = "$indentRootBulletPoint$indentListBulletPoint$explanatoryBulletPoint" |
| 59 | + |
| 60 | + fun index(index: Int) = listBulletPoint + DescriptionIterableLikeExpectation.INDEX.getDefault().format(index) |
| 61 | + |
| 62 | + |
| 63 | + @TestFactory |
| 64 | + fun empty_collection__does_not_throw() = nonNullableCases( |
| 65 | + notToHaveElementsOrAllSpec, |
| 66 | + notToHaveElementsOrAllNullableSpec |
| 67 | + ) { noToHaveElementsOrAllFun -> |
| 68 | + expect(fluentEmpty()).noToHaveElementsOrAllFun { toBeLessThan(1.1) } |
| 69 | + } |
| 70 | + |
| 71 | + @TestFactory |
| 72 | + fun one_to_7__throws() = nonNullableCases( |
| 73 | + notToHaveElementsOrAllSpec, |
| 74 | + notToHaveElementsOrAllNullableSpec |
| 75 | + ) { notToHaveElementsOrAllFun -> |
| 76 | + expect { |
| 77 | + expect(oneToSeven()).notToHaveElementsOrAllFun { toBeGreaterThan(2.5); toBeLessThan(7.1) } |
| 78 | + }.toThrow<AssertionError> { |
| 79 | + message { |
| 80 | + toContain.exactly(1).values( |
| 81 | + "$rootBulletPoint$allElementsDescr: $separator", |
| 82 | + "$explanatoryPointWithIndent$toBeGreaterThanDescr: 2.5", |
| 83 | + "$explanatoryPointWithIndent$toBeLessThanDescr: 7.1", |
| 84 | + "$warningBulletPoint$mismatches:", |
| 85 | + "${index(0)}: 1.1", |
| 86 | + "${index(1)}: 2.1", |
| 87 | + "${index(9)}: 7.1" |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @TestFactory |
| 94 | + fun one_to_seven__does_not_throw() = nonNullableCases( |
| 95 | + notToHaveElementsOrAllSpec, |
| 96 | + notToHaveElementsOrAllNullableSpec |
| 97 | + ) { notToHaveElementsOrAllFun -> |
| 98 | + expect(oneToSeven()).notToHaveElementsOrAllFun { toBeGreaterThan(0.5); toBeLessThan(7.5) } |
| 99 | + } |
| 100 | + |
| 101 | + @TestFactory |
| 102 | + fun nullable_cases() = testFactory(notToHaveElementsOrAllNullableSpec) |
| 103 | + { |
| 104 | + val iterableOfNulls = { sequenceOf<Double?>(null, null).constrainOnce().asIterable() } |
| 105 | + describe("iterable ${iterableOfNulls()}") { |
| 106 | + it("all are `null` does not throw") { |
| 107 | + expect(iterableOfNulls()).notToHaveElementsOrAll(null) |
| 108 | + } |
| 109 | + } |
| 110 | + describe("iterable ${oneToSevenNullable().toList()}") { |
| 111 | + it("$toBeGreaterThanDescr(0.5) throws because two are `null`") { |
| 112 | + expect { |
| 113 | + expect(oneToSevenNullable()).notToHaveElementsOrAll { toBeGreaterThan(0.5) } |
| 114 | + }.toThrow<AssertionError> { |
| 115 | + message { |
| 116 | + toContain.exactly(1).values( |
| 117 | + "$rootBulletPoint$allElementsDescr: $separator", |
| 118 | + "$explanatoryPointWithIndent$toBeGreaterThanDescr: 0.5", |
| 119 | + "$warningBulletPoint$mismatches:", |
| 120 | + "${index(1)}: null", |
| 121 | + "${index(5)}: null" |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments