Skip to content

Commit 71d3785

Browse files
committed
improve readme, add block syntax for abstract assertions
1 parent fd8602d commit 71d3785

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
1-
# assertk
2-
A kotlin-friendly wrapper for assertJd
1+
# AssertK - Fluent assertions for Kotlin
2+
AssertK provides a Kotlin-friendly syntax for using the amazing AssertJ assertion framework.
3+
4+
###Simple assertions
5+
```
6+
assert that Unit isEqualTo Unit
7+
assert that Any() isNotEqualTo Any()
8+
assert that Any() _is notNull
9+
assert that nullObject _is null
10+
assert that anObject isInstance of<Any>()
11+
assert that Any() describedAs "A labeled object" isInstance of<Unit>()
12+
```
13+
14+
###Chained syntax
15+
```
16+
assert that Unit isNotEqualTo Any() isEqualTo Unit _is notNull isInstance of<Any>()
17+
```
18+
19+
###Block syntax
20+
```
21+
assert that Any() isSuchThat {
22+
it _is notNull
23+
it isInstance of<Any>()
24+
it isNotEqualTo Unit
25+
it isNotEqualTo Any()
26+
}
27+
```
28+
29+
##Assertions on exceptions
30+
###Chained syntax
31+
```
32+
assert thatExceptionIsThrownBy { failFunction() } hasMessageContaining "foo" hasCause Throwable()
33+
```
34+
35+
###Block syntax
36+
```
37+
assert thatExceptionIsThrownBy {
38+
throw Throwable("exception foo", Throwable())
39+
} and {
40+
it hasMessage "exception foo"
41+
it hasCause Throwable()
42+
it hasCauseExactlyInstanceOf Throwable::class.java
43+
it hasMessageContaining "foo"
44+
it hasMessageStartingWith "ex"
45+
it hasMessageEndingWith "foo"
46+
}
47+
```

src/main/kotlin/com/memozr/assertk/AbstractAssertBuilder.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ class AbstractAssertBuilder<T : Any>(other: T?) {
4747
assertion.`as`(description)
4848
return this
4949
}
50+
51+
infix fun isSuchThat(assertionBlock: AbstractAssertBuilder<T>.(AbstractAssertBuilder<T>) -> Unit): AbstractAssertBuilder<T> {
52+
assertionBlock(this)
53+
return this
54+
}
5055
}

src/test/kotlin/com/memozr/assertk/AbstractAssertBuilderTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,21 @@ class AbstractAssertBuilderTest {
5959
assert that Any() describedAs "A labeled object" isInstance of<Unit>()
6060
} hasMessageContaining "A labeled object"
6161
}
62+
63+
@Test
64+
fun `block assertions`() {
65+
assert that Any() isSuchThat {
66+
it _is notNull
67+
it isInstance of<Any>()
68+
it isNotEqualTo Unit
69+
it isNotEqualTo Any()
70+
}
71+
72+
assert thatExceptionIsThrownBy {
73+
assert that Any() isSuchThat {
74+
it _is null
75+
it isInstance of<Unit>()
76+
}
77+
}
78+
}
6279
}

0 commit comments

Comments
 (0)