Skip to content

Commit 581b8b8

Browse files
committed
mock: drop objx.Map: "Mock.TestData() objx.Map" now returns TestData
Drop the last reference to dependency github.com/stretchr/objx. Reference to objx.Map is removed (BREAKING CHANGE) from the Mock.TestData method signature. A similar object is returned instead of objx.Map, but we do not aim to implement the full objx.Value API: instead we expose the reflect.Value API that should help enough the few users of Mock.TestData to move away from objx.Map reliance. Implemented as planned in #1852.
1 parent 3196432 commit 581b8b8

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ module github.com/stretchr/testify
44
// .github/workflows/main.yml
55
go 1.17
66

7-
require (
8-
github.com/stretchr/objx v0.5.2 // To avoid a cycle the version of testify used by objx should be excluded below
9-
gopkg.in/yaml.v3 v3.0.1
10-
)
7+
require gopkg.in/yaml.v3 v3.0.1
118

129
// Break dependency cycle with objx.
1310
// See https://github.com/stretchr/objx/pull/140

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3-
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
4-
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
51
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
62
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
73
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

mock/mock_objx.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,64 @@
66

77
package mock
88

9-
import "github.com/stretchr/objx"
9+
import (
10+
"reflect"
11+
)
1012

11-
type testData = objx.Map
13+
type testData = TestData
1214

1315
// TestData holds any data that might be useful for testing. Testify ignores
1416
// this data completely allowing you to do whatever you like with it.
15-
func (m *Mock) TestData() objx.Map {
17+
//
18+
// Deprecated: do not use. The new TestData do not return an [github.com/stretchr/objx.Map] anymore.
19+
// See https://github.com/stretchr/testify/issues/1852.
20+
func (m *Mock) TestData() TestData {
1621
if m.testData == nil {
17-
m.testData = make(objx.Map)
22+
m.testData = make(TestData)
1823
}
1924

2025
return m.testData
2126
}
27+
28+
// TestData replaces [github.com/stretchr/objx.Map].
29+
type TestData map[string]interface{}
30+
31+
type reflectValue = reflect.Value
32+
33+
// TestDataValue replaces [github.com/stretchr/objx.Value] and exposes the same methods as [reflect.Value].
34+
// Only a subset of objx.Value methods are available.
35+
type TestDataValue struct {
36+
reflectValue
37+
}
38+
39+
// Set replaces [github.com/stretchr/objx.Map.Set].
40+
func (td TestData) Set(selector string, v interface{}) {
41+
td[selector] = v
42+
}
43+
44+
// Get replaces [github.com/stretchr/objx.Map.Get].
45+
func (td TestData) Get(selector string) *TestDataValue {
46+
v, ok := td[selector]
47+
if !ok {
48+
return nil
49+
}
50+
return &TestDataValue{reflectValue: reflect.ValueOf(&v).Elem()}
51+
}
52+
53+
54+
// MustInter replaces [github.com/stretchr/objx.Value.MustInter].
55+
func (v *TestDataValue) MustInter() interface{} {
56+
if v == nil {
57+
return nil
58+
}
59+
// v.reflectValue contains an interface (ex: error), so dereference it
60+
return v.reflectValue.Elem()
61+
}
62+
63+
// MustInter replaces [github.com/stretchr/objx.Value.Data].
64+
func (v *TestDataValue) Data() interface{} {
65+
if v == nil {
66+
return nil
67+
}
68+
return v.reflectValue.Interface()
69+
}

0 commit comments

Comments
 (0)