11package `in`.rcard.assertj.arrowcore
22
33import arrow.core.NonEmptyList
4- import `in`.rcard.assertj.arrowcore.errors.NonEmptyListHasNoDuplicates.Companion.hasNoDuplicate
5- import `in`.rcard.assertj.arrowcore.errors.NonEmptyListIsNotSorted.Companion.isNotSorted
4+ import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldNotContain.Companion.shouldNotContain
5+ import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldContain.Companion.shouldContain
6+ import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldContainOnly.Companion.shouldContainOnly
7+ import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldBeSingleElement.Companion.shouldBeSingleElement
8+ import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldHaveDuplicates.Companion.shouldHaveDuplicates
9+ import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldBeSorted.Companion.shouldBeSorted
610import org.assertj.core.api.AbstractAssert
7- import `in`.rcard.assertj.arrowcore.errors.NonEmptyListContains.Companion.contains
8- import `in`.rcard.assertj.arrowcore.errors.NonEmptyListDoesNotContain.Companion.doesNotContain
9- import `in`.rcard.assertj.arrowcore.errors.NonEmptyListDoesNotContainOnly.Companion.doesNotContainOnly
10- import `in`.rcard.assertj.arrowcore.errors.NonEmptyListDoesNotHaveSingleElementEqual.Companion.doesNotHaveSingleElementEqual
11- import org.assertj.core.api.AbstractIterableAssert
12- import org.assertj.core.internal.ComparisonStrategy
11+ import org.assertj.core.api.AssertFactory
12+ import org.assertj.core.api.FactoryBasedNavigableListAssert
1313import org.assertj.core.internal.StandardComparisonStrategy
1414
15+
1516/* *
1617 * Assertions for [NonEmptyList].
1718 *
1819 * @param SELF the "self" type of this assertion class.
1920 * @param ELEMENT type of the element contained in the [NonEmptyList].
20- * @param ELEMENT_ASSERT type used for navigational assertion of element contained in the [NonEmptyList].
21+ * @param ELEMENT_ASSERT type used for assertion of element contained in the [NonEmptyList].
2122 * @author Hamza Faraji
2223 *
2324 * @since 1.2.0
2425 */
25- abstract class AbstractNonEmptyListAssert <
26+ open class AbstractNonEmptyListAssert <
2627 SELF : AbstractNonEmptyListAssert <SELF , ELEMENT , ELEMENT_ASSERT >,
2728 ELEMENT : Any? ,
2829 ELEMENT_ASSERT : AbstractAssert <ELEMENT_ASSERT , ELEMENT >
2930 > internal constructor (
3031 list: NonEmptyList <ELEMENT ?>? ,
31- ) : AbstractIterableAssert <SELF , NonEmptyList <ELEMENT ?>, ELEMENT , ELEMENT_ASSERT > (list, AbstractNonEmptyListAssert ::class .java) {
32- private val comparisonStrategy: ComparisonStrategy = StandardComparisonStrategy .instance()
32+ assertFactory: AssertFactory <ELEMENT , ELEMENT_ASSERT >? ,
33+ ) : FactoryBasedNavigableListAssert <SELF , NonEmptyList <ELEMENT ?>, ELEMENT , ELEMENT_ASSERT > (
34+ list,
35+ AbstractNonEmptyListAssert ::class .java,
36+ assertFactory
37+ ) {
38+ private val comparisonStrategy: StandardComparisonStrategy = StandardComparisonStrategy .instance()
3339
3440 /* *
3541 * Verifies that the actual [NonEmptyList] contains the expected element
@@ -50,7 +56,7 @@ abstract class AbstractNonEmptyListAssert<
5056 fun shouldContainAll (vararg elements : ELEMENT ): SELF {
5157 isNotNull
5258 if (! actual.containsAll(elements.toList())) {
53- throwAssertionError(doesNotContain (actual, elements))
59+ throwAssertionError(shouldContain (actual, elements))
5460 }
5561 return myself
5662 }
@@ -74,7 +80,7 @@ abstract class AbstractNonEmptyListAssert<
7480 fun shouldContainNoNulls (): SELF {
7581 isNotNull
7682 if (actual.contains(null )) {
77- throwAssertionError(contains (actual, null ))
83+ throwAssertionError(shouldNotContain (actual, null ))
7884 }
7985 return myself
8086 }
@@ -86,8 +92,8 @@ abstract class AbstractNonEmptyListAssert<
8692 */
8793 fun shouldContainOnlyNulls (): SELF {
8894 isNotNull
89- if (! actual.all { it = = null }) {
90- throwAssertionError(doesNotContainOnly (actual, null ))
95+ if (! actual.none { it ! = null }) {
96+ throwAssertionError(shouldContainOnly (actual, null ))
9197 }
9298 return myself
9399 }
@@ -99,8 +105,8 @@ abstract class AbstractNonEmptyListAssert<
99105 */
100106 fun shouldHaveDuplicates (): SELF {
101107 isNotNull
102- if (! actual.groupBy { it }.any { it.value. size > 1 } ) {
103- throwAssertionError(hasNoDuplicate (actual))
108+ if (actual.distinct().size == actual. size) {
109+ throwAssertionError(shouldHaveDuplicates (actual))
104110 }
105111 return myself
106112 }
@@ -113,7 +119,7 @@ abstract class AbstractNonEmptyListAssert<
113119 fun shouldBeSingleElement (expectedValue : ELEMENT ): SELF {
114120 isNotNull
115121 if (actual.size != 1 || ! comparisonStrategy.areEqual(actual.first(), expectedValue)) {
116- throwAssertionError(doesNotHaveSingleElementEqual (actual, expectedValue))
122+ throwAssertionError(shouldBeSingleElement (actual, expectedValue))
117123 }
118124 return myself
119125 }
@@ -125,8 +131,14 @@ abstract class AbstractNonEmptyListAssert<
125131 */
126132 fun shouldBeSorted (): SELF {
127133 isNotNull
128- if (! actual.zipWithNext().all { comparisonStrategy.isLessThanOrEqualTo(it.first, it.second) }) {
129- throwAssertionError(isNotSorted(actual))
134+ if (actual.sortedWith { first, second ->
135+ when {
136+ comparisonStrategy.areEqual(first, second) -> 0
137+ comparisonStrategy.isLessThanOrEqualTo(first, second) -> - 1
138+ else -> 1
139+ }
140+ } != actual) {
141+ throwAssertionError(shouldBeSorted(actual))
130142 }
131143
132144 return myself
@@ -135,8 +147,7 @@ abstract class AbstractNonEmptyListAssert<
135147 private fun assertContains (expectedValue : ELEMENT ? ) {
136148 isNotNull
137149 if (! actual.contains(expectedValue)) {
138- throwAssertionError(doesNotContain (actual, expectedValue))
150+ throwAssertionError(shouldContain (actual, expectedValue))
139151 }
140152 }
141-
142153}
0 commit comments