-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from MarkusAmshove/lambda-logic-assertions
Lambda logic assertions
- Loading branch information
Showing
5 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/kotlin/org/amshove/kluent/tests/assertions/ShouldTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} | ||
}) |