You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. package matchers
  2. import (
  3. "bytes"
  4. "fmt"
  5. "reflect"
  6. "github.com/onsi/gomega/format"
  7. )
  8. type EqualMatcher struct {
  9. Expected interface{}
  10. }
  11. func (matcher *EqualMatcher) Match(actual interface{}) (success bool, err error) {
  12. if actual == nil && matcher.Expected == nil {
  13. return false, fmt.Errorf("Refusing to compare <nil> to <nil>.\nBe explicit and use BeNil() instead. This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.")
  14. }
  15. // Shortcut for byte slices.
  16. // Comparing long byte slices with reflect.DeepEqual is very slow,
  17. // so use bytes.Equal if actual and expected are both byte slices.
  18. if actualByteSlice, ok := actual.([]byte); ok {
  19. if expectedByteSlice, ok := matcher.Expected.([]byte); ok {
  20. return bytes.Equal(actualByteSlice, expectedByteSlice), nil
  21. }
  22. }
  23. return reflect.DeepEqual(actual, matcher.Expected), nil
  24. }
  25. func (matcher *EqualMatcher) FailureMessage(actual interface{}) (message string) {
  26. actualString, actualOK := actual.(string)
  27. expectedString, expectedOK := matcher.Expected.(string)
  28. if actualOK && expectedOK {
  29. return format.MessageWithDiff(actualString, "to equal", expectedString)
  30. }
  31. return format.Message(actual, "to equal", matcher.Expected)
  32. }
  33. func (matcher *EqualMatcher) NegatedFailureMessage(actual interface{}) (message string) {
  34. return format.Message(actual, "not to equal", matcher.Expected)
  35. }