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) } }
in: `{"and": ["foo", "foo"]}`, want: gomega.And(gomega.Equal("foo"), gomega.Equal("foo")), useNegateTester: true, }, { in: `{"and": [{"have-prefix": "foo"}, "foo"]}`, want: gomega.And(gomega.HavePrefix("foo"), gomega.Equal("foo")), useNegateTester: true, }, { in: `{"not": {"have-prefix": "foo"}}`, want: gomega.Not(gomega.HavePrefix("foo")), }, { in: `{"or": ["foo", "foo"]}`, want: gomega.Or(gomega.Equal("foo"), gomega.Equal("foo")), }, { in: `{"not": {"and": [{"have-prefix": "foo"}]}}`, want: gomega.Not(gomega.And(gomega.HavePrefix("foo"))), }, } func TestMatcherToGomegaMatcher(t *testing.T) { for _, c := range gomegaTests { var dat interface{} if err := json.Unmarshal([]byte(c.in), &dat); err != nil { t.Fatal(err) } got, err := matcherToGomegaMatcher(dat) if err != nil {