diff --git a/src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt b/src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt index 87fc53f..a8b6795 100644 --- a/src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt +++ b/src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt @@ -22,7 +22,7 @@ abstract class AbstractAssertBuilder, A : Any>(a } @Suppress("UNUSED_PARAMETER") - inline infix fun isInstance(bar: InstanceMatcher): S { + inline infix fun isInstance(bar: InstanceMatcher): S { assertion.isInstanceOf(R::class.java) return myself } diff --git a/src/main/kotlin/com/memoizr/assertk/DoubleAssert.kt b/src/main/kotlin/com/memoizr/assertk/DoubleAssert.kt index e4c3a28..ad679c2 100644 --- a/src/main/kotlin/com/memoizr/assertk/DoubleAssert.kt +++ b/src/main/kotlin/com/memoizr/assertk/DoubleAssert.kt @@ -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): DoubleAssert = expect that this isBetween expected +infix fun Double.isStrictlyBetween(expected: ClosedRange): 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): 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)) : @@ -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() diff --git a/src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt b/src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt index a803d2e..2cc3020 100644 --- a/src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt +++ b/src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt @@ -3,6 +3,13 @@ package com.memoizr.assertk import org.assertj.core.api.AbstractObjectAssert import org.assertj.core.api.Assertions +infix fun A.isEqualTo(expected: A): ObjectAssert = expect that this isEqualTo expected +infix fun A.isNotEqualTo(expected: A): ObjectAssert = expect that this isNotEqualTo expected +infix fun A.is_(expected: ObjectSelector): ObjectAssert = expect that this _is expected +infix fun A.describedAs(description: String): ObjectAssert = expect that this describedAs description +inline infix fun A.isInstance(expected: AbstractAssertBuilder.InstanceMatcher): ObjectAssert = + expect.that(this).isInstance(expected) + class ObjectAssert internal constructor( actual: A?, override val assertion: AbstractObjectAssert<*, A> = Assertions.assertThat(actual)) : diff --git a/src/test/kotlin/com/memoizr/assertk/Double assert test.kt b/src/test/kotlin/com/memoizr/assertk/Double assert test.kt index d2bc24d..ea93971 100644 --- a/src/test/kotlin/com/memoizr/assertk/Double assert test.kt +++ b/src/test/kotlin/com/memoizr/assertk/Double assert test.kt @@ -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) @@ -28,81 +28,120 @@ 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() 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() } @@ -110,18 +149,21 @@ class `Double assert test` { @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() } diff --git a/src/test/kotlin/com/memoizr/assertk/Object assert test.kt b/src/test/kotlin/com/memoizr/assertk/Object assert test.kt index dc00a7e..ce7feeb 100644 --- a/src/test/kotlin/com/memoizr/assertk/Object assert test.kt +++ b/src/test/kotlin/com/memoizr/assertk/Object assert test.kt @@ -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 @@ -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 as FooBars) isInstance of() + } + + @Test + fun is_() { + A is_ notNull + } + + @Test + fun describedAs() { + A describedAs "foo" + } + @Test fun `isEqualTo performs logical equality`() { _expect that Unit isEqualTo Unit