Skip to content

Commit

Permalink
add a few tests for edge cases for contains, doesNotContain, startsWi…
Browse files Browse the repository at this point in the history
…th, endsWith
  • Loading branch information
lhitchon committed Sep 25, 2018
1 parent 6e11738 commit d95ab60
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion assertion/contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func contains(data interface{}, key, value string) (MatchResult, error) {
default:
searchResult, err := JSONStringify(data)
if err != nil {
return matches()
return matchError(err)
}
if strings.Contains(searchResult, value) {
return matches()
Expand Down
23 changes: 23 additions & 0 deletions assertion/contains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package assertion

import (
"testing"
)

// The non error cases are covered in match_test

func TestContainsWithNonJSONType(t *testing.T) {
var complexNumber complex128
_, err := contains(complexNumber, "foo", "1")
if err == nil {
t.Errorf("Expecting contains to return an error for non JSON encodable data")
}
}

func TestDoesNotContainWithNonJSONType(t *testing.T) {
var complexNumber complex128
_, err := doesNotContain(complexNumber, "foo", "1")
if err == nil {
t.Errorf("Expecting doesNotContain to return an error for non JSON encodable data")
}
}
4 changes: 4 additions & 0 deletions assertion/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func TestIsMatch(t *testing.T) {
"containsFalseForSubstring": {sliceOfTags, "contains", "oo", "", false},
"containsTrueForSliceOfStrings": {stringSlice, "contains", "One", "", true},
"containsFalseForSliceOfStrings": {stringSlice, "contains", "Three", "", false},
"containsTrueForInt": {1, "contains", "1", "", true},
"containsFalseForInt": {1, "contains", "One", "", false},
"notContainsFalseForString": {"Foo", "does-not-contain", "oo", "", false},
"notContainsTrueForString": {"Foo", "does-not-contain", "aa", "", true},
"notContainsFalseForSlice": {sliceOfTags, "does-not-contain", "Bar", "", false},
Expand Down Expand Up @@ -109,8 +111,10 @@ func TestIsMatch(t *testing.T) {
"isNotFalse": {"100", "is-false", "", "", false},
"startsWithTrue": {"FooBar", "starts-with", "Foo", "", true},
"startsWithFalse": {"FooBar", "starts-with", "Bar", "", false},
"startsWithNonString": {1, "starts-with", "Foo", "", false},
"endsWithTrue": {"FooBar", "ends-with", "Bar", "", true},
"endsWithFalse": {"FooBar", "ends-with", "Foo", "", false},
"endsartWithNonString": {1, "ends-with", "Foo", "", false},
"isArrayTrue": {sliceOfTags, "is-array", "", "", true},
"isArrayFalse": {"Foo", "is-array", "", "", false},
"isNotArrayTrue": {sliceOfTags, "is-not-array", "", "", false},
Expand Down

0 comments on commit d95ab60

Please sign in to comment.