Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | package matchers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/onsi/gomega/format" |
| 7 | ) |
| 8 | |
| 9 | type HaveLenMatcher struct { |
| 10 | Count int |
| 11 | } |
| 12 | |
| 13 | func (matcher *HaveLenMatcher) Match(actual interface{}) (success bool, err error) { |
| 14 | length, ok := lengthOf(actual) |
| 15 | if !ok { |
| 16 | return false, fmt.Errorf("HaveLen matcher expects a string/array/map/channel/slice. Got:\n%s", format.Object(actual, 1)) |
| 17 | } |
| 18 | |
| 19 | return length == matcher.Count, nil |
| 20 | } |
| 21 | |
| 22 | func (matcher *HaveLenMatcher) FailureMessage(actual interface{}) (message string) { |
| 23 | return fmt.Sprintf("Expected\n%s\nto have length %d", format.Object(actual, 1), matcher.Count) |
| 24 | } |
| 25 | |
| 26 | func (matcher *HaveLenMatcher) NegatedFailureMessage(actual interface{}) (message string) { |
| 27 | return fmt.Sprintf("Expected\n%s\nnot to have length %d", format.Object(actual, 1), matcher.Count) |
| 28 | } |