*: revendor

This commit is contained in:
Eric Chiang
2016-08-05 09:51:37 -07:00
parent df258306dc
commit ad6af58003
15 changed files with 1844 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
*.test
*.bench
*.golden
*.txt
*.prof

25
vendor/github.com/kylelemons/godebug/pretty/doc.go generated vendored Normal file
View File

@@ -0,0 +1,25 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package pretty pretty-prints Go structures.
//
// This package uses reflection to examine a Go value and can
// print out in a nice, aligned fashion. It supports three
// modes (normal, compact, and extended) for advanced use.
//
// See the Reflect and Print examples for what the output looks like.
package pretty
// TODO:
// - Catch cycles

View File

@@ -0,0 +1,281 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty_test
import (
"fmt"
"github.com/kylelemons/godebug/pretty"
)
func ExampleConfig_Sprint() {
type Pair [2]int
type Map struct {
Name string
Players map[string]Pair
Obstacles map[Pair]string
}
m := Map{
Name: "Rock Creek",
Players: map[string]Pair{
"player1": {1, 3},
"player2": {0, -1},
},
Obstacles: map[Pair]string{
Pair{0, 0}: "rock",
Pair{2, 1}: "pond",
Pair{1, 1}: "stream",
Pair{0, 1}: "stream",
},
}
// Specific output formats
compact := &pretty.Config{
Compact: true,
}
diffable := &pretty.Config{
Diffable: true,
}
// Print out a summary
fmt.Printf("Players: %s\n", compact.Sprint(m.Players))
// Print diffable output
fmt.Printf("Map State:\n%s", diffable.Sprint(m))
// Output:
// Players: {player1:[1,3],player2:[0,-1]}
// Map State:
// {
// Name: "Rock Creek",
// Players: {
// player1: [
// 1,
// 3,
// ],
// player2: [
// 0,
// -1,
// ],
// },
// Obstacles: {
// [0,0]: "rock",
// [0,1]: "stream",
// [1,1]: "stream",
// [2,1]: "pond",
// },
// }
}
func ExamplePrint() {
type ShipManifest struct {
Name string
Crew map[string]string
Androids int
Stolen bool
}
manifest := &ShipManifest{
Name: "Spaceship Heart of Gold",
Crew: map[string]string{
"Zaphod Beeblebrox": "Galactic President",
"Trillian": "Human",
"Ford Prefect": "A Hoopy Frood",
"Arthur Dent": "Along for the Ride",
},
Androids: 1,
Stolen: true,
}
pretty.Print(manifest)
// Output:
// {Name: "Spaceship Heart of Gold",
// Crew: {Arthur Dent: "Along for the Ride",
// Ford Prefect: "A Hoopy Frood",
// Trillian: "Human",
// Zaphod Beeblebrox: "Galactic President"},
// Androids: 1,
// Stolen: true}
}
var t = struct {
Errorf func(string, ...interface{})
}{
Errorf: func(format string, args ...interface{}) {
fmt.Println(fmt.Sprintf(format, args...) + "\n")
},
}
func ExampleCompare_testing() {
// Code under test:
type ShipManifest struct {
Name string
Crew map[string]string
Androids int
Stolen bool
}
// AddCrew tries to add the given crewmember to the manifest.
AddCrew := func(m *ShipManifest, name, title string) {
if m.Crew == nil {
m.Crew = make(map[string]string)
}
m.Crew[title] = name
}
// Test function:
tests := []struct {
desc string
before *ShipManifest
name, title string
after *ShipManifest
}{
{
desc: "add first",
before: &ShipManifest{},
name: "Zaphod Beeblebrox",
title: "Galactic President",
after: &ShipManifest{
Crew: map[string]string{
"Zaphod Beeblebrox": "Galactic President",
},
},
},
{
desc: "add another",
before: &ShipManifest{
Crew: map[string]string{
"Zaphod Beeblebrox": "Galactic President",
},
},
name: "Trillian",
title: "Human",
after: &ShipManifest{
Crew: map[string]string{
"Zaphod Beeblebrox": "Galactic President",
"Trillian": "Human",
},
},
},
{
desc: "overwrite",
before: &ShipManifest{
Crew: map[string]string{
"Zaphod Beeblebrox": "Galactic President",
},
},
name: "Zaphod Beeblebrox",
title: "Just this guy, you know?",
after: &ShipManifest{
Crew: map[string]string{
"Zaphod Beeblebrox": "Just this guy, you know?",
},
},
},
}
for _, test := range tests {
AddCrew(test.before, test.name, test.title)
if diff := pretty.Compare(test.before, test.after); diff != "" {
t.Errorf("%s: post-AddCrew diff: (-got +want)\n%s", test.desc, diff)
}
}
// Output:
// add first: post-AddCrew diff: (-got +want)
// {
// Name: "",
// Crew: {
// - Galactic President: "Zaphod Beeblebrox",
// + Zaphod Beeblebrox: "Galactic President",
// },
// Androids: 0,
// Stolen: false,
// }
//
// add another: post-AddCrew diff: (-got +want)
// {
// Name: "",
// Crew: {
// - Human: "Trillian",
// + Trillian: "Human",
// Zaphod Beeblebrox: "Galactic President",
// },
// Androids: 0,
// Stolen: false,
// }
//
// overwrite: post-AddCrew diff: (-got +want)
// {
// Name: "",
// Crew: {
// - Just this guy, you know?: "Zaphod Beeblebrox",
// - Zaphod Beeblebrox: "Galactic President",
// + Zaphod Beeblebrox: "Just this guy, you know?",
// },
// Androids: 0,
// Stolen: false,
// }
}
func ExampleCompare_debugging() {
type ShipManifest struct {
Name string
Crew map[string]string
Androids int
Stolen bool
}
reported := &ShipManifest{
Name: "Spaceship Heart of Gold",
Crew: map[string]string{
"Zaphod Beeblebrox": "Galactic President",
"Trillian": "Human",
"Ford Prefect": "A Hoopy Frood",
"Arthur Dent": "Along for the Ride",
},
Androids: 1,
Stolen: true,
}
expected := &ShipManifest{
Name: "Spaceship Heart of Gold",
Crew: map[string]string{
"Trillian": "Human",
"Rowan Artosok": "Captain",
},
Androids: 1,
Stolen: false,
}
fmt.Println(pretty.Compare(reported, expected))
// Output:
// {
// Name: "Spaceship Heart of Gold",
// Crew: {
// - Arthur Dent: "Along for the Ride",
// - Ford Prefect: "A Hoopy Frood",
// + Rowan Artosok: "Captain",
// Trillian: "Human",
// - Zaphod Beeblebrox: "Galactic President",
// },
// Androids: 1,
// - Stolen: true,
// + Stolen: false,
// }
}

