Skip to content

Commit

Permalink
chore: replace interface{} with any
Browse files Browse the repository at this point in the history
- See discussion in: onsi/ginkgo#1203
- Updating Gomega for consistency with Ginkgo
  • Loading branch information
blgm committed Jan 2, 2025
1 parent ced70d7 commit fd3fd96
Show file tree
Hide file tree
Showing 106 changed files with 712 additions and 704 deletions.
118 changes: 59 additions & 59 deletions docs/index.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ If the CustomFormatter does not want to handle the object it should return ("",
Strings returned by CustomFormatters are not truncated
*/
type CustomFormatter func(value interface{}) (string, bool)
type CustomFormatter func(value any) (string, bool)
type CustomFormatterKey uint

var customFormatterKey CustomFormatterKey = 1
Expand Down Expand Up @@ -125,7 +125,7 @@ If expected is omitted, then the message looks like:
<pretty printed actual>
<message>
*/
func Message(actual interface{}, message string, expected ...interface{}) string {
func Message(actual any, message string, expected ...any) string {
if len(expected) == 0 {
return fmt.Sprintf("Expected\n%s\n%s", Object(actual, 1), message)
}
Expand Down Expand Up @@ -255,7 +255,7 @@ recursing into the object.
Set PrintContextObjects to true to print the content of objects implementing context.Context
*/
func Object(object interface{}, indentation uint) string {
func Object(object any, indentation uint) string {
indent := strings.Repeat(Indent, int(indentation))
value := reflect.ValueOf(object)
commonRepresentation := ""
Expand Down Expand Up @@ -392,7 +392,7 @@ func formatValue(value reflect.Value, indentation uint) string {
}
}

func formatString(object interface{}, indentation uint) string {
func formatString(object any, indentation uint) string {
if indentation == 1 {
s := fmt.Sprintf("%s", object)
components := strings.Split(s, "\n")
Expand Down
28 changes: 14 additions & 14 deletions format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type SecretiveStruct struct {
byteArrValue [3]byte
mapValue map[string]int
structValue AStruct
interfaceValue interface{}
interfaceValue any
}

type CustomFormatted struct {
Expand All @@ -84,7 +84,7 @@ func (c *CustomError) Error() string {
return c.Details
}

func customFormatter(obj interface{}) (string, bool) {
func customFormatter(obj any) (string, bool) {
cf, ok := obj.(CustomFormatted)
if !ok {
return "", false
Expand Down Expand Up @@ -132,14 +132,14 @@ func (g gomegaStringerMultiline) GomegaString() string {
}

var _ = Describe("Format", func() {
match := func(typeRepresentation string, valueRepresentation string, args ...interface{}) types.GomegaMatcher {
match := func(typeRepresentation string, valueRepresentation string, args ...any) types.GomegaMatcher {
if len(args) > 0 {
valueRepresentation = fmt.Sprintf(valueRepresentation, args...)
}
return Equal(fmt.Sprintf("%s<%s>: %s", Indent, typeRepresentation, valueRepresentation))
}

matchRegexp := func(typeRepresentation string, valueRepresentation string, args ...interface{}) types.GomegaMatcher {
matchRegexp := func(typeRepresentation string, valueRepresentation string, args ...any) types.GomegaMatcher {
if len(args) > 0 {
valueRepresentation = fmt.Sprintf(valueRepresentation, args...)
}
Expand Down Expand Up @@ -599,18 +599,18 @@ var _ = Describe("Format", func() {
})
})

Describe("formatting nested interface{} types", func() {
Describe("formatting nested any types", func() {
It("should print out the types of the container and value", func() {
Expect(Object([]interface{}{"foo"}, 1)).
Expect(Object([]any{"foo"}, 1)).
To(match("[]interface {} | len:1, cap:1", `[<string>"foo"]`))

Expect(Object(map[string]interface{}{"foo": true}, 1)).
Expect(Object(map[string]any{"foo": true}, 1)).
To(match("map[string]interface {} | len:1", `{"foo": <bool>true}`))

Expect(Object(struct{ A interface{} }{A: 1}, 1)).
Expect(Object(struct{ A any }{A: 1}, 1)).
To(match("struct { A interface {} }", "{A: <int>1}"))

v := struct{ A interface{} }{A: struct{ B string }{B: "foo"}}
v := struct{ A any }{A: struct{ B string }{B: "foo"}}
Expect(Object(v, 1)).To(match(`struct { A interface {} }`, `{
A: <struct { B string }>{B: "foo"},
}`))
Expand Down Expand Up @@ -694,7 +694,7 @@ var _ = Describe("Format", func() {

Describe("Handling interfaces", func() {
It("should unpack the interface", func() {
outerHash := map[string]interface{}{}
outerHash := map[string]any{}
innerHash := map[string]int{}

innerHash["inner"] = 3
Expand All @@ -708,19 +708,19 @@ var _ = Describe("Format", func() {

Describe("Handling recursive things", func() {
It("should not go crazy...", func() {
m := map[string]interface{}{}
m := map[string]any{}
m["integer"] = 2
m["map"] = m
Expect(Object(m, 1)).Should(ContainSubstring("..."))
})

It("really should not go crazy...", func() {
type complexKey struct {
Value map[interface{}]int
Value map[any]int
}

complexObject := complexKey{}
complexObject.Value = make(map[interface{}]int)
complexObject.Value = make(map[any]int)

complexObject.Value[&complexObject] = 2
Expect(Object(complexObject, 1)).Should(ContainSubstring("..."))
Expand Down Expand Up @@ -784,7 +784,7 @@ var _ = Describe("Format", func() {

It("indents CustomFormatter output correctly", func() {
cf := CustomFormatted{"hey\nbob", 17}
DeferCleanup(UnregisterCustomFormatter, RegisterCustomFormatter(func(value interface{}) (string, bool) {
DeferCleanup(UnregisterCustomFormatter, RegisterCustomFormatter(func(value any) (string, bool) {
cf, ok := value.(CustomFormatted)
if !ok {
return "", false
Expand Down
17 changes: 11 additions & 6 deletions gbytes/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Subsequent matches against the buffer will only operate against data that appear
The read cursor is an opaque implementation detail that you cannot access. You should use the Say matcher to sift through the buffer. You can always
access the entire buffer's contents with Contents().
*/
package gbytes

Expand All @@ -29,7 +28,7 @@ type Buffer struct {
contents []byte
readCursor uint64
lock *sync.Mutex
detectCloser chan interface{}
detectCloser chan any
closed bool
}

Expand Down Expand Up @@ -167,19 +166,25 @@ You could do something like:
select {
case <-buffer.Detect("You are not logged in"):
//log in
case <-buffer.Detect("Success"):
//carry on
case <-time.After(time.Second):
//welp
}
//welp
}
buffer.CancelDetects()
You should always call CancelDetects after using Detect. This will close any channels that have not detected and clean up the goroutines that were spawned to support them.
Finally, you can pass detect a format string followed by variadic arguments. This will construct the regexp using fmt.Sprintf.
*/
func (b *Buffer) Detect(desired string, args ...interface{}) chan bool {
func (b *Buffer) Detect(desired string, args ...any) chan bool {
formattedRegexp := desired
if len(args) > 0 {
formattedRegexp = fmt.Sprintf(desired, args...)
Expand All @@ -190,7 +195,7 @@ func (b *Buffer) Detect(desired string, args ...interface{}) chan bool {
defer b.lock.Unlock()

if b.detectCloser == nil {
b.detectCloser = make(chan interface{})
b.detectCloser = make(chan any)
}

closer := b.detectCloser
Expand Down
14 changes: 7 additions & 7 deletions gbytes/say_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/onsi/gomega/format"
)

//Objects satisfying the BufferProvider can be used with the Say matcher.
// Objects satisfying the BufferProvider can be used with the Say matcher.
type BufferProvider interface {
Buffer() *Buffer
}
Expand Down Expand Up @@ -37,7 +37,7 @@ In such cases, Say simply operates on the *gbytes.Buffer returned by Buffer()
If the buffer is closed, the Say matcher will tell Eventually to abort.
*/
func Say(expected string, args ...interface{}) *sayMatcher {
func Say(expected string, args ...any) *sayMatcher {
if len(args) > 0 {
expected = fmt.Sprintf(expected, args...)
}
Expand All @@ -51,7 +51,7 @@ type sayMatcher struct {
receivedSayings []byte
}

func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
func (m *sayMatcher) buffer(actual any) (*Buffer, bool) {
var buffer *Buffer

switch x := actual.(type) {
Expand All @@ -66,7 +66,7 @@ func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
return buffer, true
}

func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
func (m *sayMatcher) Match(actual any) (success bool, err error) {
buffer, ok := m.buffer(actual)
if !ok {
return false, fmt.Errorf("Say must be passed a *gbytes.Buffer or BufferProvider. Got:\n%s", format.Object(actual, 1))
Expand All @@ -78,23 +78,23 @@ func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
return didSay, nil
}

func (m *sayMatcher) FailureMessage(actual interface{}) (message string) {
func (m *sayMatcher) FailureMessage(actual any) (message string) {
return fmt.Sprintf(
"Got stuck at:\n%s\nWaiting for:\n%s",
format.IndentString(string(m.receivedSayings), 1),
format.IndentString(m.re.String(), 1),
)
}

func (m *sayMatcher) NegatedFailureMessage(actual interface{}) (message string) {
func (m *sayMatcher) NegatedFailureMessage(actual any) (message string) {
return fmt.Sprintf(
"Saw:\n%s\nWhich matches the unexpected:\n%s",
format.IndentString(string(m.receivedSayings), 1),
format.IndentString(m.re.String(), 1),
)
}

func (m *sayMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
func (m *sayMatcher) MatchMayChangeInTheFuture(actual any) bool {
switch x := actual.(type) {
case *Buffer:
return !x.Closed()
Expand Down
2 changes: 1 addition & 1 deletion gcustom/make_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/onsi/gomega/format"
)

var interfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
var interfaceType = reflect.TypeOf((*any)(nil)).Elem()
var errInterface = reflect.TypeOf((*error)(nil)).Elem()

var defaultTemplate = template.Must(ParseTemplate("{{if .Failure}}Custom matcher failed for:{{else}}Custom matcher succeeded (but was expected to fail) for:{{end}}\n{{.FormattedActual}}"))
Expand Down
8 changes: 4 additions & 4 deletions gexec/exit_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Exiter interface {
ExitCode() int
}

func (m *exitMatcher) Match(actual interface{}) (success bool, err error) {
func (m *exitMatcher) Match(actual any) (success bool, err error) {
exiter, ok := actual.(Exiter)
if !ok {
return false, fmt.Errorf("Exit must be passed a gexec.Exiter (Missing method ExitCode() int) Got:\n%s", format.Object(actual, 1))
Expand All @@ -61,14 +61,14 @@ func (m *exitMatcher) Match(actual interface{}) (success bool, err error) {
return m.exitCode == m.actualExitCode, nil
}

func (m *exitMatcher) FailureMessage(actual interface{}) (message string) {
func (m *exitMatcher) FailureMessage(actual any) (message string) {
if m.actualExitCode == -1 {
return "Expected process to exit. It did not."
}
return format.Message(m.actualExitCode, "to match exit code:", m.exitCode)
}

func (m *exitMatcher) NegatedFailureMessage(actual interface{}) (message string) {
func (m *exitMatcher) NegatedFailureMessage(actual any) (message string) {
if m.actualExitCode == -1 {
return "you really shouldn't be able to see this!"
} else {
Expand All @@ -79,7 +79,7 @@ func (m *exitMatcher) NegatedFailureMessage(actual interface{}) (message string)
}
}

func (m *exitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
func (m *exitMatcher) MatchMayChangeInTheFuture(actual any) bool {
session, ok := actual.(*Session)
if ok {
return session.ExitCode() == -1
Expand Down
6 changes: 3 additions & 3 deletions gexec/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ will wait for the command to exit then return the entirety of Out's contents.
Wait uses eventually under the hood and accepts the same timeout/polling intervals that eventually does.
*/
func (s *Session) Wait(timeout ...interface{}) *Session {
func (s *Session) Wait(timeout ...any) *Session {
EventuallyWithOffset(1, s, timeout...).Should(Exit())
return s
}
Expand Down Expand Up @@ -225,7 +225,7 @@ The timeout specified is applied to each process killed.
If any of the processes already exited, KillAndWait returns silently.
*/
func KillAndWait(timeout ...interface{}) {
func KillAndWait(timeout ...any) {
trackedSessionsMutex.Lock()
defer trackedSessionsMutex.Unlock()
for _, session := range trackedSessions {
Expand All @@ -240,7 +240,7 @@ The timeout specified is applied to each process killed.
If any of the processes already exited, TerminateAndWait returns silently.
*/
func TerminateAndWait(timeout ...interface{}) {
func TerminateAndWait(timeout ...any) {
trackedSessionsMutex.Lock()
defer trackedSessionsMutex.Unlock()
for _, session := range trackedSessions {
Expand Down
Loading

0 comments on commit fd3fd96

Please sign in to comment.