Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sort & Sorted operators #267

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
steps:
- name: Setup go
run: |
curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.5/install-go.pl |
curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.6/install-go.pl |
perl - ${{ matrix.go-version }} $HOME/go

- name: Checkout code
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`Shallow`]: https://go-testdeep.zetta.rocks/operators/shallow/
[`Slice`]: https://go-testdeep.zetta.rocks/operators/slice/
[`Smuggle`]: https://go-testdeep.zetta.rocks/operators/smuggle/
[`Sort`]: https://go-testdeep.zetta.rocks/operators/sort/
[`Sorted`]: https://go-testdeep.zetta.rocks/operators/sorted/
[`SStruct`]: https://go-testdeep.zetta.rocks/operators/sstruct/
[`String`]: https://go-testdeep.zetta.rocks/operators/string/
[`Struct`]: https://go-testdeep.zetta.rocks/operators/struct/
Expand Down Expand Up @@ -407,6 +409,8 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`CmpShallow`]: https://go-testdeep.zetta.rocks/operators/shallow/#cmpshallow-shortcut
[`CmpSlice`]: https://go-testdeep.zetta.rocks/operators/slice/#cmpslice-shortcut
[`CmpSmuggle`]: https://go-testdeep.zetta.rocks/operators/smuggle/#cmpsmuggle-shortcut
[`CmpSort`]: https://go-testdeep.zetta.rocks/operators/sort/#cmpsort-shortcut
[`CmpSorted`]: https://go-testdeep.zetta.rocks/operators/sorted/#cmpsorted-shortcut
[`CmpSStruct`]: https://go-testdeep.zetta.rocks/operators/sstruct/#cmpsstruct-shortcut
[`CmpString`]: https://go-testdeep.zetta.rocks/operators/string/#cmpstring-shortcut
[`CmpStruct`]: https://go-testdeep.zetta.rocks/operators/struct/#cmpstruct-shortcut
Expand Down Expand Up @@ -471,6 +475,8 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`T.Shallow`]: https://go-testdeep.zetta.rocks/operators/shallow/#tshallow-shortcut
[`T.Slice`]: https://go-testdeep.zetta.rocks/operators/slice/#tslice-shortcut
[`T.Smuggle`]: https://go-testdeep.zetta.rocks/operators/smuggle/#tsmuggle-shortcut
[`T.Sort`]: https://go-testdeep.zetta.rocks/operators/sort/#tsort-shortcut
[`T.Sorted`]: https://go-testdeep.zetta.rocks/operators/sorted/#tsorted-shortcut
[`T.SStruct`]: https://go-testdeep.zetta.rocks/operators/sstruct/#tsstruct-shortcut
[`T.String`]: https://go-testdeep.zetta.rocks/operators/string/#tstring-shortcut
[`T.Struct`]: https://go-testdeep.zetta.rocks/operators/struct/#tstruct-shortcut
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/maxatome/go-testdeep

go 1.18

require github.com/davecgh/go-spew v1.1.1 // indirect
require github.com/davecgh/go-spew v1.1.1
3 changes: 2 additions & 1 deletion helpers/tdutil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reflect"
"sort"

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

Expand Down Expand Up @@ -44,7 +45,7 @@ func newKvSlice(l int) *kvSlice {

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
return compare.Compare(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] }

Expand Down
173 changes: 0 additions & 173 deletions helpers/tdutil/sort_test.go

This file was deleted.

7 changes: 5 additions & 2 deletions helpers/tdutil/sort_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reflect"
"sort"

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

