Skip to content
Merged
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ The available assertions are:
| `raises` | Verifies that the function in the Raise context fails with the given error. |
| `fails` | Verifies that the function in the Raise context fails, no matter the type of the logical error. |
| `result` | Verifies that the actual function in the `Raise` context succeeds and returns an `Object` assertion that allows chaining (object) assertions on the returned value. |
| `error` | Verifies that the actual function in the Raise context fails and returns an Object assertion that allows chaining (object) assertions on the raised error. |
| `error` | Verifies that the actual function in the Raise context fails and returns an Object assertion that allows chaining (object) assertions on the raised error. |

### `NonEmptyList<A>`

Use the `in.rcard.assertj.arrowcore.NonEmptyListAssert` class as an entry point to assert `NonEmptyList<A>` instances.

| Assertions | Description |
|-----------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `shouldContain` | Verifies that the actual `NonEmptyList` contains the expected element. |
| `shouldContainAll` | Verifies that the actual `NonEmptyList` contains all the expected elements. |
| `shouldContainNoNulls` | Verifies that the actual `NonEmptyList` does not contain null. |
| `shouldContainOnlyNulls` | Verifies that the actual `NonEmptyList` contains only null. |
| `shouldContainNull` | Verifies that the actual `NonEmptyList` contains null. |
| `shouldHaveDuplicates` | Verifies that the actual `NonEmptyList` contains at least one duplicate. |
| `shouldBeSingleElement` | Verifies that the actual `NonEmptyList` has a single element which is expected element. |
| `shouldBeSorted` | Verifies that the actual `NonEmptyList` is sorted. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package `in`.rcard.assertj.arrowcore

import arrow.core.NonEmptyList
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldNotContain.Companion.shouldNotContain
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldContain.Companion.shouldContain
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldContainOnly.Companion.shouldContainOnly
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldBeSingleElement.Companion.shouldBeSingleElement
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldHaveDuplicates.Companion.shouldHaveDuplicates
import `in`.rcard.assertj.arrowcore.errors.NonEmptyListShouldBeSorted.Companion.shouldBeSorted
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.AssertFactory
import org.assertj.core.api.FactoryBasedNavigableListAssert
import org.assertj.core.internal.StandardComparisonStrategy


