Skip to content

Commit

Permalink
test: add SliceEqOp
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored and shoenig committed Sep 4, 2024
1 parent 5a81108 commit 18682f8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,13 @@ func ExampleSliceEqual() {
// Output:
}

func ExampleSliceEqOp() {
s1 := []int{1, 3, 3, 7}
s2 := []int{1, 3, 3, 7}
SliceEqOp(t, s1, s2)
// Output:
}

func ExampleSliceLen() {
SliceLen(t, 4, []float64{32, 1.2, 0.01, 9e4})
// Output:
Expand Down
21 changes: 21 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,27 @@ func SliceEqual[E interfaces.EqualFunc[E]](exp, val []E) (s string) {
return
}

func SliceEqOp[A comparable, S ~[]A](exp, val S) (s string) {
lenA, lenB := len(exp), len(val)

if lenA != lenB {
s = "expected slices of same length\n"
s += bullet("len(exp): %d\n", lenA)
s += bullet("len(val): %d\n", lenB)
s += diff(exp, val, nil)
return
}

for i := 0; i < lenA; i++ {
if exp[i] != val[i] {
s += "expected slice equality via ==\n"
s += diff(exp[i], val[i], nil)
return
}
}
return
}

func Lesser[L interfaces.LessFunc[L]](exp, val L) (s string) {
if !val.Less(exp) {
s = "expected val to be less via .Less method\n"
Expand Down
7 changes: 7 additions & 0 deletions must/examples_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions must/must.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ func SliceEqual[E interfaces.EqualFunc[E]](t T, exp, val []E, settings ...Settin
invoke(t, assertions.SliceEqual(exp, val), settings...)
}

// SliceEqOp asserts exp[n] == val[n] for each element n.
func SliceEqOp[A comparable, S ~[]A](t T, exp, val S, settings ...Setting) {
t.Helper()
invoke(t, assertions.SliceEqOp(exp, val), settings...)
}

// SliceEmpty asserts slice is empty.
func SliceEmpty[A any](t T, slice []A, settings ...Setting) {
t.Helper()
Expand Down
20 changes: 20 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,26 @@ func TestSliceEqual(t *testing.T) {
})
}

func TestSliceEqOp(t *testing.T) {
t.Run("length", func(t *testing.T) {
tc := newCase(t, `expected slices of same length`)
t.Cleanup(tc.assert)

a := []int{1, 2, 3}
b := []int{1, 2, 3, 4}
SliceEqOp(tc, a, b)
})

t.Run("elements", func(t *testing.T) {
tc := newCase(t, `expected slice equality via ==`)
t.Cleanup(tc.assert)

a := []int{1, 2, 3}
b := []int{1, 2, 4}
SliceEqOp(tc, a, b)
})
}

func TestLesser(t *testing.T) {
tc := newCase(t, `expected val to be less via .Less method`)
t.Cleanup(tc.assert)
Expand Down

0 comments on commit 18682f8

Please sign in to comment.