...

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

Documentation: github.com/onsi/gomega/matchers

     1  // untested sections: 6
     2  
     3  package matchers
     4  
     5  import (
     6  	"fmt"
     7  	"reflect"
     8  
     9  	"github.com/onsi/gomega/format"
    10  )
    11  
    12  type HaveKeyMatcher struct {
    13  	Key interface{}
    14  }
    15  
    16  func (matcher *HaveKeyMatcher) Match(actual interface{}) (success bool, err error) {
    17  	if !isMap(actual) {
    18  		return false, fmt.Errorf("HaveKey matcher expects a map.  Got:%s", format.Object(actual, 1))
    19  	}
    20  
    21  	keyMatcher, keyIsMatcher := matcher.Key.(omegaMatcher)
    22  	if !keyIsMatcher {
    23  		keyMatcher = &EqualMatcher{Expected: matcher.Key}
    24  	}
    25  
    26  	keys := reflect.ValueOf(actual).MapKeys()
    27  	for i := 0; i < len(keys); i++ {
    28  		success, err := keyMatcher.Match(keys[i].Interface())
    29  		if err != nil {
    30  			return false, fmt.Errorf("HaveKey's key matcher failed with:\n%s%s", format.Indent, err.Error())
    31  		}
    32  		if success {
    33  			return true, nil
    34  		}
    35  	}
    36  
    37  	return false, nil
    38  }
    39  
    40  func (matcher *HaveKeyMatcher) FailureMessage(actual interface{}) (message string) {
    41  	switch matcher.Key.(type) {
    42  	case omegaMatcher:
    43  		return format.Message(actual, "to have key matching", matcher.Key)
    44  	default:
    45  		return format.Message(actual, "to have key", matcher.Key)
    46  	}
    47  }
    48  
    49  func (matcher *HaveKeyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    50  	switch matcher.Key.(type) {
    51  	case omegaMatcher:
    52  		return format.Message(actual, "not to have key matching", matcher.Key)
    53  	default:
    54  		return format.Message(actual, "not to have key", matcher.Key)
    55  	}
    56  }
    57  

View as plain text