Skip to content

Commit

Permalink
Add IgnoringNewLines string decorator (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Papapetrou Patroklos <1743100+ppapapetrou76@users.noreply.github.com>
  • Loading branch information
mzampetakis and ppapapetrou76 committed Jan 28, 2022
1 parent d11b502 commit 82f92fd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
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

0 comments on commit 82f92fd

Please sign in to comment.