|
| 1 | +package com.memoizr.assertk |
| 2 | + |
| 3 | +import com.nhaarman.mockito_kotlin.spy |
| 4 | +import org.assertj.core.api.AbstractDoubleAssert |
| 5 | +import org.assertj.core.api.Assertions |
| 6 | +import org.junit.Test |
| 7 | +import org.mockito.Mockito |
| 8 | + |
| 9 | +class `Double assert test` { |
| 10 | + lateinit var mockAssertion: AbstractDoubleAssert<*> |
| 11 | + @Suppress("UNCHECKED_CAST") |
| 12 | + val _expect = object : AssertionHook { |
| 13 | + override fun that(subjectUnderTest: Double?): DoubleAssert { |
| 14 | + val spy: AbstractDoubleAssert<*> = spy(Assertions.assertThat(subjectUnderTest)) |
| 15 | + mockAssertion = spy |
| 16 | + return DoubleAssert(subjectUnderTest, mockAssertion) |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + val chained = Any() |
| 21 | + infix fun DoubleAssert.andCanBe(chained: Any) = this |
| 22 | + |
| 23 | + private val verify by lazy { Mockito.verify(mockAssertion) } |
| 24 | + |
| 25 | + private val four = 4.0 |
| 26 | + private val three = 3.0 |
| 27 | + private val five = 5.0 |
| 28 | + private val one = 1.0 |
| 29 | + private val negativeOne = -one |
| 30 | + |
| 31 | + @Test |
| 32 | + fun isLessThan() { |
| 33 | + _expect that three isLessThan four andCanBe chained |
| 34 | + verify.isLessThan(four) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun isLessThanOrEqualTo() { |
| 39 | + _expect that four isLessThanOrEqualTo four andCanBe chained |
| 40 | + verify.isLessThanOrEqualTo(four) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun isGreaterThan() { |
| 45 | + _expect that five isGreaterThan four andCanBe chained |
| 46 | + verify.isGreaterThan(four) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun isGreaterThanOrEqualTo() { |
| 51 | + _expect that four isGreaterThanOrEqualTo four andCanBe chained |
| 52 | + verify.isGreaterThanOrEqualTo(four) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun isBetween() { |
| 57 | + _expect that four isBetween (three..five) andCanBe chained |
| 58 | + verify.isBetween(three, five) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun isStrictlyBetween() { |
| 63 | + _expect that four isStrictlyBetween (three..five) andCanBe chained |
| 64 | + verify.isStrictlyBetween(three, five) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `isCloseTo within`() { |
| 69 | + _expect that four isCloseTo three withinOffset one andCanBe chained |
| 70 | + verify.isCloseTo(three, Assertions.within(one)) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun `isCloseTo percentage int`() { |
| 75 | + _expect that three isCloseTo four withinPercentage 25 andCanBe chained |
| 76 | + verify.isCloseTo(four, Assertions.withinPercentage(25)) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `isCloseTo percentage double`() { |
| 81 | + _expect that three isCloseTo four withinPercentage 25.3 andCanBe chained |
| 82 | + verify.isCloseTo(four, Assertions.withinPercentage(25.3)) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `isCloseTo percentage float`() { |
| 87 | + _expect that three isCloseTo four withinPercentage 25f andCanBe chained |
| 88 | + verify.isCloseTo(four, Assertions.withinPercentage(25)) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun isZero() { |
| 93 | + _expect that 0.0 _is zero andCanBe chained |
| 94 | + verify.isZero() |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun isNotZero() { |
| 99 | + _expect that one _is notZero andCanBe chained |
| 100 | + verify.isNotZero() |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun isPositive() { |
| 105 | + _expect that one _is positive andCanBe chained |
| 106 | + verify.isPositive() |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + @Test |
| 111 | + fun isNotPositive() { |
| 112 | + _expect that negativeOne _is notPositive andCanBe chained |
| 113 | + verify.isNotPositive() |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun isNegative() { |
| 118 | + _expect that negativeOne _is negative andCanBe chained |
| 119 | + verify.isNegative() |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun isNotNegative() { |
| 124 | + _expect that one _is notNegative andCanBe chained |
| 125 | + verify.isNotNegative() |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun `supports block syntax`() { |
| 130 | + _expect that one isSuchThat { |
| 131 | + it _is positive |
| 132 | + it _is notNegative |
| 133 | + it isBetween (negativeOne..three) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
0 commit comments