Skip to content

Commit 213c3b6

Browse files
committed
add float, double, long assertions
1 parent acd0fc0 commit 213c3b6

File tree

10 files changed

+632
-16
lines changed

10 files changed

+632
-16
lines changed

src/main/kotlin/com/memoizr/assertk/AssertionHooks.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ interface AssertionHook {
88
infix fun <T : Any> that(subjectUnderTest: T?) = ObjectAssert(subjectUnderTest)
99
infix fun that(subjectUnderTest: CharSequence?) = CharSequenceAssert(subjectUnderTest)
1010
infix fun thatThrownBy(expressionUnderTest: () -> Unit) = ThrowableAssertion(expressionUnderTest)
11-
infix fun <ELEMENT: Any?> that(subjectUnderTest: Iterable<ELEMENT>?): IterableAssert<ELEMENT, Iterable<ELEMENT>> = IterableAssert(subjectUnderTest)
11+
infix fun <ELEMENT : Any?> that(subjectUnderTest: Iterable<ELEMENT>?): IterableAssert<ELEMENT, Iterable<ELEMENT>> = IterableAssert(subjectUnderTest)
1212
infix fun that(subjectUnderTest: Int?) = IntegerAssert(subjectUnderTest)
13+
infix fun that(subjectUnderTest: Float?) = FloatAssert(subjectUnderTest)
14+
infix fun that(subjectUnderTest: Double?) = DoubleAssert(subjectUnderTest)
15+
infix fun that(subjectUnderTest: Long?) = LongAssert(subjectUnderTest)
1316
}
1417

1518
class RealAssertionHook : AssertionHook
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.memoizr.assertk
2+
3+
import org.assertj.core.api.AbstractDoubleAssert
4+
import org.assertj.core.api.Assertions
5+
6+
class DoubleAssert(
7+
subjectUnderTest: Double?,
8+
override val assertion: AbstractDoubleAssert<*> = Assertions.assertThat(subjectUnderTest)) :
9+
AbstractAssertBuilder<DoubleAssert, Double>(subjectUnderTest, DoubleAssert::class.java) {
10+
11+
infix fun isLessThan(expected: Double): DoubleAssert {
12+
assertion.isLessThan(expected)
13+
return this
14+
}
15+
16+
infix fun isLessThanOrEqualTo(expected: Double): DoubleAssert {
17+
assertion.isLessThanOrEqualTo(expected)
18+
return this
19+
}
20+
21+
infix fun isGreaterThan(expected: Double): DoubleAssert {
22+
assertion.isGreaterThan(expected)
23+
return this
24+
}
25+
26+
infix fun isGreaterThanOrEqualTo(expected: Double): DoubleAssert {
27+
assertion.isGreaterThanOrEqualTo(expected)
28+
return this
29+
}
30+
31+
infix fun isBetween(expected: ClosedRange<Double>): DoubleAssert {
32+
assertion.isBetween(expected.start, expected.endInclusive)
33+
return this
34+
}
35+
36+
infix fun isStrictlyBetween(expected: ClosedRange<Double>): DoubleAssert {
37+
assertion.isStrictlyBetween(expected.start, expected.endInclusive)
38+
return this
39+
}
40+
41+
infix fun isCloseTo(expected: Double): Close {
42+
return Close(expected, assertion, this)
43+
}
44+
45+
infix fun _is(expected: NumberSelector): DoubleAssert {
46+
when (expected) {
47+
zero -> assertion.isZero()
48+
notZero -> assertion.isNotZero()
49+
positive -> assertion.isPositive()
50+
notPositive -> assertion.isNotPositive()
51+
negative -> assertion.isNegative()
52+
notNegative -> assertion.isNotNegative()
53+
}
54+
return this
55+
}
56+
57+
class Close(private val actual: Double, private val assertion: AbstractDoubleAssert<*>, private val assert: DoubleAssert) {
58+
infix fun withinOffset(expected: Double): DoubleAssert {
59+
assertion.isCloseTo(actual, Assertions.within(expected))
60+
return assert
61+
}
62+
63+
infix fun withinPercentage(expected: Number): DoubleAssert {
64+
assertion.isCloseTo(actual, Assertions.withinPercentage(expected.toDouble()))
65+
return assert
66+
}
67+
}
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.memoizr.assertk
2+
3+
import org.assertj.core.api.AbstractFloatAssert
4+
import org.assertj.core.api.Assertions
5+
6+
class FloatAssert(
7+
subjectUnderTest: Float?,
8+
override val assertion: AbstractFloatAssert<*> = Assertions.assertThat(subjectUnderTest)) :
9+
AbstractAssertBuilder<FloatAssert, Float>(subjectUnderTest, FloatAssert::class.java) {
10+
11+
infix fun isLessThan(expected: Float): FloatAssert {
12+
assertion.isLessThan(expected)
13+
return this
14+
}
15+
16+
infix fun isLessThanOrEqualTo(expected: Float): FloatAssert {
17+
assertion.isLessThanOrEqualTo(expected)
18+
return this
19+
}
20+
21+
infix fun isGreaterThan(expected: Float): FloatAssert {
22+
assertion.isGreaterThan(expected)
23+
return this
24+
}
25+
26+
infix fun isGreaterThanOrEqualTo(expected: Float): FloatAssert {
27+
assertion.isGreaterThanOrEqualTo(expected)
28+
return this
29+
}
30+
31+
infix fun isBetween(expected: ClosedRange<Float>): FloatAssert {
32+
assertion.isBetween(expected.start, expected.endInclusive)
33+
return this
34+
}
35+
36+
infix fun isStrictlyBetween(expected: ClosedRange<Float>): FloatAssert {
37+
assertion.isStrictlyBetween(expected.start, expected.endInclusive)
38+
return this
39+
}
40+
41+
infix fun isCloseTo(expected: Float): Close {
42+
return Close(expected, assertion, this)
43+
}
44+
45+
infix fun _is(expected: NumberSelector): FloatAssert {
46+
when (expected) {
47+
zero -> assertion.isZero()
48+
notZero -> assertion.isNotZero()
49+
positive -> assertion.isPositive()
50+
notPositive -> assertion.isNotPositive()
51+
negative -> assertion.isNegative()
52+
notNegative -> assertion.isNotNegative()
53+
}
54+
return this
55+
}
56+
57+
class Close(private val actual: Float, private val assertion: AbstractFloatAssert<*>, private val assert: FloatAssert) {
58+
infix fun withinOffset(expected: Float): FloatAssert {
59+
assertion.isCloseTo(actual, Assertions.within(expected))
60+
return assert
61+
}
62+
63+
infix fun withinPercentage(expected: Number): FloatAssert {
64+
assertion.isCloseTo(actual, Assertions.withinPercentage(expected.toDouble()))
65+
return assert
66+
}
67+
}
68+
}

src/main/kotlin/com/memoizr/assertk/IntegerAssert.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class IntegerAssert(
4242
return Close(expected, assertion, this)
4343
}
4444

45-
infix fun _is(expected: IntegerSelector): IntegerAssert {
45+
infix fun _is(expected: NumberSelector): IntegerAssert {
4646
when (expected) {
4747
zero -> assertion.isZero()
4848
notZero -> assertion.isNotZero()
@@ -60,8 +60,8 @@ class IntegerAssert(
6060
return assert
6161
}
6262

63-
infix fun withinPercentage(expected: Int): IntegerAssert {
64-
assertion.isCloseTo(actual, Assertions.withinPercentage(expected))
63+
infix fun withinPercentage(expected: Number): IntegerAssert {
64+
assertion.isCloseTo(actual, Assertions.withinPercentage(expected.toDouble()))
6565
return assert
6666
}
6767
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.memoizr.assertk
2+
3+
import org.assertj.core.api.AbstractLongAssert
4+
import org.assertj.core.api.Assertions
5+
6+
class LongAssert(
7+
subjectUnderTest: Long?,
8+
override val assertion: AbstractLongAssert<*> = Assertions.assertThat(subjectUnderTest)) :
9+
AbstractAssertBuilder<LongAssert, Long>(subjectUnderTest, LongAssert::class.java) {
10+
11+
infix fun isLessThan(expected: Long): LongAssert {
12+
assertion.isLessThan(expected)
13+
return this
14+
}
15+
16+
infix fun isLessThanOrEqualTo(expected: Long): LongAssert {
17+
assertion.isLessThanOrEqualTo(expected)
18+
return this
19+
}
20+
21+
infix fun isGreaterThan(expected: Long): LongAssert {
22+
assertion.isGreaterThan(expected)
23+
return this
24+
}
25+
26+
infix fun isGreaterThanOrEqualTo(expected: Long): LongAssert {
27+
assertion.isGreaterThanOrEqualTo(expected)
28+
return this
29+
}
30+
31+
infix fun isBetween(expected: ClosedRange<Long>): LongAssert {
32+
assertion.isBetween(expected.start, expected.endInclusive)
33+
return this
34+
}
35+
36+
infix fun isStrictlyBetween(expected: ClosedRange<Long>): LongAssert {
37+
assertion.isStrictlyBetween(expected.start, expected.endInclusive)
38+
return this
39+
}
40+
41+
infix fun isCloseTo(expected: Long): Close {
42+
return Close(expected, assertion, this)
43+
}
44+
45+
infix fun _is(expected: NumberSelector): LongAssert {
46+
when (expected) {
47+
zero -> assertion.isZero()
48+
notZero -> assertion.isNotZero()
49+
positive -> assertion.isPositive()
50+
notPositive -> assertion.isNotPositive()
51+
negative -> assertion.isNegative()
52+
notNegative -> assertion.isNotNegative()
53+
}
54+
return this
55+
}
56+
57+
class Close(private val actual: Long, private val assertion: AbstractLongAssert<*>, private val assert: LongAssert) {
58+
infix fun withinOffset(expected: Long): LongAssert {
59+
assertion.isCloseTo(actual, Assertions.within(expected))
60+
return assert
61+
}
62+
63+
infix fun withinPercentage(expected: Number): LongAssert {
64+
assertion.isCloseTo(actual, Assertions.withinPercentage(expected.toDouble()))
65+
return assert
66+
}
67+
}
68+
}
69+

src/main/kotlin/com/memoizr/assertk/Selectors.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ object duplicates
1717

1818
object onlyNotNull
1919

20-
sealed class IntegerSelector
21-
object zero : IntegerSelector()
22-
object notZero : IntegerSelector()
23-
object positive : IntegerSelector()
24-
object notPositive : IntegerSelector()
25-
object negative : IntegerSelector()
26-
object notNegative : IntegerSelector()
20+
sealed class NumberSelector
21+
object zero : NumberSelector()
22+
object notZero : NumberSelector()
23+
object positive : NumberSelector()
24+
object notPositive : NumberSelector()
25+
object negative : NumberSelector()
26+
object notNegative : NumberSelector()
27+
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)