func TestQueue_ReceiveBeforeSend_ReturnsExpectedResult(t *testing.T) { gomega.RegisterTestingT(t) underTest := NewQueue("TestQueue") var receivedMessage *message.Message go func() { receivedMessage = <-underTest.OutputChannel }() time.Sleep(time.Millisecond * 10) testMessagePayload := []byte("TestMessage") underTest.InputChannel <- (message.NewHeaderlessMessage(&testMessagePayload)) gomega.Eventually(func() *message.Message { return receivedMessage }).Should(gomega.Not(gomega.BeNil())) if !bytes.Equal(*receivedMessage.Body, testMessagePayload) { t.Fail() } }
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: `{"consist-of": ["foo"]}`, want: gomega.ConsistOf(gomega.Equal("foo")), }, { in: `{"contain-element": "foo"}`, want: gomega.ContainElement(gomega.Equal("foo")), }, { in: `{"have-len": 3}`, want: gomega.HaveLen(3), }, // Negation { in: `{"not": "foo"}`, want: gomega.Not(gomega.Equal("foo")), }, // Complex logic { 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")),