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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class AbstractAssertBuilder<S : AbstractAssertBuilder<S, A>, A : Any>(a
}

@Suppress("UNUSED_PARAMETER")
inline infix fun <reified R : Any> isInstance(bar: InstanceMatcher<R>): S {
inline infix fun <reified R : Any> isInstance(bar: InstanceMatcher<out R>): S {
assertion.isInstanceOf(R::class.java)
return myself
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/com/memoizr/assertk/DoubleAssert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ package com.memoizr.assertk
import org.assertj.core.api.AbstractDoubleAssert
import org.assertj.core.api.Assertions

infix fun Double.isLessThan(expected: Double): DoubleAssert = expect that this isLessThan expected
infix fun Double.isLessThanOrEqualTo(expected: Double): DoubleAssert = expect that this isLessThanOrEqualTo expected
infix fun Double.isGreaterThan(expected: Double): DoubleAssert = expect that this isGreaterThan expected
infix fun Double.isGreaterThanOrEqualTo(expected: Double): DoubleAssert = expect that this isGreaterThanOrEqualTo expected
infix fun Double.isBetween(expected: ClosedRange<Double>): DoubleAssert = expect that this isBetween expected
infix fun Double.isStrictlyBetween(expected: ClosedRange<Double>): DoubleAssert = expect that this isStrictlyBetween expected
infix fun Double.isCloseTo(expected: Double): DoubleAssert.Close = expect that this isCloseTo expected
infix fun Double.isEqualTo(expected: Double): DoubleAssert = expect that this isEqualTo expected
infix fun Double.isNotEqualTo(expected: Double): DoubleAssert = expect that this isNotEqualTo expected
infix fun Double.isInstance(expected: AbstractAssertBuilder.InstanceMatcher<Double>): DoubleAssert = expect that this isInstance expected
infix fun Double.is_(expected: NumberSelector): DoubleAssert = expect that this _is expected
infix fun Double.is_(expected: ObjectSelector): DoubleAssert = expect that this _is expected
infix fun Double._is(expected: NumberSelector): DoubleAssert = expect that this _is expected
infix fun Double._is(expected: ObjectSelector): DoubleAssert = expect that this _is expected
infix fun Double.describedAs(description: String): DoubleAssert = expect that this describedAs description

class DoubleAssert internal constructor(
subjectUnderTest: Double?,
override val assertion: AbstractDoubleAssert<*> = Assertions.assertThat(subjectUnderTest)) :
Expand Down Expand Up @@ -42,6 +58,8 @@ class DoubleAssert internal constructor(
return Close(expected, assertion, this)
}

infix fun is_(expected: NumberSelector): DoubleAssert = _is(expected)

infix fun _is(expected: NumberSelector): DoubleAssert {
when (expected) {
zero -> assertion.isZero()
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package com.memoizr.assertk
import org.assertj.core.api.AbstractObjectAssert
import org.assertj.core.api.Assertions

infix fun <A: Any> A.isEqualTo(expected: A): ObjectAssert<A> = expect that this isEqualTo expected
infix fun <A: Any> A.isNotEqualTo(expected: A): ObjectAssert<A> = expect that this isNotEqualTo expected
infix fun <A: Any> A.is_(expected: ObjectSelector): ObjectAssert<A> = expect that this _is expected
infix fun <A: Any> A.describedAs(description: String): ObjectAssert<A> = expect that this describedAs description
inline infix fun <reified A: Any> A.isInstance(expected: AbstractAssertBuilder.InstanceMatcher<out A>): ObjectAssert<A> =
expect.that(this).isInstance(expected)

class ObjectAssert<A : Any> internal constructor(
actual: A?,
override val assertion: AbstractObjectAssert<*, A> = Assertions.assertThat(actual)) :
Expand Down
44 changes: 43 additions & 1 deletion src/test/kotlin/com/memoizr/assertk/Double assert test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class `Double assert test` {
lateinit var mockAssertion: AbstractDoubleAssert<*>
@Suppress("UNCHECKED_CAST")
val _expect = object : AssertionHook {
override fun that(subjectUnderTest: Double?): DoubleAssert {
override fun that(subjectUnderTest: Double?): DoubleAssert {
val spy: AbstractDoubleAssert<*> = spy(Assertions.assertThat(subjectUnderTest))
mockAssertion = spy
return DoubleAssert(subjectUnderTest, mockAssertion)
Expand All @@ -28,100 +28,142 @@ class `Double assert test` {
private val one = 1.0
private val negativeOne = -one

@Test
fun isEqualTo() {
three isEqualTo three andCanBe chained
}

@Test
fun isNotEqualTo() {
three isNotEqualTo four andCanBe chained
}

@Test
fun isInstance() {
three isInstance of<Double>() andCanBe chained
}

@Test
fun is_() {
three is_ notNull andCanBe chained
three is_ notNegative andCanBe chained
}

@Test
fun describedAs() {
three describedAs "foo" andCanBe chained
}

@Test
fun isLessThan() {
_expect that three isLessThan four andCanBe chained
three isLessThan four andCanBe chained
verify.isLessThan(four)
}

@Test
fun isLessThanOrEqualTo() {
_expect that four isLessThanOrEqualTo four andCanBe chained
four isLessThanOrEqualTo four andCanBe chained
verify.isLessThanOrEqualTo(four)
}

@Test
fun isGreaterThan() {
_expect that five isGreaterThan four andCanBe chained
five isGreaterThan four andCanBe chained
verify.isGreaterThan(four)
}

@Test
fun isGreaterThanOrEqualTo() {
_expect that four isGreaterThanOrEqualTo four andCanBe chained
four isGreaterThanOrEqualTo four andCanBe chained
verify.isGreaterThanOrEqualTo(four)
}

@Test
fun isBetween() {
_expect that four isBetween (three..five) andCanBe chained
four isBetween (three..five) andCanBe chained
verify.isBetween(three, five)
}

@Test
fun isStrictlyBetween() {
_expect that four isStrictlyBetween (three..five) andCanBe chained
four isStrictlyBetween (three..five) andCanBe chained
verify.isStrictlyBetween(three, five)
}

@Test
fun `isCloseTo within`() {
_expect that four isCloseTo three withinOffset one andCanBe chained
four isCloseTo three withinOffset one andCanBe chained
verify.isCloseTo(three, Assertions.within(one))
}

@Test
fun `isCloseTo percentage int`() {
_expect that three isCloseTo four withinPercentage 25 andCanBe chained
three isCloseTo four withinPercentage 25 andCanBe chained
verify.isCloseTo(four, Assertions.withinPercentage(25))
}

@Test
fun `isCloseTo percentage double`() {
_expect that three isCloseTo four withinPercentage 25.3 andCanBe chained
three isCloseTo four withinPercentage 25.3 andCanBe chained
verify.isCloseTo(four, Assertions.withinPercentage(25.3))
}

@Test
fun `isCloseTo percentage float`() {
_expect that three isCloseTo four withinPercentage 25f andCanBe chained
three isCloseTo four withinPercentage 25f andCanBe chained
verify.isCloseTo(four, Assertions.withinPercentage(25))
}

@Test
fun isZero() {
_expect that 0.0 _is zero andCanBe chained
0.0 _is zero andCanBe chained
verify.isZero()
}

@Test
fun isNotZero() {
_expect that one _is notZero andCanBe chained
one _is notZero andCanBe chained
verify.isNotZero()
}

@Test
fun isPositive() {
_expect that one _is positive andCanBe chained
one _is positive andCanBe chained
verify.isPositive()
}


@Test
fun isNotPositive() {
_expect that negativeOne _is notPositive andCanBe chained
negativeOne _is notPositive andCanBe chained
verify.isNotPositive()
}

@Test
fun isNegative() {
_expect that negativeOne _is negative andCanBe chained
negativeOne _is negative andCanBe chained
verify.isNegative()
}

@Test
fun isNotNegative() {
_expect that one _is notNegative andCanBe chained
one _is notNegative andCanBe chained
verify.isNotNegative()
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/kotlin/com/memoizr/assertk/Object assert test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import org.assertj.core.api.AbstractObjectAssert
import org.assertj.core.api.Assertions
import org.junit.Test

interface FooBars
object A : FooBars
object B

class `Object assert test` {
val nullObject: Any? = null

Expand All @@ -19,6 +23,32 @@ class `Object assert test` {
}
}

@Test
fun isEqualTo() {
Unit isEqualTo Unit
}

@Test
fun isNotEqualTo() {
A isNotEqualTo B
}

@Test
fun isInstance() {
expect that (A as FooBars) isInstance of<A>()
(A as FooBars) isInstance of<A>()
}

@Test
fun is_() {
A is_ notNull
}

@Test
fun describedAs() {
A describedAs "foo"
}

@Test
fun `isEqualTo performs logical equality`() {
_expect that Unit isEqualTo Unit
Expand Down