Skip to content

Commit cae7bb8

Browse files
danny-cheungh2non
authored andcommitted
NewMatcher() will now return objects that completely separate one another. (#55)
* NewMatcher() will now return objects that completely separate one another. * Remove package name when referencing vars in this package. * Formatting changes. * Update tests since funcs are not comparable. * Add the Clone() method for a MockMatcher * Add unit tests for MockMatcher.Clone() * Check for length of []MatchFunc since funcs are not actually comparable. * All instances of Mock had a matcher field that used the same underlying array. * Add comment for exported method.
1 parent 30a20be commit cae7bb8

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

matcher.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,23 @@ type MockMatcher struct {
5454
// NewMatcher creates a new mock matcher
5555
// using the default matcher functions.
5656
func NewMatcher() *MockMatcher {
57-
return &MockMatcher{Matchers: Matchers}
57+
m := NewEmptyMatcher()
58+
for _, matchFn := range Matchers {
59+
m.Add(matchFn)
60+
}
61+
return m
5862
}
5963

6064
// NewBasicMatcher creates a new matcher with header only mock matchers.
6165
func NewBasicMatcher() *MockMatcher {
62-
return &MockMatcher{Matchers: MatchersHeader}
66+
m := NewEmptyMatcher()
67+
for _, matchFn := range MatchersHeader {
68+
m.Add(matchFn)
69+
}
70+
return m
6371
}
6472

65-
// NewEmptyMatcher creates a new empty matcher with out default amtchers.
73+
// NewEmptyMatcher creates a new empty matcher without default matchers.
6674
func NewEmptyMatcher() *MockMatcher {
6775
return &MockMatcher{Matchers: []MatchFunc{}}
6876
}
@@ -87,6 +95,15 @@ func (m *MockMatcher) Flush() {
8795
m.Matchers = []MatchFunc{}
8896
}
8997

98+
// Clone returns a separate MockMatcher instance that has a copy of the same MatcherFuncs
99+
func (m *MockMatcher) Clone() *MockMatcher {
100+
m2 := NewEmptyMatcher()
101+
for _, mFn := range m.Get() {
102+
m2.Add(mFn)
103+
}
104+
return m2
105+
}
106+
90107
// Match matches the given http.Request with a mock request
91108
// returning true in case that the request matches, otherwise false.
92109
func (m *MockMatcher) Match(req *http.Request, ereq *Request) (bool, error) {

matcher_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ func TestRegisteredMatchers(t *testing.T) {
1515

1616
func TestNewMatcher(t *testing.T) {
1717
matcher := NewMatcher()
18-
st.Expect(t, matcher.Matchers, Matchers)
19-
st.Expect(t, matcher.Get(), Matchers)
18+
// Funcs are not comparable, checking slice length as it's better than nothing
19+
// See https://golang.org/pkg/reflect/#DeepEqual
20+
st.Expect(t, len(matcher.Matchers), len(Matchers))
21+
st.Expect(t, len(matcher.Get()), len(Matchers))
2022
}
2123

2224
func TestNewBasicMatcher(t *testing.T) {
2325
matcher := NewBasicMatcher()
24-
st.Expect(t, matcher.Matchers, MatchersHeader)
25-
st.Expect(t, matcher.Get(), MatchersHeader)
26+
// Funcs are not comparable, checking slice length as it's better than nothing
27+
// See https://golang.org/pkg/reflect/#DeepEqual
28+
st.Expect(t, len(matcher.Matchers), len(MatchersHeader))
29+
st.Expect(t, len(matcher.Get()), len(MatchersHeader))
2630
}
2731

2832
func TestNewEmptyMatcher(t *testing.T) {
@@ -67,6 +71,11 @@ func TestMatcherFlush(t *testing.T) {
6771
st.Expect(t, len(matcher.Get()), 0)
6872
}
6973

74+
func TestMatcherClone(t *testing.T) {
75+
matcher := DefaultMatcher.Clone()
76+
st.Expect(t, len(matcher.Get()), len(DefaultMatcher.Get()))
77+
}
78+
7079
func TestMatcher(t *testing.T) {
7180
cases := []struct {
7281
method string

mock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func NewMock(req *Request, res *Response) *Mocker {
5555
mock := &Mocker{
5656
request: req,
5757
response: res,
58-
matcher: DefaultMatcher,
58+
matcher: DefaultMatcher.Clone(),
5959
}
6060
res.Mock = mock
6161
req.Mock = mock

mock_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestNewMock(t *testing.T) {
1414
res := NewResponse()
1515
mock := NewMock(req, res)
1616
st.Expect(t, mock.disabled, false)
17-
st.Expect(t, mock.matcher, DefaultMatcher)
17+
st.Expect(t, len(mock.matcher.Get()), len(DefaultMatcher.Get()))
1818

1919
st.Expect(t, mock.Request(), req)
2020
st.Expect(t, mock.Request().Mock, mock)
@@ -71,7 +71,7 @@ func TestMockSetMatcher(t *testing.T) {
7171
res := NewResponse()
7272
mock := NewMock(req, res)
7373

74-
st.Expect(t, mock.matcher, DefaultMatcher)
74+
st.Expect(t, len(mock.matcher.Get()), len(DefaultMatcher.Get()))
7575
matcher := NewMatcher()
7676
matcher.Flush()
7777
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
@@ -93,7 +93,7 @@ func TestMockAddMatcher(t *testing.T) {
9393
res := NewResponse()
9494
mock := NewMock(req, res)
9595

96-
st.Expect(t, mock.matcher, DefaultMatcher)
96+
st.Expect(t, len(mock.matcher.Get()), len(DefaultMatcher.Get()))
9797
matcher := NewMatcher()
9898
matcher.Flush()
9999
mock.SetMatcher(matcher)

0 commit comments

Comments
 (0)