Skip to content

Commit 7480253

Browse files
committed
add CharSequenceAssert
1 parent bbe6576 commit 7480253

File tree

6 files changed

+360
-7
lines changed

6 files changed

+360
-7
lines changed

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
ext.kotlin_version = '1.0.3'
66

77
repositories {
8-
mavenCentral()
8+
jcenter()
99
}
1010
dependencies {
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
@@ -15,11 +15,13 @@ buildscript {
1515
apply plugin: 'kotlin'
1616

1717
repositories {
18-
mavenCentral()
18+
jcenter()
1919
}
2020

2121
dependencies {
2222
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2323
compile group: 'org.assertj', name: 'assertj-core', version: '3.5.0'
2424
testCompile group: 'junit', name: 'junit', version: '4.12'
25+
testCompile "org.mockito:mockito-core:1.10.19"
26+
testCompile "com.nhaarman:mockito-kotlin:0.2.0"
2527
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.memozr.assertk
22

33
import com.memozr.assertk.ObjectStuff.notNull
4+
import org.assertj.core.api.AbstractAssert
45
import org.assertj.core.api.Assertions
56

67
enum class ObjectStuff {
@@ -9,10 +10,10 @@ enum class ObjectStuff {
910

1011
inline fun <reified R : Any> of() = AbstractAssertBuilder.InstanceMatcher<R>()
1112

12-
class AbstractAssertBuilder<T : Any>(other: T?) {
13+
open class AbstractAssertBuilder<T : Any>(other: T?) {
1314
class InstanceMatcher<R>
1415

15-
val assertion = Assertions.assertThat(other)
16+
open protected val assertion: AbstractAssert<*, T?> = Assertions.assertThat(other)
1617

1718
infix fun isEqualTo(other: T): AbstractAssertBuilder<T> {
1819
assertion.isEqualTo(other)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.memozr.assertk
2+
3+
import com.memozr.assertk.CharSequenceAssertTest.*
4+
import org.assertj.core.api.AbstractCharSequenceAssert
5+
import org.assertj.core.api.Assertions.assertThat
6+
import java.util.regex.Pattern
7+
8+
enum class CharSequenceAssertTest {
9+
nullOrEmpty, empty, notEmpty
10+
}
11+
12+
class CharSequenceAssert internal constructor(
13+
private val subject: String?,
14+
override protected val assertion: AbstractCharSequenceAssert<*, String> = assertThat(subject))
15+
: AbstractAssertBuilder<String>(subject) {
16+
17+
object onlyDigits
18+
19+
infix fun _is(assertionTest: CharSequenceAssertTest): CharSequenceAssert {
20+
when (assertionTest) {
21+
empty -> assertion.isEmpty()
22+
notEmpty -> assertion.isNotEmpty()
23+
nullOrEmpty -> assertion.isNullOrEmpty()
24+
}
25+
return this
26+
}
27+
28+
infix fun hasSize(expectedSize: Int): CharSequenceAssert {
29+
assertion.hasSize(expectedSize)
30+
return this
31+
}
32+
33+
infix fun hasLineCount(expectedLineCount: Int): CharSequenceAssert {
34+
assertion.hasLineCount(expectedLineCount)
35+
return this
36+
}
37+
38+
infix fun isEqualToIgnoringCase(expected: String): CharSequenceAssert {
39+
assertion.isEqualToIgnoringCase(expected)
40+
return this
41+
}
42+
43+
infix fun isNotEqualToIgnoringCase(expected: String): CharSequenceAssert {
44+
assertion.isNotEqualToIgnoringCase(expected)
45+
return this
46+
}
47+
48+
infix fun contains(onlyDigits: onlyDigits): CharSequenceAssert {
49+
assertion.containsOnlyDigits()
50+
return this
51+
}
52+
53+
infix fun contains(expected: CharSequence): CharSequenceAssert {
54+
assertion.contains(expected)
55+
return this
56+
}
57+
58+
infix fun containsOnlyOnce(expected: String): CharSequenceAssert {
59+
assertion.containsOnlyOnce(expected)
60+
return this
61+
}
62+
63+
infix fun contains(expected: Iterable<CharSequence>): CharSequenceAssert {
64+
assertion.contains(expected)
65+
return this
66+
}
67+
68+
infix fun containsSequence(expected: Iterable<CharSequence>): CharSequenceAssert {
69+
assertion.containsSequence(expected)
70+
return this
71+
}
72+
73+
infix fun containsIgnoringCase(expected: String): CharSequenceAssert {
74+
assertion.containsIgnoringCase(expected)
75+
return this
76+
}
77+
78+
infix fun doesNotContain(expected: CharSequence): CharSequenceAssert {
79+
assertion.doesNotContain(expected)
80+
return this
81+
}
82+
83+
infix fun startsWith(expected: String): CharSequenceAssert {
84+
assertion.startsWith(expected)
85+
return this
86+
}
87+
88+
infix fun doesNotStartWith(expected: CharSequence): CharSequenceAssert {
89+
assertion.doesNotStartWith(expected)
90+
return this
91+
}
92+
93+
infix fun endsWith(expected: CharSequence): CharSequenceAssert {
94+
assertion.endsWith(expected)
95+
return this
96+
}
97+
98+
infix fun doesNotEndWith(expected: CharSequence): CharSequenceAssert {
99+
assertion.doesNotEndWith(expected)
100+
return this
101+
}
102+
103+
infix fun matches(expected: Pattern): CharSequenceAssert {
104+
assertion.matches(expected)
105+
return this
106+
}
107+
108+
infix fun doesNotMatch(expected: Pattern): CharSequenceAssert {
109+
assertion.doesNotMatch(expected)
110+
return this
111+
}
112+
113+
infix fun matches(expected: CharSequence): CharSequenceAssert {
114+
assertion.matches(expected)
115+
return this
116+
}
117+
118+
infix fun doesNotMatch(expected: CharSequence): CharSequenceAssert {
119+
assertion.doesNotMatch(expected)
120+
return this
121+
}
122+
123+
infix fun isEqualToIgnoringWhitespace(expected: CharSequence): CharSequenceAssert {
124+
assertion.isEqualToIgnoringWhitespace(expected)
125+
return this
126+
}
127+
128+
infix fun isNotEqualToIgnoringWhitespace(expected: CharSequence): CharSequenceAssert {
129+
assertion.isNotEqualToIgnoringWhitespace(expected)
130+
return this
131+
}
132+
133+
infix fun isSubstringOf(expected: CharSequence): CharSequenceAssert {
134+
assertion.isSubstringOf(expected)
135+
return this
136+
}
137+
138+
infix fun containsPattern(pattern: String): CharSequenceAssert {
139+
assertion.containsPattern(pattern)
140+
return this
141+
}
142+
143+
infix fun containsPattern(pattern: Pattern): CharSequenceAssert {
144+
assertion.containsPattern(pattern)
145+
return this
146+
}
147+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.memozr.assertk
22

3-
val assert: AssertionHook get() = AssertionHook()
4-
val expect: AssertionHook get() = AssertionHook()
3+
val assert: AssertionHook get() = RealAssertionHook()
4+
val expect: AssertionHook get() = RealAssertionHook()
55

6-
class AssertionHook {
6+
interface AssertionHook {
77
infix fun <T : Any> that(subjectUnderTest: T?) = AbstractAssertBuilder(subjectUnderTest)
8+
infix fun that(subjectUnderTest: String?) = CharSequenceAssert(subjectUnderTest)
89
infix fun thatThrownBy(expressionUnderTest: () -> Unit) = ThrowableAssertionBuilder(expressionUnderTest)
910
}
1011

12+
class RealAssertionHook : AssertionHook
13+
1114

0 commit comments

Comments
 (0)