Move util/deepprint{"" _test}.go into print package

This fully eliminates the util package.
This commit is contained in:
Cheer Xiao 2015-02-26 17:10:37 +01:00
parent dfe6dfbc7b
commit 900d814edb
5 changed files with 22 additions and 24 deletions

View File

@ -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))

View File

@ -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))
}
}
}

View File

@ -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)

View File

@ -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)
}
}
}

View File

@ -1,2 +0,0 @@
// Package util contains utility functions used by multiple packages of elvish.
package util