...

Source file src/github.com/onsi/gomega/matchers/be_comparable_to_matcher.go

Documentation: github.com/onsi/gomega/matchers

     1  package matchers
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/onsi/gomega/format"
     9  )
    10  
    11  type BeComparableToMatcher struct {
    12  	Expected interface{}
    13  	Options  cmp.Options
    14  }
    15  
    16  func (matcher *BeComparableToMatcher) Match(actual interface{}) (success bool, matchErr error) {
    17  	if actual == nil && matcher.Expected == nil {
    18  		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.")
    19  	}
    20  	// Shortcut for byte slices.
    21  	// Comparing long byte slices with reflect.DeepEqual is very slow,
    22  	// so use bytes.Equal if actual and expected are both byte slices.
    23  	if actualByteSlice, ok := actual.([]byte); ok {
    24  		if expectedByteSlice, ok := matcher.Expected.([]byte); ok {
    25  			return bytes.Equal(actualByteSlice, expectedByteSlice), nil
    26  		}
    27  	}
    28  
    29  	defer func() {
    30  		if r := recover(); r != nil {
    31  			success = false
    32  			if err, ok := r.(error); ok {
    33  				matchErr = err
    34  			} else if errMsg, ok := r.(string); ok {
    35  				matchErr = fmt.Errorf(errMsg)
    36  			}
    37  		}
    38  	}()
    39  
    40  	return cmp.Equal(actual, matcher.Expected, matcher.Options...), nil
    41  }
    42  
    43  func (matcher *BeComparableToMatcher) FailureMessage(actual interface{}) (message string) {
    44  	return fmt.Sprint("Expected object to be comparable, diff: ", cmp.Diff(actual, matcher.Expected, matcher.Options...))
    45  }
    46  
    47  func (matcher *BeComparableToMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    48  	return format.Message(actual, "not to be comparable to", matcher.Expected)
    49  }
    50  

View as plain text