Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception expectation. #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ listOf(1, 2, 3).should.contain(3).and.have.length.above(2)
listOf(1, 2, 3).should.contain.any.elements(1, 3, 4)
listOf(1, 2, 3).should.have.all.elements(1, 2, 3)
mapOf("foo" to "bar", "bar" to "foo").should.contain("foo" to "bar")
({ throw NullPointerException() }).should.raise(NullPointerException::class)
({ throw NullPointerException() }).should.raiseAny()
({ throw NotImplementedError() }).should.raise(NotImplementedError::class)
({ throw NotImplementedError() }).should.raiseAny()
```

Example assertions using the `expect` function:
Expand All @@ -111,8 +115,12 @@ expect(listOf(1, 2, 3)).to.contain(3).and.to.have.length.above(2)
expect(listOf(1, 2, 3)).to.contain.any.elements(1, 3, 4)
expect(listOf(1, 2, 3)).to.have.all.elements(1, 2, 3)
expect(mapOf("foo" to "bar", "bar" to "foo")).to.contain("foo" to "bar")
expect({ throw NullPointerException() }).raise(NullPointerException::class)
expect({ throw NullPointerException() }).raiseAny()
expect({ throw NotImplementedError() }).raise(NotImplementedError::class)
expect({ throw NotImplementedError() }).raiseAny()
```

### License

MIT
MIT
75 changes: 75 additions & 0 deletions src/main/kotlin/com/winterbe/expekt/ExpectClosure.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.winterbe.expekt

import kotlin.reflect.KClass

/**
* Expectation context for any kind of value. Every specific expectation context extends from this context.
*
* @author Yoichiro Sakurai
*/
class ExpectClosure(protected val subject: () -> Unit, protected val flavor: Flavor) {

internal val words = arrayListOf<String>()

init {
when (flavor) {
Flavor.EXPECT -> {
words.add("expect")
words.add("closure")
}
Flavor.SHOULD -> {
words.add("closure")
words.add("should")
}
}
}

fun <T : Throwable> raise(expected: KClass<T>): ExpectClosure {
var success = false
val expectedClass = expected.java
var actual: Throwable? = null

words.add("raise")
words.add(expectedClass.toString())

try {
subject()
} catch (t: Throwable) {
success = expectedClass.equals(t.javaClass)
if (!success) actual = t
} finally {
if (!success) fail(actual)
}

return this
}

fun raiseAny(): ExpectClosure {
var success = false

words.add("raise")
words.add("some exception")

try {
subject()
} catch (t: Throwable) {
success = true
} finally {
if (!success) fail(null)
}

return this
}

private fun fail(actual: Throwable?) {
actual?.let {
words.add("but actual thrown")
words.add(it.javaClass.toString())
} ?: fun() {
words.add("but nothing to thrown")
}()

val message = words.joinToString(separator = " ")
throw AssertionError(message)
}
}
10 changes: 9 additions & 1 deletion src/main/kotlin/com/winterbe/expekt/Expekt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ fun <K, V> expect(subject: Map<K, V>?): ExpectMap<K, V> {
return ExpectMap(subject, Flavor.EXPECT)
}

fun expect(subject: () -> Unit): ExpectClosure {
return ExpectClosure(subject, Flavor.EXPECT)
}

val <T> T?.should: ExpectAny<T> get() {
return ExpectAny(this, Flavor.SHOULD)
}
Expand Down Expand Up @@ -62,4 +66,8 @@ val <T> Sequence<T>?.should: ExpectCollection<T> get() {

val <K, V> Map<K, V>?.should: ExpectMap<K, V> get() {
return ExpectMap(this, Flavor.SHOULD)
}
}

val (() -> Unit).should: ExpectClosure get() {
return ExpectClosure(this, Flavor.SHOULD)
}
63 changes: 63 additions & 0 deletions src/test/kotlin/com/winterbe/expekt/ExpectClosureTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.winterbe.expekt

import org.junit.Test

class ExpectClosureTest {

@Test
fun raise() {
// Exception
passes {
expect({ throw NullPointerException() }).raise(NullPointerException::class)
expect({ throw CustomException() }).raise(CustomException::class)
}
fails("expect closure raise ${NullPointerException::class.java} but nothing to thrown") {
expect({ /* nothing to thrown */ }).raise(NullPointerException::class)
}
fails("expect closure raise ${NullPointerException::class.java} but actual thrown ${CustomException::class.java}") {
expect({ throw CustomException() }).raise(NullPointerException::class)
}
fails("expect closure raise ${CustomException::class.java} but nothing to thrown") {
expect({ /* nothing to thrown */ }).raise(CustomException::class)
}
fails("expect closure raise ${CustomException::class.java} but actual thrown ${NullPointerException::class.java}") {
expect({ throw NullPointerException() }).raise(CustomException::class)
}

// Error
passes {
expect({ throw NotImplementedError() }).raise(NotImplementedError::class)
expect({ throw CustomError() }).raise(CustomError::class)
}
fails("expect closure raise ${NotImplementedError::class.java} but nothing to thrown") {
expect({ /* nothing to thrown */ }).raise(NotImplementedError::class)
}
fails("expect closure raise ${NotImplementedError::class.java} but actual thrown ${CustomError::class.java}") {
expect({ throw CustomError() }).raise(NotImplementedError::class)
}
fails("expect closure raise ${CustomError::class.java} but nothing to thrown") {
expect({ /* nothing to thrown */ }).raise(CustomError::class)
}
fails("expect closure raise ${CustomError::class.java} but actual thrown ${NotImplementedError::class.java}") {
expect({ throw NotImplementedError() }).raise(CustomError::class)
}
}

@Test
fun raiseAny() {
passes {
expect({ throw NullPointerException() }).raiseAny()
expect({ throw CustomException() }).raiseAny()

expect({ throw NotImplementedError() }).raiseAny()
expect({ throw CustomError() }).raiseAny()
}
fails("expect closure raise some exception but nothing to thrown") {
expect({ /* nothing to thrown */ }).raiseAny()
}
}

}

class CustomException : Exception()
class CustomError : Error()