Skip to content

Commit

Permalink
tests: add StructEqual() for verifying Equal method of structs
Browse files Browse the repository at this point in the history
This PR adds StructEqual which is helpful for verifying that modifying
the fields of a struct will cause the Equal method of the struct to
no longer return true.
  • Loading branch information
shoenig committed Mar 3, 2023
1 parent 56f8832 commit 79af6e7
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 1 deletion.
14 changes: 14 additions & 0 deletions interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ type EqualFunc[A any] interface {
Equal(A) bool
}

// CopyFunc represents a type implementing the Copy method.
type CopyFunc[A any] interface {
Copy() A
}

// CopyEqual represents a type satisfying both EqualFunc and CopyFunc.
type CopyEqual[T any] interface {
EqualFunc[T]
CopyFunc[T]
}

// TweakFunc is used for modifying a value in tests.
type TweakFunc[E CopyEqual[E]] func(E)

// LessFunc represents any type implementing the Less method.
type LessFunc[A any] interface {
Less(A) bool
Expand Down
26 changes: 25 additions & 1 deletion internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ func Equal[E interfaces.EqualFunc[E]](exp, val E) (s string) {
func NotEqual[E interfaces.EqualFunc[E]](exp, val E) (s string) {
if val.Equal(exp) {
s = "expected inequality via .Equal method\n"
s += diff(exp, val, nil)
}
return
}
Expand Down Expand Up @@ -1224,6 +1223,31 @@ func Wait(wc *wait.Constraint) (s string) {
return
}

type Tweak[E interfaces.CopyEqual[E]] struct {
Field string
Apply interfaces.TweakFunc[E]
}

// StructEqual will apply each Tweak and assert E.Equal captures the modification.
func StructEqual[E interfaces.CopyEqual[E]](original E, tweaks []Tweak[E]) (s string) {
for _, tweak := range tweaks {
if tweak.Field == "" {
return "Tweak.Field must be set"
} else if tweak.Apply == nil {
return "Tweak.Apply must be set"
}
clone := original.Copy()
if s = Equal[E](original, clone); s != "" {
return
}
tweak.Apply(clone)
if s = NotEqual[E](original, clone); s != "" {
return
}
}
return
}

func bullet(msg string, args ...any) string {
return fmt.Sprintf("↪ "+msg, args...)
}
10 changes: 10 additions & 0 deletions internal/util/slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package util

// CloneSliceFunc creates a copy of A by first applying convert to each element.
func CloneSliceFunc[A, B any](original []A, convert func(item A) B) []B {
clone := make([]B, len(original))
for i := 0; i < len(original); i++ {
clone[i] = convert(original[i])
}
return clone
}
36 changes: 36 additions & 0 deletions internal/util/slices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package util

import (
"strconv"
"testing"
)

func TestCloneSliceFunc(t *testing.T) {
t.Run("empty", func(t *testing.T) {
result := CloneSliceFunc([]int{}, func(i int) string {
return strconv.Itoa(i)
})
if len(result) > 0 {
t.Fatal("expected empty slice")
}
})

t.Run("non empty", func(t *testing.T) {
original := []int{1, 4, 5}
result := CloneSliceFunc(original, func(i int) string {
return strconv.Itoa(i)
})
if len(result) != 3 {
t.Fatal("expected length of 3")
}
if result[0] != "1" {
t.Fatal("expected result[0] == 1")
}
if result[1] != "4" {
t.Fatal("expected result[1] == 4")
}
if result[2] != "5" {
t.Fatal("expected result[2] == 5")
}
})
}
25 changes: 25 additions & 0 deletions must/must.go

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

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

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

25 changes: 25 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/shoenig/test/internal/assertions"
"github.com/shoenig/test/internal/brokenfs"
"github.com/shoenig/test/internal/constraints"
"github.com/shoenig/test/internal/util"
"github.com/shoenig/test/wait"
)

Expand Down Expand Up @@ -713,3 +714,27 @@ func Wait(t T, wc *wait.Constraint, settings ...Setting) {
t.Helper()
invoke(t, assertions.Wait(wc), settings...)
}

// Tweak is used to modify a struct and assert its Equal method captures the
// modification.
//
// Field is the name of the struct field and is used only for error printing.
// Apply is a function that modifies E.
type Tweak[E interfaces.CopyEqual[E]] struct {
Field string
Apply interfaces.TweakFunc[E]
}

// StructEqual will apply each Tweak and assert E.Equal captures the modification.
func StructEqual[E interfaces.CopyEqual[E]](t T, original E, tweaks []Tweak[E], settings ...Setting) {
t.Helper()
invoke(t, assertions.StructEqual(
original,
util.CloneSliceFunc[Tweak[E], assertions.Tweak[E]](
tweaks,
func(tweak Tweak[E]) assertions.Tweak[E] {
return assertions.Tweak[E]{Field: tweak.Field, Apply: tweak.Apply}
},
),
), settings...)
}
50 changes: 50 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,32 @@ func (c *container[T]) Len() int {
return c.length
}

func (c *container[T]) Copy() *container[T] {
return &container[T]{
contains: c.contains,
empty: c.empty,
size: c.size,
length: c.length,
}
}

func (c *container[T]) Equal(o *container[T]) bool {
if c == nil || o == nil {
return c == o
}
switch {
case c.contains != o.contains:
return false
case c.empty != o.empty:
return false
case c.size != o.size:
return false
case c.length != o.length:
return false
}
return true
}

func TestEmpty(t *testing.T) {
tc := newCase(t, `expected to be empty, but was not`)
t.Cleanup(tc.assert)
Expand Down Expand Up @@ -1560,3 +1586,27 @@ func TestWait_TestFunc(t *testing.T) {
wait.Timeout(100*time.Millisecond),
))
}

func TestStructEqual(t *testing.T) {
tc := newCase(t, `expected inequality via .Equal method`)
t.Cleanup(tc.assert)

StructEqual[*container[int]](tc, &container[int]{
contains: true,
empty: true,
size: 1,
length: 2,
}, []Tweak[*container[int]]{{
Field: "contains",
Apply: func(c *container[int]) { c.contains = false },
}, {
Field: "empty",
Apply: func(c *container[int]) { c.empty = false },
}, {
Field: "size",
Apply: func(c *container[int]) { c.size = 9 },
}, {
Field: "length",
Apply: func(c *container[int]) { c.length = 2 }, // no mod
}})
}

0 comments on commit 79af6e7

Please sign in to comment.