Esempio n. 1
0
func matcherToGomegaMatcher(matcher interface{}) (types.GomegaMatcher, error) {
	switch x := matcher.(type) {
	case string, int, bool, float64:
		return gomega.Equal(x), nil
	case []interface{}:
		var matchers []types.GomegaMatcher
		for _, valueI := range x {
			if subMatcher, ok := valueI.(types.GomegaMatcher); ok {
				matchers = append(matchers, subMatcher)
			} else {
				matchers = append(matchers, gomega.ContainElement(valueI))
			}
		}
		return gomega.And(matchers...), nil
	}
	matcher = sanitizeExpectedValue(matcher)
	if matcher == nil {
		return nil, fmt.Errorf("Missing Required Attribute")
	}
	matcherMap, ok := matcher.(map[string]interface{})
	if !ok {
		panic(fmt.Sprintf("Unexpected matcher type: %T\n\n", matcher))
	}
	var matchType string
	var value interface{}
	for matchType, value = range matcherMap {
		break
	}
	switch matchType {
	case "have-prefix":
		return gomega.HavePrefix(value.(string)), nil
	case "have-suffix":
		return gomega.HaveSuffix(value.(string)), nil
	case "match-regexp":
		return gomega.MatchRegexp(value.(string)), nil
	case "have-len":
		value = sanitizeExpectedValue(value)
		return gomega.HaveLen(value.(int)), nil
	case "contain-element":
		subMatcher, err := matcherToGomegaMatcher(value)
		if err != nil {
			return nil, err
		}
		return gomega.ContainElement(subMatcher), nil
	case "not":
		subMatcher, err := matcherToGomegaMatcher(value)
		if err != nil {
			return nil, err
		}
		return gomega.Not(subMatcher), nil
	case "consist-of":
		subMatchers, err := sliceToGomega(value)
		if err != nil {
			return nil, err
		}
		var interfaceSlice []interface{}
		for _, d := range subMatchers {
			interfaceSlice = append(interfaceSlice, d)
		}
		return gomega.ConsistOf(interfaceSlice...), nil
	case "and":
		subMatchers, err := sliceToGomega(value)
		if err != nil {
			return nil, err
		}
		return gomega.And(subMatchers...), nil
	case "or":
		subMatchers, err := sliceToGomega(value)
		if err != nil {
			return nil, err
		}
		return gomega.Or(subMatchers...), nil
	case "gt", "ge", "lt", "le":
		// Golang json escapes '>', '<' symbols, so we use 'gt', 'le' instead
		comparator := map[string]string{
			"gt": ">",
			"ge": ">=",
			"lt": "<",
			"le": "<=",
		}[matchType]
		return gomega.BeNumerically(comparator, value), nil

	default:
		return nil, fmt.Errorf("Unknown matcher: %s", matchType)

	}
}
Esempio n. 2
0
		in:   `{"lt": 1}`,
		want: gomega.BeNumerically("<", float64(1)),
	},
	{
		in:   `{"le": 1}`,
		want: gomega.BeNumerically("<=", float64(1)),
	},

	// String
	{
		in:   `{"have-prefix": "foo"}`,
		want: gomega.HavePrefix("foo"),
	},
	{
		in:   `{"have-suffix": "foo"}`,
		want: gomega.HaveSuffix("foo"),
	},
	// Regex support is based on golangs regex engine https://golang.org/pkg/regexp/syntax/
	{
		in:   `{"match-regexp": "foo"}`,
		want: gomega.MatchRegexp("foo"),
	},

	// Collection
	{
		in:   `{"consist-of": ["foo"]}`,
		want: gomega.ConsistOf(gomega.Equal("foo")),
	},
	{
		in:   `{"contain-element": "foo"}`,
		want: gomega.ContainElement(gomega.Equal("foo")),