Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Assert<String> extensions into Assert<CharSequence> extensions. #309

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)}")
}
}
Loading