Skip to content
Open
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
12 changes: 9 additions & 3 deletions src/main/kotlin/com/winterbe/expekt/ExpectAny.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.winterbe.expekt
*
* @author Benjamin Winterberg
*/
open class ExpectAny<T>(protected val subject: T?, protected val flavor: Flavor) {
open class ExpectAny<T>(protected val subject: T?, protected val flavor: Flavor, protected var message: String? = null) {

internal val words = arrayListOf<String>()

Expand Down Expand Up @@ -45,7 +45,7 @@ open class ExpectAny<T>(protected val subject: T?, protected val flavor: Flavor)
/**
* Assert that the subject of this expectation is an instance of the given type.
*/
open fun <S: T> instanceof(type: Class<S>): ExpectAny<T> {
open fun <S : T> instanceof(type: Class<S>): ExpectAny<T> {
words.add("instanceof")
words.add(type.toString())
verify { type.isInstance(subject) }
Expand All @@ -63,6 +63,11 @@ open class ExpectAny<T>(protected val subject: T?, protected val flavor: Flavor)
return this
}

open fun withMessage(message:String): ExpectAny<T> {
this.message = message
return this
}

/**
* Assert that the subject of this expectation is equal to the given value,
* such as the following is true: `subject == expected`
Expand Down Expand Up @@ -98,7 +103,8 @@ open class ExpectAny<T>(protected val subject: T?, protected val flavor: Flavor)
}

private fun fail() {
val message = words.joinToString(separator = " ")

val message = if(this.message!=null) this.message else words.joinToString(separator = " ")
throw AssertionError(message)
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/com/winterbe/expekt/ExpectBoolean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.winterbe.expekt
*
* @author Benjamin Winterberg
*/
class ExpectBoolean(subject: Boolean?, flavor: Flavor) : ExpectAny<Boolean>(subject, flavor) {
class ExpectBoolean(subject: Boolean?, flavor: Flavor, message: String?=null) : ExpectAny<Boolean>(subject, flavor, message) {

/**
* Assert that the subject of this expectation is `true`.
Expand Down Expand Up @@ -132,4 +132,9 @@ class ExpectBoolean(subject: Boolean?, flavor: Flavor) : ExpectAny<Boolean>(subj
super.satisfy(predicate)
return this
}

override fun withMessage(message:String): ExpectBoolean {
super.withMessage(message)
return this
}
}
9 changes: 7 additions & 2 deletions src/main/kotlin/com/winterbe/expekt/ExpectCollection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.winterbe.expekt
/**
* @author Benjamin Winterberg
*/
class ExpectCollection<T>(subject: Collection<T>?, flavor: Flavor): ExpectAny<Collection<T>>(subject, flavor) {
class ExpectCollection<T>(subject: Collection<T>?, flavor: Flavor, message: String?=null): ExpectAny<Collection<T>>(subject, flavor, message) {

private var anyMode = false

Expand Down Expand Up @@ -81,7 +81,7 @@ class ExpectCollection<T>(subject: Collection<T>?, flavor: Flavor): ExpectAny<Co

val size: ExpectComparable<Int> get() {
words.add("size")
val expectInt = ExpectComparable(subject!!.size, flavor)
val expectInt = ExpectComparable(subject!!.size, flavor, message)
expectInt.negated = negated
expectInt.words.addAll(words)
expectInt.words.removeAt(0)
Expand Down Expand Up @@ -200,4 +200,9 @@ class ExpectCollection<T>(subject: Collection<T>?, flavor: Flavor): ExpectAny<Co
super.satisfy(predicate)
return this
}

override fun withMessage(message:String): ExpectCollection<T> {
super.withMessage(message)
return this
}
}
7 changes: 6 additions & 1 deletion src/main/kotlin/com/winterbe/expekt/ExpectComparable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.winterbe.expekt
*
* @author Benjamin Winterberg
*/
open class ExpectComparable<T: Comparable<T>>(subject: T?, flavor: Flavor) : ExpectAny<T>(subject, flavor) {
open class ExpectComparable<T: Comparable<T>>(subject: T?, flavor: Flavor, message: String?=null) : ExpectAny<T>(subject, flavor, message) {

/**
* Assert that the subject of this expectation is within the given `min` and `max` value
Expand Down Expand Up @@ -168,4 +168,9 @@ open class ExpectComparable<T: Comparable<T>>(subject: T?, flavor: Flavor) : Exp
super.satisfy(predicate)
return this
}

override fun withMessage(message:String): ExpectComparable<T> {
super.withMessage(message)
return this
}
}
7 changes: 6 additions & 1 deletion src/main/kotlin/com/winterbe/expekt/ExpectDouble.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.winterbe.expekt
*
* @author Benjamin Winterberg
*/
class ExpectDouble(subject: Double?, flavor: Flavor) : ExpectComparable<Double>(subject, flavor) {
class ExpectDouble(subject: Double?, flavor: Flavor, message: String?=null) : ExpectComparable<Double>(subject, flavor, message) {

/**
* Assert that the subject of this expectation is equal or close to the given value.
Expand Down Expand Up @@ -152,4 +152,9 @@ class ExpectDouble(subject: Double?, flavor: Flavor) : ExpectComparable<Double>(
super.satisfy(predicate)
return this
}

override fun withMessage(message:String): ExpectDouble {
super.withMessage(message)
return this
}
}
9 changes: 7 additions & 2 deletions src/main/kotlin/com/winterbe/expekt/ExpectMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.winterbe.expekt
/**
* @author Benjamin Winterberg
*/
class ExpectMap<K, V>(subject: Map<K, V>?, flavor: Flavor): ExpectAny<Map<K, V>>(subject, flavor) {
class ExpectMap<K, V>(subject: Map<K, V>?, flavor: Flavor, message: String?=null): ExpectAny<Map<K, V>>(subject, flavor, message) {

private var anyMode = false

Expand Down Expand Up @@ -114,7 +114,7 @@ class ExpectMap<K, V>(subject: Map<K, V>?, flavor: Flavor): ExpectAny<Map<K, V>>

val size: ExpectComparable<Int> get() {
words.add("size")
val expectInt = ExpectComparable(subject!!.size, flavor)
val expectInt = ExpectComparable(subject!!.size, flavor, message)
expectInt.negated = negated
expectInt.words.addAll(words)
expectInt.words.removeAt(0)
Expand Down Expand Up @@ -233,4 +233,9 @@ class ExpectMap<K, V>(subject: Map<K, V>?, flavor: Flavor): ExpectAny<Map<K, V>>
super.satisfy(predicate)
return this
}

override fun withMessage(message:String): ExpectMap<K, V> {
super.withMessage(message)
return this
}
}
9 changes: 7 additions & 2 deletions src/main/kotlin/com/winterbe/expekt/ExpectString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.winterbe.expekt
/**
* @author Benjamin Winterberg
*/
class ExpectString(subject: String?, flavor: Flavor) : ExpectComparable<String>(subject, flavor) {
class ExpectString(subject: String?, flavor: Flavor, message: String?=null) : ExpectComparable<String>(subject, flavor, message) {

val length: ExpectComparable<Int> get() {
words.add("length")
val expectInt = ExpectComparable(subject!!.length, this.flavor)
val expectInt = ExpectComparable(subject!!.length, this.flavor, message)
expectInt.words.addAll(words)
expectInt.words.removeAt(0)
expectInt.words.removeAt(0)
Expand Down Expand Up @@ -190,4 +190,9 @@ class ExpectString(subject: String?, flavor: Flavor) : ExpectComparable<String>(
super.satisfy(predicate)
return this
}

override fun withMessage(message:String): ExpectString {
super.withMessage(message)
return this
}
}
34 changes: 17 additions & 17 deletions src/main/kotlin/com/winterbe/expekt/Expekt.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package com.winterbe.expekt

fun <T> expect(subject: T?): ExpectAny<T?> {
return ExpectAny(subject, Flavor.EXPECT)
fun <T> expect(subject: T?, message: String? = null): ExpectAny<T?> {
return ExpectAny(subject, Flavor.EXPECT, message)
}

fun expect(subject: Boolean?): ExpectBoolean {
return ExpectBoolean(subject, Flavor.EXPECT)
fun expect(subject: Boolean?, message: String? = null): ExpectBoolean {
return ExpectBoolean(subject, Flavor.EXPECT, message)
}

fun <T: Comparable<T>> expect(subject: T?): ExpectComparable<T> {
return ExpectComparable(subject, Flavor.EXPECT)
fun <T : Comparable<T>> expect(subject: T?, message: String? = null): ExpectComparable<T> {
return ExpectComparable(subject, Flavor.EXPECT, message)
}

fun expect(subject: Double?): ExpectDouble {
return ExpectDouble(subject, Flavor.EXPECT)
fun expect(subject: Double?, message: String? = null): ExpectDouble {
return ExpectDouble(subject, Flavor.EXPECT, message)
}

fun expect(subject: String?): ExpectString {
return ExpectString(subject, Flavor.EXPECT)
fun expect(subject: String?, message: String? = null): ExpectString {
return ExpectString(subject, Flavor.EXPECT, message)
}

fun <T> expect(subject: Collection<T>?): ExpectCollection<T> {
return ExpectCollection(subject, Flavor.EXPECT)
fun <T> expect(subject: Collection<T>?, message: String? = null): ExpectCollection<T> {
return ExpectCollection(subject, Flavor.EXPECT, message)
}

fun <T> expect(subject: Sequence<T>?): ExpectCollection<T> {
return ExpectCollection(subject?.toList(), Flavor.EXPECT)
fun <T> expect(subject: Sequence<T>?, message: String? = null): ExpectCollection<T> {
return ExpectCollection(subject?.toList(), Flavor.EXPECT, message)
}

fun <K, V> expect(subject: Map<K, V>?): ExpectMap<K, V> {
return ExpectMap(subject, Flavor.EXPECT)
fun <K, V> expect(subject: Map<K, V>?, message: String? = null): ExpectMap<K, V> {
return ExpectMap(subject, Flavor.EXPECT, message)
}

val <T> T?.should: ExpectAny<T> get() {
Expand All @@ -44,7 +44,7 @@ val String?.should: ExpectString get() {
return ExpectString(this, Flavor.SHOULD)
}

val <T: Comparable<T>> T?.should: ExpectComparable<T> get() {
val <T : Comparable<T>> T?.should: ExpectComparable<T> get() {
return ExpectComparable(this, Flavor.SHOULD)
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/kotlin/com/example/ExampleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@ class ExampleTest {
}
}

@Test
fun failingTest3Message() {
try {
expect(3.4,"fail").to.be.closeTo(3.2, delta = 0.1)
Assert.fail()
} catch(e: AssertionError) {
expect(e.message).to.be.equal("fail")
}
}

@Test
fun failingTest4withMessage() {
try {
3.4.should.be.withMessage("fail").closeTo(3.2, delta = 0.1)
Assert.fail()
} catch(e: AssertionError) {
expect(e.message).to.be.equal("fail")
}
}

}