diff --git a/src/commonMain/kotlin/com/varabyte/truthish/Truth.kt b/src/commonMain/kotlin/com/varabyte/truthish/Truth.kt index 9fd21be..c2ba53d 100644 --- a/src/commonMain/kotlin/com/varabyte/truthish/Truth.kt +++ b/src/commonMain/kotlin/com/varabyte/truthish/Truth.kt @@ -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 > assertThat(actual: I) = IterableSubject(actual) fun > assertThat(actual: T) = MapSubject(actual) fun > assertThat(actual: S) = IterableSubject(actual.asIterable()) @@ -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 > that(actual: I) = IterableSubject(actual).withStrategy(deferredStrategy) fun > that(actual: T) = MapSubject(actual).withStrategy(deferredStrategy) fun > that(actual: S) = IterableSubject(actual.asIterable()).withStrategy(deferredStrategy) @@ -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 > that(actual: I) = IterableSubject(actual).withMessage(message).withStrategyOverride() fun > that(actual: T) = MapSubject(actual).withMessage(message).withStrategyOverride() fun > that(actual: S) = IterableSubject(actual.asIterable()).withMessage(message).withStrategyOverride() diff --git a/src/commonMain/kotlin/com/varabyte/truthish/subjects/StringSubject.kt b/src/commonMain/kotlin/com/varabyte/truthish/subjects/TextSubjects.kt similarity index 70% rename from src/commonMain/kotlin/com/varabyte/truthish/subjects/StringSubject.kt rename to src/commonMain/kotlin/com/varabyte/truthish/subjects/TextSubjects.kt index 5ef2a8a..d08cc02 100644 --- a/src/commonMain/kotlin/com/varabyte/truthish/subjects/StringSubject.kt +++ b/src/commonMain/kotlin/com/varabyte/truthish/subjects/TextSubjects.kt @@ -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(actual) { +class CharSequenceSubject(private val actual: CharSequence) : NotNullSubject(actual) { fun isEmpty() { - if (!actual.isEmpty()) { + if (actual.isNotEmpty()) { report(Report(Summaries.EXPECTED_EMPTY, DetailsFor.actual(actual))) } } @@ -19,7 +19,7 @@ class StringSubject(private val actual: String) : ComparableSubject(actu } fun isBlank() { - if (!actual.isBlank()) { + if (actual.isNotBlank()) { report(Report(Summaries.EXPECTED_BLANK, DetailsFor.actual(actual))) } } @@ -148,4 +148,28 @@ class StringSubject(private val actual: String) : ComparableSubject(actu fun doesNotContainMatch(regex: String) { doesNotContainMatch(Regex(regex)) } -} \ No newline at end of file +} + +class StringSubject(actual: String) : ComparableSubject(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) } +} diff --git a/src/commonTest/kotlin/com/varabyte/truthish/StringAsserts.kt b/src/commonTest/kotlin/com/varabyte/truthish/StringAsserts.kt deleted file mode 100644 index 781f4bb..0000000 --- a/src/commonTest/kotlin/com/varabyte/truthish/StringAsserts.kt +++ /dev/null @@ -1,110 +0,0 @@ -package com.varabyte.truthish - -import com.varabyte.truthish.failure.ReportError -import com.varabyte.truthish.failure.Summaries -import com.varabyte.truthish.failure.assertSubstrings -import kotlin.test.Test - -class StringAsserts { - @Test - fun stringChecks() { - run { - // Test true statements - assertThat("").isEmpty() - assertThat(" ").isBlank() - assertThat(" ").isNotEmpty() - assertThat("Hello World").isNotEmpty() - assertThat("Hello World").isNotBlank() - - assertThat("").hasLength(0) - assertThat(" ").hasLength(2) - assertThat("Hello World").hasLength(11) - - assertThat("Hello World").startsWith("Hell") - assertThat("Hello World").endsWith("orld") - assertThat("Hello World").doesNotStartWith("orld") - assertThat("Hello World").doesNotEndWith("Hell") - - assertThat("Hello World").matches("He.+ ...ld") - assertThat("Hello World").matches(Regex(".+")) - - assertThat("Hello World").doesNotMatch("Goodbye") - assertThat("Hello World").doesNotMatch(Regex("A")) - - assertThat("Hello World").contains("ello") - assertThat("Hello World").containsMatch("elll?o") - assertThat("Hello World").containsMatch(Regex("l.+ W")) - - assertThat("Hello World").doesNotContain("Jello") - assertThat("Hello World").doesNotContainMatch("0+") - assertThat("Hello World").doesNotContainMatch(Regex("[0-9]")) - } - - run { - // Test false statements - assertThrows { - assertThat("ASDF").isEmpty() - }.assertSubstrings(Summaries.EXPECTED_EMPTY) - assertThrows { - assertThat("???").isBlank() - }.assertSubstrings(Summaries.EXPECTED_BLANK) - assertThrows { - assertThat("").isNotEmpty() - }.assertSubstrings(Summaries.EXPECTED_NOT_EMPTY) - assertThrows { - assertThat(" ").isNotBlank() - }.assertSubstrings(Summaries.EXPECTED_NOT_BLANK) - - assertThrows { - assertThat("Hello World").hasLength(3) - }.assertSubstrings(Summaries.EXPECTED_COMPARISON) - - assertThrows { - assertThat("Hello World").startsWith("llo") - }.assertSubstrings(Summaries.EXPECTED_STARTS_WITH) - assertThrows { - assertThat("Hello World").endsWith("llo") - }.assertSubstrings(Summaries.EXPECTED_ENDS_WITH) - assertThrows { - assertThat("Hello World").doesNotStartWith("He") - }.assertSubstrings(Summaries.EXPECTED_NOT_STARTS_WITH) - assertThrows { - assertThat("Hello World").doesNotEndWith("ld") - }.assertSubstrings(Summaries.EXPECTED_NOT_ENDS_WITH) - - assertThrows { - assertThat("Hello World").matches("ASDF") - }.assertSubstrings(Summaries.EXPECTED_MATCH) - assertThrows { - assertThat("Hello World").matches(Regex("[0-9]+")) - }.assertSubstrings(Summaries.EXPECTED_MATCH) - - assertThrows { - assertThat("Hello World").doesNotMatch("..... .....") - }.assertSubstrings(Summaries.EXPECTED_NOT_MATCH) - assertThrows { - assertThat("Hello World").doesNotMatch(Regex(".+")) - }.assertSubstrings(Summaries.EXPECTED_NOT_MATCH) - - assertThrows { - assertThat("Hello World").contains("Wello") - }.assertSubstrings(Summaries.EXPECTED_CONTAINS) - assertThrows { - assertThat("Hello World").containsMatch("AAA?A") - }.assertSubstrings(Summaries.EXPECTED_CONTAINS) - assertThrows { - assertThat("Hello World").containsMatch(Regex("12(34)")) - }.assertSubstrings(Summaries.EXPECTED_CONTAINS) - - assertThrows { - assertThat("Hello World").doesNotContain("o Wo") - }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) - assertThrows { - assertThat("Hello World").doesNotContainMatch("l+") - }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) - assertThrows { - assertThat("Hello World").doesNotContainMatch(Regex("or.")) - }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) - } - } -} \ No newline at end of file diff --git a/src/commonTest/kotlin/com/varabyte/truthish/TextAsserts.kt b/src/commonTest/kotlin/com/varabyte/truthish/TextAsserts.kt new file mode 100644 index 0000000..d5ea4ba --- /dev/null +++ b/src/commonTest/kotlin/com/varabyte/truthish/TextAsserts.kt @@ -0,0 +1,213 @@ +package com.varabyte.truthish + +import com.varabyte.truthish.failure.ReportError +import com.varabyte.truthish.failure.Summaries +import com.varabyte.truthish.failure.assertSubstrings +import kotlin.test.Test + +class TextAsserts { + @Test + fun charSequenceChecks() { + run { + // Test true statements + assertThat(StringBuilder()).isEmpty() + assertThat(StringBuilder(" ")).isBlank() + assertThat(StringBuilder(" ")).isNotEmpty() + assertThat(StringBuilder("Hello World")).isNotEmpty() + assertThat(StringBuilder("Hello World")).isNotBlank() + + assertThat(StringBuilder("")).hasLength(0) + assertThat(StringBuilder(" ")).hasLength(2) + assertThat(StringBuilder("Hello World")).hasLength(11) + + assertThat(StringBuilder("Hello World")).startsWith("Hell") + assertThat(StringBuilder("Hello World")).endsWith("orld") + assertThat(StringBuilder("Hello World")).doesNotStartWith("orld") + assertThat(StringBuilder("Hello World")).doesNotEndWith("Hell") + + assertThat(StringBuilder("Hello World")).matches("He.+ ...ld") + assertThat(StringBuilder("Hello World")).matches(Regex(".+")) + + assertThat(StringBuilder("Hello World")).doesNotMatch("Goodbye") + assertThat(StringBuilder("Hello World")).doesNotMatch(Regex("A")) + + assertThat(StringBuilder("Hello World")).contains("ello") + assertThat(StringBuilder("Hello World")).containsMatch("elll?o") + assertThat(StringBuilder("Hello World")).containsMatch(Regex("l.+ W")) + + assertThat(StringBuilder("Hello World")).doesNotContain("Jello") + assertThat(StringBuilder("Hello World")).doesNotContainMatch("0+") + assertThat(StringBuilder("Hello World")).doesNotContainMatch(Regex("[0-9]")) + } + + run { + // Test false statements + assertThrows { + assertThat(StringBuilder("ASDF")).isEmpty() + }.assertSubstrings(Summaries.EXPECTED_EMPTY) + assertThrows { + assertThat(StringBuilder("???")).isBlank() + }.assertSubstrings(Summaries.EXPECTED_BLANK) + assertThrows { + assertThat(StringBuilder("")).isNotEmpty() + }.assertSubstrings(Summaries.EXPECTED_NOT_EMPTY) + assertThrows { + assertThat(StringBuilder(" ")).isNotBlank() + }.assertSubstrings(Summaries.EXPECTED_NOT_BLANK) + + assertThrows { + assertThat(StringBuilder("Hello World")).hasLength(3) + }.assertSubstrings(Summaries.EXPECTED_COMPARISON) + + assertThrows { + assertThat(StringBuilder("Hello World")).startsWith("llo") + }.assertSubstrings(Summaries.EXPECTED_STARTS_WITH) + assertThrows { + assertThat(StringBuilder("Hello World")).endsWith("llo") + }.assertSubstrings(Summaries.EXPECTED_ENDS_WITH) + assertThrows { + assertThat(StringBuilder("Hello World")).doesNotStartWith("He") + }.assertSubstrings(Summaries.EXPECTED_NOT_STARTS_WITH) + assertThrows { + assertThat(StringBuilder("Hello World")).doesNotEndWith("ld") + }.assertSubstrings(Summaries.EXPECTED_NOT_ENDS_WITH) + + assertThrows { + assertThat(StringBuilder("Hello World")).matches("ASDF") + }.assertSubstrings(Summaries.EXPECTED_MATCH) + assertThrows { + assertThat(StringBuilder("Hello World")).matches(Regex("[0-9]+")) + }.assertSubstrings(Summaries.EXPECTED_MATCH) + + assertThrows { + assertThat(StringBuilder("Hello World")).doesNotMatch("..... .....") + }.assertSubstrings(Summaries.EXPECTED_NOT_MATCH) + assertThrows { + assertThat(StringBuilder("Hello World")).doesNotMatch(Regex(".+")) + }.assertSubstrings(Summaries.EXPECTED_NOT_MATCH) + + assertThrows { + assertThat(StringBuilder("Hello World")).contains("Wello") + }.assertSubstrings(Summaries.EXPECTED_CONTAINS) + assertThrows { + assertThat(StringBuilder("Hello World")).containsMatch("AAA?A") + }.assertSubstrings(Summaries.EXPECTED_CONTAINS) + assertThrows { + assertThat(StringBuilder("Hello World")).containsMatch(Regex("12(34)")) + }.assertSubstrings(Summaries.EXPECTED_CONTAINS) + + assertThrows { + assertThat(StringBuilder("Hello World")).doesNotContain("o Wo") + }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) + assertThrows { + assertThat(StringBuilder("Hello World")).doesNotContainMatch("l+") + }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) + assertThrows { + assertThat(StringBuilder("Hello World")).doesNotContainMatch(Regex("or.")) + }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) + } + } + + @Test + fun stringChecks() { + run { + // Test true statements + assertThat("").isEmpty() + assertThat(" ").isBlank() + assertThat(" ").isNotEmpty() + assertThat("Hello World").isNotEmpty() + assertThat("Hello World").isNotBlank() + assertThat("Z").isGreaterThan("A") // Comparable methods work on strings + + assertThat("").hasLength(0) + assertThat(" ").hasLength(2) + assertThat("Hello World").hasLength(11) + + assertThat("Hello World").startsWith("Hell") + assertThat("Hello World").endsWith("orld") + assertThat("Hello World").doesNotStartWith("orld") + assertThat("Hello World").doesNotEndWith("Hell") + + assertThat("Hello World").matches("He.+ ...ld") + assertThat("Hello World").matches(Regex(".+")) + + assertThat("Hello World").doesNotMatch("Goodbye") + assertThat("Hello World").doesNotMatch(Regex("A")) + + assertThat("Hello World").contains("ello") + assertThat("Hello World").containsMatch("elll?o") + assertThat("Hello World").containsMatch(Regex("l.+ W")) + + assertThat("Hello World").doesNotContain("Jello") + assertThat("Hello World").doesNotContainMatch("0+") + assertThat("Hello World").doesNotContainMatch(Regex("[0-9]")) + } + + run { + // Test false statements + assertThrows { + assertThat("ASDF").isEmpty() + }.assertSubstrings(Summaries.EXPECTED_EMPTY) + assertThrows { + assertThat("???").isBlank() + }.assertSubstrings(Summaries.EXPECTED_BLANK) + assertThrows { + assertThat("").isNotEmpty() + }.assertSubstrings(Summaries.EXPECTED_NOT_EMPTY) + assertThrows { + assertThat(" ").isNotBlank() + }.assertSubstrings(Summaries.EXPECTED_NOT_BLANK) + + assertThrows { + assertThat("Hello World").hasLength(3) + }.assertSubstrings(Summaries.EXPECTED_COMPARISON) + + assertThrows { + assertThat("Hello World").startsWith("llo") + }.assertSubstrings(Summaries.EXPECTED_STARTS_WITH) + assertThrows { + assertThat("Hello World").endsWith("llo") + }.assertSubstrings(Summaries.EXPECTED_ENDS_WITH) + assertThrows { + assertThat("Hello World").doesNotStartWith("He") + }.assertSubstrings(Summaries.EXPECTED_NOT_STARTS_WITH) + assertThrows { + assertThat("Hello World").doesNotEndWith("ld") + }.assertSubstrings(Summaries.EXPECTED_NOT_ENDS_WITH) + + assertThrows { + assertThat("Hello World").matches("ASDF") + }.assertSubstrings(Summaries.EXPECTED_MATCH) + assertThrows { + assertThat("Hello World").matches(Regex("[0-9]+")) + }.assertSubstrings(Summaries.EXPECTED_MATCH) + + assertThrows { + assertThat("Hello World").doesNotMatch("..... .....") + }.assertSubstrings(Summaries.EXPECTED_NOT_MATCH) + assertThrows { + assertThat("Hello World").doesNotMatch(Regex(".+")) + }.assertSubstrings(Summaries.EXPECTED_NOT_MATCH) + + assertThrows { + assertThat("Hello World").contains("Wello") + }.assertSubstrings(Summaries.EXPECTED_CONTAINS) + assertThrows { + assertThat("Hello World").containsMatch("AAA?A") + }.assertSubstrings(Summaries.EXPECTED_CONTAINS) + assertThrows { + assertThat("Hello World").containsMatch(Regex("12(34)")) + }.assertSubstrings(Summaries.EXPECTED_CONTAINS) + + assertThrows { + assertThat("Hello World").doesNotContain("o Wo") + }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) + assertThrows { + assertThat("Hello World").doesNotContainMatch("l+") + }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) + assertThrows { + assertThat("Hello World").doesNotContainMatch(Regex("or.")) + }.assertSubstrings(Summaries.EXPECTED_NOT_CONTAINS) + } + } +} \ No newline at end of file