Skip to content

Commit

Permalink
refactor: matcher on mock sent simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorsalgado committed Jul 16, 2023
1 parent d0f7191 commit 981fb8f
Show file tree
Hide file tree
Showing 22 changed files with 16 additions and 119 deletions.
4 changes: 2 additions & 2 deletions dzgrpc/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ type GRPCMock struct {

Reply any

after []matcher.OnAfterMockServed
after []matcher.OnMockSent
unaryExpectations []*dzstd.Expectation[*UnaryValueSelectorIn]
streamExpectations []*dzstd.Expectation[*StreamValueSelectorIn]
}

func newMock() *GRPCMock {
return &GRPCMock{BaseMock: dzstd.NewMock(), after: make([]matcher.OnAfterMockServed, 0)}
return &GRPCMock{BaseMock: dzstd.NewMock(), after: make([]matcher.OnMockSent, 0)}
}

func (m *GRPCMock) getRef() string {
Expand Down
2 changes: 1 addition & 1 deletion dzhttp/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

for _, idx := range mock.after {
err = mock.expectations[idx].Matcher.(matcher.OnAfterMockServed).AfterMockServed()
err = mock.expectations[idx].Matcher.(matcher.OnMockSent).OnMockSent()
if err != nil {
h.lifecycle.OnWarning(reqValues,
fmt.Errorf("http: after mock served event: matcher[%d] %w", idx, err))
Expand Down
2 changes: 1 addition & 1 deletion dzhttp/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (m *HTTPMock) Build() (*HTTPMock, error) {

func (m *HTTPMock) Prepare() {
for i, e := range m.expectations {
_, ok := e.Matcher.(matcher.OnAfterMockServed)
_, ok := e.Matcher.(matcher.OnMockSent)
if ok {
m.after = append(m.after, i)
}
Expand Down
8 changes: 0 additions & 8 deletions matcher/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,10 @@ func (m *allOfMatcher) Match(v any) (r Result, err error) {
Message: strings.Join([]string{"All(", strconv.Itoa(len(m.matchers)), ")\n", mfmt.Indent(strings.Join(mismatches, "\n"))}, "")}, nil
}

func (m *allOfMatcher) AfterMockServed() error {
return runAfterMockServed(m.matchers...)
}

// All matches when all the given matchers pass.
// Example:
//
// All(Equal("test"), EqualIgnoreCase("test"), Contain("tes"))
func All(matchers ...Matcher) Matcher {
if len(matchers) == 0 {
panic("all: requires at least 1 matcher")
}

return &allOfMatcher{matchers: matchers}
}
6 changes: 0 additions & 6 deletions matcher/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,4 @@ func TestAllOf(t *testing.T) {
require.Error(t, err)
require.False(t, res.Pass)
})

t.Run("no matchers", func(t *testing.T) {
require.Panics(t, func() {
All()
})
})
}
8 changes: 0 additions & 8 deletions matcher/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,10 @@ func (m *anyOfMatcher) Match(v any) (r Result, e error) {
return Result{Message: strings.Join([]string{"Any(", strconv.Itoa(len(m.matchers)), ")\n", mfmt.Indent(strings.Join(mismatches, "\n"))}, "")}, nil
}

func (m *anyOfMatcher) AfterMockServed() error {
return runAfterMockServed(m.matchers...)
}

// Any matches when any of the given matchers pass.
// Example:
//
// Any(Equal("test"), EqualIgnoreCase("TEST"), Contain("tes"))
func Any(matchers ...Matcher) Matcher {
if len(matchers) == 0 {
panic("any: requires at least 1 matcher")
}

return &anyOfMatcher{matchers: matchers}
}
6 changes: 0 additions & 6 deletions matcher/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,4 @@ func TestAnyOf(t *testing.T) {
require.Error(t, err)
require.False(t, res.Pass)
})

t.Run("no matchers", func(t *testing.T) {
require.Panics(t, func() {
Any()
})
})
}
43 changes: 6 additions & 37 deletions matcher/base.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,20 @@
package matcher

import (
"errors"
"strings"
)

// Matcher matches values.
type Matcher interface {
Match(v any) (Result, error)
}

// OnAfterMockServed describes a Matcher that has post processes that need to be executed.
// AfterMockServed() function will be called after the mock HTTP response.
// Useful for stateful Matchers.
type OnAfterMockServed interface {
AfterMockServed() error
}

// Result represents a Matcher expected.
type Result struct {
// Pass defines if Matcher passed or not.
Pass bool

// Message describes why the associated Matcher did not pass.
Message string
Pass bool // Pass defines if Matcher passed or not.
Message string // Message describes why the associated Matcher did not pass.
}

func runAfterMockServed(matchers ...Matcher) error {
var errs []string

for _, matcher := range matchers {
m, ok := matcher.(OnAfterMockServed)
if !ok {
continue
}

err := m.AfterMockServed()
if err != nil {
errs = append(errs, err.Error())
}
}

if len(errs) > 0 {
return errors.New(strings.Join(errs, ", "))
}

return nil
// OnMockSent describes a Matcher that has post processes that need to be executed after the matched mock is served.
// Dedicated for stateful matchers.
type OnMockSent interface {
OnMockSent() error
}

func success() Result { return Result{Pass: true, Message: ""} }
Expand Down
4 changes: 0 additions & 4 deletions matcher/both.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ func (m *bothMatcher) Match(value any) (Result, error) {
return Result{Message: strings.Join([]string{"Both() ", message}, "")}, nil
}

func (m *bothMatcher) AfterMockServed() error {
return runAfterMockServed(m.first, m.second)
}

// Both passes when both the given matchers pass.
func Both(first Matcher, second Matcher) Matcher {
return &bothMatcher{first: first, second: second}
Expand Down
4 changes: 0 additions & 4 deletions matcher/each.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ func (m *eachMatcher) Match(v any) (Result, error) {
return Result{}, fmt.Errorf("type %s is not supported. accepted types: map, array", valType.String())
}

func (m *eachMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// Each applies the given matcher on all items of the incoming request value.
// It works with slices and maps.
func Each(matcher Matcher) Matcher {
Expand Down
4 changes: 0 additions & 4 deletions matcher/either.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ func (m *eitherMatcher) Match(v any) (Result, error) {
return Result{Message: strings.Join([]string{"Either() ", message}, "")}, nil
}

func (m *eitherMatcher) AfterMockServed() error {
return runAfterMockServed(m.first, m.second)
}

// Either passes when any of the two given matchers pass.
func Either(first Matcher, second Matcher) Matcher {
return &eitherMatcher{first: first, second: second}
Expand Down
4 changes: 0 additions & 4 deletions matcher/jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func (m *jsonPathMatcher) Match(v any) (Result, error) {
}, nil
}

func (m *jsonPathMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// JSONPath applies the provided matcher to the JSON field value in the given path.
// Example:
//
Expand Down
4 changes: 0 additions & 4 deletions matcher/lowercase.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ func (m *lowerCaseMatcher) Match(v any) (Result, error) {
}, nil
}

func (m *lowerCaseMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// ToLower lowers the incoming request value case value before submitting it to provided matcher.
func ToLower(matcher Matcher) Matcher {
return &lowerCaseMatcher{matcher: matcher}
Expand Down
2 changes: 1 addition & 1 deletion matcher/mfeat/repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (m *repeatMatcher) Match(_ any) (matcher.Result, error) {
}, nil
}

func (m *repeatMatcher) AfterMockServed() error {
func (m *repeatMatcher) OnMockSent() error {
atomic.AddInt64(&m.hits, 1)

return nil
Expand Down
8 changes: 4 additions & 4 deletions matcher/mfeat/repeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ func TestRepeat(t *testing.T) {
rep := Repeat(2).(*repeatMatcher)

res, err := rep.Match(nil)
assert.NoError(t, rep.AfterMockServed())
assert.NoError(t, rep.OnMockSent())

assert.NoError(t, err)
assert.True(t, res.Pass)

res, err = rep.Match(nil)
assert.NoError(t, rep.AfterMockServed())
assert.NoError(t, rep.OnMockSent())

assert.NoError(t, err)
assert.True(t, res.Pass)

res, err = rep.Match(nil)
assert.NoError(t, rep.AfterMockServed())
assert.NoError(t, rep.OnMockSent())

assert.NoError(t, err)
assert.False(t, res.Pass)

res, err = rep.Match(nil)
assert.NoError(t, rep.AfterMockServed())
assert.NoError(t, rep.OnMockSent())

assert.NoError(t, err)
assert.False(t, res.Pass)
Expand Down
2 changes: 1 addition & 1 deletion matcher/mfeat/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (m *scenarioMatcher) Match(_ any) (matcher.Result, error) {
}, nil
}

func (m *scenarioMatcher) AfterMockServed() error {
func (m *scenarioMatcher) OnMockSent() error {
scn, ok := m.store.fetchByName(m.name)
if !ok {
return nil
Expand Down
4 changes: 0 additions & 4 deletions matcher/not.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ func (m *notMatcher) Match(v any) (Result, error) {
}, nil
}

func (m *notMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// Not negates the provided matcher.
func Not(matcher Matcher) Matcher {
return &notMatcher{matcher: matcher}
Expand Down
4 changes: 0 additions & 4 deletions matcher/peek.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ func (m *peekMatcher) Match(v any) (Result, error) {
return m.matcher.Match(v)
}

func (m *peekMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// Peek will return the expected of the given matcher, after executing the provided function.
// Peek can be used to check the matcher argument.
func Peek(matcher Matcher, action func(v any) error) Matcher {
Expand Down
4 changes: 0 additions & 4 deletions matcher/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func (m *splitMatcher) Match(v any) (Result, error) {
return Result{Message: strings.Join([]string{"Split(", m.separator, ") ", result.Message}, "")}, nil
}

func (m *splitMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// Split splits the incoming request value text using the separator parameter and
// test each element with the provided matcher.
func Split(separator string, matcher Matcher) Matcher {
Expand Down
4 changes: 0 additions & 4 deletions matcher/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func (m *trimMatcher) Match(v any) (Result, error) {
return Result{Message: strings.Join([]string{"Trim(", txt, ") ", result.Message}, "")}, nil
}

func (m *trimMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// Trim trims' spaces of the incoming request value before submitting it to the provided matcher.
func Trim(matcher Matcher) Matcher {
return &trimMatcher{matcher: matcher}
Expand Down
4 changes: 0 additions & 4 deletions matcher/uppercase.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func (m *upperCaseMatcher) Match(v any) (Result, error) {
return Result{Message: strings.Join([]string{"Upper(", txt, ") ", result.Message}, "")}, nil
}

func (m *upperCaseMatcher) AfterMockServed() error {
return runAfterMockServed(m.matcher)
}

// ToUpper uppers the case of the incoming request value before submitting it to provided matcher.
func ToUpper(matcher Matcher) Matcher {
return &upperCaseMatcher{matcher: matcher}
Expand Down
4 changes: 0 additions & 4 deletions matcher/xor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ func (m *xorMatcher) Match(v any) (Result, error) {
return Result{Message: strings.Join([]string{"XOR()", message}, " ")}, nil
}

func (m *xorMatcher) AfterMockServed() error {
return runAfterMockServed(m.first, m.second)
}

// XOR is an exclusive OR matcher.
func XOR(first Matcher, second Matcher) Matcher {
return &xorMatcher{first: first, second: second}
Expand Down

0 comments on commit 981fb8f

Please sign in to comment.