Set

func Set(expectedItems ...interface{}) TestDeep

Set operator compares the contents of an array or a slice (or a pointer on array/slice) ignoring duplicates and without taking care of the order of items.

During a match, each expected item should match in the compared array/slice, and each array/slice item should be matched by an expected item to succeed.

td.Cmp(t, []int{1, 1, 2}, td.Set(1, 2))    // succeeds
td.Cmp(t, []int{1, 1, 2}, td.Set(2, 1))    // succeeds
td.Cmp(t, []int{1, 1, 2}, td.Set(1, 2, 3)) // fails, 3 is missing

// works with slices/arrays of any type
td.Cmp(t, personSlice, td.Set(
  Person{Name: "Bob", Age: 32},
  Person{Name: "Alice", Age: 26},
))

To flatten a non-[]interface{} slice/array, use Flatten function and so avoid boring and inefficient copies:

expected := []int{2, 1}
td.Cmp(t, []int{1, 1, 2}, td.Set(td.Flatten(expected))) // succeeds
// = td.Cmp(t, []int{1, 1, 2}, td.Set(2, 1))

exp1 := []int{2, 1}
exp2 := []int{5, 8}
td.Cmp(t, []int{1, 5, 1, 2, 8, 3, 3},
  td.Set(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds
// = td.Cmp(t, []int{1, 5, 1, 2, 8, 3, 3}, td.Set(2, 1, 3, 5, 8))

See also Set godoc.

Example

Base example

CmpSet shortcut

func CmpSet(t TestingT, got interface{}, expectedItems []interface{}, args ...interface{}) bool

CmpSet is a shortcut for:

td.Cmp(t, got, td.Set(expectedItems...), args...)

See above for details.

Returns true if the test is OK, false if it fails.

args… are optional and allow to name the test. This name is used in case of failure to qualify the test. If len(args) > 1 and the first item of args is a string and contains a ‘%’ rune then fmt.Fprintf is used to compose the name, else args are passed to fmt.Fprint. Do not forget it is the name of the test, not the reason of a potential failure.

See also CmpSet godoc.

Example

Base example

T.Set shortcut

func (t *T) Set(got interface{}, expectedItems []interface{}, args ...interface{}) bool

Set is a shortcut for:

t.Cmp(got, td.Set(expectedItems...), args...)

See above for details.

Returns true if the test is OK, false if it fails.

args… are optional and allow to name the test. This name is used in case of failure to qualify the test. If len(args) > 1 and the first item of args is a string and contains a ‘%’ rune then fmt.Fprintf is used to compose the name, else args are passed to fmt.Fprint. Do not forget it is the name of the test, not the reason of a potential failure.

See also T.Set godoc.

Example

Base example