Skip to content

Commit 5c2f341

Browse files
authored
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
1 parent df565ae commit 5c2f341

File tree

6 files changed

+56
-70
lines changed

6 files changed

+56
-70
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
sudo: false
22
language: go
3-
# TODO(light): Enable Go 1.6 (#2).
43
go:
4+
- 1.6
55
- 1.x

cmp/compare.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ package cmp
2828
import (
2929
"fmt"
3030
"reflect"
31-
"sort"
3231
)
3332

3433
// BUG: Maps with keys containing NaN values cannot be properly compared due to
@@ -126,10 +125,13 @@ func newState(opts []Option) *state {
126125
for _, opt := range opts {
127126
s.processOption(opt)
128127
}
129-
// Sort options such that Ignore options are evaluated first.
130-
sort.SliceStable(s.opts, func(i, j int) bool {
131-
return s.opts[i].op == nil && s.opts[j].op != nil
132-
})
128+
// Move Ignore options to the front so that they are evaluated first.
129+
for i, j := 0, 0; i < len(s.opts); i++ {
130+
if s.opts[i].op == nil {
131+
s.opts[i], s.opts[j] = s.opts[j], s.opts[i]
132+
j++
133+
}
134+
}
133135
return s
134136
}
135137

cmp/compare_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestDiff(t *testing.T) {
8383
tests = append(tests, project4Tests()...)
8484

8585
for _, tt := range tests {
86-
t.Run(tt.label, func(t *testing.T) {
86+
tRun(t, tt.label, func(t *testing.T) {
8787
var gotDiff, gotPanic string
8888
func() {
8989
defer func() {
@@ -1444,22 +1444,22 @@ func project1Tests() []test {
14441444
}}
14451445
}
14461446

1447+
type germSorter []*pb.Germ
1448+
1449+
func (gs germSorter) Len() int { return len(gs) }
1450+
func (gs germSorter) Less(i, j int) bool { return gs[i].String() < gs[j].String() }
1451+
func (gs germSorter) Swap(i, j int) { gs[i], gs[j] = gs[j], gs[i] }
1452+
14471453
func project2Tests() []test {
14481454
const label = "Project2"
14491455

14501456
sortGerms := cmp.FilterValues(func(x, y []*pb.Germ) bool {
1451-
ok1 := sort.SliceIsSorted(x, func(i, j int) bool {
1452-
return x[i].String() < x[j].String()
1453-
})
1454-
ok2 := sort.SliceIsSorted(y, func(i, j int) bool {
1455-
return y[i].String() < y[j].String()
1456-
})
1457+
ok1 := sort.IsSorted(germSorter(x))
1458+
ok2 := sort.IsSorted(germSorter(y))
14571459
return !ok1 || !ok2
14581460
}, cmp.Transformer("Sort", func(in []*pb.Germ) []*pb.Germ {
14591461
out := append([]*pb.Germ(nil), in...) // Make copy
1460-
sort.Slice(out, func(i, j int) bool {
1461-
return out[i].String() < out[j].String()
1462-
})
1462+
sort.Sort(germSorter(out))
14631463
return out
14641464
}))
14651465

@@ -1759,3 +1759,17 @@ func project4Tests() []test {
17591759
+: <non-existent>`,
17601760
}}
17611761
}
1762+
1763+
// TODO: Delete this hack when we drop Go1.6 support.
1764+
func tRun(t *testing.T, name string, f func(t *testing.T)) {
1765+
type runner interface {
1766+
Run(string, func(t *testing.T)) bool
1767+
}
1768+
var ti interface{} = t
1769+
if r, ok := ti.(runner); ok {
1770+
r.Run(name, f)
1771+
} else {
1772+
t.Logf("Test: %s", name)
1773+
f(t)
1774+
}
1775+
}

cmp/example_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"reflect"
1111
"sort"
1212
"strings"
13-
"time"
1413

1514
"github.com/google/go-cmp/cmp"
1615
)
@@ -135,56 +134,6 @@ func ExampleOption_equalEmpty() {
135134
// false
136135
}
137136

138-
// Equal compares map keys using Go's == operator. To use Equal itself on
139-
// map keys, transform the map into something else, like a slice of
140-
// key-value pairs.
141-
func ExampleOption_transformMap() {
142-
type KV struct {
143-
K time.Time
144-
V string
145-
}
146-
// This transformer flattens the map as a slice of sorted key-value pairs.
147-
// We can now safely rely on the Time.Equal to be used for equality.
148-
trans := cmp.Transformer("", func(m map[time.Time]string) (s []KV) {
149-
for k, v := range m {
150-
s = append(s, KV{k, v})
151-
}
152-
sort.Slice(s, func(i, j int) bool {
153-
return s[i].K.Before(s[j].K)
154-
})
155-
return s
156-
})
157-
158-
t1 := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
159-
t2 := time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)
160-
t3 := time.Date(2011, time.November, 10, 23, 0, 0, 0, time.UTC)
161-
162-
x := map[time.Time]string{
163-
t1.In(time.UTC): "0th birthday",
164-
t2.In(time.UTC): "1st birthday",
165-
t3.In(time.UTC): "2nd birthday",
166-
}
167-
y := map[time.Time]string{
168-
t1.In(time.Local): "0th birthday",
169-
t2.In(time.Local): "1st birthday",
170-
t3.In(time.Local): "2nd birthday",
171-
}
172-
z := map[time.Time]string{
173-
time.Now(): "a long long",
174-
time.Now(): "time ago",
175-
time.Now(): "in a galaxy far far away",
176-
}
177-
178-
fmt.Println(cmp.Equal(x, y, trans))
179-
fmt.Println(cmp.Equal(y, z, trans))
180-
fmt.Println(cmp.Equal(z, x, trans))
181-
182-
// Output:
183-
// true
184-
// false
185-
// false
186-
}
187-
188137
// Two slices may be considered equal if they have the same elements,
189138
// regardless of the order that they appear in. Transformations can be used
190139
// to sort the slice.

cmp/options_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func TestOptionPanic(t *testing.T) {
185185
}}
186186

187187
for _, tt := range tests {
188-
t.Run(tt.label, func(t *testing.T) {
188+
tRun(t, tt.label, func(t *testing.T) {
189189
var gotPanic string
190190
func() {
191191
defer func() {
@@ -215,3 +215,17 @@ func TestOptionPanic(t *testing.T) {
215215
})
216216
}
217217
}
218+
219+
// TODO: Delete this hack when we drop Go1.6 support.
220+
func tRun(t *testing.T, name string, f func(t *testing.T)) {
221+
type runner interface {
222+
Run(string, func(t *testing.T)) bool
223+
}
224+
var ti interface{} = t
225+
if r, ok := ti.(runner); ok {
226+
r.Run(name, f)
227+
} else {
228+
t.Logf("Test: %s", name)
229+
f(t)
230+
}
231+
}

cmp/reporter.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ func sortKeys(vs []reflect.Value) []reflect.Value {
385385
return vs
386386
}
387387

388-
// Sort the map keys
389-
sort.Slice(vs, func(i, j int) bool { return isLess(vs[i], vs[j]) })
388+
// Sort the map keys.
389+
sort.Sort(valueSorter(vs))
390390

391391
// Deduplicate keys (fails for NaNs).
392392
vs2 := vs[:1]
@@ -397,3 +397,10 @@ func sortKeys(vs []reflect.Value) []reflect.Value {
397397
}
398398
return vs2
399399
}
400+
401+
// TODO: Use sort.Slice once Google AppEngine is on Go1.8 or above.
402+
type valueSorter []reflect.Value
403+
404+
func (vs valueSorter) Len() int { return len(vs) }
405+
func (vs valueSorter) Less(i, j int) bool { return isLess(vs[i], vs[j]) }
406+
func (vs valueSorter) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }

0 commit comments

Comments
 (0)