be_identical_to.go 988 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package matchers
  2. import (
  3. "fmt"
  4. "runtime"
  5. "github.com/onsi/gomega/format"
  6. )
  7. type BeIdenticalToMatcher struct {
  8. Expected interface{}
  9. }
  10. func (matcher *BeIdenticalToMatcher) Match(actual interface{}) (success bool, matchErr error) {
  11. if actual == nil && matcher.Expected == nil {
  12. 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.")
  13. }
  14. defer func() {
  15. if r := recover(); r != nil {
  16. if _, ok := r.(runtime.Error); ok {
  17. success = false
  18. matchErr = nil
  19. }
  20. }
  21. }()
  22. return actual == matcher.Expected, nil
  23. }
  24. func (matcher *BeIdenticalToMatcher) FailureMessage(actual interface{}) string {
  25. return format.Message(actual, "to be identical to", matcher.Expected)
  26. }
  27. func (matcher *BeIdenticalToMatcher) NegatedFailureMessage(actual interface{}) string {
  28. return format.Message(actual, "not to be identical to", matcher.Expected)
  29. }