github.com/s7techlab/cckit@v0.10.5/testing/gomega/matchers/stringer.go (about)

     1  package matchers
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/onsi/gomega/format"
     8  )
     9  
    10  type StringerEqualMatcher struct {
    11  	Expected interface{}
    12  }
    13  
    14  type Stringer interface {
    15  	String() string
    16  }
    17  
    18  func (matcher *StringerEqualMatcher) Match(actual interface{}) (success bool, err error) {
    19  	if actual == nil && matcher.Expected == nil {
    20  		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.")
    21  	}
    22  
    23  	actualStringer, okActual := actual.(Stringer)
    24  	if !okActual {
    25  		return false, errors.New("refusing to compare non-stringer actual value")
    26  	}
    27  
    28  	expectedStringer, okExpected := matcher.Expected.(Stringer)
    29  	if !okExpected {
    30  		return false, errors.New("refusing to compare non-stringer expected value")
    31  	}
    32  	return actualStringer.String() == expectedStringer.String(), nil
    33  }
    34  
    35  func (matcher *StringerEqualMatcher) FailureMessage(actual interface{}) (message string) {
    36  	actualStringer, actualOK := actual.(Stringer)
    37  	expectedStringer, expectedOK := matcher.Expected.(Stringer)
    38  	if actualOK && expectedOK {
    39  		return format.MessageWithDiff(actualStringer.String(), "to equal", expectedStringer.String())
    40  	}
    41  
    42  	return format.Message(actual, "to equal", matcher.Expected)
    43  }
    44  
    45  func (matcher *StringerEqualMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    46  	return format.Message(actual, "not to equal", matcher.Expected)
    47  }