Expand All @@ -24,10 +25,12 @@ import (
// documentation.
//
// Sorting rules are as follows:
// - invalid value is always lower
// - nil is always lower
// - different types are sorted by their name
// - if method TYPE.Compare(TYPE) int exits, calls it
// - false is lesser than true
// - float and int numbers are sorted by their value
// - float and int numbers are sorted by their value, NaN is always lower
// - complex numbers are sorted by their real, then by their imaginary parts
// - strings are sorted by their value
// - map: shorter length is lesser, then sorted by address
Expand Down Expand Up @@ -59,7 +62,7 @@ func (v *rValues) Len() int {
}

func (v *rValues) Less(i, j int) bool {
return cmp(v.Visited, v.Slice[i], v.Slice[j]) < 0
return compare.Compare(v.Visited, v.Slice[i], v.Slice[j]) < 0
}

func (v *rValues) Swap(i, j int) {
Expand Down
41 changes: 6 additions & 35 deletions internal/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ package color
import (
"fmt"
"os"
"reflect"
"strings"
"sync"

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

const (
Expand Down Expand Up @@ -212,47 +213,17 @@ func Bad(s string, args ...any) string {
}

// BadUsage returns a string surrounded by BAD color to notice the
// user he passes a bad parameter to a function. Typically used in a
// user she/he passes a bad parameter to a function. Typically used in a
// panic().
func BadUsage(usage string, param any, pos int, kind bool) string {
Init()

var b strings.Builder
fmt.Fprintf(&b, "%susage: %s, but received ", BadOnBold, usage)

if param == nil {
b.WriteString("nil")
} else {
t := reflect.TypeOf(param)
if kind && t.String() != t.Kind().String() {
fmt.Fprintf(&b, "%s (%s)", t, t.Kind())
} else {
b.WriteString(t.String())
}
}

b.WriteString(" as ")
switch pos {
case 1:
b.WriteString("1st")
case 2:
b.WriteString("2nd")
case 3:
b.WriteString("3rd")
default:
fmt.Fprintf(&b, "%dth", pos)
}
b.WriteString(" parameter")
b.WriteString(BadOff)
return b.String()
return Bad("usage: %s, %s", usage, util.BadParam(param, pos, kind))
}

// TooManyParams returns a string surrounded by BAD color to notice
// the user he called a variadic function with too many
// the user she/he called a variadic function with too many
// parameters. Typically used in a panic().
func TooManyParams(usage string) string {
Init()
return BadOnBold + "usage: " + usage + ", too many parameters" + BadOff
return Bad("usage: " + usage + ", too many parameters")
}

// UnBad returns s with bad color prefix & suffix removed.
Expand Down
24 changes: 0 additions & 24 deletions internal/color/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,6 @@ func TestBadUsage(t *testing.T) {
test.EqualStr(t,
color.BadUsage("Zzz(STRING)", nil, 1, true),
"usage: Zzz(STRING), but received nil as 1st parameter")

test.EqualStr(t,
color.BadUsage("Zzz(STRING)", 42, 1, true),
"usage: Zzz(STRING), but received int as 1st parameter")

test.EqualStr(t,
color.BadUsage("Zzz(STRING)", []int{}, 1, true),
"usage: Zzz(STRING), but received []int (slice) as 1st parameter")
test.EqualStr(t,
color.BadUsage("Zzz(STRING)", []int{}, 1, false),
"usage: Zzz(STRING), but received []int as 1st parameter")

test.EqualStr(t,
color.BadUsage("Zzz(STRING)", nil, 1, true),
"usage: Zzz(STRING), but received nil as 1st parameter")
test.EqualStr(t,
color.BadUsage("Zzz(STRING)", nil, 2, true),
"usage: Zzz(STRING), but received nil as 2nd parameter")
test.EqualStr(t,
color.BadUsage("Zzz(STRING)", nil, 3, true),
"usage: Zzz(STRING), but received nil as 3rd parameter")
test.EqualStr(t,
color.BadUsage("Zzz(STRING)", nil, 4, true),
"usage: Zzz(STRING), but received nil as 4th parameter")
}

func TestTooManyParams(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions internal/compare/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2024, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

//go:build !go1.18
// +build !go1.18

package compare

type any = interface{}
12 changes: 12 additions & 0 deletions internal/compare/any_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2024, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

//go:build !go1.18
// +build !go1.18

package compare_test

type any = interface{}
Loading
Loading