116
vendor/github.com/kylelemons/godebug/pretty/public.go generated vendored Normal file
View File

@@ -0,0 +1,116 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty
import (
"bytes"
"fmt"
"io"
"reflect"
"github.com/kylelemons/godebug/diff"
)
// A Config represents optional configuration parameters for formatting.
//
// Some options, notably ShortList, dramatically increase the overhead
// of pretty-printing a value.
type Config struct {
// Verbosity options
Compact bool // One-line output. Overrides Diffable.
Diffable bool // Adds extra newlines for more easily diffable output.
// Field and value options
IncludeUnexported bool // Include unexported fields in output
PrintStringers bool // Call String on a fmt.Stringer
PrintTextMarshalers bool // Call MarshalText on an encoding.TextMarshaler
SkipZeroFields bool // Skip struct fields that have a zero value.
// Output transforms
ShortList int // Maximum character length for short lists if nonzero.
}
// Default Config objects
var (
// CompareConfig is the default configuration used for Compare.
CompareConfig = &Config{
Diffable: true,
IncludeUnexported: true,
}
// DefaultConfig is the default configuration used for all other top-level functions.
DefaultConfig = &Config{}
)
func (cfg *Config) fprint(buf *bytes.Buffer, vals ...interface{}) {
for i, val := range vals {
if i > 0 {
buf.WriteByte('\n')
}
cfg.val2node(reflect.ValueOf(val)).WriteTo(buf, "", cfg)
}
}
// Print writes the DefaultConfig representation of the given values to standard output.
func Print(vals ...interface{}) {
DefaultConfig.Print(vals...)
}
// Print writes the configured presentation of the given values to standard output.
func (cfg *Config) Print(vals ...interface{}) {
fmt.Println(cfg.Sprint(vals...))
}
// Sprint returns a string representation of the given value according to the DefaultConfig.
func Sprint(vals ...interface{}) string {
return DefaultConfig.Sprint(vals...)
}
// Sprint returns a string representation of the given value according to cfg.
func (cfg *Config) Sprint(vals ...interface{}) string {
buf := new(bytes.Buffer)
cfg.fprint(buf, vals...)
return buf.String()
}
// Fprint writes the representation of the given value to the writer according to the DefaultConfig.
func Fprint(w io.Writer, vals ...interface{}) (n int64, err error) {
return DefaultConfig.Fprint(w, vals...)
}
// Fprint writes the representation of the given value to the writer according to the cfg.
func (cfg *Config) Fprint(w io.Writer, vals ...interface{}) (n int64, err error) {
buf := new(bytes.Buffer)
cfg.fprint(buf, vals...)
return buf.WriteTo(w)
}
// Compare returns a string containing a line-by-line unified diff of the
// values in got and want, using the CompareConfig.
//
// Each line in the output is prefixed with '+', '-', or ' ' to indicate if it
// should be added to, removed from, or is correct for the "got" value with
// respect to the "want" value.
func Compare(got, want interface{}) string {
return CompareConfig.Compare(got, want)
}
// Compare returns a string containing a line-by-line unified diff of the
// values in got and want according to the cfg.
func (cfg *Config) Compare(got, want interface{}) string {
diffCfg := *cfg
diffCfg.Diffable = true
return diff.Diff(cfg.Sprint(got), cfg.Sprint(want))
}

