Skip to content

Commit b0e4219

Browse files
committed
feat(31): resolve code review comments
1 parent 745d76a commit b0e4219

16 files changed

+75
-67
lines changed

src/main/kotlin/in/rcard/assertj/arrowcore/AbstractNonEmptyListAssert.kt

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
package `in`.rcard.assertj.arrowcore
22

33
import 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
610
import 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
1313
import 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
}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.NonEmptyList
4-
import arrow.core.toNonEmptyListOrNull
4+
import org.assertj.core.api.AssertFactory
55
import org.assertj.core.api.ObjectAssert
66

77
/**
@@ -13,15 +13,12 @@ import org.assertj.core.api.ObjectAssert
1313
* @since 1.2.0
1414
*/
1515
class NonEmptyListAssert<ELEMENT : Any?> private constructor(nel: NonEmptyList<ELEMENT>?) :
16-
AbstractNonEmptyListAssert<NonEmptyListAssert<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>>(nel) {
16+
AbstractNonEmptyListAssert<NonEmptyListAssert<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>>(
17+
nel,
18+
AssertFactory { actual: ELEMENT -> ObjectAssert(actual) }
19+
) {
1720
companion object {
1821
fun <VALUE : Any?> assertThat(list: NonEmptyList<VALUE>?): NonEmptyListAssert<VALUE> =
1922
NonEmptyListAssert(list)
2023
}
21-
22-
override fun toAssert(value: ELEMENT, description: String?): ObjectAssert<ELEMENT> =
23-
ObjectAssert(value).`as`(description)
24-
25-
override fun newAbstractIterableAssert(iterable: MutableIterable<ELEMENT>?): NonEmptyListAssert<ELEMENT> =
26-
NonEmptyListAssert(iterable?.toNonEmptyListOrNull())
2724
}

src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListDoesNotHaveSingleElementEqual.kt renamed to src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListShouldBeSingleElement.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import org.assertj.core.error.BasicErrorMessageFactory
99
* @author Hamza Faraji
1010
* @since 1.2.0
1111
*/
12-
class NonEmptyListDoesNotHaveSingleElementEqual private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
12+
class NonEmptyListShouldBeSingleElement private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
1313
BasicErrorMessageFactory(message, actual, expected) {
1414
companion object {
1515
private const val EXPECTING_TO_HAVE_SINGLE_ELEMENT_EQUAL =
1616
"%nExpecting:%n <%s>%nto have single element:%n <%s>%nbut did not."
1717

18-
internal fun <ELEMENT : Any> doesNotHaveSingleElementEqual(
18+
internal fun <ELEMENT : Any> shouldBeSingleElement(
1919
actual: NonEmptyList<ELEMENT?>,
2020
expected: ELEMENT?
21-
): NonEmptyListDoesNotHaveSingleElementEqual = NonEmptyListDoesNotHaveSingleElementEqual(
21+
): NonEmptyListShouldBeSingleElement = NonEmptyListShouldBeSingleElement(
2222
EXPECTING_TO_HAVE_SINGLE_ELEMENT_EQUAL,
2323
actual,
2424
expected

src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListIsNotSorted.kt renamed to src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListShouldBeSorted.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import org.assertj.core.error.BasicErrorMessageFactory
99
* @author Hamza Faraji
1010
* @since 1.2.0
1111
*/
12-
class NonEmptyListIsNotSorted private constructor(message: String, actual: NonEmptyList<Any?>) :
12+
class NonEmptyListShouldBeSorted private constructor(message: String, actual: NonEmptyList<Any?>) :
1313
BasicErrorMessageFactory(message, actual) {
1414
companion object {
1515
private const val EXPECTING_TO_BE_SORTED =
1616
"%nExpecting:%n <%s>%nto be sorted, but it is not."
1717

18-
internal fun <ELEMENT : Any> isNotSorted(
18+
internal fun <ELEMENT : Any> shouldBeSorted(
1919
actual: NonEmptyList<ELEMENT?>
20-
): NonEmptyListIsNotSorted = NonEmptyListIsNotSorted(
20+
): NonEmptyListShouldBeSorted = NonEmptyListShouldBeSorted(
2121
EXPECTING_TO_BE_SORTED,
2222
actual
2323
)

src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListDoesNotContain.kt renamed to src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListShouldContain.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import org.assertj.core.error.BasicErrorMessageFactory
99
* @author Hamza Faraji
1010
* @since 1.2.0
1111
*/
12-
internal class NonEmptyListDoesNotContain private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
12+
internal class NonEmptyListShouldContain private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
1313
BasicErrorMessageFactory(message, actual, expected) {
1414
companion object {
1515
private const val EXPECTING_TO_CONTAIN =
1616
"%nExpecting:%n <%s>%nto contain:%n <%s>%nbut did not."
1717

18-
internal fun <ELEMENT : Any> doesNotContain(
18+
internal fun <ELEMENT : Any> shouldContain(
1919
actual: NonEmptyList<ELEMENT?>,
2020
vararg expected: ELEMENT?
21-
): NonEmptyListDoesNotContain = NonEmptyListDoesNotContain(
21+
): NonEmptyListShouldContain = NonEmptyListShouldContain(
2222
EXPECTING_TO_CONTAIN,
2323
actual,
2424
expected

src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListDoesNotContainOnly.kt renamed to src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListShouldContainOnly.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import org.assertj.core.error.BasicErrorMessageFactory
99
* @author Hamza Faraji
1010
* @since 1.2.0
1111
*/
12-
class NonEmptyListDoesNotContainOnly private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
12+
class NonEmptyListShouldContainOnly private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
1313
BasicErrorMessageFactory(message, actual, expected) {
1414

1515
companion object {
1616
private const val EXPECTING_TO_CONTAIN_ONLY =
1717
"%nExpecting:%n <%s>%nto contain only:%n <%s>%nbut did not."
1818

19-
internal fun <ELEMENT : Any> doesNotContainOnly(
19+
internal fun <ELEMENT : Any> shouldContainOnly(
2020
actual: NonEmptyList<ELEMENT?>,
2121
expected: ELEMENT?
22-
): NonEmptyListDoesNotContainOnly = NonEmptyListDoesNotContainOnly(
22+
): NonEmptyListShouldContainOnly = NonEmptyListShouldContainOnly(
2323
EXPECTING_TO_CONTAIN_ONLY,
2424
actual,
2525
expected

src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListHasNoDuplicates.kt renamed to src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListShouldHaveDuplicates.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import org.assertj.core.error.BasicErrorMessageFactory
99
* @author Hamza Faraji
1010
* @since 1.2.0
1111
*/
12-
class NonEmptyListHasNoDuplicates private constructor(message: String, actual: NonEmptyList<Any?>) :
12+
class NonEmptyListShouldHaveDuplicates private constructor(message: String, actual: NonEmptyList<Any?>) :
1313
BasicErrorMessageFactory(message, actual) {
1414
companion object {
1515
private const val EXPECTING_TO_CONTAIN_DUPLICATES =
1616
"%nExpecting:%n <%s>%nto contain duplicates but did not."
1717

18-
internal fun <ELEMENT : Any> hasNoDuplicate(
18+
internal fun <ELEMENT : Any> shouldHaveDuplicates(
1919
actual: NonEmptyList<ELEMENT?>
20-
): NonEmptyListHasNoDuplicates = NonEmptyListHasNoDuplicates(
20+
): NonEmptyListShouldHaveDuplicates = NonEmptyListShouldHaveDuplicates(
2121
EXPECTING_TO_CONTAIN_DUPLICATES,
2222
actual
2323
)

src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListContains.kt renamed to src/main/kotlin/in/rcard/assertj/arrowcore/errors/NonEmptyListShouldNotContain.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import org.assertj.core.error.BasicErrorMessageFactory
99
* @author Hamza Faraji
1010
* @since 1.2.0
1111
*/
12-
class NonEmptyListContains(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
12+
class NonEmptyListShouldNotContain(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
1313
BasicErrorMessageFactory(message, actual, expected) {
1414
companion object {
1515
private const val EXPECTING_NOT_TO_CONTAIN =
16-
"%nExpecting:%n <%s>%nto not contain any:%n <%s>%nbut it did."
16+
"%nExpecting:%n <%s>%nnot to contain any:%n <%s>%nbut it did."
1717

18-
internal fun <ELEMENT : Any?> contains(
18+
internal fun <ELEMENT : Any?> shouldNotContain(
1919
actual: NonEmptyList<ELEMENT>,
2020
expected: ELEMENT
21-
): NonEmptyListContains = NonEmptyListContains(
21+
): NonEmptyListShouldNotContain = NonEmptyListShouldNotContain(
2222
EXPECTING_NOT_TO_CONTAIN,
2323
actual,
2424
expected

src/test/kotlin/in/rcard/assertj/arrowcore/NonEmptyListAssert_shouldBeSingleElement_Test.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.NonEmptyList
44
import arrow.core.nonEmptyListOf
5-
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListDoesNotHaveSingleElementEqual.Companion.doesNotHaveSingleElementEqual
5+
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldBeSingleElement.Companion.shouldBeSingleElement
66
import org.assertj.core.api.Assertions
77
import org.assertj.core.util.FailureMessages.actualIsNull
88
import org.junit.jupiter.api.Test
@@ -16,7 +16,7 @@ internal class NonEmptyListAssert_shouldBeSingleElement_Test {
1616
// WHEN/THEN
1717
Assertions.assertThatThrownBy { NonEmptyListAssert.assertThat(list).shouldBeSingleElement(1) }
1818
.isInstanceOf(AssertionError::class.java)
19-
.hasMessage(doesNotHaveSingleElementEqual(list, 1).create())
19+
.hasMessage(shouldBeSingleElement(list, 1).create())
2020
}
2121

2222
@Test
@@ -26,7 +26,7 @@ internal class NonEmptyListAssert_shouldBeSingleElement_Test {
2626
// WHEN/THEN
2727
Assertions.assertThatThrownBy { NonEmptyListAssert.assertThat(list).shouldBeSingleElement(42) }
2828
.isInstanceOf(AssertionError::class.java)
29-
.hasMessage(doesNotHaveSingleElementEqual(list, 42).create())
29+
.hasMessage(shouldBeSingleElement(list, 42).create())
3030
}
3131

3232
@Test

src/test/kotlin/in/rcard/assertj/arrowcore/NonEmptyListAssert_shouldBeSorted_Test.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package `in`.rcard.assertj.arrowcore
22

33
import arrow.core.NonEmptyList
44
import arrow.core.nonEmptyListOf
5-
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListIsNotSorted.Companion.isNotSorted
5+
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldBeSorted.Companion.shouldBeSorted
66
import org.assertj.core.api.Assertions
77
import org.assertj.core.util.FailureMessages.actualIsNull
88
import org.junit.jupiter.api.Test
@@ -16,7 +16,7 @@ internal class NonEmptyListAssert_shouldBeSorted_Test {
1616
// WHEN/THEN
1717
Assertions.assertThatThrownBy { NonEmptyListAssert.assertThat(list).shouldBeSorted() }
1818
.isInstanceOf(AssertionError::class.java)
19-
.hasMessage(isNotSorted(list).create())
19+
.hasMessage(shouldBeSorted(list).create())
2020
}
2121

2222
@Test

0 commit comments

Comments
 (0)