From 44cfa9d77c6536d6334d039113f6ecd0d72b0a23 Mon Sep 17 00:00:00 2001 From: Victor Gaydov Date: Sun, 13 Nov 2022 14:32:38 +0400 Subject: [PATCH] Fix builders and matchers copying --- expect.go | 12 +++++- expect_test.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/expect.go b/expect.go index c43a6c685..c1a2e0165 100644 --- a/expect.go +++ b/expect.go @@ -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 } @@ -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 } diff --git a/expect_test.go b/expect_test.go index 44647fecf..09fd37e78 100644 --- a/expect_test.go +++ b/expect_test.go @@ -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{} @@ -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{}