Switch to using the dep tool and update all the dependencies

This commit is contained in:
Nick Craig-Wood
2017-05-11 15:39:54 +01:00
parent 5135ff73cb
commit 98c2d2c41b
5321 changed files with 4483197 additions and 5918 deletions
+78
View File
@@ -0,0 +1,78 @@
// Copyright 2016 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 (
"fmt"
"io/ioutil"
"os"
"os/exec"
"syscall"
)
// Diff compares the pretty-printed representation of two values. The second
// return value reports whether the two values' representations are identical.
// If it is false, the first return value contains the diffs.
//
// The output labels the first value "want" and the second "got".
//
// Diff works by invoking the "diff" command. It will only succeed in
// environments where "diff" is on the shell path.
func Diff(want, got interface{}) (string, bool, error) {
fname1, err := writeToTemp(want)
if err != nil {
return "", false, err
}
defer os.Remove(fname1)
fname2, err := writeToTemp(got)
if err != nil {
return "", false, err
}
defer os.Remove(fname2)
cmd := exec.Command("diff", "-u", "--label=want", "--label=got", fname1, fname2)
out, err := cmd.Output()
if err == nil {
return string(out), true, nil
}
eerr, ok := err.(*exec.ExitError)
if !ok {
return "", false, err
}
ws, ok := eerr.Sys().(syscall.WaitStatus)
if !ok {
return "", false, err
}
if ws.ExitStatus() != 1 {
return "", false, err
}
// Exit status of 1 means no error, but diffs were found.
return string(out), false, nil
}
func writeToTemp(v interface{}) (string, error) {
f, err := ioutil.TempFile("", "prettyDiff")
if err != nil {
return "", err
}
if _, err := fmt.Fprintf(f, "%+v\n", Value(v)); err != nil {
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
return f.Name(), nil
}
+50
View File
@@ -0,0 +1,50 @@
// Copyright 2016 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) {
for _, test := range []struct {
v1, v2 interface{}
ok bool
want string
}{
{5, 5, true, ""},
{"foo", "foo", true, ""},
{[]int{1, 2, 3}, []int{1, 0, 3}, false, `--- want
+++ got
@@ -1,5 +1,5 @@
[]int{
1,
- 2,
+ 0,
3,
}
`},
} {
got, ok, err := Diff(test.v1, test.v2)
if err != nil {
t.Errorf("%v vs. %v: %v", test.v1, test.v2, err)
continue
}
if ok != test.ok {
t.Errorf("%v vs. %v: got %t, want %t", test.v1, test.v2, ok, test.ok)
}
if got != test.want {
t.Errorf("%v vs. %v: got:\n%q\nwant:\n%q", test.v1, test.v2, got, test.want)
}
}
}
+241
View File
@@ -0,0 +1,241 @@
// Copyright 2016 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 implements a simple pretty-printer. It is intended for
// debugging the output of tests.
//
// It follows pointers and produces multi-line output for complex values like
// slices, maps and structs.
package pretty
import (
"fmt"
"io"
"reflect"
"sort"
"strings"
)
// Indent is the string output at each level of indentation.
var Indent = " "
// Value returns a value that will print prettily when used as an
// argument for the %v or %s format specifiers.
// With no flags, struct fields and map keys with default values are omitted.
// With the '+' or '#' flags, all values are displayed.
//
// This package does not detect cycles. Attempting to print a Value that
// contains cycles will result in unbounded recursion.
func Value(v interface{}) val { return val{v: v} }
type val struct{ v interface{} }
// Format implements the fmt.Formatter interface.
func (v val) Format(s fmt.State, c rune) {
if c == 'v' || c == 's' {
fprint(s, reflect.ValueOf(v.v), state{
defaults: s.Flag('+') || s.Flag('#'),
})
} else {
fmt.Fprintf(s, "%%!%c(pretty.Val)", c)
}
}
type state struct {
level int
prefix, suffix string
defaults bool
}
func fprint(w io.Writer, v reflect.Value, s state) {
indent := strings.Repeat(Indent, s.level)
fmt.Fprintf(w, "%s%s", indent, s.prefix)
if isNil(v) {
fmt.Fprintf(w, "nil%s", s.suffix)
return
}
if v.Type().Kind() == reflect.Interface {
v = v.Elem()
}
for v.Type().Kind() == reflect.Ptr {
fmt.Fprintf(w, "&")
v = v.Elem()
}
switch v.Type().Kind() {
default:
fmt.Fprintf(w, "%s%s", short(v), s.suffix)
case reflect.Array:
fmt.Fprintf(w, "%s{\n", v.Type())
for i := 0; i < v.Len(); i++ {
fprint(w, v.Index(i), state{
level: s.level + 1,
prefix: "",
suffix: ",",
defaults: s.defaults,
})
fmt.Fprintln(w)
}
fmt.Fprintf(w, "%s}", indent)
case reflect.Slice:
fmt.Fprintf(w, "%s{", v.Type())
if v.Len() > 0 {
fmt.Fprintln(w)
for i := 0; i < v.Len(); i++ {
fprint(w, v.Index(i), state{
level: s.level + 1,
prefix: "",
suffix: ",",
defaults: s.defaults,
})
fmt.Fprintln(w)
}
}
fmt.Fprintf(w, "%s}%s", indent, s.suffix)
case reflect.Map:
fmt.Fprintf(w, "%s{", v.Type())
if v.Len() > 0 {
fmt.Fprintln(w)
keys := v.MapKeys()
maybeSort(keys, v.Type().Key())
for _, key := range keys {
val := v.MapIndex(key)
if s.defaults || !isDefault(val) {
fprint(w, val, state{
level: s.level + 1,
prefix: short(key) + ": ",
suffix: ",",
defaults: s.defaults,
})
fmt.Fprintln(w)
}
}
}
fmt.Fprintf(w, "%s}%s", indent, s.suffix)
case reflect.Struct:
t := v.Type()
fmt.Fprintf(w, "%s{\n", t)
for i := 0; i < t.NumField(); i++ {
f := v.Field(i)
if s.defaults || !isDefault(f) {
fprint(w, f, state{
level: s.level + 1,
prefix: t.Field(i).Name + ": ",
suffix: ",",
defaults: s.defaults,
})
fmt.Fprintln(w)
}
}
fmt.Fprintf(w, "%s}%s", indent, s.suffix)
}
}
func isNil(v reflect.Value) bool {
if !v.IsValid() {
return true
}
switch v.Type().Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
default:
return false
}
}
func isDefault(v reflect.Value) bool {
if !v.IsValid() {
return true
}
t := v.Type()
switch t.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
default:
if !v.CanInterface() {
return false
}
return t.Comparable() && v.Interface() == reflect.Zero(t).Interface()
}
}
// short returns a short, one-line string for v.
func short(v reflect.Value) string {
if !v.IsValid() {
return "nil"
}
if v.Type().Kind() == reflect.String {
return fmt.Sprintf("%q", v)
}
return fmt.Sprintf("%v", v)
}
func indent(w io.Writer, level int) {
for i := 0; i < level; i++ {
io.WriteString(w, Indent) // ignore errors
}
}
func maybeSort(vs []reflect.Value, t reflect.Type) {
if less := lessFunc(t); less != nil {
sort.Sort(&sorter{vs, less})
}
}
// lessFunc returns a function that implements the "<" operator
// for the given type, or nil if the type doesn't support "<" .
func lessFunc(t reflect.Type) func(v1, v2 interface{}) bool {
switch t.Kind() {
case reflect.String:
return func(v1, v2 interface{}) bool { return v1.(string) < v2.(string) }
case reflect.Int:
return func(v1, v2 interface{}) bool { return v1.(int) < v2.(int) }
case reflect.Int8:
return func(v1, v2 interface{}) bool { return v1.(int8) < v2.(int8) }
case reflect.Int16:
return func(v1, v2 interface{}) bool { return v1.(int16) < v2.(int16) }
case reflect.Int32:
return func(v1, v2 interface{}) bool { return v1.(int32) < v2.(int32) }
case reflect.Int64:
return func(v1, v2 interface{}) bool { return v1.(int64) < v2.(int64) }
case reflect.Uint:
return func(v1, v2 interface{}) bool { return v1.(uint) < v2.(uint) }
case reflect.Uint8:
return func(v1, v2 interface{}) bool { return v1.(uint8) < v2.(uint8) }
case reflect.Uint16:
return func(v1, v2 interface{}) bool { return v1.(uint16) < v2.(uint16) }
case reflect.Uint32:
return func(v1, v2 interface{}) bool { return v1.(uint32) < v2.(uint32) }
case reflect.Uint64:
return func(v1, v2 interface{}) bool { return v1.(uint64) < v2.(uint64) }
case reflect.Float32:
return func(v1, v2 interface{}) bool { return v1.(float32) < v2.(float32) }
case reflect.Float64:
return func(v1, v2 interface{}) bool { return v1.(float64) < v2.(float64) }
default:
return nil
}
}
type sorter struct {
vs []reflect.Value
less func(v1, v2 interface{}) bool
}
func (s *sorter) Len() int { return len(s.vs) }
func (s *sorter) Swap(i, j int) { s.vs[i], s.vs[j] = s.vs[j], s.vs[i] }
func (s *sorter) Less(i, j int) bool { return s.less(s.vs[i].Interface(), s.vs[j].Interface()) }
+105
View File
@@ -0,0 +1,105 @@
// Copyright 2016 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 (
"fmt"
"strings"
"testing"
)
type S struct {
X int
Y bool
z *string
}
func TestSprint(t *testing.T) {
Indent = "~"
i := 17
for _, test := range []struct {
value interface{}
want string
}{
// primitives and pointer
{nil, "nil"},
{3, "3"},
{9.8, "9.8"},
{true, "true"},
{"foo", `"foo"`},
{&i, "&17"},
// array and slice
{[3]int{1, 2, 3}, "[3]int{\n~1,\n~2,\n~3,\n}"},
{[]int{1, 2, 3}, "[]int{\n~1,\n~2,\n~3,\n}"},
{[]int{}, "[]int{}"},
{[]string{"foo"}, "[]string{\n~\"foo\",\n}"},
// map
{map[int]bool{}, "map[int]bool{}"},
{map[int]bool{1: true, 2: false, 3: true},
"map[int]bool{\n~1: true,\n~3: true,\n}"},
// struct
{S{}, "pretty.S{\n}"},
{S{3, true, ptr("foo")},
"pretty.S{\n~X: 3,\n~Y: true,\n~z: &\"foo\",\n}"},
// interface
{[]interface{}{&i}, "[]interface {}{\n~&17,\n}"},
// nesting
{[]S{{1, false, ptr("a")}, {2, true, ptr("b")}},
`[]pretty.S{
~pretty.S{
~~X: 1,
~~z: &"a",
~},
~pretty.S{
~~X: 2,
~~Y: true,
~~z: &"b",
~},
}`},
} {
got := fmt.Sprintf("%v", Value(test.value))
if got != test.want {
t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
}
}
}
func TestWithDefaults(t *testing.T) {
Indent = "~"
for _, test := range []struct {
value interface{}
want string
}{
{map[int]bool{1: true, 2: false, 3: true},
"map[int]bool{\n~1: true,\n~2: false,\n~3: true,\n}"},
{S{}, "pretty.S{\n~X: 0,\n~Y: false,\n~z: nil,\n}"},
} {
got := fmt.Sprintf("%+v", Value(test.value))
if got != test.want {
t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
}
}
}
func TestBadVerb(t *testing.T) {
got := fmt.Sprintf("%d", Value(8))
want := "%!d("
if !strings.HasPrefix(got, want) {
t.Errorf("got %q, want prefix %q", got, want)
}
}
func ptr(s string) *string { return &s }