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

[FR] Whitespace + line-terminator insensitive String comparison #267

Closed
GavinRay97 opened this issue Jun 8, 2022 · 0 comments · Fixed by #268
Closed

[FR] Whitespace + line-terminator insensitive String comparison #267

GavinRay97 opened this issue Jun 8, 2022 · 0 comments · Fixed by #268

Comments

@GavinRay97
Copy link
Contributor

It's often the case that when comparing strings, you don't care about whitespace or line terminators

A big headache in many projects test suites is that multiline strings are compared, but the author copy-pastes the output resulting in OS-specific line terminators \n vs \r.

Also, outside of formatting tools, you generally don't care whether the whitespace is different between texts, only the content

I suggest a new function, something like:

infix fun strikt.api.Assertion.Builder<String>.isEqualToIgnoringWhitespace(
    expected: String
): strikt.api.Assertion.Builder<String> {
    val expectedNormalized = expected.filterNot { it.isWhitespace() }
    return assert("contains %s (ignoring whitespace)", expectedNormalized) {
        val actualNormalized = it.filterNot { it.isWhitespace() }
        if (actualNormalized == expectedNormalized) {
            pass(actual = actualNormalized)
        } else {
            fail(actual = actualNormalized)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant