mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-12 17:27:50 +08:00
Move util/deepprint{"" _test}.go into print package
This fully eliminates the util package.
This commit is contained in:
parent
dfe6dfbc7b
commit
900d814edb
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
EXE := elvish
|
||||
PKGS := edit eval parse util sys store errutil sysutil strutil
|
||||
PKGS := edit eval parse sys store errutil sysutil strutil print
|
||||
PKG_PATHS := $(addprefix ./,$(PKGS)) # go tools want an explicit ./
|
||||
PKG_COVERS := $(addprefix cover/,$(PKGS))
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/elves/elvish/util"
|
||||
"github.com/elves/elvish/print"
|
||||
)
|
||||
|
||||
func compoundOfOnePrimary(p *Primary) *Compound {
|
||||
|
@ -197,7 +197,7 @@ func TestParse(t *testing.T) {
|
|||
for i, tt := range parseTests {
|
||||
out, err := Parse(fmt.Sprintf("<test %d>", i), tt.in)
|
||||
if !reflect.DeepEqual(out, tt.wanted) || err != nil {
|
||||
t.Errorf("Parse(*, %q) =>\n(%s, %v), want\n(%s, <nil>) (up to DeepEqual)", tt.in, util.DeepPrint(out), err, util.DeepPrint(tt.wanted))
|
||||
t.Errorf("Parse(*, %q) =>\n(%s, %v), want\n(%s, <nil>) (up to DeepEqual)", tt.in, print.Deep(out), err, print.Deep(tt.wanted))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package print
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -6,15 +6,15 @@ import (
|
|||
"reflect"
|
||||
)
|
||||
|
||||
// DeepPrint is like printing with the %#v formatter of fmt, but it prints
|
||||
// Deep is like printing with the %#v formatter of fmt, but it prints
|
||||
// pointer fields recursively.
|
||||
func DeepPrint(x interface{}) string {
|
||||
func Deep(x interface{}) string {
|
||||
b := &bytes.Buffer{}
|
||||
deepPrint(b, reflect.ValueOf(x))
|
||||
deep(b, reflect.ValueOf(x))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func deepPrint(b *bytes.Buffer, v reflect.Value) {
|
||||
func deep(b *bytes.Buffer, v reflect.Value) {
|
||||
i := v.Interface()
|
||||
t := v.Type()
|
||||
|
||||
|
@ -44,7 +44,7 @@ func deepPrint(b *bytes.Buffer, v reflect.Value) {
|
|||
if i > 0 {
|
||||
b.WriteString(", ")
|
||||
}
|
||||
deepPrint(b, v.Index(i))
|
||||
deep(b, v.Index(i))
|
||||
}
|
||||
case reflect.Map:
|
||||
keys := v.MapKeys()
|
||||
|
@ -52,9 +52,9 @@ func deepPrint(b *bytes.Buffer, v reflect.Value) {
|
|||
if i > 0 {
|
||||
b.WriteString(", ")
|
||||
}
|
||||
deepPrint(b, k)
|
||||
deep(b, k)
|
||||
b.WriteString(": ")
|
||||
deepPrint(b, v.MapIndex(k))
|
||||
deep(b, v.MapIndex(k))
|
||||
}
|
||||
case reflect.Struct:
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
|
@ -63,16 +63,16 @@ func deepPrint(b *bytes.Buffer, v reflect.Value) {
|
|||
}
|
||||
b.WriteString(t.Field(i).Name)
|
||||
b.WriteString(": ")
|
||||
deepPrint(b, v.Field(i))
|
||||
deep(b, v.Field(i))
|
||||
}
|
||||
}
|
||||
b.WriteRune('}')
|
||||
case reflect.Ptr:
|
||||
b.WriteRune('&')
|
||||
deepPrint(b, reflect.Indirect(v))
|
||||
deep(b, reflect.Indirect(v))
|
||||
return
|
||||
case reflect.Interface:
|
||||
deepPrint(b, v.Elem())
|
||||
deep(b, v.Elem())
|
||||
return
|
||||
default:
|
||||
fmt.Fprintf(b, "%#v", i)
|
|
@ -1,4 +1,4 @@
|
|||
package util
|
||||
package print
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
@ -27,7 +27,7 @@ func (g G) GoString() string {
|
|||
return "<G>"
|
||||
}
|
||||
|
||||
var deepPrintTests = []struct {
|
||||
var deepTests = []struct {
|
||||
in interface{}
|
||||
wanted string
|
||||
}{
|
||||
|
@ -38,14 +38,14 @@ var deepPrintTests = []struct {
|
|||
{[]int(nil), `nil`},
|
||||
{(*int)(nil), `nil`},
|
||||
{&S{42, "DON'T PANIC", &T{map[string]string{"foo": "bar"}}, G{}},
|
||||
`&util.S{I: 42, S: "DON'T PANIC", Pt: &util.T{M: map[string]string{"foo": "bar"}}, G: <G>}`},
|
||||
{[]interface{}{&U{42, "DON'T PANIC"}, 42, "DON'T PANIC"}, `[]interface {}{&util.U{I: 42, S: "DON'T PANIC"}, 42, "DON'T PANIC"}`},
|
||||
`&print.S{I: 42, S: "DON'T PANIC", Pt: &print.T{M: map[string]string{"foo": "bar"}}, G: <G>}`},
|
||||
{[]interface{}{&U{42, "DON'T PANIC"}, 42, "DON'T PANIC"}, `[]interface {}{&print.U{I: 42, S: "DON'T PANIC"}, 42, "DON'T PANIC"}`},
|
||||
}
|
||||
|
||||
func TestDeepPrint(t *testing.T) {
|
||||
for _, tt := range deepPrintTests {
|
||||
if out := DeepPrint(tt.in); out != tt.wanted {
|
||||
t.Errorf("GoPrint(%v) => %#q, want %#q", tt.in, out, tt.wanted)
|
||||
func TestDeep(t *testing.T) {
|
||||
for _, tt := range deepTests {
|
||||
if out := Deep(tt.in); out != tt.wanted {
|
||||
t.Errorf("Deep(%v) => %#q, want %#q", tt.in, out, tt.wanted)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
// Package util contains utility functions used by multiple packages of elvish.
|
||||
package util
|
Loading…
Reference in New Issue
Block a user