Skip to content

Commit 038b42e

Browse files
committed
Properly ask for CharSequence, some refactoring
1 parent 56fac3f commit 038b42e

File tree

7 files changed

+177
-143
lines changed

7 files changed

+177
-143
lines changed

src/main/kotlin/com/memozr/assertk/General.kt renamed to src/main/kotlin/com/memozr/assertk/AssertionHooks.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ val expect: AssertionHook get() = RealAssertionHook()
55

66
interface AssertionHook {
77
infix fun <T : Any> that(subjectUnderTest: T?) = ObjectAssert(subjectUnderTest)
8-
infix fun that(subjectUnderTest: String?) = CharSequenceAssert(subjectUnderTest)
9-
infix fun thatThrownBy(expressionUnderTest: () -> Unit) = ThrowableAssertionBuilder(expressionUnderTest)
8+
infix fun that(subjectUnderTest: CharSequence?) = CharSequenceAssert(subjectUnderTest)
9+
infix fun thatThrownBy(expressionUnderTest: () -> Unit) = ThrowableAssertion(expressionUnderTest)
1010
}
1111

1212
class RealAssertionHook : AssertionHook

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ enum class CharSequenceAssertTest {
1010
}
1111

1212
class CharSequenceAssert internal constructor(
13-
private val subject: String?,
14-
override val assertion: AbstractCharSequenceAssert<*, String> = assertThat(subject)) :
15-
AbstractAssertBuilder<CharSequenceAssert, String>(subject, CharSequenceAssert::class.java) {
13+
private val subject: CharSequence?,
14+
override val assertion: AbstractCharSequenceAssert<*, out CharSequence> = assertThat(subject)) :
15+
AbstractAssertBuilder<CharSequenceAssert, CharSequence>(subject, CharSequenceAssert::class.java) {
1616

1717
object onlyDigits
1818

File renamed without changes.

src/main/kotlin/com/memozr/assertk/ThrowableAssertionsExtension.kt renamed to src/main/kotlin/com/memozr/assertk/ThrowableAssertion.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
package com.memozr.assertk
22

3+
import org.assertj.core.api.AbstractThrowableAssert
34
import org.assertj.core.api.Assertions
45

5-
class ThrowableAssertionBuilder(func: () -> Unit) {
6-
private val assertion = Assertions.assertThatThrownBy(func)
6+
class ThrowableAssertion(
7+
func: () -> Unit,
8+
private val assertion: AbstractThrowableAssert<*, out Throwable> = Assertions.assertThatThrownBy(func)) {
79

8-
infix fun hasMessage(message: String): ThrowableAssertionBuilder {
10+
infix fun hasMessage(message: String): ThrowableAssertion {
911
assertion.hasMessage(message)
1012
return this
1113
}
1214

13-
infix fun hasCause(throwable: Throwable): ThrowableAssertionBuilder {
15+
infix fun hasCause(throwable: Throwable): ThrowableAssertion {
1416
assertion.hasCause(throwable)
1517
return this
1618
}
1719

18-
infix fun has(noCause: noCause): ThrowableAssertionBuilder {
20+
infix fun has(noCause: noCause): ThrowableAssertion {
1921
assertion.hasNoCause()
2022
return this
2123
}
2224

23-
infix fun hasMessageStartingWith(message: String): ThrowableAssertionBuilder {
25+
infix fun hasMessageStartingWith(message: String): ThrowableAssertion {
2426
assertion.hasMessageStartingWith(message)
2527
return this
2628
}
2729

28-
infix fun hasMessageEndingWith(message: String): ThrowableAssertionBuilder {
30+
infix fun hasMessageEndingWith(message: String): ThrowableAssertion {
2931
assertion.hasMessageEndingWith(message)
3032
return this
3133
}
3234

33-
infix fun hasMessageContaining(message: String): ThrowableAssertionBuilder {
35+
infix fun hasMessageContaining(message: String): ThrowableAssertion {
3436
assertion.hasMessageContaining(message)
3537
return this
3638
}
3739

38-
infix fun hasStackTraceContaining(message: String): ThrowableAssertionBuilder {
40+
infix fun hasStackTraceContaining(message: String): ThrowableAssertion {
3941
assertion.hasStackTraceContaining(message)
4042
return this
4143
}
4244

43-
infix fun hasCauseExactlyInstanceOf(throwable: Class<out Throwable>): ThrowableAssertionBuilder {
45+
infix fun hasCauseExactlyInstanceOf(throwable: Class<out Throwable>): ThrowableAssertion {
4446
assertion.hasCauseExactlyInstanceOf(throwable)
4547
return this
4648
}
4749

48-
infix fun and(assertions: ThrowableAssertionBuilder.(ThrowableAssertionBuilder) -> Unit) {
50+
infix fun and(assertions: ThrowableAssertion.(ThrowableAssertion) -> Unit) {
4951
assertions(this)
5052
}
5153

src/test/kotlin/com/memozr/assertk/Char sequence assert test.kt renamed to src/test/kotlin/com/memozr/assertk/CharSequence assert test.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import org.assertj.core.api.Assertions.assertThat
99
import org.junit.Test
1010
import java.util.regex.Pattern
1111

12-
class `Char sequence assert test` {
13-
lateinit var mockAssertion: AbstractCharSequenceAssert<*, String>
12+
class `CharSequence assert test` {
13+
lateinit var mockAssertion: AbstractCharSequenceAssert<*, out CharSequence>
1414
val _expect = object : AssertionHook {
15-
override fun that(subjectUnderTest: String?): CharSequenceAssert {
15+
override fun that(subjectUnderTest: CharSequence?): CharSequenceAssert {
1616
mockAssertion = spy(assertThat(subjectUnderTest))
1717
return CharSequenceAssert(subjectUnderTest, mockAssertion)
1818
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.memozr.assertk
2+
3+
import com.memozr.assertk.ThrowableAssertion.noCause
4+
import com.nhaarman.mockito_kotlin.spy
5+
import com.nhaarman.mockito_kotlin.verify
6+
import org.assertj.core.api.AbstractThrowableAssert
7+
import org.assertj.core.api.Assertions
8+
import org.junit.Test
9+
10+
class `Throwable assert test` {
11+
val aThrowable = Throwable()
12+
13+
private fun aThrowableWithCause() = Throwable("hello", aThrowable)
14+
15+
lateinit var mockAssertion: AbstractThrowableAssert<*, out Throwable>
16+
val _expect = object : AssertionHook {
17+
override fun thatThrownBy(expressionUnderTest: () -> Unit): ThrowableAssertion {
18+
mockAssertion = spy(Assertions.assertThatThrownBy(expressionUnderTest))
19+
return ThrowableAssertion(expressionUnderTest, mockAssertion)
20+
}
21+
}
22+
23+
@Test
24+
fun `fails when no exception is thrown`() {
25+
Assertions.assertThatThrownBy {
26+
_expect thatThrownBy { }
27+
}
28+
}
29+
30+
@Test
31+
fun `passes when exception is thrown`() {
32+
_expect thatThrownBy { throw Throwable() }
33+
}
34+
35+
@Test
36+
fun `passes when throwable message matches the specified message`() {
37+
_expect thatThrownBy { throw Throwable("hello") } hasMessage "hello"
38+
verify(mockAssertion).hasMessage("hello")
39+
}
40+
41+
@Test
42+
fun `fails when throwable message does not match specified mesage`() {
43+
expect thatThrownBy {
44+
_expect thatThrownBy { throw Throwable("this") } hasMessage "that"
45+
}
46+
verify(mockAssertion).hasMessage("that")
47+
}
48+
49+
@Test
50+
fun `passes when throwable cause and specified cause match`() {
51+
_expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable hasMessage "hello"
52+
_expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable
53+
verify(mockAssertion).hasCause(aThrowable)
54+
}
55+
56+
@Test
57+
fun `fails when throwable cause and specified cause do not match`() {
58+
val exception = Exception()
59+
expect thatThrownBy {
60+
_expect thatThrownBy { throw aThrowableWithCause() } hasCause exception
61+
}
62+
verify(mockAssertion).hasCause(exception)
63+
}
64+
65+
@Test
66+
fun `hasNoCause passes when throwable has no cause`() {
67+
_expect thatThrownBy { throw aThrowable } has noCause
68+
verify(mockAssertion).hasNoCause()
69+
}
70+
71+
@Test
72+
fun `hasNoCause fails when throwable has cause`() {
73+
expect thatThrownBy {
74+
_expect thatThrownBy { throw aThrowableWithCause() } has noCause
75+
}
76+
verify(mockAssertion).hasNoCause()
77+
}
78+
79+
@Test
80+
fun `hasMessageStartingWith checks throwable message starts with specified substring`() {
81+
_expect thatThrownBy { throw Throwable("exception foo") } hasMessageStartingWith "exc"
82+
verify(mockAssertion).hasMessageStartingWith("exc")
83+
expect thatThrownBy {
84+
_expect thatThrownBy { throw Throwable("excepiton foo") } hasMessageStartingWith "foo"
85+
}
86+
verify(mockAssertion).hasMessageStartingWith("foo")
87+
}
88+
89+
@Test
90+
fun `hasMessageEndingWith checks throwable message ends with specified substring`() {
91+
_expect thatThrownBy { throw Throwable("exception foo") } hasMessageEndingWith "foo"
92+
verify(mockAssertion).hasMessageEndingWith("foo")
93+
expect thatThrownBy {
94+
_expect thatThrownBy { throw Throwable("exception foo") } hasMessageEndingWith "exec"
95+
}
96+
verify(mockAssertion).hasMessageEndingWith("exec")
97+
}
98+
99+
@Test
100+
fun `hasMessageContaining checks throwable message ends with specified substring`() {
101+
_expect thatThrownBy { throw Throwable("exception foo") } hasMessageContaining "foo"
102+
verify(mockAssertion).hasMessageContaining("foo")
103+
104+
expect thatThrownBy {
105+
_expect thatThrownBy { throw Throwable("exception foo") } hasMessageContaining "lololol"
106+
}
107+
verify(mockAssertion).hasMessageContaining("lololol")
108+
}
109+
110+
@Test
111+
fun `hasStackTraceContaining checks stacktrace contains specified string`() {
112+
_expect thatThrownBy { throw aThrowable } hasStackTraceContaining "init"
113+
verify(mockAssertion).hasStackTraceContaining("init")
114+
115+
expect thatThrownBy {
116+
_expect thatThrownBy { throw Throwable("exception foo") } hasStackTraceContaining "not in there!!"
117+
}
118+
verify(mockAssertion).hasStackTraceContaining("not in there!!")
119+
}
120+
121+
@Test
122+
fun `hasCauseExactlyInstanceOf checks throwable cause is exactly instance of specified class`() {
123+
_expect thatThrownBy { throw Throwable(Throwable()) } hasCauseExactlyInstanceOf Throwable::class.java
124+
verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable::class.java)
125+
126+
expect thatThrownBy {
127+
_expect thatThrownBy { throw Throwable(Exception()) } hasCauseExactlyInstanceOf Throwable::class.java
128+
}
129+
verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable::class.java)
130+
131+
expect thatThrownBy {
132+
_expect thatThrownBy { throw Throwable(Throwable()) } hasCauseExactlyInstanceOf Exception::class.java
133+
}
134+
verify(mockAssertion).hasCauseExactlyInstanceOf(Exception::class.java)
135+
}
136+
137+
@Test
138+
fun `block syntax is supported`() {
139+
_expect thatThrownBy {
140+
throw Throwable("exception foo", Throwable())
141+
} and {
142+
it hasMessage "exception foo"
143+
it hasCause Throwable()
144+
it hasCauseExactlyInstanceOf Throwable::class.java
145+
it hasMessageContaining "foo"
146+
it hasMessageStartingWith "ex"
147+
it hasMessageEndingWith "foo"
148+
}
149+
150+
_expect thatThrownBy {
151+
_expect thatThrownBy { throw Throwable("exception foo", Throwable()) } and {
152+
it hasMessage "blah blah"
153+
}
154+
}
155+
}
156+
}

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

Lines changed: 0 additions & 124 deletions
This file was deleted.

0 commit comments

Comments
 (0)