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 IsSubstringOf assertion #45

Merged
merged 1 commit into from Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -95,5 +95,5 @@ For the following types basic assertions are supported
* [x] IsLowerCase
* [x] IsNotEqualToIgnoringCase
* [x] IsNotEqualToIgnoringWhitespace
* [ ] IsSubstringOf
* [x] IsSubstringOf
* [x] IsUpperCase
4 changes: 4 additions & 0 deletions assert/error_messages.go
Expand Up @@ -113,6 +113,10 @@ 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)
}

func shouldBeSubstringOf(actual types.Assertable, elements interface{}) string {
return fmt.Sprintf("assertion failed: containable [%v] should be substring of [%+v], but it does", actual.Value(), elements)
}

func shouldBeMap(actual types.Assertable) string {
return fmt.Sprintf("assertion failed: assertable should be a map but it is %T", reflect.ValueOf(actual.Value()).Kind())
}
Expand Down
10 changes: 10 additions & 0 deletions assert/string.go
Expand Up @@ -195,6 +195,16 @@ func (a AssertableString) DoesNotContain(substring string) AssertableString {
return a
}

// IsSubstringOf asserts if the assertable string is a substring of the given element
// It errors the test if it does not contain it.
func (a AssertableString) IsSubstringOf(someString string) AssertableString {
a.t.Helper()
if !a.actual.IsSubstringOf(someString) {
a.t.Error(shouldBeSubstringOf(a.actual, someString))
}
return a
}

// StartsWith asserts if the assertable string starts with the given substring
// It errors the test if it doesn't start with the given substring.
func (a AssertableString) StartsWith(substring string) AssertableString {
Expand Down
44 changes: 44 additions & 0 deletions assert/string_test.go
Expand Up @@ -527,6 +527,50 @@ func TestAssertableString_DoesNotContain(t *testing.T) {
}
}

func TestAssertableString_IsSubstringOf(t *testing.T) {
tests := []struct {
name string
actual string
containerString string
shouldFail bool
stringOpts []StringOpt
}{
{
name: "should succeed if it does not contain the expected sub-string",
actual: "happy man",
containerString: "I'm a happy man",
shouldFail: false,
},
{
name: "should fail if it is substring of the containerString",
actual: "I'M a HAPPY",
containerString: "I'm a happy man",
shouldFail: true,
},
{
name: "should succeed if it is substring of the containerString with ignoring case",
actual: "I'M a HAPPY",
containerString: "I'm a happy man",
shouldFail: false,
stringOpts: []StringOpt{IgnoringCase()},
},
{
name: "should succeed if is is an empty string",
actual: "",
containerString: "I'm a happy man",
shouldFail: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
ft := NewFluentT(test)
ft.AssertThatString(tt.actual, tt.stringOpts...).IsSubstringOf(tt.containerString)
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
}

func TestAssertableString_StartsWith(t *testing.T) {
tests := []struct {
name string
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/values/string_value.go
Expand Up @@ -75,6 +75,11 @@ func (s StringValue) DoesNotContain(expected interface{}) bool {
return !s.Contains(s.decoratedValue(expected))
}

// IsSubstringOf returns true if the string is substring of the given string.
func (s StringValue) IsSubstringOf(expected interface{}) bool {
return strings.Contains(NewStringValue(s.decoratedValue(expected)).value, s.DecoratedValue())
}

// HasSize returns true if the string has the expected size else false.
func (s StringValue) HasSize(length int) bool {
return s.Size() == length
Expand Down