/**
* Assertions for [NonEmptyList].
*
* @param SELF the "self" type of this assertion class.
* @param ELEMENT type of the element contained in the [NonEmptyList].
* @param ELEMENT_ASSERT type used for assertion of element contained in the [NonEmptyList].
* @author Hamza Faraji
*
* @since 1.2.0
*/
open class AbstractNonEmptyListAssert<
SELF : AbstractNonEmptyListAssert<SELF, ELEMENT, ELEMENT_ASSERT>,
ELEMENT : Any?,
ELEMENT_ASSERT : AbstractAssert<ELEMENT_ASSERT, ELEMENT>
> internal constructor(
list: NonEmptyList<ELEMENT?>?,
assertFactory: AssertFactory<ELEMENT, ELEMENT_ASSERT>?,
) : FactoryBasedNavigableListAssert<SELF, NonEmptyList<ELEMENT?>, ELEMENT, ELEMENT_ASSERT>(
list,
AbstractNonEmptyListAssert::class.java,
assertFactory
) {
private val comparisonStrategy: StandardComparisonStrategy = StandardComparisonStrategy.instance()

/**
* Verifies that the actual [NonEmptyList] contains the expected element
*
* @return the assertion object
*/
fun shouldContain(expectedValue: ELEMENT): SELF {
isNotNull
assertContains(expectedValue)
return myself
}

/**
* Verifies that the actual [NonEmptyList] contains all the expected elements
*
* @return the assertion object
*/
fun shouldContainAll(vararg elements: ELEMENT): SELF {
isNotNull
if (!actual.containsAll(elements.toList())) {
throwAssertionError(shouldContain(actual, elements))
}
return myself
}

/**
* Verifies that the actual [NonEmptyList] contains null
*
* @return the assertion object
*/
fun shouldContainNull(): SELF {
isNotNull
assertContains(null)
return myself
}

/**
* Verifies that the actual [NonEmptyList] does not contain null
*
* @return the assertion object
*/
fun shouldContainNoNulls(): SELF {
isNotNull
if (actual.contains(null)) {
throwAssertionError(shouldNotContain(actual, null))
}
return myself
}

/**
* Verifies that the actual [NonEmptyList] contains only null
*
* @return the assertion object
*/
fun shouldContainOnlyNulls(): SELF {
isNotNull
if (!actual.none { it != null }) {
throwAssertionError(shouldContainOnly(actual, null))
}
return myself
}

/**
* Verifies that the actual [NonEmptyList] contains at least one duplicate
*
* @return the assertion object
*/
fun shouldHaveDuplicates(): SELF {
isNotNull
if (actual.distinct().size == actual.size) {
throwAssertionError(shouldHaveDuplicates(actual))
}
return myself
}

/**
* Verifies that the actual [NonEmptyList] has a single element which is expected element
*
* @return the assertion object
*/
fun shouldBeSingleElement(expectedValue: ELEMENT): SELF {
isNotNull
if (actual.size != 1 || !comparisonStrategy.areEqual(actual.first(), expectedValue)) {
throwAssertionError(shouldBeSingleElement(actual, expectedValue))
}
return myself
}

/**
* Verifies that the actual [NonEmptyList] is sorted
*
* @return the assertion object
*/
fun shouldBeSorted(): SELF {
isNotNull
if (actual.sortedWith { first, second ->
when {
comparisonStrategy.areEqual(first, second) -> 0
comparisonStrategy.isLessThanOrEqualTo(first, second) -> -1
else -> 1
}
} != actual) {
throwAssertionError(shouldBeSorted(actual))
}

return myself
}

private fun assertContains(expectedValue: ELEMENT?) {
isNotNull
if (!actual.contains(expectedValue)) {
throwAssertionError(shouldContain(actual, expectedValue))
}
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/in/rcard/assertj/arrowcore/NonEmptyListAssert.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package `in`.rcard.assertj.arrowcore

import arrow.core.NonEmptyList
import org.assertj.core.api.AssertFactory
import org.assertj.core.api.ObjectAssert

/**
* Assertions for [NonEmptyList].
*
* @param ELEMENT type of the element contained in the [NonEmptyList].
* @author Hamza Faraji
*
* @since 1.2.0
*/
class NonEmptyListAssert<ELEMENT : Any?> private constructor(nel: NonEmptyList<ELEMENT>?) :
AbstractNonEmptyListAssert<NonEmptyListAssert<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>>(
nel,
AssertFactory { actual: ELEMENT -> ObjectAssert(actual) }
) {
companion object {
fun <VALUE : Any?> assertThat(list: NonEmptyList<VALUE>?): NonEmptyListAssert<VALUE> =
NonEmptyListAssert(list)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package `in`.rcard.assertj.arrowcore.errors

import arrow.core.NonEmptyList
import org.assertj.core.error.BasicErrorMessageFactory

/**
* Build error message when a [NonEmptyList] does not have a single element equal a specific value.
*
* @author Hamza Faraji
* @since 1.2.0
*/
class NonEmptyListShouldBeSingleElement private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
BasicErrorMessageFactory(message, actual, expected) {
companion object {
private const val EXPECTING_TO_HAVE_SINGLE_ELEMENT_EQUAL =
"%nExpecting:%n <%s>%nto have single element:%n <%s>%nbut did not."

internal fun <ELEMENT : Any> shouldBeSingleElement(
actual: NonEmptyList<ELEMENT?>,
expected: ELEMENT?
): NonEmptyListShouldBeSingleElement = NonEmptyListShouldBeSingleElement(
EXPECTING_TO_HAVE_SINGLE_ELEMENT_EQUAL,
actual,
expected
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package `in`.rcard.assertj.arrowcore.errors

import arrow.core.NonEmptyList
import org.assertj.core.error.BasicErrorMessageFactory

/**
* Build error message when a [NonEmptyList] is not sorted.
*
* @author Hamza Faraji
* @since 1.2.0
*/
class NonEmptyListShouldBeSorted private constructor(message: String, actual: NonEmptyList<Any?>) :
BasicErrorMessageFactory(message, actual) {
companion object {
private const val EXPECTING_TO_BE_SORTED =
"%nExpecting:%n <%s>%nto be sorted, but it is not."

internal fun <ELEMENT : Any> shouldBeSorted(
actual: NonEmptyList<ELEMENT?>
): NonEmptyListShouldBeSorted = NonEmptyListShouldBeSorted(
EXPECTING_TO_BE_SORTED,
actual
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package `in`.rcard.assertj.arrowcore.errors

import arrow.core.NonEmptyList
import org.assertj.core.error.BasicErrorMessageFactory

/**
* Build error message when a [NonEmptyList] does not contain a specific value.
*
* @author Hamza Faraji
* @since 1.2.0
*/
internal class NonEmptyListShouldContain private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
BasicErrorMessageFactory(message, actual, expected) {
companion object {
private const val EXPECTING_TO_CONTAIN =
"%nExpecting:%n <%s>%nto contain:%n <%s>%nbut did not."

internal fun <ELEMENT : Any> shouldContain(
actual: NonEmptyList<ELEMENT?>,
vararg expected: ELEMENT?
): NonEmptyListShouldContain = NonEmptyListShouldContain(
EXPECTING_TO_CONTAIN,
actual,
expected
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package `in`.rcard.assertj.arrowcore.errors

import arrow.core.NonEmptyList
import org.assertj.core.error.BasicErrorMessageFactory

/**
* Build error message when a [NonEmptyList] does not contain only a specific value.
*
* @author Hamza Faraji
* @since 1.2.0
*/
class NonEmptyListShouldContainOnly private constructor(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
BasicErrorMessageFactory(message, actual, expected) {

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

internal fun <ELEMENT : Any> shouldContainOnly(
actual: NonEmptyList<ELEMENT?>,
expected: ELEMENT?
): NonEmptyListShouldContainOnly = NonEmptyListShouldContainOnly(
EXPECTING_TO_CONTAIN_ONLY,
actual,
expected
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package `in`.rcard.assertj.arrowcore.errors

import arrow.core.NonEmptyList
import org.assertj.core.error.BasicErrorMessageFactory

/**
* Build error message when a [NonEmptyList] has no duplicates.
*
* @author Hamza Faraji
* @since 1.2.0
*/
class NonEmptyListShouldHaveDuplicates private constructor(message: String, actual: NonEmptyList<Any?>) :
BasicErrorMessageFactory(message, actual) {
companion object {
private const val EXPECTING_TO_CONTAIN_DUPLICATES =
"%nExpecting:%n <%s>%nto contain duplicates but did not."

internal fun <ELEMENT : Any> shouldHaveDuplicates(
actual: NonEmptyList<ELEMENT?>
): NonEmptyListShouldHaveDuplicates = NonEmptyListShouldHaveDuplicates(
EXPECTING_TO_CONTAIN_DUPLICATES,
actual
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package `in`.rcard.assertj.arrowcore.errors

import arrow.core.NonEmptyList
import org.assertj.core.error.BasicErrorMessageFactory

/**
* Build error message when a [NonEmptyList] contains a specific value.
*
* @author Hamza Faraji
* @since 1.2.0
*/
class NonEmptyListShouldNotContain(message: String, actual: NonEmptyList<Any?>, expected: Any?) :
BasicErrorMessageFactory(message, actual, expected) {
companion object {
private const val EXPECTING_NOT_TO_CONTAIN =
"%nExpecting:%n <%s>%nnot to contain any:%n <%s>%nbut it did."

internal fun <ELEMENT : Any?> shouldNotContain(
actual: NonEmptyList<ELEMENT>,
expected: ELEMENT
): NonEmptyListShouldNotContain = NonEmptyListShouldNotContain(
EXPECTING_NOT_TO_CONTAIN,
actual,
expected
)
}
}
Loading