From e7f6fd3d9e2f1dc076d42daf22be228c2bbbc40f Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Tue, 1 Oct 2019 18:26:16 +0200 Subject: [PATCH 1/8] Add assertions for empty/non-empty sequences --- .../main/kotlin/org/amshove/kluent/Collections.kt | 8 +++++++- .../kotlin/org/amshove/kluent/internal/Assertions.kt | 5 ++++- .../kluent/collections/ShouldBeEmptyShould.kt | 11 +++++++++++ .../kluent/collections/ShouldNotBeEmptyShould.kt | 12 ++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 77fac260..92c5e76b 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -444,6 +444,9 @@ infix fun > I.shouldHaveSize(expectedSize: Int) = apply { } } +fun > S.shouldBeEmpty(): S = apply { assertEmpty(asIterable(), "Sequence") } +fun > S.shouldNotBeEmpty(): S = apply { assertNotEmpty(asIterable(), "Sequence") } + infix fun > M.shouldEqual(expected: M): M = apply { assertMapEquals(this, expected) } infix fun > M.shouldNotEqual(expected: M): M = apply { assertMapNotEquals(this, expected) } @@ -500,7 +503,10 @@ fun Iterable.shouldMatchAllWith(predicate: (E) -> Boolean): Iterable { return this } -internal fun assertEmpty(iterable: Iterable, collectionType: String) = assertTrue("Expected the $collectionType to be empty, but has ${iterable.count()} elements", iterable.count() == 0) +internal fun assertEmpty(iterable: Iterable, collectionType: String) { + assertTrue(iterable.none()) { "Expected the $collectionType to be empty, but has ${iterable.count()} elements" } +} + internal fun assertNotEmpty(iterable: Iterable, collectionType: String) = assertTrue("Expected the $collectionType to contain elements, but is empty", iterable.count() > 0) internal fun > I.assertBothIterablesContainsSame(expected: Iterable, actual: Iterable): I { diff --git a/common/src/main/kotlin/org/amshove/kluent/internal/Assertions.kt b/common/src/main/kotlin/org/amshove/kluent/internal/Assertions.kt index 31cb81ad..3e6d8a46 100644 --- a/common/src/main/kotlin/org/amshove/kluent/internal/Assertions.kt +++ b/common/src/main/kotlin/org/amshove/kluent/internal/Assertions.kt @@ -1,10 +1,13 @@ package org.amshove.kluent.internal -import kotlin.test.assertTrue import kotlin.test.assertFalse +import kotlin.test.assertTrue import kotlin.test.fail internal fun assertTrue(message: String, boolean: Boolean) = assertTrue(boolean, message) +internal inline fun assertTrue(boolean: Boolean, lazyMessage: () -> String) { + if (!boolean) fail(lazyMessage()) +} internal fun assertFalse(message: String, boolean: Boolean) = assertFalse(boolean, message) diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldBeEmptyShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldBeEmptyShould.kt index 4ea896cd..8ccbd3e4 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldBeEmptyShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldBeEmptyShould.kt @@ -17,6 +17,12 @@ class ShouldBeEmptyShould { iterable.shouldBeEmpty() } + @Test + fun passWhenTestingAnEmptySequence() { + val sequence = emptySequence() + sequence.shouldBeEmpty() + } + @Test fun passWhenTestingAnEmptyMap() { val map = mapOf() @@ -33,6 +39,11 @@ class ShouldBeEmptyShould { assertFails { listOf("Hi").shouldBeEmpty() } } + @Test + fun failWhenTestingANonEmptySequence() { + assertFails { sequenceOf("Hi").shouldBeEmpty() } + } + @Test fun failWhenTestingANonEmptyMap() { assertFails { mapOf(1 to "Hi").shouldBeEmpty() } diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotBeEmptyShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotBeEmptyShould.kt index 39537f40..89d58f40 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotBeEmptyShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotBeEmptyShould.kt @@ -17,6 +17,12 @@ class ShouldNotBeEmptyShould { iterable.shouldNotBeEmpty() } + @Test + fun passWhenTestingANonEmptySequence() { + val sequence = sequenceOf("Hi") + sequence.shouldNotBeEmpty() + } + @Test fun passWhenTestingANonEmptyMap() { val map = mapOf(1 to "Hi") @@ -35,6 +41,12 @@ class ShouldNotBeEmptyShould { assertFails { iterable.shouldNotBeEmpty() } } + @Test + fun failWhenTestingAnEmptySequence() { + val sequence: Sequence = emptySequence() + assertFails { sequence.shouldNotBeEmpty() } + } + @Test fun failWhenTestingAnEmptyMap() { val map = mapOf() From 55b33160f5979632a4cb16fe0eee0313860d0e62 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Tue, 1 Oct 2019 18:50:31 +0200 Subject: [PATCH 2/8] Add shouldContain/shouldNotContain --- .../kotlin/org/amshove/kluent/Collections.kt | 14 ++++++++++++- .../kluent/collections/ShouldContainShould.kt | 20 +++++++++++++++++-- .../collections/ShouldNotContainShould.kt | 17 ++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 92c5e76b..4c44a687 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -447,6 +447,16 @@ infix fun > I.shouldHaveSize(expectedSize: Int) = apply { fun > S.shouldBeEmpty(): S = apply { assertEmpty(asIterable(), "Sequence") } fun > S.shouldNotBeEmpty(): S = apply { assertNotEmpty(asIterable(), "Sequence") } +infix fun > S.shouldContain(expected: T): S = apply { + if (expected !in this) + failExpectedActual("Sequence doesn't contain \"$expected\"", "the Sequence to contain \"$expected\"", join(asIterable())) +} + +infix fun > I.shouldNotContain(expected: S): I = apply { + if (expected in this) + failExpectedActual("Sequence should not contain \"$expected\"", "the Sequence to not contain \"$expected\"", join(asIterable())) +} + infix fun > M.shouldEqual(expected: M): M = apply { assertMapEquals(this, expected) } infix fun > M.shouldNotEqual(expected: M): M = apply { assertMapNotEquals(this, expected) } @@ -507,7 +517,9 @@ internal fun assertEmpty(iterable: Iterable, collectionType: String) { assertTrue(iterable.none()) { "Expected the $collectionType to be empty, but has ${iterable.count()} elements" } } -internal fun assertNotEmpty(iterable: Iterable, collectionType: String) = assertTrue("Expected the $collectionType to contain elements, but is empty", iterable.count() > 0) +internal fun assertNotEmpty(iterable: Iterable, collectionType: String) { + assertTrue(iterable.any()) { "Expected the $collectionType to contain elements, but is empty" } +} internal fun > I.assertBothIterablesContainsSame(expected: Iterable, actual: Iterable): I { assertBothCollectionsContainsSame(expected.toList(), actual.toList()) diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainShould.kt index 9e1efd61..b5f66dab 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainShould.kt @@ -1,9 +1,9 @@ package org.amshove.kluent.collections -import org.amshove.kluent.shouldContain import org.amshove.kluent.Person -import kotlin.test.assertFails +import org.amshove.kluent.shouldContain import kotlin.test.Test +import kotlin.test.assertFails class ShouldContainShould { @Test @@ -26,6 +26,14 @@ class ShouldContainShould { list shouldContain jon } + @Test + fun passWhenTestingASequenceWhichContainsTheValue() { + val alice = Person("Alice", "Bob") + val jon = Person("Jon", "Doe") + val sequence = sequenceOf(alice, jon) + sequence shouldContain jon + } + @Test fun failWhenTestingAnIterableWhichDoesNotContainTheValue() { val alice = Person("Alice", "Bob") @@ -34,6 +42,14 @@ class ShouldContainShould { assertFails { list shouldContain jon } } + @Test + fun failWhenTestingASequenceWhichDoesNotContainTheValue() { + val alice = Person("Alice", "Bob") + val jon = Person("Jon", "Doe") + val sequence = sequenceOf(alice) + assertFails { sequence shouldContain jon } + } + @Test fun passWhenTestingAMapWhichContainsAPair() { val map = mapOf(1 to "one", 2 to "two") diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotContainShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotContainShould.kt index 2a8d2531..1420e953 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotContainShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotContainShould.kt @@ -34,6 +34,23 @@ class ShouldNotContainShould { assertFails { list shouldNotContain jon } } + @Test + fun passWhenTestingASequenceWhichDoesNotContainTheValue() { + val alice = Person("Alice", "Bob") + val jon = Person("Jon", "Doe") + val sequence = sequenceOf(alice) + sequence shouldNotContain jon + } + + @Test + fun failWhenTestingASequenceWhichContainsTheValue() { + val alice = Person("Alice", "Bob") + val jon = Person("Jon", "Doe") + val sequence = sequenceOf(alice, jon) + assertFails { sequence shouldNotContain jon } + } + + @Test fun passWhenTestingAMapWhichDoesNotContainAPair() { val map = mapOf(1 to "one", 2 to "two") From a7baef73b9b021889241d771fabbf49965094f30 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Tue, 1 Oct 2019 19:08:29 +0200 Subject: [PATCH 3/8] Add shouldContainAll and shouldContainNone for sequences --- .../kotlin/org/amshove/kluent/Collections.kt | 14 ++++++++++++++ .../kluent/collections/ShouldContainAllShould.kt | 12 ++++++++++++ .../collections/ShouldContainNoneShould.kt | 16 ++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 4c44a687..7dd10978 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -457,6 +457,20 @@ infix fun > I.shouldNotContain(expected: S): I = apply { failExpectedActual("Sequence should not contain \"$expected\"", "the Sequence to not contain \"$expected\"", join(asIterable())) } +infix fun > S.shouldContainAll(expected: Sequence): S = apply { + val set = toHashSet() + expected.forEach { + if(it !in set) + failExpectedActual("Sequence doesn't contain \"$expected\"", "the Sequence to contain \"$expected\"", join(asIterable())) + } +} + +infix fun > S.shouldContainNone(expected: Sequence): S = apply { + assertTrue(none { it in expected }) { + "Expected Sequence to contain none of \"${expected.toList()}\"" + } +} + infix fun > M.shouldEqual(expected: M): M = apply { assertMapEquals(this, expected) } infix fun > M.shouldNotEqual(expected: M): M = apply { assertMapNotEquals(this, expected) } diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainAllShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainAllShould.kt index 27261f81..b5a7d359 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainAllShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainAllShould.kt @@ -143,6 +143,18 @@ class ShouldContainAllShould { assertFails { set shouldContainAll setOf(5, 9) } } + @Test + fun passWhenTestingASequenceWhichContainsAllValues() { + val sequence = sequenceOf(5, 8, 12) + sequence shouldContainAll sequenceOf(12, 8) + } + + @Test + fun failWhenTestingASequenceWhichDoesNotContainAllValues() { + val sequence = sequenceOf(4, 9) + assertFails { sequence shouldContainAll sequenceOf(5, 9) } + } + @Test fun passWhenTestingAMapWhichContainsAllValues() { val map = mapOf('a' to 1, 'b' to 2, 'c' to 3) diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainNoneShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainNoneShould.kt index 19973e74..60909143 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainNoneShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainNoneShould.kt @@ -35,6 +35,22 @@ class ShouldContainNoneShould { assertFails { actual shouldContainNone cities } } + @Test + fun passWhenTestingASequenceWhichDoesNotContainAtLeastOneElement() { + val cities = sequenceOf("Israel", "Phoenix", "Egypt") + val actual = sequenceOf("Berlin", "Stuttgart") + + actual shouldContainNone cities + } + + @Test + fun failWhenTestingASequenceWhichContainsAtLeastOneElement() { + val cities = sequenceOf("Israel", "Phoenix", "Stuttgart", "Egypt") + val actual = sequenceOf("Berlin", "Stuttgart") + + assertFails { actual shouldContainNone cities } + } + @Test fun passWhenTestingAPrimitiveIntegerArrayWhichDoesNotContainAtLeastOneElement() { val theArray = intArrayOf(1, 5, 7, 13) From 76bf71b977049b1ac5c4b383dc1c196ff7815460 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Tue, 1 Oct 2019 19:52:54 +0200 Subject: [PATCH 4/8] Add shouldContainSame for sequences --- .../main/kotlin/org/amshove/kluent/Collections.kt | 5 ++++- .../kluent/collections/ShouldContainSameShould.kt | 14 +++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 7dd10978..3fd5819f 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -471,6 +471,9 @@ infix fun > S.shouldContainNone(expected: Sequence): S = a } } +infix fun > S.shouldContainSame(expected: Sequence): S = + assertBothIterablesContainsSame(expected.toList(), this.toList()) + infix fun > M.shouldEqual(expected: M): M = apply { assertMapEquals(this, expected) } infix fun > M.shouldNotEqual(expected: M): M = apply { assertMapNotEquals(this, expected) } @@ -535,7 +538,7 @@ internal fun assertNotEmpty(iterable: Iterable, collectionType: String) { assertTrue(iterable.any()) { "Expected the $collectionType to contain elements, but is empty" } } -internal fun > I.assertBothIterablesContainsSame(expected: Iterable, actual: Iterable): I { +internal fun C.assertBothIterablesContainsSame(expected: Iterable, actual: Iterable): C { assertBothCollectionsContainsSame(expected.toList(), actual.toList()) return this } diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSameShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSameShould.kt index 4d7a6dce..75ec83ea 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSameShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSameShould.kt @@ -143,6 +143,18 @@ class ShouldContainSameShould { assertFails { set shouldContainSame setOf(4, 6) } } + @Test + fun passWhenTestingASequenceWhichContainsSameValues() { + val sequence = sequenceOf(5, 8, 12) + sequence shouldContainSame sequenceOf(12, 8, 5) + } + + @Test + fun failWhenTestingASequenceWhichDoesNotContainSameValues() { + val sequence = sequenceOf(4, 9) + assertFails { sequence shouldContainSame sequenceOf(4, 6) } + } + @Test fun passWhenTestingAMapWhichContainsSameValues() { val map = mapOf('a' to 1, 'b' to 2, 'c' to 3) @@ -170,4 +182,4 @@ class ShouldContainSameShould { anArray.shouldContainSame(anIterable) } -} \ No newline at end of file +} From de4666bb328faaa6d3e38cd54661797bd52256e6 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Tue, 1 Oct 2019 20:08:30 +0200 Subject: [PATCH 5/8] Add shouldContainSingleItem and shouldHaveSize for sequences --- .../kotlin/org/amshove/kluent/Collections.kt | 10 ++++++ .../ShouldContainSingleItemShould.kt | 31 ++++++++++++++++++- .../collections/ShouldHaveSizeShould.kt | 16 +++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 3fd5819f..85ac8eeb 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -471,6 +471,16 @@ infix fun > S.shouldContainNone(expected: Sequence): S = a } } +fun , T> S.shouldHaveSingleItem(): T { + shouldHaveSize(1) + return first() +} + +infix fun > S.shouldHaveSize(expectedSize: Int) = apply { + val actualSize = count() + assertTrue(actualSize == expectedSize) { "Expected collection size to be $expectedSize but was $actualSize" } +} + infix fun > S.shouldContainSame(expected: Sequence): S = assertBothIterablesContainsSame(expected.toList(), this.toList()) diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSingleItemShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSingleItemShould.kt index 4375b5c2..f3a3220f 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSingleItemShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSingleItemShould.kt @@ -36,6 +36,35 @@ class ShouldContainSingleItemShould { assertEquals("Hello", item) } + @Test + fun notThrowWhenASequenceHasOneItem() { + val sequence = sequenceOf(1) + sequence.shouldHaveSingleItem() + } + + @Test + fun failWhenASequenceIsEmpty() { + val sequence = emptySequence() + assertFails { + sequence.shouldHaveSingleItem() + } + } + + @Test + fun failWhenASequenceHasMoreThanOneItem() { + val sequence = sequenceOf(1, 2, 3, 4, 5) + assertFails { + sequence.shouldHaveSingleItem() + } + } + + @Test + fun returnTheItemInsideTheSequence() { + val sequence = sequenceOf("Hello") + val item = sequence.shouldHaveSingleItem() + assertEquals("Hello", item) + } + @Test fun workWithArrays() { val arr = arrayOf("World") @@ -47,4 +76,4 @@ class ShouldContainSingleItemShould { val arr = shortArrayOf(5) arr.shouldHaveSingleItem().shouldEqual(5) } -} \ No newline at end of file +} diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldHaveSizeShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldHaveSizeShould.kt index a12cc167..8ab8f57c 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldHaveSizeShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldHaveSizeShould.kt @@ -19,6 +19,20 @@ class ShouldHaveSizeShould { } } + @Test + fun passWhenTestingASequenceWithTheCorrectSize() { + val sequence = sequenceOf("First", "valueIchi", "Second", "valueNi") + sequence.shouldHaveSize(4) + } + + @Test + fun failWhenTestingASequenceWithAnIncorrectSize() { + val sequence = sequenceOf("First", "valueIchi", "Second", "valueNi") + assertFails { + sequence.shouldHaveSize(2) + } + } + @Test fun workWithArrays() { val arr = arrayOf(1, 2, 3, 4, 5) @@ -36,4 +50,4 @@ class ShouldHaveSizeShould { val intArray = intArrayOf(1, 2) intArray.shouldHaveSize(2) } -} \ No newline at end of file +} From 9c5ada45f204d5deeedd5bfbdb801e53090f5952 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Tue, 1 Oct 2019 20:59:37 +0200 Subject: [PATCH 6/8] Add shouldContainSome for sequences --- .../kotlin/org/amshove/kluent/Collections.kt | 5 +++++ .../collections/ShouldContainSomeShould.kt | 20 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 85ac8eeb..49f79a2b 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -471,6 +471,11 @@ infix fun > S.shouldContainNone(expected: Sequence): S = a } } +infix fun > S.shouldContainSome(expected: Sequence): S = apply { + val expectedSet = expected.toHashSet() + assertTrue(any { it in expectedSet }) { "Expected Iterable to contain at least one of \"$expected\"" } +} + fun , T> S.shouldHaveSingleItem(): T { shouldHaveSize(1) return first() diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSomeShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSomeShould.kt index f5b05285..03cf6108 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSomeShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldContainSomeShould.kt @@ -22,6 +22,24 @@ class ShouldContainSomeShould { assertFails { actual shouldContainSome cities } } + @Test + fun passWhenTestingASequenceWhichContainsAtLeastOneElement() { + val cities = sequenceOf("Israel", "Berlin", "Phoenix", "Egypt") + val actual = sequenceOf("Berlin", "Stuttgart") + + actual shouldContainSome cities + } + + @Test + fun failWhenTestingASequenceWhichDoesNotContainAtLeastOneElement() { + + val cities = sequenceOf("Israel", "Phoenix", "Egypt") + val actual = sequenceOf("Berlin", "Stuttgart") + + assertFails { actual shouldContainSome cities } + } + + @Test fun passWhenTestingIfAListContainsASubsetOfAnArrayWhenItDoes() { @@ -151,4 +169,4 @@ class ShouldContainSomeShould { assertFails { theArray shouldContainSome listOf(false) } } -} \ No newline at end of file +} From c1e490d5fd5d5498363293971bb69111bb586bd8 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Tue, 1 Oct 2019 21:30:51 +0200 Subject: [PATCH 7/8] assert shouldEqual and shouldNotEqual for sequences --- .../kotlin/org/amshove/kluent/Collections.kt | 25 +++++++++++++++-- .../kluent/collections/ShouldEqualShould.kt | 22 +++++++++++++++ .../collections/ShouldNotEqualShould.kt | 28 +++++++++++++++++-- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 49f79a2b..1d01ccc9 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -444,6 +444,27 @@ infix fun > I.shouldHaveSize(expectedSize: Int) = apply { } } +private fun areSequencesEqual(sequence1: Sequence, sequence2: Sequence): Boolean { + val iterator1 = sequence1.iterator() + val iterator2 = sequence2.iterator() + + while (iterator1.hasNext() && iterator2.hasNext()) { + if (iterator1.next() != iterator2.next()) return false + } + + return !iterator1.hasNext() && !iterator2.hasNext() +} + +infix fun > S.shouldEqual(expected: Sequence): S = apply { + if (!areSequencesEqual(this, expected)) + failExpectedActual("Sequence should equal \"${join(expected.asIterable())}\"", join(expected.asIterable()), join(asIterable())) +} + +infix fun > S.shouldNotEqual(expected: Sequence): S = apply { + if (areSequencesEqual(this, expected)) + failExpectedActual("Sequence should not equal \"${join(expected.asIterable())}\"", join(expected.asIterable()), join(asIterable())) +} + fun > S.shouldBeEmpty(): S = apply { assertEmpty(asIterable(), "Sequence") } fun > S.shouldNotBeEmpty(): S = apply { assertNotEmpty(asIterable(), "Sequence") } @@ -460,14 +481,14 @@ infix fun > I.shouldNotContain(expected: S): I = apply { infix fun > S.shouldContainAll(expected: Sequence): S = apply { val set = toHashSet() expected.forEach { - if(it !in set) + if (it !in set) failExpectedActual("Sequence doesn't contain \"$expected\"", "the Sequence to contain \"$expected\"", join(asIterable())) } } infix fun > S.shouldContainNone(expected: Sequence): S = apply { assertTrue(none { it in expected }) { - "Expected Sequence to contain none of \"${expected.toList()}\"" + "Expected Sequence to contain none of \"${join(expected.asIterable())}\"" } } diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt index e7b29f76..149d03b2 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt @@ -35,6 +35,28 @@ class ShouldEqualShould { assertFails { firstIterable shouldEqual secondIterable } } + @Test + fun passWhenTestingEqualSequence() { + val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + firstSequence shouldEqual secondSequence + } + + @Test + fun failWhenTestingDifferentSequence() { + val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Jon", "Doe"), Person("Peter", "Meyer")) + val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + assertFails { firstSequence shouldEqual secondSequence } + } + + @Test + fun failWhenTestingSequenceOfDifferentSize() { + val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob")) + assertFails { firstSequence shouldEqual secondSequence } + assertFails { secondSequence shouldEqual firstSequence } + } + @Test fun passWhenTestingEqualMaps() { val firstMap = mapOf(1 to Person("A", "B"), 2 to Person("C", "D")) diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt index 39e2309d..d1b59234 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt @@ -1,9 +1,9 @@ package org.amshove.kluent.collections -import org.amshove.kluent.shouldNotEqual import org.amshove.kluent.Person -import kotlin.test.assertFails +import org.amshove.kluent.shouldNotEqual import kotlin.test.Test +import kotlin.test.assertFails class ShouldNotEqualShould { @Test @@ -34,6 +34,28 @@ class ShouldNotEqualShould { assertFails { firstIterable shouldNotEqual secondIterable } } + @Test + fun passWhenTestingUnequalSequence() { + val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Jon", "Doe"), Person("Peter", "Meyer")) + val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + firstSequence shouldNotEqual secondSequence + } + + @Test + fun failWhenTestingEqualSequence() { + val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + assertFails { firstSequence shouldNotEqual secondSequence } + } + + @Test + fun passWhenTestingSequencesOfDifferentSizes() { + val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) + val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob")) + firstSequence shouldNotEqual secondSequence + secondSequence shouldNotEqual firstSequence + } + @Test fun passWhenTestingDifferentMaps() { val firstMap = mapOf(1 to Person("A", "B"), 2 to Person("C", "D")) @@ -175,4 +197,4 @@ class ShouldNotEqualShould { assertFails { firstArray shouldNotEqual secondArray } } -} \ No newline at end of file +} From 8f0fa492651cd346c57090311603e31715d100e9 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Sat, 16 Nov 2019 18:01:56 +0100 Subject: [PATCH 8/8] Prevent usage of `shouldEqual` and `shouldNotEqual` for sequences --- .../kotlin/org/amshove/kluent/Collections.kt | 14 +++++------- .../kluent/collections/ShouldEqualShould.kt | 22 ------------------- .../collections/ShouldNotEqualShould.kt | 22 ------------------- 3 files changed, 6 insertions(+), 52 deletions(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 318bb649..b026f366 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -512,15 +512,13 @@ private fun areSequencesEqual(sequence1: Sequence, sequence2: Sequence return !iterator1.hasNext() && !iterator2.hasNext() } -infix fun > S.shouldEqual(expected: Sequence): S = apply { - if (!areSequencesEqual(this, expected)) - failExpectedActual("Sequence should equal \"${join(expected.asIterable())}\"", join(expected.asIterable()), join(asIterable())) -} +@Deprecated("Equality should not be tested on sequences", level = DeprecationLevel.ERROR) +infix fun > S.shouldEqual(expected: Sequence): S = + fail("Equality should not be tested on sequences") -infix fun > S.shouldNotEqual(expected: Sequence): S = apply { - if (areSequencesEqual(this, expected)) - failExpectedActual("Sequence should not equal \"${join(expected.asIterable())}\"", join(expected.asIterable()), join(asIterable())) -} +@Deprecated("Equality should not be tested on sequences", level = DeprecationLevel.ERROR) +infix fun > S.shouldNotEqual(expected: Sequence): S = + fail("Equality should not be tested on sequences") fun > S.shouldBeEmpty(): S = apply { assertEmpty(asIterable(), "Sequence") } fun > S.shouldNotBeEmpty(): S = apply { assertNotEmpty(asIterable(), "Sequence") } diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt index 149d03b2..e7b29f76 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldEqualShould.kt @@ -35,28 +35,6 @@ class ShouldEqualShould { assertFails { firstIterable shouldEqual secondIterable } } - @Test - fun passWhenTestingEqualSequence() { - val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - firstSequence shouldEqual secondSequence - } - - @Test - fun failWhenTestingDifferentSequence() { - val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Jon", "Doe"), Person("Peter", "Meyer")) - val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - assertFails { firstSequence shouldEqual secondSequence } - } - - @Test - fun failWhenTestingSequenceOfDifferentSize() { - val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob")) - assertFails { firstSequence shouldEqual secondSequence } - assertFails { secondSequence shouldEqual firstSequence } - } - @Test fun passWhenTestingEqualMaps() { val firstMap = mapOf(1 to Person("A", "B"), 2 to Person("C", "D")) diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt index d1b59234..819abeeb 100644 --- a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotEqualShould.kt @@ -34,28 +34,6 @@ class ShouldNotEqualShould { assertFails { firstIterable shouldNotEqual secondIterable } } - @Test - fun passWhenTestingUnequalSequence() { - val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Jon", "Doe"), Person("Peter", "Meyer")) - val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - firstSequence shouldNotEqual secondSequence - } - - @Test - fun failWhenTestingEqualSequence() { - val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - assertFails { firstSequence shouldNotEqual secondSequence } - } - - @Test - fun passWhenTestingSequencesOfDifferentSizes() { - val firstSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe")) - val secondSequence = sequenceOf(Person("Tom", "Guy"), Person("Alice", "Bob")) - firstSequence shouldNotEqual secondSequence - secondSequence shouldNotEqual firstSequence - } - @Test fun passWhenTestingDifferentMaps() { val firstMap = mapOf(1 to Person("A", "B"), 2 to Person("C", "D"))