Skip to content

Commit

Permalink
feat: Alias spew.Dump and so on (#230)
Browse files Browse the repository at this point in the history
* feat: Alias `spew.Dump` and so on

* tests: Optimize test
  • Loading branch information
flc1125 authored Jul 4, 2023
1 parent b3a6a62 commit 3882742
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
25 changes: 25 additions & 0 deletions support/debug/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package debug

import (
"io"

"github.com/davecgh/go-spew/spew"
)

// Dump is used to display detailed information about variables
// And this is a wrapper around spew.Dump.
func Dump(v ...interface{}) {
spew.Dump(v...)
}

// FDump is used to display detailed information about variables to the specified io.Writer
// And this is a wrapper around spew.Fdump.
func FDump(w io.Writer, v ...interface{}) {
spew.Fdump(w, v...)
}

// SDump is used to display detailed information about variables as a string,
// And this is a wrapper around spew.Sdump.
func SDump(v ...interface{}) string {
return spew.Sdump(v...)
}
49 changes: 49 additions & 0 deletions support/debug/dump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package debug

import (
"bytes"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func redirectStdout(fn func()) ([]byte, error) {
f, err := os.CreateTemp("", "stdout")
if err != nil {
return nil, err
}
defer os.Remove(f.Name())
defer f.Close()

orig := os.Stdout
os.Stdout = f
fn()
os.Stdout = orig

return os.ReadFile(f.Name())
}

func TestDump(t *testing.T) {
buf, err := redirectStdout(func() {
Dump("foo")
})
assert.NoError(t, err)
assert.Equal(t, `(string) (len=3) "foo"
`, string(buf))
}

func TestFDump(t *testing.T) {
var buf bytes.Buffer
w := &buf

FDump(w, "foo")

assert.Equal(t, `(string) (len=3) "foo"
`, buf.String())
}

func TestSDump(t *testing.T) {
assert.Equal(t, `(string) (len=3) "foo"
`, SDump("foo"))
}

0 comments on commit 3882742

Please sign in to comment.