diff options
Diffstat (limited to 'test/utils/matchers.go')
-rw-r--r-- | test/utils/matchers.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/test/utils/matchers.go b/test/utils/matchers.go index 69fb0cdfe..288779b63 100644 --- a/test/utils/matchers.go +++ b/test/utils/matchers.go @@ -1,6 +1,7 @@ package utils import ( + "encoding/json" "fmt" "net/url" @@ -166,3 +167,32 @@ func (matcher *ExitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool { } return true } + +type ValidJSONMatcher struct { + types.GomegaMatcher +} + +func BeValidJSON() *ValidJSONMatcher { + return &ValidJSONMatcher{} +} + +func (matcher *ValidJSONMatcher) Match(actual interface{}) (success bool, err error) { + s, ok := actual.(string) + if !ok { + return false, fmt.Errorf("ValidJSONMatcher expects a string, not %q", actual) + } + + var i interface{} + if err := json.Unmarshal([]byte(s), &i); err != nil { + return false, nil + } + return true, nil +} + +func (matcher *ValidJSONMatcher) FailureMessage(actual interface{}) (message string) { + return format.Message(actual, "to be valid JSON") +} + +func (matcher *ValidJSONMatcher) NegatedFailureMessage(actual interface{}) (message string) { + return format.Message(actual, "to _not_ be valid JSON") +} |