Skip to content

Commit

Permalink
Fix builders and matchers copying
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Nov 13, 2022
1 parent 2913070 commit 44cfa9d
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
12 changes: 10 additions & 2 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ func NewJar() http.CookieJar {
// Status(http.StatusOK)
func (e *Expect) Builder(builder func(*Request)) *Expect {
ret := *e
ret.builders = append(e.builders, builder)

ret.builders = nil
ret.builders = append(ret.builders, e.builders...)
ret.builders = append(ret.builders, builder)

return &ret
}

Expand All @@ -384,7 +388,11 @@ func (e *Expect) Builder(builder func(*Request)) *Expect {
// Status(http.StatusNotFound)
func (e *Expect) Matcher(matcher func(*Response)) *Expect {
ret := *e
ret.matchers = append(e.matchers, matcher)

ret.matchers = nil
ret.matchers = append(ret.matchers, e.matchers...)
ret.matchers = append(ret.matchers, matcher)

return &ret
}

Expand Down
112 changes: 112 additions & 0 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,62 @@ func TestExpectBuilders(t *testing.T) {
assert.Equal(t, r1, reqs2[0])
}

func TestExpectBuildersCopying(t *testing.T) {
client := &mockClient{}

reporter := NewAssertReporter(t)

config := Config{
Client: client,
Reporter: reporter,
}

counter1 := 0
counter2a := 0
counter2b := 0

e0 := WithConfig(config)

// Simulate the case when many builders are added, and the builders slice
// have some additioonal capacity. We are going to check that the slice
// is cloned properly when a new builder is appended.
for i := 0; i < 10; i++ {
e0 = e0.Builder(func(r *Request) {})
}

e1 := e0.Builder(func(r *Request) {
counter1++
})

e2a := e1.Builder(func(r *Request) {
counter2a++
})

e2b := e1.Builder(func(r *Request) {
counter2b++
})

e0.Request("METHOD", "/url")
assert.Equal(t, 0, counter1)
assert.Equal(t, 0, counter2a)
assert.Equal(t, 0, counter2b)

e1.Request("METHOD", "/url")
assert.Equal(t, 1, counter1)
assert.Equal(t, 0, counter2a)
assert.Equal(t, 0, counter2b)

e2a.Request("METHOD", "/url")
assert.Equal(t, 2, counter1)
assert.Equal(t, 1, counter2a)
assert.Equal(t, 0, counter2b)

e2b.Request("METHOD", "/url")
assert.Equal(t, 3, counter1)
assert.Equal(t, 1, counter2a)
assert.Equal(t, 1, counter2b)
}

func TestExpectMatchers(t *testing.T) {
client := &mockClient{}

Expand Down Expand Up @@ -124,6 +180,62 @@ func TestExpectMatchers(t *testing.T) {
assert.Equal(t, resp2, resps2[0])
}

func TestExpectMatchersCopying(t *testing.T) {
client := &mockClient{}

reporter := NewAssertReporter(t)

config := Config{
Client: client,
Reporter: reporter,
}

counter1 := 0
counter2a := 0
counter2b := 0

e0 := WithConfig(config)

// Simulate the case when many builders are added, and the builders slice
// have some additioonal capacity. We are going to check that the slice
// is cloned properly when a new builder is appended.
for i := 0; i < 10; i++ {
e0 = e0.Matcher(func(r *Response) {})
}

e1 := e0.Matcher(func(r *Response) {
counter1++
})

e2a := e1.Matcher(func(r *Response) {
counter2a++
})

e2b := e1.Matcher(func(r *Response) {
counter2b++
})

e0.Request("METHOD", "/url").Expect()
assert.Equal(t, 0, counter1)
assert.Equal(t, 0, counter2a)
assert.Equal(t, 0, counter2b)

e1.Request("METHOD", "/url").Expect()
assert.Equal(t, 1, counter1)
assert.Equal(t, 0, counter2a)
assert.Equal(t, 0, counter2b)

e2a.Request("METHOD", "/url").Expect()
assert.Equal(t, 2, counter1)
assert.Equal(t, 1, counter2a)
assert.Equal(t, 0, counter2b)

e2b.Request("METHOD", "/url").Expect()
assert.Equal(t, 3, counter1)
assert.Equal(t, 1, counter2a)
assert.Equal(t, 1, counter2b)
}

func TestExpectValues(t *testing.T) {
client := &mockClient{}

Expand Down

0 comments on commit 44cfa9d

Please sign in to comment.