Skip to content

Commit a6849ca

Browse files
committed
mock: isolate objx-dependent code in their own source files
In order to ease maintenance of downstream forks of Testify that would remove dependency on github.com/stretchr/objx we move all uses of that module in isolated source files. A fork without that dependency could just remove those files. See #1752 (comment) The use of objx is quite contained: it is only used in Mock.TestData(). Note that we can't just remove that method or change the return value because that would be a breaking change.
1 parent 65697ce commit a6849ca

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

mock/mock.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"sync"
1212
"time"
1313

14-
"github.com/stretchr/objx"
15-
1614
"github.com/stretchr/testify/assert"
1715
"github.com/stretchr/testify/internal/difflib"
1816
"github.com/stretchr/testify/internal/spew"
@@ -311,7 +309,7 @@ type Mock struct {
311309

312310
// TestData holds any data that might be useful for testing. Testify ignores
313311
// this data completely allowing you to do whatever you like with it.
314-
testData objx.Map
312+
testData testData
315313

316314
mutex sync.Mutex
317315
}
@@ -324,16 +322,6 @@ func (m *Mock) String() string {
324322
return fmt.Sprintf("%[1]T<%[1]p>", m)
325323
}
326324

327-
// TestData holds any data that might be useful for testing. Testify ignores
328-
// this data completely allowing you to do whatever you like with it.
329-
func (m *Mock) TestData() objx.Map {
330-
if m.testData == nil {
331-
m.testData = make(objx.Map)
332-
}
333-
334-
return m.testData
335-
}
336-
337325
/*
338326
Setting expectations
339327
*/

mock/mock_objx.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This source file isolates the uses of the objx module to ease
2+
// maintenance of downstream forks that remove that dependency.
3+
// See https://github.com/stretchr/testify/issues/1752
4+
5+
package mock
6+
7+
import "github.com/stretchr/objx"
8+
9+
type testData = objx.Map
10+
11+
// TestData holds any data that might be useful for testing. Testify ignores
12+
// this data completely allowing you to do whatever you like with it.
13+
func (m *Mock) TestData() objx.Map {
14+
if m.testData == nil {
15+
m.testData = make(objx.Map)
16+
}
17+
18+
return m.testData
19+
}

mock/mock_objx_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package mock
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_Mock_TestData(t *testing.T) {
10+
t.Parallel()
11+
12+
var mockedService = new(TestExampleImplementation)
13+
14+
if assert.NotNil(t, mockedService.TestData()) {
15+
16+
mockedService.TestData().Set("something", 123)
17+
assert.Equal(t, 123, mockedService.TestData().Get("something").Data())
18+
}
19+
}

mock/mock_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,6 @@ func (m *MockTestingT) FailNow() {
164164
Mock
165165
*/
166166

167-
func Test_Mock_TestData(t *testing.T) {
168-
t.Parallel()
169-
170-
var mockedService = new(TestExampleImplementation)
171-
172-
if assert.NotNil(t, mockedService.TestData()) {
173-
174-
mockedService.TestData().Set("something", 123)
175-
assert.Equal(t, 123, mockedService.TestData().Get("something").Data())
176-
}
177-
}
178-
179167
func Test_Mock_On(t *testing.T) {
180168
t.Parallel()
181169

0 commit comments

Comments
 (0)