Skip to content

Commit

Permalink
Add string assertions for ContainsOnlyWhitespaces and DoesNotContainO…
Browse files Browse the repository at this point in the history
…nlyWhitespaces (#24)
  • Loading branch information
mzampetakis committed Apr 25, 2021
1 parent 8a747c0 commit ae821a7
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -75,10 +75,10 @@ For the following types basic assertions are supported
* time.Duration (basic assertions)
* time.Time (basic assertions)
* moar string assertions
* [ ] ContainsOnlyWhitespaces
* [x] ContainsOnlyWhitespaces
* [x] ContainsWhitespaces
* [x] DoesNotContainAnyWhitespaces
* [ ] DoesNotContainOnlyWhitespaces
* [x] DoesNotContainOnlyWhitespaces
* [x] DoesNotEndWith
* [x] DoesNotStartWith
* [x] EndsWith
Expand Down
8 changes: 8 additions & 0 deletions assert/error_messages.go
Expand Up @@ -101,6 +101,14 @@ func shouldNotContainAnyWhiteSpace(actual types.Assertable) string {
return fmt.Sprintf("assertion failed: containable [%v] should not contain any whitespace, but it does", actual.Value())
}

func shouldContainOnlyWhiteSpaces(actual types.Assertable) string {
return fmt.Sprintf("assertion failed: containable [%v] should contain only whitespaces, but it does not", actual.Value())
}

func shouldNotContainOnlyWhiteSpaces(actual types.Assertable) string {
return fmt.Sprintf("assertion failed: containable [%v] should not contain only whitespaces, but it does", actual.Value())
}

func shouldNotContain(actual types.Assertable, elements interface{}) string {
return fmt.Sprintf("assertion failed: containable [%v] should not contain [%+v], but it does", actual.Value(), elements)
}
Expand Down
18 changes: 18 additions & 0 deletions assert/string.go
Expand Up @@ -153,6 +153,24 @@ func (a AssertableString) DoesNotContainAnyWhitespaces() AssertableString {
return a
}

// ContainsOnlyWhitespaces asserts if the assertable string contains only whitespaces
// It errors the test if it contains any other character.
func (a AssertableString) ContainsOnlyWhitespaces() AssertableString {
if !a.actual.ContainsOnlyWhitespaces() {
a.t.Error(shouldContainOnlyWhiteSpaces(a.actual))
}
return a
}

// DoesNotContainOnlyWhitespaces asserts if the assertable string does not contain only whitespaces
// It errors the test if it contains only whitespaces.
func (a AssertableString) DoesNotContainOnlyWhitespaces() AssertableString {
if a.actual.ContainsOnlyWhitespaces() {
a.t.Error(shouldNotContainOnlyWhiteSpaces(a.actual))
}
return a
}

// DoesNotContain asserts if the assertable string does not contain the given substring
// It errors the test if it contains it.
func (a AssertableString) DoesNotContain(substring string) AssertableString {
Expand Down
94 changes: 94 additions & 0 deletions assert/string_test.go
Expand Up @@ -395,6 +395,100 @@ func TestAssertableString_DoesNotContainAnyWhitespaces(t *testing.T) {
}
}

func TestAssertableString_ContainsOnlyWhitespaces(t *testing.T) {
tests := []struct {
name string
actual string
shouldFail bool
}{
{
name: "should succeed if it contains one whitespace",
actual: " ",
shouldFail: false,
},
{
name: "should succeed if contains many whitespaces",
actual: " ",
shouldFail: false,
},
{
name: "should fail if has one letter at the beginning of the string",
actual: "a ",
shouldFail: true,
},
{
name: "should fail if has one letter at the middle of the string",
actual: " a ",
shouldFail: true,
},
{
name: "should fail if has one letter at the end of the string",
actual: " a",
shouldFail: true,
},
{
name: "should fail if has no whitespace",
actual: "abcDEF123",
shouldFail: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
ft := NewFluentT(test)
ft.AssertThatString(tt.actual).ContainsOnlyWhitespaces()
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
}

func TestAssertableString_DoesNotContainOnlyWhitespaces(t *testing.T) {
tests := []struct {
name string
actual string
shouldFail bool
}{
{
name: "should succeed if has one letter at the beginning of the string",
actual: "a ",
shouldFail: false,
},
{
name: "should succeed if has one letter at the middle of the string",
actual: " a ",
shouldFail: false,
},
{
name: "should succeed if has one letter at the end of the string",
actual: " a",
shouldFail: false,
},
{
name: "should succeed if has no whitespace",
actual: "abcDEF123",
shouldFail: false,
},
{
name: "should fail if it contains one whitespace",
actual: " ",
shouldFail: true,
},
{
name: "should fail if contains many whitespaces",
actual: " ",
shouldFail: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
ft := NewFluentT(test)
ft.AssertThatString(tt.actual).DoesNotContainOnlyWhitespaces()
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
}

func TestAssertableString_DoesNotContain(t *testing.T) {
tests := []struct {
name string
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/values/string_value.go
Expand Up @@ -121,6 +121,11 @@ func (s StringValue) ContainsWhitespaces() bool {
return strings.Count(s.DecoratedValue(), " ") > 0
}

// ContainsOnlyWhitespaces returns true if the string contains only whitespaces.
func (s StringValue) ContainsOnlyWhitespaces() bool {
return strings.Count(s.DecoratedValue(), " ") == len(s.DecoratedValue())
}

func (s StringValue) greaterThan(expected StringValue) bool {
return s.DecoratedValue() > expected.value
}
Expand Down

0 comments on commit ae821a7

Please sign in to comment.