View File

@@ -0,0 +1,128 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty
import (
"testing"
)
func TestDiff(t *testing.T) {
type example struct {
Name string
Age int
Friends []string
}
tests := []struct {
desc string
got, want interface{}
diff string
}{
{
desc: "basic struct",
got: example{
Name: "Zaphd",
Age: 42,
Friends: []string{
"Ford Prefect",
"Trillian",
"Marvin",
},
},
want: example{
Name: "Zaphod",
Age: 42,
Friends: []string{
"Ford Prefect",
"Trillian",
},
},
diff: ` {
- Name: "Zaphd",
+ Name: "Zaphod",
Age: 42,
Friends: [
"Ford Prefect",
"Trillian",
- "Marvin",
],
}`,
},
}
for _, test := range tests {
if got, want := Compare(test.got, test.want), test.diff; got != want {
t.Errorf("%s:", test.desc)
t.Errorf(" got: %q", got)
t.Errorf(" want: %q", want)
}
}
}
func TestSkipZeroFields(t *testing.T) {
type example struct {
Name string
Species string
Age int
Friends []string
}
tests := []struct {
desc string
got, want interface{}
diff string
}{
{
desc: "basic struct",
got: example{
Name: "Zaphd",
Species: "Betelgeusian",
Age: 42,
},
want: example{
Name: "Zaphod",
Species: "Betelgeusian",
Age: 42,
Friends: []string{
"Ford Prefect",
"Trillian",
"",
},
},
diff: ` {
- Name: "Zaphd",
+ Name: "Zaphod",
Species: "Betelgeusian",
Age: 42,
+ Friends: [
+ "Ford Prefect",
+ "Trillian",
+ "",
+ ],
}`,
},
}
cfg := *CompareConfig
cfg.SkipZeroFields = true
for _, test := range tests {
if got, want := cfg.Compare(test.got, test.want), test.diff; got != want {
t.Errorf("%s:", test.desc)
t.Errorf(" got: %q", got)
t.Errorf(" want: %q", want)
}
}
}

