Skip to content

Commit

Permalink
Add IsSubstringOf assertion (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzampetakis committed Jan 28, 2022
1 parent 47d1f22 commit d11b502
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
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

0 comments on commit d11b502

Please sign in to comment.