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

Add IgnoringNewLines string decorator #44

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -90,7 +90,7 @@ For the following types basic assertions are supported
* [x] HasSizeLessThan
* [x] HasSizeLessThanOrEqualTo
* [x] IsEqualToIgnoringCase
* [ ] IsEqualToIgnoringNewLines
* [x] IsEqualToIgnoringNewLines
* [x] IsEqualToIgnoringWhitespace
* [x] IsLowerCase
* [x] IsNotEqualToIgnoringCase
Expand Down
7 changes: 7 additions & 0 deletions assert/string.go
Expand Up @@ -30,6 +30,13 @@ func IgnoringWhiteSpaces() StringOpt {
}
}

// IgnoringNewLines removes the new lines from the value under assertion.
func IgnoringNewLines() StringOpt {
return func(c *AssertableString) {
c.actual = c.actual.AddDecorator(values.RemoveNewLines)
}
}

// ThatString returns an AssertableString structure initialized with the test reference and the actual value to assert.
func ThatString(t *testing.T, actual string, opts ...StringOpt) AssertableString {
t.Helper()
Expand Down
15 changes: 12 additions & 3 deletions assert/string_test.go
Expand Up @@ -63,7 +63,7 @@ func TestAssertableString_IsEqualTo(t *testing.T) {
actual string
expected string
shouldFail bool
StringOpt
stringOpts []StringOpt
}{
{
name: "should assert not equal strings",
Expand All @@ -82,13 +82,22 @@ func TestAssertableString_IsEqualTo(t *testing.T) {
actual: "somestring with white spaces",
expected: "some stringwith white spaces",
shouldFail: false,
StringOpt: IgnoringWhiteSpaces(),
stringOpts: []StringOpt{IgnoringWhiteSpaces()},
},
{
name: "should assert equal strings ignoring new lines",
actual: `some string
with white spaces`,
expected: `some string with
white spaces`,
shouldFail: false,
stringOpts: []StringOpt{IgnoringNewLines()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
ThatString(test, tt.actual, tt.StringOpt).IsEqualTo(tt.expected)
ThatString(test, tt.actual, tt.stringOpts...).IsEqualTo(tt.expected)
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/values/string_value.go
Expand Up @@ -20,6 +20,12 @@ func RemoveSpaces(value string) string {
return strings.ReplaceAll(value, " ", "")
}

// RemoveNewLines removes all new lines from the given string.
func RemoveNewLines(value string) string {
value = strings.ReplaceAll(value, "\r\n", "")
return strings.ReplaceAll(value, "\n", "")
}

// IsEqualTo returns true if the value is equal to the expected value, else false.
func (s StringValue) IsEqualTo(expected interface{}) bool {
return s.DecoratedValue() == s.decoratedValue(expected)
Expand Down