Skip to content

Commit 9728008

Browse files
authored
Merge pull request #3 from memoizr/iterables
Iterables, integer, long, double, float assertions, Kotlin 1.1
2 parents 4b363ce + 3ab68e2 commit 9728008

19 files changed

+1166
-28
lines changed

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
buildscript {
2+
ext.KOTLIN_VERSION="1.1-M01"
23
ext.kotlin_version = '1.0.3'
34

45
repositories {
56
jcenter()
7+
maven { url "https://dl.bintray.com/kotlin/kotlin-eap-1.1" }
68
}
79
dependencies {
8-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
10+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"
911
}
1012
}
1113

@@ -23,11 +25,12 @@ version '0.1'
2325
apply plugin: 'kotlin'
2426

2527
repositories {
28+
maven { url "https://dl.bintray.com/kotlin/kotlin-eap-1.1" }
2629
jcenter()
2730
}
2831

2932
dependencies {
30-
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
33+
compile "org.jetbrains.kotlin:kotlin-stdlib:$KOTLIN_VERSION"
3134
compile group: 'org.assertj', name: 'assertj-core', version: '2.5.0'
3235
testCompile group: 'junit', name: 'junit', version: '4.12'
3336
testCompile "org.mockito:mockito-core:1.10.19"

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
package com.memoizr.assertk
22

3-
import com.memoizr.assertk.ObjectStuff.notNull
43
import org.assertj.core.api.AbstractAssert
54
import org.assertj.core.api.Assertions
65

7-
enum class ObjectStuff {
8-
notNull
9-
}
10-
11-
inline fun <reified R : Any> of() = AbstractAssertBuilder.InstanceMatcher<R>()
12-
13-
abstract class AbstractAssertBuilder<S : AbstractAssertBuilder<S, A>, A : Any> internal constructor(actual: A?, selfType: Class<*>) {
6+
abstract class AbstractAssertBuilder<S : AbstractAssertBuilder<S, A>, A : Any>(actual: A?, selfType: Class<*>) {
147
class InstanceMatcher<R>
158

169
@Suppress("UNCHECKED_CAST", "LeakingThis")
@@ -34,13 +27,13 @@ abstract class AbstractAssertBuilder<S : AbstractAssertBuilder<S, A>, A : Any> i
3427
return myself
3528
}
3629

37-
infix fun _is(objectStuff: ObjectStuff?): S {
38-
return when (objectStuff) {
30+
infix fun _is(objectSelector: ObjectSelector?): S {
31+
return when (objectSelector) {
3932
notNull -> {
4033
assertion.isNotNull()
4134
myself
4235
}
43-
else -> {
36+
null -> {
4437
assertion.isNull()
4538
myself
4639
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.memoizr.assertk
22

3+
34
val assert: AssertionHook get() = RealAssertionHook()
45
val expect: AssertionHook get() = RealAssertionHook()
56

67
interface AssertionHook {
78
infix fun <T : Any> that(subjectUnderTest: T?) = ObjectAssert(subjectUnderTest)
89
infix fun that(subjectUnderTest: CharSequence?) = CharSequenceAssert(subjectUnderTest)
910
infix fun thatThrownBy(expressionUnderTest: () -> Unit) = ThrowableAssertion(expressionUnderTest)
11+
infix fun <ELEMENT : Any?> that(subjectUnderTest: Iterable<ELEMENT>?): IterableAssert<ELEMENT, Iterable<ELEMENT>> = IterableAssert(subjectUnderTest)
12+
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)
1016
}
1117

1218
class RealAssertionHook : AssertionHook

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package com.memoizr.assertk
22

3-
import com.memoizr.assertk.CharSequenceAssertTest.*
43
import org.assertj.core.api.AbstractCharSequenceAssert
54
import org.assertj.core.api.Assertions.assertThat
65
import java.util.regex.Pattern
76

8-
enum class CharSequenceAssertTest {
9-
nullOrEmpty, empty, notEmpty
10-
}
11-
127
class CharSequenceAssert internal constructor(
138
private val subject: CharSequence?,
149
override val assertion: AbstractCharSequenceAssert<*, out CharSequence> = assertThat(subject)) :
1510
AbstractAssertBuilder<CharSequenceAssert, CharSequence>(subject, CharSequenceAssert::class.java) {
1611

17-
object onlyDigits
18-
19-
infix fun _is(assertionTest: CharSequenceAssertTest): CharSequenceAssert {
12+
infix fun _is(assertionTest: SequenceSelector): CharSequenceAssert {
2013
when (assertionTest) {
2114
empty -> assertion.isEmpty()
2215
notEmpty -> assertion.isNotEmpty()
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 internal constructor(
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 internal constructor(
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+
}
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.AbstractIntegerAssert
4+
import org.assertj.core.api.Assertions
5+
6+
class IntegerAssert internal constructor(
7+
subjectUnderTest: Int?,
8+
override val assertion: AbstractIntegerAssert<*> = Assertions.assertThat(subjectUnderTest)) :
9+
AbstractAssertBuilder<IntegerAssert, Int>(subjectUnderTest, IntegerAssert::class.java) {
10+
11+
infix fun isLessThan(expected: Int): IntegerAssert {
12+
assertion.isLessThan(expected)
13+
return this
14+
}
15+
16+
infix fun isLessThanOrEqualTo(expected: Int): IntegerAssert {
17+
assertion.isLessThanOrEqualTo(expected)
18+
return this
19+
}
20+
21+
infix fun isGreaterThan(expected: Int): IntegerAssert {
22+
assertion.isGreaterThan(expected)
23+
return this
24+
}
25+
26+
infix fun isGreaterThanOrEqualTo(expected: Int): IntegerAssert {
27+
assertion.isGreaterThanOrEqualTo(expected)
28+
return this
29+
}
30+
31+
infix fun isBetween(expected: IntRange): IntegerAssert {
32+
assertion.isBetween(expected.start, expected.endInclusive)
33+
return this
34+
}
35+
36+
infix fun isStrictlyBetween(expected: IntRange): IntegerAssert {
37+
assertion.isStrictlyBetween(expected.start, expected.endInclusive)
38+
return this
39+
}
40+
41+
infix fun isCloseTo(expected: Int): Close {
42+
return Close(expected, assertion, this)
43+
}
44+
45+
infix fun _is(expected: NumberSelector): IntegerAssert {
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: Int, private val assertion: AbstractIntegerAssert<*>, private val assert: IntegerAssert) {
58+
infix fun withinOffset(expected: Int): IntegerAssert {
59+
assertion.isCloseTo(actual, Assertions.within(expected))
60+
return assert
61+
}
62+
63+
infix fun withinPercentage(expected: Number): IntegerAssert {
64+
assertion.isCloseTo(actual, Assertions.withinPercentage(expected.toDouble()))
65+
return assert
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)