https://github.com/odd-poet/kotlin-expect
assertion library for kotlin test.
https://github.com/odd-poet/kotlin-expect
expect kotlin kotlin-library rspec test-framework
Last synced: about 2 months ago
JSON representation
assertion library for kotlin test.
- Host: GitHub
- URL: https://github.com/odd-poet/kotlin-expect
- Owner: odd-poet
- License: apache-2.0
- Created: 2017-07-05T04:58:47.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-12-13T18:11:57.000Z (6 months ago)
- Last Synced: 2025-03-25T23:15:16.463Z (2 months ago)
- Topics: expect, kotlin, kotlin-library, rspec, test-framework
- Language: Kotlin
- Size: 285 KB
- Stars: 4
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kotlin-expect
[](https://travis-ci.org/odd-poet/kotlin-expect)[](https://codecov.io/gh/odd-poet/kotlin-expect)
`kotlin-expect` is a assertion library for kotlin test. it's inspried by [Rspec Expectation].
## Setup
```gradle
dependencies {
testCompile("net.oddpoet:kotlin-expect:1.3.2")
}
```## Basic Usage
### `expect(s).to`
You can write an assertion for a subject in the form `expect(subject).to`.
```kotlin
val list = listOf(1, 2, 3)
expect(list).to.haveSizeOf(3)
expect(list).to.satisfy { size == 3 }
expect(list).to.not.contain(5)
expect(list).to.containAll { it < 10 }
expect(list).to.not.beInstanceOf(Set::class)
```### `should`
Alternatively, you can write assertions in the form `subject.should` more simply.
```kotlin
"hello".should.startWith("h")
"hello".should.not.endWith("x", ignoreCase = true)
"believe".should.match("lie")
"hello".length.should.be(5)
```### `expect(s) { ... }`
You can also create multiple assertions for a subject in the form `expect(s) {...}`
```kotlin
expect(aList) {
it.should.haveSizeOf(10)
it.should.not.contain("hello")
it.should.containAny { it.lenngth < 2 }
}
```### `expect { }.throws()`
An assertion for an exception can be written in the form `expect { ... }.throws()`.
```kotlin
expect {
throw IOExpection()
}.throws()expect {
throw NoSuchFileException("file.txt")
}.throws(IOExcpetion::class) {
it.message.should.be("file.txt")
}```
## Write own your assertion
`Kotlin-expect` has built-in assertions for java base types(`String`, `Collection`, `Map`, `Number` and so on).
You can define new assertions for your class.
An assertion for a class is defined as an extension of the `Expect` class.### example
```kotlin
// for your classes
abstract class Person(
val name: String,
val birthdate: LocalDate
)class Employee(
name: String, birthdate: LocalDate,
val empNo: String?,
val dept: String?
) : Person(name, birthdate)
``````kotlin
// you can write your own assertion
fun Expect.beUnderage() =
satisfyThat("be underage") {
it.birthdate.plusYears(19) > LocalDate.now()
}fun Expect.beValid() =
satisfyThat("be valid") {
it.empNo != null && it.dept != null
}fun Expect.beAssignedTo(dept: String) =
satisfyThat("be assigned to $dept") {
it.dept == dept
}
``````kotlin
// then you can use your assertion.
val emp = Employee(
"yunsang.choi",
LocalDate.of(1976, 4, 2),
"X00000",
"DevTeam"
)
expect(emp) {
it.should.beValid()
it.should.not.beUnderage()
it.should.not.beAssignedTo("DesignTeam")
}```
[Rspec Expectation]:https://github.com/rspec/rspec-expectations