Skip to content

Commit

Permalink
feedback: Add nil comparison tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcarnie committed Nov 21, 2018
1 parent f3d15be commit 79f06f6
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions pkg/slices/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,35 @@ func TestCopyChunkedByteSlices_multipleChunks(t *testing.T) {
}
}

const NIL = "<nil>"

// ss returns a sorted slice of byte slices.
func ss(s ...string) [][]byte {
r := make([][]byte, len(s))
for i := range s {
r[i] = []byte(s[i])
if s[i] != NIL {
r[i] = []byte(s[i])
}
}
bytesutil.Sort(r)
return r
}

func TestCompareSlice(t *testing.T) {
name := func(a, b [][]byte, exp int) string {
return fmt.Sprintf("%s <=> %s is %d", bytes.Join(a, nil), bytes.Join(b, nil), exp)
var as string
if a != nil {
as = string(bytes.Join(a, nil))
} else {
as = NIL
}
var bs string
if b != nil {
bs = string(bytes.Join(b, nil))
} else {
bs = NIL
}
return fmt.Sprintf("%s <=> %s is %d", as, bs, exp)
}
tests := []struct {
a, b [][]byte
Expand Down Expand Up @@ -130,11 +146,53 @@ func TestCompareSlice(t *testing.T) {
exp: 1,
},

{
a: ss("aaa", "bbb", NIL),
b: ss("aaa", "bbb", "ccc"),
exp: -1,
},

{
a: ss("aaa", NIL, "ccc"),
b: ss("aaa", NIL, "ccc"),
exp: 0,
},

{
a: ss(NIL, "bbb", "ccc"),
b: ss("aaa", "bbb", "ccc"),
exp: -1,
},

{
a: ss("aaa", "aaa"),
b: ss("aaa", "bbb", "ccc"),
exp: -1,
},

{
a: nil,
b: ss("aaa", "bbb", "ccc"),
exp: -1,
},

{
a: ss("aaa", "bbb"),
b: nil,
exp: 1,
},

{
a: nil,
b: nil,
exp: 0,
},

{
a: [][]byte{},
b: nil,
exp: 0,
},
}
for _, test := range tests {
t.Run(name(test.a, test.b, test.exp), func(t *testing.T) {
Expand Down

0 comments on commit 79f06f6

Please sign in to comment.