Skip to content

Commit

Permalink
Convert Assert<String> extensions into Assert<CharSequence> extension…
Browse files Browse the repository at this point in the history
…s. (#309)
  • Loading branch information
Sean Amos committed Aug 14, 2020
1 parent 5c615db commit 40f640b
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 293 deletions.
91 changes: 91 additions & 0 deletions assertk/src/commonMain/kotlin/assertk/assertions/charsequence.kt
Expand Up @@ -53,3 +53,94 @@ fun Assert<CharSequence>.hasSameLengthAs(other: CharSequence) = given { actual -
if (actualLength == otherLength) return
expected("to have same length as:${show(other)} ($otherLength) but was:${show(actual)} ($actualLength)")
}

/**
* Asserts the char sequence has the expected number of lines.
*/
fun Assert<CharSequence>.hasLineCount(lineCount: Int) = given { actual ->
val actualLineCount = actual.lines().size
if (actualLineCount == lineCount) return
expected("to have line count:${show(lineCount)} but was:${show(actualLineCount)}")
}

/**
* Asserts the char sequence contains the expected subsequence.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<CharSequence>.contains(expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (actual.contains(expected, ignoreCase)) return
expected("to contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the char sequence contains the expected subsequence(s).
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<CharSequence>.contains(vararg expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (expected.all { actual.contains(it, ignoreCase) }) return
expected("to contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the char sequence contains the expected char sequences.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<CharSequence>.contains(expected: Iterable<CharSequence>, ignoreCase: Boolean = false) = given { actual ->
if (expected.all { actual.contains(it, ignoreCase) }) return
expected("to contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the char sequence does not contain the specified char sequence.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<CharSequence>.doesNotContain(expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (!actual.contains(expected, ignoreCase)) return
expected("to not contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the char sequence does not contain the specified char sequence(s).
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<CharSequence>.doesNotContain(vararg expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (expected.none { actual.contains(it, ignoreCase) }) return
expected("to not contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the char sequence does not contain the specified char sequences.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<CharSequence>.doesNotContain(expected: Iterable<CharSequence>, ignoreCase: Boolean = false) = given { actual ->
if (expected.none { actual.contains(it, ignoreCase) }) return
expected("to not contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the char sequence starts with the expected char sequence.
* @param ignoreCase true to compare ignoring case, the default if false.
* @see [endsWith]
*/
fun Assert<CharSequence>.startsWith(other: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (actual.startsWith(other, ignoreCase)) return
expected("to start with:${show(other)} but was:${show(actual)}")
}

/**
* Asserts the char sequence ends with the expected char sequence.
* @param ignoreCase true to compare ignoring case, the default if false.
* @see [startsWith]
*/
fun Assert<CharSequence>.endsWith(other: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (actual.endsWith(other, ignoreCase)) return
expected("to end with:${show(other)} but was:${show(actual)}")
}

/**
* Asserts the char sequence matches the expected regular expression.
*/
fun Assert<CharSequence>.matches(regex: Regex) = given { actual ->
if (actual.matches(regex)) return
expected("to match:${show(regex)} but was:${show(actual)}")
}
93 changes: 1 addition & 92 deletions assertk/src/commonMain/kotlin/assertk/assertions/string.kt
Expand Up @@ -5,15 +5,6 @@ import assertk.assertions.support.expected
import assertk.assertions.support.fail
import assertk.assertions.support.show

/**
* Asserts the string has the expected number of lines.
*/
fun Assert<String>.hasLineCount(lineCount: Int) = given { actual ->
val actualLineCount = actual.lines().size
if (actualLineCount == lineCount) return
expected("to have line count:${show(lineCount)} but was:${show(actualLineCount)}")
}

/**
* Asserts the string is equal to the expected string.
* @param ignoreCase true to compare ignoring case, the default if false.
Expand All @@ -36,86 +27,4 @@ fun Assert<String?>.isNotEqualTo(other: String?, ignoreCase: Boolean = false) =
} else {
expected("to not be equal to:${show(actual)}")
}
}

/**
* Asserts the string contains the expected substring.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<String>.contains(expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (actual.contains(expected, ignoreCase)) return
expected("to contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the string contains the expected substring(s).
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<String>.contains(vararg expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (expected.all { actual.contains(it, ignoreCase) }) return
expected("to contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the string contains the expected strings.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<String>.contains(expected: Iterable<CharSequence>, ignoreCase: Boolean = false) = given { actual ->
if (expected.all { actual.contains(it, ignoreCase) }) return
expected("to contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the string does not contain the specified string.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<String>.doesNotContain(expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (!actual.contains(expected, ignoreCase)) return
expected("to not contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the string does not contain the specified string(s).
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<String>.doesNotContain(vararg expected: CharSequence, ignoreCase: Boolean = false) = given { actual ->
if (expected.none { actual.contains(it, ignoreCase) }) return
expected("to not contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the string does not contain the specified strings.
* @param ignoreCase true to compare ignoring case, the default if false.
*/
fun Assert<String>.doesNotContain(expected: Iterable<CharSequence>, ignoreCase: Boolean = false) = given { actual ->
if (expected.none { actual.contains(it, ignoreCase) }) return
expected("to not contain:${show(expected)} but was:${show(actual)}")
}

/**
* Asserts the string starts with the expected string.
* @param ignoreCase true to compare ignoring case, the default if false.
* @see [endsWith]
*/
fun Assert<String>.startsWith(other: String, ignoreCase: Boolean = false) = given { actual ->
if (actual.startsWith(other, ignoreCase)) return
expected("to start with:${show(other)} but was:${show(actual)}")
}

/**
* Asserts the string ends with the expected string.
* @param ignoreCase true to compare ignoring case, the default if false.
* @see [startsWith]
*/
fun Assert<String>.endsWith(other: String, ignoreCase: Boolean = false) = given { actual ->
if (actual.endsWith(other, ignoreCase)) return
expected("to end with:${show(other)} but was:${show(actual)}")
}

/**
* Asserts the string matches the expected regular expression.
*/
fun Assert<String>.matches(regex: Regex) = given { actual ->
if (actual.matches(regex)) return
expected("to match:${show(regex)} but was:${show(actual)}")
}
}

0 comments on commit 40f640b

Please sign in to comment.