Skip to content

Commit

Permalink
Merge pull request #81 from MarkusAmshove/lambda-logic-assertions
Browse files Browse the repository at this point in the history
Lambda logic assertions
  • Loading branch information
MarkusAmshove authored Dec 10, 2017
2 parents e15459f + 88d3c64 commit 7961a75
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.1.+'
ext.kotlin_version = '1.2.+'
ext.spek_version = "1.0.+"
ext.mockito_version = "1.5.+"
repositories {
Expand Down Expand Up @@ -110,3 +110,9 @@ publishing {
}
}
}

compileTestKotlin {
kotlinOptions {
apiVersion = "1.2"
}
}
18 changes: 18 additions & 0 deletions docs/DefineOwn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Define own assertions

Starting with Kluent 1.32, a generic `should` method was added to build domain specific assertions.

When the receiver function passed to `should` returns `false` *or* throws an exception, it will fail with an optional failure message.

## Example

```kt
data class Person(val name: String, val surname: String)

fun Person.shouldHaveUppercaseName() = this.should("The name of $this should be uppercase") {
name[0].isUpperCase()
}


Person("Pan", "Peter").shouldHaveUppercaseName()
```
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ It uses the [Infix-Notations](https://kotlinlang.org/docs/reference/functions.ht

[FileSystem](FileSystem.md)

[Define own assertions](DefineOwn.md)

## Using backticks

Every method that is included in Kluent also has a "backtick version", to make it feel more like a describing sentence.
Expand All @@ -33,13 +35,18 @@ Some examples:

### assertEquals ##
"hello" shouldEqual "hello"
"hello".shouldEqual("hello")
"hello" `should equal` "hello"

### assertNotEquals ##
"hello" shouldNotEqual "world"
"hello".shouldNotEqual("world")
"hello" `should not equal` "world"

# Changelog
# 1.32 (WIP)
* Support lambda logic assertion | [Issue](https://github.com/MarkusAmshove/Kluent/issues/77) | [PR](https://github.com/MarkusAmshove/Kluent/pull/81) | Issue by [@goreRatzete](https://github.com/goreratzete)

# 1.31
* Expect an Exception instance to be thrown compared by equals | [PR](https://github.com/MarkusAmshove/Kluent/pull/78) | thanks to [@nhellwig](https://github.com/nhellwig)

Expand Down
17 changes: 15 additions & 2 deletions src/main/kotlin/org/amshove/kluent/Basic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlin.reflect.KClass

infix fun Any?.shouldEqual(theOther: Any?) = assertEquals(theOther, this)

infix fun Any?.shouldNotEqual(theOther: Any?) = assertNotEquals(theOther, this)
infix fun Any?.shouldNotEqual(theOther: Any?) = assertNotEquals(theOther, this)

infix fun Any?.shouldBe(theOther: Any?) = assertSame(theOther, this)

Expand All @@ -25,8 +25,21 @@ fun Any?.shouldNotBeNull() = assertNotNull(this)

fun Boolean.shouldBeTrue() = assertTrue(this)

fun Boolean.shouldBeFalse() = assertFalse(this)
fun Boolean.shouldBeFalse() = assertFalse(this)

fun Boolean.shouldNotBeTrue() = this.shouldBeFalse()

fun Boolean.shouldNotBeFalse() = this.shouldBeTrue()

infix fun <T> T.should(assertion: T.() -> Boolean) = should("Expected the assertion to return true, but returned false", assertion)

fun <T> T.should(message: String, assertion: T.() -> Boolean) = try {
if (assertion()) Unit else fail(message)
} catch (e: Exception) {
fail("""$message
|
| An exception occured:
| ${e::class.qualifiedName}: ${e.message}
| ${"\tat "}${e.stackTrace.joinToString("\n\tat ")}
""".trimMargin())
}
54 changes: 54 additions & 0 deletions src/test/kotlin/org/amshove/kluent/tests/assertions/ShouldTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.amshove.kluent.tests.assertions

import org.amshove.kluent.should
import org.amshove.kluent.shouldEqual
import org.amshove.kluent.tests.helpclasses.Person
import org.jetbrains.spek.api.Spek
import kotlin.test.assertFails

class ShouldTests : Spek({
given("the should method") {
on("passing a lambda that returns true") {
it("should pass") {
Person("", "") should { name.length == 0 }
}
}
on("passing a lambda that returns false") {
it("should fail") {
assertFails {
Person("", "").should {
name.length > 0
}
}
}
}

on("defining a domain specific assertion method") {
fun Person.shouldHaveUppercaseName() = this.should("The name of $this should be uppercase") {
name[0].isUpperCase()
}

it("should pass when returning true") {
Person("Pan", "Peter").shouldHaveUppercaseName()
}
it("should fail when returning false") {
assertFails {
Person("pan", "peter").shouldHaveUppercaseName()
}
}
it("should fail with the provided message") {
val peter = Person("pan", "peter")
try {
peter.shouldHaveUppercaseName()
} catch (e: AssertionError) {
e.message.shouldEqual("The name of $peter should be uppercase")
}
}
it("should fail when an exception occurs") {
assertFails {
Person("", "").shouldHaveUppercaseName()
}
}
}
}
})

0 comments on commit 7961a75

Please sign in to comment.