114
vendor/github.com/kylelemons/godebug/pretty/reflect.go generated vendored Normal file
View File

@@ -0,0 +1,114 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty
import (
"encoding"
"fmt"
"reflect"
"sort"
)
func isZeroVal(val reflect.Value) bool {
if !val.CanInterface() {
return false
}
z := reflect.Zero(val.Type()).Interface()
return reflect.DeepEqual(val.Interface(), z)
}
func (c *Config) val2node(val reflect.Value) node {
// TODO(kevlar): pointer tracking?
if !val.IsValid() {
return rawVal("nil")
}
if val.CanInterface() {
v := val.Interface()
if s, ok := v.(fmt.Stringer); ok && c.PrintStringers {
return stringVal(s.String())
}
if t, ok := v.(encoding.TextMarshaler); ok && c.PrintTextMarshalers {
if raw, err := t.MarshalText(); err == nil { // if NOT an error
return stringVal(string(raw))
}
}
}
switch kind := val.Kind(); kind {
case reflect.Ptr, reflect.Interface:
if val.IsNil() {
return rawVal("nil")
}
return c.val2node(val.Elem())
case reflect.String:
return stringVal(val.String())
case reflect.Slice, reflect.Array:
n := list{}
length := val.Len()
for i := 0; i < length; i++ {
n = append(n, c.val2node(val.Index(i)))
}
return n
case reflect.Map:
n := keyvals{}
keys := val.MapKeys()
for _, key := range keys {
// TODO(kevlar): Support arbitrary type keys?
n = append(n, keyval{compactString(c.val2node(key)), c.val2node(val.MapIndex(key))})
}
sort.Sort(n)
return n
case reflect.Struct:
n := keyvals{}
typ := val.Type()
fields := typ.NumField()
for i := 0; i < fields; i++ {
sf := typ.Field(i)
if !c.IncludeUnexported && sf.PkgPath != "" {
continue
}
field := val.Field(i)
if c.SkipZeroFields && isZeroVal(field) {
continue
}
n = append(n, keyval{sf.Name, c.val2node(field)})
}
return n
case reflect.Bool:
if val.Bool() {
return rawVal("true")
}
return rawVal("false")
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return rawVal(fmt.Sprintf("%d", val.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return rawVal(fmt.Sprintf("%d", val.Uint()))
case reflect.Uintptr:
return rawVal(fmt.Sprintf("0x%X", val.Uint()))
case reflect.Float32, reflect.Float64:
return rawVal(fmt.Sprintf("%v", val.Float()))
case reflect.Complex64, reflect.Complex128:
return rawVal(fmt.Sprintf("%v", val.Complex()))
}
// Fall back to the default %#v if we can
if val.CanInterface() {
return rawVal(fmt.Sprintf("%#v", val.Interface()))
}
return rawVal(val.String())
}

View File

@@ -0,0 +1,168 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty
import (
"reflect"
"testing"
"time"
)
func TestVal2nodeDefault(t *testing.T) {
tests := []struct {
desc string
raw interface{}
want node
}{
{
desc: "nil",
raw: nil,
want: rawVal("nil"),
},
{
desc: "nil ptr",
raw: (*int)(nil),
want: rawVal("nil"),
},
{
desc: "nil slice",
raw: []string(nil),
want: list{},
},
{
desc: "nil map",
raw: map[string]string(nil),
want: keyvals{},
},
{
desc: "string",
raw: "zaphod",
want: stringVal("zaphod"),
},
{
desc: "slice",
raw: []string{"a", "b"},
want: list{stringVal("a"), stringVal("b")},
},
{
desc: "map",
raw: map[string]string{
"zaphod": "beeblebrox",
"ford": "prefect",
},
want: keyvals{
{"ford", stringVal("prefect")},
{"zaphod", stringVal("beeblebrox")},
},
},
{
desc: "map of [2]int",
raw: map[[2]int]string{
[2]int{-1, 2}: "school",
[2]int{0, 0}: "origin",
[2]int{1, 3}: "home",
},
want: keyvals{
{"[-1,2]", stringVal("school")},
{"[0,0]", stringVal("origin")},
{"[1,3]", stringVal("home")},
},
},
{
desc: "struct",
raw: struct{ Zaphod, Ford string }{"beeblebrox", "prefect"},
want: keyvals{
{"Zaphod", stringVal("beeblebrox")},
{"Ford", stringVal("prefect")},
},
},
{
desc: "int",
raw: 3,
want: rawVal("3"),
},
}
for _, test := range tests {
if got, want := DefaultConfig.val2node(reflect.ValueOf(test.raw)), test.want; !reflect.DeepEqual(got, want) {
t.Errorf("%s: got %#v, want %#v", test.desc, got, want)
}
}
}
func TestVal2node(t *testing.T) {
tests := []struct {
desc string
raw interface{}
cfg *Config
want node
}{
{
desc: "struct default",
raw: struct{ Zaphod, Ford, foo string }{"beeblebrox", "prefect", "BAD"},
cfg: DefaultConfig,
want: keyvals{
{"Zaphod", stringVal("beeblebrox")},
{"Ford", stringVal("prefect")},
},
},
{
desc: "struct w/ IncludeUnexported",
raw: struct{ Zaphod, Ford, foo string }{"beeblebrox", "prefect", "GOOD"},
cfg: &Config{
IncludeUnexported: true,
},
want: keyvals{
{"Zaphod", stringVal("beeblebrox")},
{"Ford", stringVal("prefect")},
{"foo", stringVal("GOOD")},
},
},
{
desc: "time default",
raw: struct{ Date time.Time }{time.Unix(1234567890, 0).UTC()},
cfg: DefaultConfig,
want: keyvals{
{"Date", keyvals{}},
},
},
{
desc: "time w/ PrintTextMarshalers",
raw: struct{ Date time.Time }{time.Unix(1234567890, 0).UTC()},
cfg: &Config{
PrintTextMarshalers: true,
},
want: keyvals{
{"Date", stringVal("2009-02-13T23:31:30Z")},
},
},
{
desc: "time w/ PrintStringers",
raw: struct{ Date time.Time }{time.Unix(1234567890, 0).UTC()},
cfg: &Config{
PrintStringers: true,
},
want: keyvals{
{"Date", stringVal("2009-02-13 23:31:30 +0000 UTC")},
},
},
}
for _, test := range tests {
if got, want := test.cfg.val2node(reflect.ValueOf(test.raw)), test.want; !reflect.DeepEqual(got, want) {
t.Errorf("%s: got %#v, want %#v", test.desc, got, want)
}
}
}

View File

@@ -0,0 +1,160 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty
import (
"bytes"
"strconv"
"strings"
)
type node interface {
WriteTo(w *bytes.Buffer, indent string, cfg *Config)
}
func compactString(n node) string {
switch k := n.(type) {
case stringVal:
return string(k)
case rawVal:
return string(k)
}
buf := new(bytes.Buffer)
n.WriteTo(buf, "", &Config{Compact: true})
return buf.String()
}
type stringVal string
func (str stringVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
w.WriteString(strconv.Quote(string(str)))
}
type rawVal string
func (r rawVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
w.WriteString(string(r))
}
type keyval struct {
key string
val node
}
type keyvals []keyval
func (l keyvals) Len() int { return len(l) }
func (l keyvals) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l keyvals) Less(i, j int) bool { return l[i].key < l[j].key }
func (l keyvals) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
w.WriteByte('{')
switch {
case cfg.Compact:
// All on one line:
for i, kv := range l {
if i > 0 {
w.WriteByte(',')
}
w.WriteString(kv.key)
w.WriteByte(':')
kv.val.WriteTo(w, indent, cfg)
}
case cfg.Diffable:
w.WriteByte('\n')
inner := indent + " "
// Each value gets its own line:
for _, kv := range l {
w.WriteString(inner)
w.WriteString(kv.key)
w.WriteString(": ")
kv.val.WriteTo(w, inner, cfg)
w.WriteString(",\n")
}
w.WriteString(indent)
default:
keyWidth := 0
for _, kv := range l {
if kw := len(kv.key); kw > keyWidth {
keyWidth = kw
}
}
alignKey := indent + " "
alignValue := strings.Repeat(" ", keyWidth)
inner := alignKey + alignValue + " "
// First and last line shared with bracket:
for i, kv := range l {
if i > 0 {
w.WriteString(",\n")
w.WriteString(alignKey)
}
w.WriteString(kv.key)
w.WriteString(": ")
w.WriteString(alignValue[len(kv.key):])
kv.val.WriteTo(w, inner, cfg)
}
}
w.WriteByte('}')
}
type list []node
func (l list) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
if max := cfg.ShortList; max > 0 {
short := compactString(l)
if len(short) <= max {
w.WriteString(short)
return
}
}
w.WriteByte('[')
switch {
case cfg.Compact:
// All on one line:
for i, v := range l {
if i > 0 {
w.WriteByte(',')
}
v.WriteTo(w, indent, cfg)
}
case cfg.Diffable:
w.WriteByte('\n')
inner := indent + " "
// Each value gets its own line:
for _, v := range l {
w.WriteString(inner)
v.WriteTo(w, inner, cfg)
w.WriteString(",\n")
}
w.WriteString(indent)
default:
inner := indent + " "
// First and last line shared with bracket:
for i, v := range l {
if i > 0 {
w.WriteString(",\n")
w.WriteString(inner)
}
v.WriteTo(w, inner, cfg)
}
}
w.WriteByte(']')
}

