aboutsummaryrefslogtreecommitdiff
path: root/cmp
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2017-07-07 16:30:14 -0700
committerGitHub <noreply@github.com>2017-07-07 16:30:14 -0700
commit5c2f3415b05eca653272685cb8b1fdf4acfb6cd6 (patch)
tree4f46f4a62342e6d9020a9410cb8e6df948f83ee5 /cmp
parentdf565aecfca1377a64c83df6fb3bc0b7bad9c6f4 (diff)
downloadgo-cmp-5c2f3415b05eca653272685cb8b1fdf4acfb6cd6.tar.gz
Support versions of Go down to Go1.6 (#3)
Avoid any features introduced in newer versions such as: * testing.T.Run * sort.Slice * sort.SliceStable * sort.IsSliceSorted Fixes #2
Diffstat (limited to 'cmp')
-rw-r--r--cmp/compare.go12
-rw-r--r--cmp/compare_test.go34
-rw-r--r--cmp/example_test.go51
-rw-r--r--cmp/options_test.go16
-rw-r--r--cmp/reporter.go11
5 files changed, 55 insertions, 69 deletions
diff --git a/cmp/compare.go b/cmp/compare.go
index 0d8ef77..455ecb8 100644
--- a/cmp/compare.go
+++ b/cmp/compare.go
@@ -28,7 +28,6 @@ package cmp
import (
"fmt"
"reflect"
- "sort"
)
// BUG: Maps with keys containing NaN values cannot be properly compared due to
@@ -126,10 +125,13 @@ func newState(opts []Option) *state {
for _, opt := range opts {
s.processOption(opt)
}
- // Sort options such that Ignore options are evaluated first.
- sort.SliceStable(s.opts, func(i, j int) bool {
- return s.opts[i].op == nil && s.opts[j].op != nil
- })
+ // Move Ignore options to the front so that they are evaluated first.
+ for i, j := 0, 0; i < len(s.opts); i++ {
+ if s.opts[i].op == nil {
+ s.opts[i], s.opts[j] = s.opts[j], s.opts[i]
+ j++
+ }
+ }
return s
}
diff --git a/cmp/compare_test.go b/cmp/compare_test.go
index ba6a7e1..817672e 100644
--- a/cmp/compare_test.go
+++ b/cmp/compare_test.go
@@ -83,7 +83,7 @@ func TestDiff(t *testing.T) {
tests = append(tests, project4Tests()...)
for _, tt := range tests {
- t.Run(tt.label, func(t *testing.T) {
+ tRun(t, tt.label, func(t *testing.T) {
var gotDiff, gotPanic string
func() {
defer func() {
@@ -1444,22 +1444,22 @@ func project1Tests() []test {
}}
}
+type germSorter []*pb.Germ
+
+func (gs germSorter) Len() int { return len(gs) }
+func (gs germSorter) Less(i, j int) bool { return gs[i].String() < gs[j].String() }
+func (gs germSorter) Swap(i, j int) { gs[i], gs[j] = gs[j], gs[i] }
+
func project2Tests() []test {
const label = "Project2"
sortGerms := cmp.FilterValues(func(x, y []*pb.Germ) bool {
- ok1 := sort.SliceIsSorted(x, func(i, j int) bool {
- return x[i].String() < x[j].String()
- })
- ok2 := sort.SliceIsSorted(y, func(i, j int) bool {
- return y[i].String() < y[j].String()
- })
+ ok1 := sort.IsSorted(germSorter(x))
+ ok2 := sort.IsSorted(germSorter(y))
return !ok1 || !ok2
}, cmp.Transformer("Sort", func(in []*pb.Germ) []*pb.Germ {
out := append([]*pb.Germ(nil), in...) // Make copy
- sort.Slice(out, func(i, j int) bool {
- return out[i].String() < out[j].String()
- })
+ sort.Sort(germSorter(out))
return out
}))
@@ -1759,3 +1759,17 @@ func project4Tests() []test {
+: <non-existent>`,
}}
}
+
+// TODO: Delete this hack when we drop Go1.6 support.
+func tRun(t *testing.T, name string, f func(t *testing.T)) {
+ type runner interface {
+ Run(string, func(t *testing.T)) bool
+ }
+ var ti interface{} = t
+ if r, ok := ti.(runner); ok {
+ r.Run(name, f)
+ } else {
+ t.Logf("Test: %s", name)
+ f(t)
+ }
+}
diff --git a/cmp/example_test.go b/cmp/example_test.go
index a01f4ad..8de65a1 100644
--- a/cmp/example_test.go
+++ b/cmp/example_test.go
@@ -10,7 +10,6 @@ import (
"reflect"
"sort"
"strings"
- "time"
"github.com/google/go-cmp/cmp"
)
@@ -135,56 +134,6 @@ func ExampleOption_equalEmpty() {
// false
}
-// Equal compares map keys using Go's == operator. To use Equal itself on
-// map keys, transform the map into something else, like a slice of
-// key-value pairs.
-func ExampleOption_transformMap() {
- type KV struct {
- K time.Time
- V string
- }
- // This transformer flattens the map as a slice of sorted key-value pairs.
- // We can now safely rely on the Time.Equal to be used for equality.
- trans := cmp.Transformer("", func(m map[time.Time]string) (s []KV) {
- for k, v := range m {
- s = append(s, KV{k, v})
- }
- sort.Slice(s, func(i, j int) bool {
- return s[i].K.Before(s[j].K)
- })
- return s
- })
-
- t1 := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
- t2 := time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)
- t3 := time.Date(2011, time.November, 10, 23, 0, 0, 0, time.UTC)
-
- x := map[time.Time]string{
- t1.In(time.UTC): "0th birthday",
- t2.In(time.UTC): "1st birthday",
- t3.In(time.UTC): "2nd birthday",
- }
- y := map[time.Time]string{
- t1.In(time.Local): "0th birthday",
- t2.In(time.Local): "1st birthday",
- t3.In(time.Local): "2nd birthday",
- }
- z := map[time.Time]string{
- time.Now(): "a long long",
- time.Now(): "time ago",
- time.Now(): "in a galaxy far far away",
- }
-
- fmt.Println(cmp.Equal(x, y, trans))
- fmt.Println(cmp.Equal(y, z, trans))
- fmt.Println(cmp.Equal(z, x, trans))
-
- // Output:
- // true
- // false
- // false
-}
-
// Two slices may be considered equal if they have the same elements,
// regardless of the order that they appear in. Transformations can be used
// to sort the slice.
diff --git a/cmp/options_test.go b/cmp/options_test.go
index 76b76a0..2bde798 100644
--- a/cmp/options_test.go
+++ b/cmp/options_test.go
@@ -185,7 +185,7 @@ func TestOptionPanic(t *testing.T) {
}}
for _, tt := range tests {
- t.Run(tt.label, func(t *testing.T) {
+ tRun(t, tt.label, func(t *testing.T) {
var gotPanic string
func() {
defer func() {
@@ -215,3 +215,17 @@ func TestOptionPanic(t *testing.T) {
})
}
}
+
+// TODO: Delete this hack when we drop Go1.6 support.
+func tRun(t *testing.T, name string, f func(t *testing.T)) {
+ type runner interface {
+ Run(string, func(t *testing.T)) bool
+ }
+ var ti interface{} = t
+ if r, ok := ti.(runner); ok {
+ r.Run(name, f)
+ } else {
+ t.Logf("Test: %s", name)
+ f(t)
+ }
+}
diff --git a/cmp/reporter.go b/cmp/reporter.go
index 85cf977..14e3d8b 100644
--- a/cmp/reporter.go
+++ b/cmp/reporter.go
@@ -385,8 +385,8 @@ func sortKeys(vs []reflect.Value) []reflect.Value {
return vs
}
- // Sort the map keys
- sort.Slice(vs, func(i, j int) bool { return isLess(vs[i], vs[j]) })
+ // Sort the map keys.
+ sort.Sort(valueSorter(vs))
// Deduplicate keys (fails for NaNs).
vs2 := vs[:1]
@@ -397,3 +397,10 @@ func sortKeys(vs []reflect.Value) []reflect.Value {
}
return vs2
}
+
+// TODO: Use sort.Slice once Google AppEngine is on Go1.8 or above.
+type valueSorter []reflect.Value
+
+func (vs valueSorter) Len() int { return len(vs) }
+func (vs valueSorter) Less(i, j int) bool { return isLess(vs[i], vs[j]) }
+func (vs valueSorter) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }