Struct

func Struct(model interface{}, expectedFields StructFields) TestDeep

Struct operator compares the contents of a struct or a pointer on a struct against the non-zero values of model (if any) and the values of expectedFields. See SStruct to compares against zero fields without specifying them in expectedFields.

model must be the same type as compared data.

expectedFields can be nil, if no zero entries are expected and no TestDeep operators are involved.

td.Cmp(t, td.Struct(
  Person{
    Name: "John Doe",
  },
  td.StructFields{
    "Age":      td.Between(40, 45),
    "Children": 0,
  }),
)

During a match, all expected fields must be found to succeed. Non-expected fields are ignored.

TypeBehind method returns the reflect.Type of model.

See also Struct godoc.

Example

Base example

CmpStruct shortcut

func CmpStruct(t TestingT, got interface{}, model interface{}, expectedFields StructFields, args ...interface{}) bool

CmpStruct is a shortcut for:

td.Cmp(t, got, td.Struct(model, expectedFields), 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 CmpStruct godoc.

Example

Base example

T.Struct shortcut

func (t *T) Struct(got interface{}, model interface{}, expectedFields StructFields, args ...interface{}) bool

Struct is a shortcut for:

t.Cmp(got, td.Struct(model, expectedFields), 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.Struct godoc.

Example

Base example