From ae821a704adbb095e58a08e2c3fc552ff608c5e6 Mon Sep 17 00:00:00 2001 From: Michalis Zampetakis Date: Sun, 25 Apr 2021 21:14:24 +0300 Subject: [PATCH] Add string assertions for ContainsOnlyWhitespaces and DoesNotContainOnlyWhitespaces (#24) --- README.md | 4 +- assert/error_messages.go | 8 +++ assert/string.go | 18 ++++++ assert/string_test.go | 94 +++++++++++++++++++++++++++++ internal/pkg/values/string_value.go | 5 ++ 5 files changed, 127 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e3509e..c1f07f6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/assert/error_messages.go b/assert/error_messages.go index 285ec71..6c17727 100644 --- a/assert/error_messages.go +++ b/assert/error_messages.go @@ -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) } diff --git a/assert/string.go b/assert/string.go index c573bbf..d8a342d 100644 --- a/assert/string.go +++ b/assert/string.go @@ -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 { diff --git a/assert/string_test.go b/assert/string_test.go index 2e38bcd..d8dc5ab 100644 --- a/assert/string_test.go +++ b/assert/string_test.go @@ -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 diff --git a/internal/pkg/values/string_value.go b/internal/pkg/values/string_value.go index 61831b7..91d963c 100644 --- a/internal/pkg/values/string_value.go +++ b/internal/pkg/values/string_value.go @@ -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 }