View File

@@ -0,0 +1,316 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty
import (
"bytes"
"strings"
"testing"
)
func TestWriteTo(t *testing.T) {
tests := []struct {
desc string
node node
// All strings have a leading newline trimmed before comparison:
normal string
diffable string
}{
{
desc: "string",
node: stringVal("zaphod"),
normal: `"zaphod"`,
diffable: `"zaphod"`,
},
{
desc: "raw",
node: rawVal("42"),
normal: `42`,
diffable: `42`,
},
{
desc: "keyvals",
node: keyvals{
{"name", stringVal("zaphod")},
{"age", rawVal("42")},
},
normal: `
{name: "zaphod",
age: 42}`,
diffable: `
{
name: "zaphod",
age: 42,
}`,
},
{
desc: "empty list",
node: list{},
normal: `
[]`,
diffable: `
[
]`,
},
{
desc: "empty nested list",
node: list{list{}},
normal: `
[[]]`,
diffable: `
[
[
],
]`,
},
{
desc: "list",
node: list{
stringVal("zaphod"),
rawVal("42"),
},
normal: `
["zaphod",
42]`,
diffable: `
[
"zaphod",
42,
]`,
},
{
desc: "empty keyvals",
node: keyvals{},
normal: `
{}`,
diffable: `
{
}`,
},
{
desc: "empty nested keyvals",
node: keyvals{{"k", keyvals{}}},
normal: `
{k: {}}`,
diffable: `
{
k: {
},
}`,
},
{
desc: "nested",
node: list{
stringVal("first"),
list{rawVal("1"), rawVal("2"), rawVal("3")},
keyvals{
{"trillian", keyvals{
{"race", stringVal("human")},
{"age", rawVal("36")},
}},
{"zaphod", keyvals{
{"occupation", stringVal("president of the galaxy")},
{"features", stringVal("two heads")},
}},
},
keyvals{},
},
normal: `
["first",
[1,
2,
3],
{trillian: {race: "human",
age: 36},
zaphod: {occupation: "president of the galaxy",
features: "two heads"}},
{}]`,
diffable: `
[
"first",
[
1,
2,
3,
],
{
trillian: {
race: "human",
age: 36,
},
zaphod: {
occupation: "president of the galaxy",
features: "two heads",
},
},
{
},
]`,
},
}
for _, test := range tests {
// For readability, we have a newline that won't be there in the output
test.normal = strings.TrimPrefix(test.normal, "\n")
test.diffable = strings.TrimPrefix(test.diffable, "\n")
buf := new(bytes.Buffer)
test.node.WriteTo(buf, "", &Config{})
if got, want := buf.String(), test.normal; got != want {
t.Errorf("%s: normal rendendered incorrectly\ngot:\n%s\nwant:\n%s", test.desc, got, want)
}
buf.Reset()
test.node.WriteTo(buf, "", &Config{Diffable: true})
if got, want := buf.String(), test.diffable; got != want {
t.Errorf("%s: diffable rendendered incorrectly\ngot:\n%s\nwant:\n%s", test.desc, got, want)
}
}
}
func TestCompactString(t *testing.T) {
tests := []struct {
node
compact string
}{
{
stringVal("abc"),
"abc",
},
{
rawVal("2"),
"2",
},
{
list{
rawVal("2"),
rawVal("3"),
},
"[2,3]",
},
{
keyvals{
{"name", stringVal("zaphod")},
{"age", rawVal("42")},
},
`{name:"zaphod",age:42}`,
},
{
list{
list{
rawVal("0"),
rawVal("1"),
rawVal("2"),
rawVal("3"),
},
list{
rawVal("1"),
rawVal("2"),
rawVal("3"),
rawVal("0"),
},
list{
rawVal("2"),
rawVal("3"),
rawVal("0"),
rawVal("1"),
},
},
`[[0,1,2,3],[1,2,3,0],[2,3,0,1]]`,
},
}
for _, test := range tests {
if got, want := compactString(test.node), test.compact; got != want {
t.Errorf("%#v: compact = %q, want %q", test.node, got, want)
}
}
}
func TestShortList(t *testing.T) {
cfg := &Config{
ShortList: 16,
}
tests := []struct {
node
want string
}{
{
list{
list{
rawVal("0"),
rawVal("1"),
rawVal("2"),
rawVal("3"),
},
list{
rawVal("1"),
rawVal("2"),
rawVal("3"),
rawVal("0"),
},
list{
rawVal("2"),
rawVal("3"),
rawVal("0"),
rawVal("1"),
},
},
`[[0,1,2,3],
[1,2,3,0],
[2,3,0,1]]`,
},
}
for _, test := range tests {
buf := new(bytes.Buffer)
test.node.WriteTo(buf, "", cfg)
if got, want := buf.String(), test.want; got != want {
t.Errorf("%#v: got:\n%s\nwant:\n%s", test.node, got, want)
}
}
}
var benchNode = keyvals{
{"list", list{
rawVal("0"),
rawVal("1"),
rawVal("2"),
rawVal("3"),
}},
{"keyvals", keyvals{
{"a", stringVal("b")},
{"c", stringVal("e")},
{"d", stringVal("f")},
}},
}
func benchOpts(b *testing.B, cfg *Config) {
buf := new(bytes.Buffer)
benchNode.WriteTo(buf, "", cfg)
b.SetBytes(int64(buf.Len()))
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
benchNode.WriteTo(buf, "", cfg)
}
}
func BenchmarkWriteDefault(b *testing.B) { benchOpts(b, DefaultConfig) }
func BenchmarkWriteShortList(b *testing.B) { benchOpts(b, &Config{ShortList: 16}) }
func BenchmarkWriteCompact(b *testing.B) { benchOpts(b, &Config{Compact: true}) }
func BenchmarkWriteDiffable(b *testing.B) { benchOpts(b, &Config{Diffable: true}) }