Skip to content

Commit

Permalink
refactor: go1.16 required now, so drop support for go1.9 to go1.15
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Oct 8, 2022
1 parent 725d6d2 commit 8e3956d
Show file tree
Hide file tree
Showing 25 changed files with 269 additions and 493 deletions.
14 changes: 2 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, tip]
go-version: [1.16.x, 1.17.x, 1.18.x, tip]
full-tests: [false]
include:
- go-version: 1.19.x
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
if: matrix.full-tests
run: |
curl -sL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |
sh -s -- -b $HOME/go/bin v1.49.0
sh -s -- -b $HOME/go/bin v1.50.0
echo $PATH
$HOME/go/bin/golangci-lint run --max-issues-per-linter 0 \
--max-same-issues 0 \
Expand Down Expand Up @@ -71,16 +71,6 @@ jobs:
GO_TEST_RACE_SAFE_FLAGS="$cover_flags-race-safe.out"
fi
case ${{ matrix.go-version }} in
1.9.x | 1.10.x) # Before go 1.11, go modules are not available
mkdir -p ../src/github.com/maxatome
ln -s $(pwd) ../src/github.com/$GITHUB_REPOSITORY
export GOPATH=$(dirname $(pwd))
cd $GOPATH/src/github.com/$GITHUB_REPOSITORY
go get -t ./...
;;
esac
export GORACE="halt_on_error=1"
echo "CLASSIC ==========================================="
go test $GO_TEST_FLAGS ./...
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ go-testdeep

**Extremely flexible golang deep comparison, extends the go testing package.**

Currently supports go 1.9 → 1.19.
Currently supports go 1.16 → 1.19.

- [Latest news](#latest-news)
- [Synopsis](#synopsis)
Expand Down
14 changes: 6 additions & 8 deletions helpers/tdsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ var tType = reflect.TypeOf((*td.T)(nil))
// tests suite, Setup method is called once before any test runs. If
// Setup returns an error, the tests suite aborts: no tests are run.
//
// Starting go1.14, t.Cleanup() can be called in Setup method. It can
// replace the definition of a [Destroy] method. It can also be used
// together, in this case cleanup registered functions are called
// after [Destroy].
// t.Cleanup() can be called in Setup method. It can replace the
// definition of a [Destroy] method. It can also be used together, in
// this case cleanup registered functions are called after [Destroy].
type Setup interface {
Setup(t *td.T) error
}
Expand All @@ -37,10 +36,9 @@ type Setup interface {
// itself. If PreTest returns an error, the subtest aborts: the test
// is not run.
//
// Starting go1.14, t.Cleanup() can be called in PreTest method. It can
// replace the definition of a [PostTest] method. It can also be used
// together, in this case cleanup registered functions are called
// after [PostTest].
// t.Cleanup() can be called in PreTest method. It can replace the
// definition of a [PostTest] method. It can also be used together, in
// this case cleanup registered functions are called after [PostTest].
type PreTest interface {
PreTest(t *td.T, testName string) error
}
Expand Down
104 changes: 0 additions & 104 deletions helpers/tdsuite/suite_go114_test.go

This file was deleted.

87 changes: 87 additions & 0 deletions helpers/tdsuite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,90 @@ func TestRunErrors(t *testing.T) {
})
})
}

// FullCleanup has tests and all possible hooks.
type FullCleanup struct{ base }

func (f *FullCleanup) Setup(t *td.T) error { f.rec(); return nil }
func (f *FullCleanup) PreTest(t *td.T, tn string) error {
f.rec(tn)
t.Cleanup(func() { f.rec(tn) })
return nil
}

func (f *FullCleanup) PostTest(t *td.T, tn string) error {
f.rec(tn)
t.Cleanup(func() { f.rec(tn) })
return nil
}

func (f *FullCleanup) BetweenTests(t *td.T, prev, next string) error {
f.rec(prev, next)
return nil
}
func (f *FullCleanup) Destroy(t *td.T) error { f.rec(); return nil }

func (f *FullCleanup) Test1(t *td.T) {
f.rec()
t.Cleanup(func() { f.rec() })
}

func (f *FullCleanup) Test2(assert *td.T, require *td.T) {
f.rec()
assert.Cleanup(func() { f.rec() })
}

