Skip to content

Commit d6431e8

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 106d711 commit d6431e8

3 files changed

Lines changed: 52 additions & 10 deletions

File tree

go.mod

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

7-
require (
8-
github.com/stretchr/objx v0.5.3
9-
gopkg.in/yaml.v3 v3.0.1
10-
)
7+
require gopkg.in/yaml.v3 v3.0.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
2-
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
31
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
42
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
53
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

mock/mock_objx.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,63 @@
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+
// MustInter replaces [github.com/stretchr/objx.Value.MustInter].
54+
func (v *TestDataValue) MustInter() interface{} {
55+
if v == nil {
56+
return nil
57+
}
58+
// v.reflectValue contains an interface (ex: error), so dereference it
59+
return v.reflectValue.Elem()
60+
}
61+
62+
// MustInter replaces [github.com/stretchr/objx.Value.Data].
63+
func (v *TestDataValue) Data() interface{} {
64+
if v == nil {
65+
return nil
66+
}
67+
return v.reflectValue.Interface()
68+
}

0 commit comments

Comments
 (0)