func (lm *logMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return fmt.Sprintf(
		"Expected\n\t%s\nnot to contain log sequence \n\t%s",
		format.Object(lm.actual, 0),
		format.Object(lm.expected, 0),
	)
}
func (matcher *HaveOccurredMatcher) Match(actual interface{}) (success bool, message string, err error) {
	if actual == nil {
		return false, format.Message(actual, "to have occurred"), nil
	} else {
		if isError(actual) {
			return true, fmt.Sprintf("Expected error:\n%s\n%s\n%s", format.Object(actual, 1), format.IndentString(actual.(error).Error(), 1), "not to have occurred"), nil
		} else {
			return false, "", fmt.Errorf("Expected an error.  Got:\n%s", format.Object(actual, 1))
		}
	}
}
func (matcher *ReceiveMatcher) Match(actual interface{}) (success bool, message string, err error) {
	if !isChan(actual) {
		return false, "", fmt.Errorf("ReceiveMatcher expects a channel.  Got:\n%s", format.Object(actual, 1))
	}

	channelType := reflect.TypeOf(actual)
	channelValue := reflect.ValueOf(actual)

	if channelType.ChanDir() == reflect.SendDir {
		return false, "", fmt.Errorf("ReceiveMatcher matcher cannot be passed a send-only channel.  Got:\n%s", format.Object(actual, 1))
	}

	if matcher.Arg != nil {
		argType := reflect.TypeOf(matcher.Arg)
		if argType.Kind() != reflect.Ptr {
			return false, "", fmt.Errorf("Cannot assign a value from the channel:\n%s\nTo:\n%s\nYou need to pass a pointer!", format.Object(actual, 1), format.Object(matcher.Arg, 1))
		}

		assignable := channelType.Elem().AssignableTo(argType.Elem())
		if !assignable {
			return false, "", fmt.Errorf("Cannot assign a value from the channel:\n%s\nTo:\n%s", format.Object(actual, 1), format.Object(matcher.Arg, 1))
		}
	}

	var closed bool
	var didReceive bool

	winnerIndex, value, open := reflect.Select([]reflect.SelectCase{
		reflect.SelectCase{Dir: reflect.SelectRecv, Chan: channelValue},
		reflect.SelectCase{Dir: reflect.SelectDefault},
	})

	if winnerIndex == 0 {
		closed = !open
		didReceive = open
	} else if winnerIndex == 1 {
		closed = false
		didReceive = false
	}

	if closed {
		return false, "", fmt.Errorf("ReceiveMatcher was given a closed channel:\n%s", format.Object(actual, 1))
	}

	if didReceive {
		if matcher.Arg != nil {
			outValue := reflect.ValueOf(matcher.Arg)
			reflect.Indirect(outValue).Set(value)
		}
		return true, format.Message(actual, "not to receive anything"), nil
	} else {
		return false, format.Message(actual, "to receive something"), nil
	}
}
Exemple #4
0
func (p *panics) FailureMessage(actual interface{}) (message string) {
	if p.actPanicValue == nil {
		return format.Message(actual, "to panic with:", p.err)
	}

	return fmt.Sprintf(
		"Expected\n%s\nto panic with:\n%s\nbut got:\n%s",
		format.Object(actual, 1),
		format.Object(p.err, 1),
		format.Object(p.actPanicValue, 1),
	)
}
func (matcher *HaveLenMatcher) Match(actual interface{}) (success bool, message string, err error) {
	length, ok := lengthOf(actual)
	if ok {
		if length == matcher.Count {
			return true, fmt.Sprintf("Expected\n%s\nnot to have length %d", format.Object(actual, 1), matcher.Count), nil
		} else {
			return false, fmt.Sprintf("Expected\n%s\nto have length %d", format.Object(actual, 1), matcher.Count), nil
		}
	} else {
		return false, "", fmt.Errorf("HaveLen matcher expects a string/array/map/channel/slice.  Got:\n%s", format.Object(actual, 1))
	}
}
Exemple #6
0
func (p *panicsWithSubstring) FailureMessage(actual interface{}) string {
	if p.panics.actPanicValue == nil {
		return format.Message(actual, "to panic contains:", p.substr)
	}

	return fmt.Sprintf(
		"Expected\n%s\nto panic contains:\n%s\nbut got:\n%s",
		format.Object(actual, 1),
		format.Object(p.substr, 1),
		format.Object(p.actPanicValue, 1),
	)
}
Exemple #7
0
func (m *OneOfMatcher) expectedValues() string {
	expectedValues := make([]string, len(m.Elements), len(m.Elements))
	for i, matcher := range m.Elements {
		expectedValues[i] = format.Object(matcher, 1)
	}
	return strings.Join(expectedValues, "\nor\n")
}
Exemple #8
0
func (p *panics) Match(actual interface{}) (success bool, err error) {
	v := reflect.ValueOf(actual)

	if v.Kind() != reflect.Func {
		return false, fmt.Errorf("Panics expects a function.  Got:\n%s", format.Object(actual, 1))
	}

	t := v.Type()
	if !(t.NumIn() == 0 && t.NumOut() == 0) {
		return false, fmt.Errorf("Panics expects a function with no arguments and no return value.  Got:\n%s", format.Object(actual, 1))
	}

	defer func() {
		if e := recover(); e != nil {
			p.actPanicValue = e
			if m, ok := p.err.(types.GomegaMatcher); ok {
				success, err = m.Match(e)
			} else {
				success = reflect.DeepEqual(e, p.err)
			}
		}
	}()

	v.Call([]reflect.Value{})

	return
}
func (matcher *PanicMatcher) Match(actual interface{}) (success bool, message string, err error) {
	if actual == nil {
		return false, "", fmt.Errorf("PanicMatcher expects a non-nil actual.")
	}
	actualType := reflect.TypeOf(actual)
	if actualType.Kind() != reflect.Func {
		return false, "", fmt.Errorf("PanicMatcher expects a function.  Got:\n%s", format.Object(actual, 1))
	}
	if !(actualType.NumIn() == 0 && actualType.NumOut() == 0) {
		return false, "", fmt.Errorf("PanicMatcher expects a function with no arguments and no return value.  Got:\n%s", format.Object(actual, 1))
	}

	success = false
	message = format.Message(actual, "to panic")
	err = nil

	defer func() {
		if e := recover(); e != nil {
			success = true
			message = format.Message(actual, "not to panic")
			err = nil
		}
	}()

	reflect.ValueOf(actual).Call([]reflect.Value{})

	return
}
func (matcher *HaveKeyWithValueMatcher) Match(actual interface{}) (success bool, err error) {
	if !isMap(actual) {
		return false, fmt.Errorf("HaveKeyWithValue matcher expects a map.  Got:%s", format.Object(actual, 1))
	}

	keyMatcher, keyIsMatcher := matcher.Key.(omegaMatcher)
	if !keyIsMatcher {
		keyMatcher = &EqualMatcher{Expected: matcher.Key}
	}

	valueMatcher, valueIsMatcher := matcher.Value.(omegaMatcher)
	if !valueIsMatcher {
		valueMatcher = &EqualMatcher{Expected: matcher.Value}
	}

	keys := reflect.ValueOf(actual).MapKeys()
	for i := 0; i < len(keys); i++ {
		success, err := keyMatcher.Match(keys[i].Interface())
		if err != nil {
			return false, fmt.Errorf("HaveKeyWithValue's key matcher failed with:\n%s%s", format.Indent, err.Error())
		}
		if success {
			actualValue := reflect.ValueOf(actual).MapIndex(keys[i])
			success, err := valueMatcher.Match(actualValue.Interface())
			if err != nil {
				return false, fmt.Errorf("HaveKeyWithValue's value matcher failed with:\n%s%s", format.Indent, err.Error())
			}
			return success, nil
		}
	}

	return false, nil
}
Exemple #11
0
func (matcher HaveSameLenMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	itemLength, ok := lengthOf(matcher.Item)
	if !ok {
		return fmt.Sprintf("HaveSameLen matcher expects a string/array/map/channel/slice.  Got:\n%s", format.Object(actual, 1))
	}
	return fmt.Sprintf("Expected\n%s\nnot to have length %d", format.Object(actual, 1), itemLength)
}
func (matcher *HaveKeyMatcher) Match(actual interface{}) (success bool, message string, err error) {
	if !isMap(actual) {
		return false, "", fmt.Errorf("HaveKey matcher expects a map.  Got:%s", format.Object(actual, 1))
	}

	keyMatcher, keyIsMatcher := matcher.Key.(omegaMatcher)
	matchingString := " matching"
	if !keyIsMatcher {
		keyMatcher = &EqualMatcher{Expected: matcher.Key}
		matchingString = ""
	}

	keys := reflect.ValueOf(actual).MapKeys()
	for i := 0; i < len(keys); i++ {
		success, _, err := keyMatcher.Match(keys[i].Interface())
		if err != nil {
			return false, "", fmt.Errorf("HaveKey's key matcher failed with:\n%s%s", format.Indent, err.Error())
		}
		if success {
			return true, format.Message(actual, "not to have key"+matchingString, matcher.Key), nil
		}
	}

	return false, format.Message(actual, "to have key"+matchingString, matcher.Key), nil
}
func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, err error) {
	if isNil(actual) {
		return false, fmt.Errorf("Expected an error, got nil")
	}

	if !isError(actual) {
		return false, fmt.Errorf("Expected an error.  Got:\n%s", format.Object(actual, 1))
	}

	actualErr := actual.(error)

	if isString(matcher.Expected) {
		return reflect.DeepEqual(actualErr.Error(), matcher.Expected), nil
	}

	if isError(matcher.Expected) {
		return reflect.DeepEqual(actualErr, matcher.Expected), nil
	}

	var subMatcher omegaMatcher
	var hasSubMatcher bool
	if matcher.Expected != nil {
		subMatcher, hasSubMatcher = (matcher.Expected).(omegaMatcher)
		if hasSubMatcher {
			return subMatcher.Match(actualErr.Error())
		}
	}

	return false, fmt.Errorf("MatchError must be passed an error, string, or Matcher that can match on strings.  Got:\n%s", format.Object(matcher.Expected, 1))
}
func (matcher *BeFalseMatcher) Match(actual interface{}) (success bool, err error) {
	if !isBool(actual) {
		return false, fmt.Errorf("Expected a boolean.  Got:\n%s", format.Object(actual, 1))
	}

	return actual == false, nil
}
Exemple #15
0
func (m *HaveCSSMatcher) Match(actual interface{}) (success bool, err error) {
	actualSelection, ok := actual.(interface {
		CSS(property string) (string, error)
	})

	if !ok {
		return false, fmt.Errorf("HaveCSS matcher requires a *Selection.  Got:\n%s", format.Object(actual, 1))
	}

	m.actualValue, err = actualSelection.CSS(m.ExpectedProperty)
	if err != nil {
		return false, err
	}

	expectedColor, err := colorparser.ParseCSSColor(m.ExpectedValue)
	if err != nil {
		return m.actualValue == m.ExpectedValue, nil
	}

	actualColor, err := colorparser.ParseCSSColor(m.actualValue)
	if err != nil {
		return false, errors.New(expectedColorMessage(m.ExpectedValue, expectedColor, m.actualValue))
	}

	m.isColorComparison = true
	m.expectedColor = expectedColor
	m.actualColor = actualColor
	return reflect.DeepEqual(actualColor, expectedColor), nil
}
func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, message string, err error) {
	if isNil(actual) {
		return false, "", fmt.Errorf("Expected an error, got nil")
	}

	if !isError(actual) {
		return false, "", fmt.Errorf("Expected an error.  Got:\n%s", format.Object(actual, 1))
	}

	actualErr := actual.(error)

	if isString(matcher.Expected) {
		if reflect.DeepEqual(actualErr.Error(), matcher.Expected) {
			return true, format.Message(actual, "not to match error", matcher.Expected), nil
		} else {
			return false, format.Message(actual, "to match error", matcher.Expected), nil
		}
	}

	if isError(matcher.Expected) {
		if reflect.DeepEqual(actualErr, matcher.Expected) {
			return true, format.Message(actual, "not to match error", matcher.Expected), nil
		} else {
			return false, format.Message(actual, "to match error", matcher.Expected), nil
		}
	}

	return false, "", fmt.Errorf("MatchError must be passed an error or string.  Got:\n%s", format.Object(matcher.Expected, 1))
}
func (matcher *BeClosedMatcher) Match(actual interface{}) (success bool, err error) {
	if !isChan(actual) {
		return false, fmt.Errorf("BeClosed matcher expects a channel.  Got:\n%s", format.Object(actual, 1))
	}

	channelType := reflect.TypeOf(actual)
	channelValue := reflect.ValueOf(actual)

	if channelType.ChanDir() == reflect.SendDir {
		return false, fmt.Errorf("BeClosed matcher cannot determine if a send-only channel is closed or open.  Got:\n%s", format.Object(actual, 1))
	}

	winnerIndex, _, open := reflect.Select([]reflect.SelectCase{
		{Dir: reflect.SelectRecv, Chan: channelValue},
		{Dir: reflect.SelectDefault},
	})

	var closed bool
	if winnerIndex == 0 {
		closed = !open
	} else if winnerIndex == 1 {
		closed = false
	}

	return closed, nil
}
func (matcher *BeNumericallyMatcher) Match(actual interface{}) (success bool, message string, err error) {
	if len(matcher.CompareTo) == 0 || len(matcher.CompareTo) > 2 {
		return false, "", fmt.Errorf("BeNumerically requires 1 or 2 CompareTo arguments.  Got:\n%s", format.Object(matcher.CompareTo, 1))
	}
	if !isNumber(actual) {
		return false, "", fmt.Errorf("Expected a number.  Got:\n%s", format.Object(actual, 1))
	}
	if !isNumber(matcher.CompareTo[0]) {
		return false, "", fmt.Errorf("Expected a number.  Got:\n%s", format.Object(matcher.CompareTo[0], 1))
	}
	if len(matcher.CompareTo) == 2 && !isNumber(matcher.CompareTo[1]) {
		return false, "", fmt.Errorf("Expected a number.  Got:\n%s", format.Object(matcher.CompareTo[0], 1))
	}
	switch matcher.Comparator {
	case "==", "~", ">", ">=", "<", "<=":
	default:
		return false, "", fmt.Errorf("Unknown comparator: %s", matcher.Comparator)
	}

	if isFloat(actual) || isFloat(matcher.CompareTo[0]) {
		var secondOperand float64 = 1e-8
		if len(matcher.CompareTo) == 2 {
			secondOperand = toFloat(matcher.CompareTo[1])
		}
		success = matcher.matchFloats(toFloat(actual), toFloat(matcher.CompareTo[0]), secondOperand)
	} else if isInteger(actual) {
		var secondOperand int64 = 0
		if len(matcher.CompareTo) == 2 {
			secondOperand = toInteger(matcher.CompareTo[1])
		}
		success = matcher.matchIntegers(toInteger(actual), toInteger(matcher.CompareTo[0]), secondOperand)
	} else if isUnsignedInteger(actual) {
		var secondOperand uint64 = 0
		if len(matcher.CompareTo) == 2 {
			secondOperand = toUnsignedInteger(matcher.CompareTo[1])
		}
		success = matcher.matchUnsignedIntegers(toUnsignedInteger(actual), toUnsignedInteger(matcher.CompareTo[0]), secondOperand)
	} else {
		return false, "", fmt.Errorf("Failed to compare:\n%s\n%s:\n%s", format.Object(actual, 1), matcher.Comparator, format.Object(matcher.CompareTo[0], 1))
	}

	if success {
		return true, format.Message(actual, fmt.Sprintf("not to be %s", matcher.Comparator), matcher.CompareTo[0]), nil
	} else {
		return false, format.Message(actual, fmt.Sprintf("to be %s", matcher.Comparator), matcher.CompareTo[0]), nil
	}
}
Exemple #19
0
func (matcher *moduloIndexMatcher) Match(actual interface{}) (success bool, err error) {
	actualNode, ok := actual.(storeadapter.StoreNode)
	if !ok {
		return false, fmt.Errorf("Expected a store node.  Got:\n%s", format.Object(actual, 1))
	}

	matcher.expected.Index = actualNode.Index
	return reflect.DeepEqual(matcher.expected, actual), nil
}
func (matcher *BeTrueMatcher) Match(actual interface{}) (success bool, message string, err error) {
	if !isBool(actual) {
		return false, "", fmt.Errorf("Expected a boolean.  Got:\n%s", format.Object(actual, 1))
	}
	if actual == true {
		return true, format.Message(actual, "not to be true"), nil
	} else {
		return false, format.Message(actual, "to be true"), nil
	}
}
func (m *registryMessageMatcher) Match(a interface{}) (success bool, err error) {
	actual, ok := a.(routing_table.RegistryMessage)
	if !ok {
		return false, fmt.Errorf("%s is not a routing_table.RegistryMessage", format.Object(actual, 1))
	}

	sort.Sort(sort.StringSlice(m.expected.URIs))
	sort.Sort(sort.StringSlice(actual.URIs))
	return reflect.DeepEqual(actual, m.expected), nil
}
func (m *messagesToEmitMatcher) Match(a interface{}) (success bool, err error) {
	actual, ok := a.(routing_table.MessagesToEmit)
	if !ok {
		return false, fmt.Errorf("%s is not a routing_table.MessagesToEmit", format.Object(actual, 1))
	}

	registrationsMatch := m.matchArrs(actual.RegistrationMessages, m.expected.RegistrationMessages)
	unregistrationsMatch := m.matchArrs(actual.UnregistrationMessages, m.expected.UnregistrationMessages)
	return registrationsMatch && unregistrationsMatch, nil
}
func (matcher *HaveOccurredMatcher) Match(actual interface{}) (success bool, err error) {
	if isNil(actual) {
		return false, nil
	}

	if isError(actual) {
		return true, nil
	}

	return false, fmt.Errorf("Expected an error.  Got:\n%s", format.Object(actual, 1))
}
func (matcher *SucceedMatcher) Match(actual interface{}) (success bool, err error) {
	if actual == nil {
		return true, nil
	}

	if isError(actual) {
		return false, nil
	}

	return false, fmt.Errorf("Expected an error-type.  Got:\n%s", format.Object(actual, 1))
}
Exemple #25
0
func (matcher *MatchRegexpMatcher) Match(actual interface{}) (success bool, err error) {
	actualString, ok := toString(actual)
	if !ok {
		return false, fmt.Errorf("RegExp matcher requires a string or stringer.\nGot:%s", format.Object(actual, 1))
	}

	match, err := regexp.Match(matcher.regexp(), []byte(actualString))
	if err != nil {
		return false, fmt.Errorf("RegExp match failed to compile with error:\n\t%s", err.Error())
	}

	return match, nil
}
Exemple #26
0
func (matcher *SucceedMatcher) Match(actual interface{}) (success bool, err error) {
	// is purely nil?
	if actual == nil {
		return true, nil
	}

	// must be an 'error' type
	if !isError(actual) {
		return false, fmt.Errorf("Expected an error-type.  Got:\n%s", format.Object(actual, 1))
	}

	// must be nil (or a pointer to a nil)
	return isNil(actual), nil
}
Exemple #27
0
func (m *HaveAttributeMatcher) Match(actual interface{}) (success bool, err error) {
	actualSelection, ok := actual.(interface {
		Attribute(attribute string) (string, error)
	})

	if !ok {
		return false, fmt.Errorf("HaveAttribute matcher requires a *Selection.  Got:\n%s", format.Object(actual, 1))
	}

	m.actualValue, err = actualSelection.Attribute(m.ExpectedAttribute)
	if err != nil {
		return false, err
	}

	return m.actualValue == m.ExpectedValue, nil
}
Exemple #28
0
func (m *MatchTextMatcher) Match(actual interface{}) (success bool, err error) {
	actualSelection, ok := actual.(interface {
		Text() (string, error)
	})

	if !ok {
		return false, fmt.Errorf("MatchText matcher requires a *Selection.  Got:\n%s", format.Object(actual, 1))
	}

	m.actualText, err = actualSelection.Text()
	if err != nil {
		return false, err
	}

	return regexp.MatchString(m.Regexp, m.actualText)
}
Exemple #29
0
func (m *EqualElementMatcher) Match(actual interface{}) (success bool, err error) {
	actualSelection, ok := actual.(interface {
		EqualsElement(selection interface{}) (bool, error)
	})

	if !ok {
		return false, fmt.Errorf("EqualElement matcher requires a *Selection.  Got:\n%s", format.Object(actual, 1))
	}

	same, err := actualSelection.EqualsElement(m.ExpectedSelection)
	if err != nil {
		return false, err
	}

	return same, nil
}
Exemple #30
0
func (s save) Match(actual interface{}) (bool, error) {
	argType := reflect.TypeOf(s.p)
	if argType.Kind() != reflect.Ptr {
		return false, fmt.Errorf("Cannot save a value From:\n%s\nTo:\n%s\nYou need to pass a pointer!", format.Object(actual, 1), format.Object(s.p, 1))
	}

	actualType := reflect.TypeOf(actual)
	assignable := actualType.AssignableTo(argType.Elem())
	if !assignable {
		return false, fmt.Errorf("Cannot assign a value from:\n%s\nTo:\n%s", format.Object(actual, 1), format.Object(s.p, 1))
	}

	outValue := reflect.ValueOf(s.p)
	reflect.Indirect(outValue).Set(reflect.ValueOf(actual))
	return true, nil
}