Skip to content

Commit

Permalink
Add subject for CharSequence
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspittle committed Dec 7, 2024
1 parent 6c95f36 commit 66020f1
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 114 deletions.
3 changes: 3 additions & 0 deletions src/commonMain/kotlin/com/varabyte/truthish/Truth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fun assertThat(actual: Long) = LongSubject(actual)
fun assertThat(actual: Float) = FloatSubject(actual)
fun assertThat(actual: Double) = DoubleSubject(actual)
fun assertThat(actual: String) = StringSubject(actual)
fun assertThat(actual: CharSequence) = CharSequenceSubject(actual)
fun <T, I: Iterable<T>> assertThat(actual: I) = IterableSubject(actual)
fun <K, V, T: Map<K, V>> assertThat(actual: T) = MapSubject(actual)
fun <T, S: Sequence<T>> assertThat(actual: S) = IterableSubject(actual.asIterable())
Expand Down Expand Up @@ -83,6 +84,7 @@ class AssertAllScope(summary: String?) {
fun that(actual: Float) = FloatSubject(actual).withStrategy(deferredStrategy)
fun that(actual: Double) = DoubleSubject(actual).withStrategy(deferredStrategy)
fun that(actual: String) = StringSubject(actual).withStrategy(deferredStrategy)
fun that(actual: CharSequence) = CharSequenceSubject(actual).withStrategy(deferredStrategy)
fun <T, I: Iterable<T>> that(actual: I) = IterableSubject(actual).withStrategy(deferredStrategy)
fun <K, V, T: Map<K, V>> that(actual: T) = MapSubject(actual).withStrategy(deferredStrategy)
fun <T, S: Sequence<T>> that(actual: S) = IterableSubject(actual.asIterable()).withStrategy(deferredStrategy)
Expand Down Expand Up @@ -146,6 +148,7 @@ class SummarizedSubjectBuilder(private val message: String, private val strategy
fun that(actual: Float) = FloatSubject(actual).withMessage(message).withStrategyOverride()
fun that(actual: Double) = DoubleSubject(actual).withMessage(message).withStrategyOverride()
fun that(actual: String) = StringSubject(actual).withMessage(message).withStrategyOverride()
fun that(actual: CharSequence) = CharSequenceSubject(actual).withMessage(message).withStrategyOverride()
fun <T, I: Iterable<T>> that(actual: I) = IterableSubject(actual).withMessage(message).withStrategyOverride()
fun <K, V, T: Map<K, V>> that(actual: T) = MapSubject(actual).withMessage(message).withStrategyOverride()
fun <T, S: Sequence<T>> that(actual: S) = IterableSubject(actual.asIterable()).withMessage(message).withStrategyOverride()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.varabyte.truthish.failure.DetailsFor
import com.varabyte.truthish.failure.Report
import com.varabyte.truthish.failure.Summaries

class StringSubject(private val actual: String) : ComparableSubject<String>(actual) {
class CharSequenceSubject(private val actual: CharSequence) : NotNullSubject<CharSequence>(actual) {

fun isEmpty() {
if (!actual.isEmpty()) {
if (actual.isNotEmpty()) {
report(Report(Summaries.EXPECTED_EMPTY, DetailsFor.actual(actual)))
}
}
Expand All @@ -19,7 +19,7 @@ class StringSubject(private val actual: String) : ComparableSubject<String>(actu
}

fun isBlank() {
if (!actual.isBlank()) {
if (actual.isNotBlank()) {
report(Report(Summaries.EXPECTED_BLANK, DetailsFor.actual(actual)))
}
}
Expand Down Expand Up @@ -148,4 +148,28 @@ class StringSubject(private val actual: String) : ComparableSubject<String>(actu
fun doesNotContainMatch(regex: String) {
doesNotContainMatch(Regex(regex))
}
}
}

class StringSubject(actual: String) : ComparableSubject<String>(actual) {
private val delegateSubject = CharSequenceSubject(actual)

fun isEmpty() { delegateSubject.isEmpty() }
fun isNotEmpty() { delegateSubject.isNotEmpty() }
fun isBlank() { delegateSubject.isBlank() }
fun isNotBlank() { delegateSubject.isNotBlank() }
fun hasLength(expectedLength: Int) { delegateSubject.hasLength(expectedLength) }
fun startsWith(expected: CharSequence) { delegateSubject.startsWith(expected) }
fun doesNotStartWith(expected: CharSequence) { delegateSubject.doesNotStartWith(expected) }
fun endsWith(expected: CharSequence) { delegateSubject.endsWith(expected) }
fun doesNotEndWith(expected: CharSequence) { delegateSubject.doesNotEndWith(expected) }
fun matches(regex: Regex) { delegateSubject.matches(regex) }
fun matches(regex: String) { delegateSubject.matches(regex) }
fun doesNotMatch(regex: Regex) { delegateSubject.doesNotMatch(regex) }
fun doesNotMatch(regex: String) { delegateSubject.doesNotMatch(regex) }
fun contains(expected: CharSequence) { delegateSubject.contains(expected) }
fun containsMatch(regex: Regex) { delegateSubject.containsMatch(regex) }
fun containsMatch(regex: String) { delegateSubject.containsMatch(regex) }
fun doesNotContain(expected: CharSequence) { delegateSubject.doesNotContain(expected) }
fun doesNotContainMatch(regex: Regex) { delegateSubject.doesNotContainMatch(regex) }
fun doesNotContainMatch(regex: String) { delegateSubject.doesNotContainMatch(regex) }
}
110 changes: 0 additions & 110 deletions src/commonTest/kotlin/com/varabyte/truthish/StringAsserts.kt

This file was deleted.

Loading

0 comments on commit 66020f1

Please sign in to comment.