func (f *FullCleanup) Test3(t *td.T) {
f.rec()
t.Cleanup(func() { f.rec() })
}
func (f *FullCleanup) Testimony(t *td.T) {} // not a test method

var (
_ tdsuite.Setup = (*FullCleanup)(nil)
_ tdsuite.PreTest = (*FullCleanup)(nil)
_ tdsuite.PostTest = (*FullCleanup)(nil)
_ tdsuite.BetweenTests = (*FullCleanup)(nil)
_ tdsuite.Destroy = (*FullCleanup)(nil)
)

func TestRunCleanup(t *testing.T) {
t.Run("Full", func(t *testing.T) {
suite := FullCleanup{}
td.CmpTrue(t, tdsuite.Run(t, &suite))
ok := td.Cmp(t, suite.calls, []string{
"Setup",
/**/ "PreTest+Test1",
/**/ "Test1",
/**/ "PostTest+Test1",
/**/ "PostTest.Cleanup+Test1",
/**/ "Test1.Cleanup",
/**/ "PreTest.Cleanup+Test1",
"BetweenTests+Test1+Test2",
/**/ "PreTest+Test2",
/**/ "Test2",
/**/ "PostTest+Test2",
/**/ "PostTest.Cleanup+Test2",
/**/ "Test2.Cleanup",
/**/ "PreTest.Cleanup+Test2",
"BetweenTests+Test2+Test3",
/**/ "PreTest+Test3",
/**/ "Test3",
/**/ "PostTest+Test3",
/**/ "PostTest.Cleanup+Test3",
/**/ "Test3.Cleanup",
/**/ "PreTest.Cleanup+Test3",
"Destroy",
})
if !ok {
for _, c := range suite.calls {
switch c[0] {
case 'S', 'B', 'D':
t.Log(c)
default:
t.Log(" ", c)
}
}
}
})
}
71 changes: 71 additions & 0 deletions helpers/tdutil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package tdutil
import (
"reflect"
"sort"

"github.com/maxatome/go-testdeep/internal/visited"
)

// MapSortedKeys returns a slice of all sorted keys of map m. It
Expand All @@ -18,3 +20,72 @@ func MapSortedKeys(m reflect.Value) []reflect.Value {
sort.Sort(SortableValues(ks))
return ks
}

type kv struct {
key reflect.Value
value reflect.Value
}

type kvSlice struct {
v visited.Visited
s []kv
}

func newKvSlice(l int) *kvSlice {
s := kvSlice{}
if l > 0 {
s.s = make([]kv, 0, l)
if l > 1 {
s.v = visited.NewVisited()
}
}
return &s
}

func (s *kvSlice) Len() int { return len(s.s) }
func (s *kvSlice) Less(i, j int) bool {
return cmp(s.v, s.s[i].key, s.s[j].key) < 0
}
func (s *kvSlice) Swap(i, j int) { s.s[i], s.s[j] = s.s[j], s.s[i] }

// MapEach calls fn for each key/value pair of map m. If fn
// returns false, it will not be called again.
func MapEach(m reflect.Value, fn func(k, v reflect.Value) bool) bool {
kvs := newKvSlice(m.Len())
iter := m.MapRange()
for iter.Next() {
kvs.s = append(kvs.s, kv{key: iter.Key(), value: iter.Value()})
}
sort.Sort(kvs)

for _, kv := range kvs.s {
if !fn(kv.key, kv.value) {
return false
}
}
return true
}

// MapEachValue calls fn for each value of map m. If fn returns
// false, it will not be called again.
func MapEachValue(m reflect.Value, fn func(k reflect.Value) bool) bool {
iter := m.MapRange()
for iter.Next() {
if !fn(iter.Value()) {
return false
}
}
return true
}

// MapSortedValues returns a slice of all sorted values of map m. It
// panics if m's [reflect.Kind] is not [reflect.Map].
func MapSortedValues(m reflect.Value) []reflect.Value {
vs := make([]reflect.Value, 0, m.Len())
iter := m.MapRange()
for iter.Next() {
vs = append(vs, iter.Value())
}
sort.Sort(SortableValues(vs))
return vs
}

0 comments on commit 8e3956d

Please sign in to comment.