Skip to content

All assertion methods

bayashi edited this page May 2, 2024 · 9 revisions

Assertion for 1 object

Assertion for boolean value

True

actually.Got(v).True(t)

If v would be a true of boolean type, then it will pass. Otherwise, it will be failed.

False

actually.Got(v).False(t)

If v would be a false of boolean type, then it will pass. Otherwise, it will be failed.


Assertion for nil value

Nil

actually.Got(v).Nil(t)

If v would be a nil, then it will pass.

NotNil

actually.Got(v).NotNil(t)

If v would be other than nil, then it will pass.


Assertion for error

NoError

actually.Got(err).NoError(t)

If err would be nil, then it will pass.

There is a special setter for error value for NoError method: GotError.

actually.GotError(err).NoError(t)

GotError accepts only error type. You can specify more strict on compile.


Assertion for panic

Panic

actually.Got(func(){ panic("OMG") }).Panic(t)

If a func of Got panics, then it will pass. Otherwise, it will be failed.

PanicMessage

actually.Got(func(){ panic("OMG") }).Expect("OMG").PanicMessage(t)

If a func of Got panics, and a value of Expect matches with a message from panic, then it will pass. Otherwise, it will be failed.

NoPanic

actually.Got(func(){}).NoPanic(t)

If a func of Got doesn't panic, then it will pass. Otherwise, it will be failed.


Assertion for string value by regexp

Match

actually.Got("target string").Expect(`.ing$`).Match(t)

If a value of Got would match a value of Expect as regexp, then it will pass. Otherwise, it will be failed.

Values of Got and Expect should be string type, anyway, it's going to be stringified by fmt.Sprint.

NotMatch

actually.Got("target string").Expect(`^[a-z]+$`).NotMatch(t)

If a value of Got would NOT match a value of Expect as regexp, then it will pass. Otherwise, it will be failed. No matter why didn't match these.

Values of Got and Expect should be string type, anyway, it's going to be stringified by fmt.Sprint.


Assertion for length of an object

Len

actually.Got([]int{1, 2}).Expect(2).Len(t)

Len asserts that the specified object has specific length.

Len also fails if the object has a type that len() not accept.


Assertion for 2 objects

Same*

All assertions for 2 objects of Actually have prefix 'Same'.

Same

actually.Got(v).Expect(vv).Same(t)

If v and vv are same, then pass. The 2 objects should be same type. If these are same value, then it passes even if pointer addresses are different.

SameNumber

actually.Got(v).Expect(vv).SameNumber(t)

If v and vv are able to convert and able to assume objects are same value, then it's pass. For example, int32 and int64, or int and float, for kind of numbers.

SamePointer

actually.Got(ptr1).Expect(ptr2).SamePointer(t)

If ptr1 and ptr2 are pointing same address, then pass.

SameType

actually.Got(v).Expect(vv).SameType(t)

If a type of v and a type of vv are same, then pass. No matter values. Just verify only types.


Cmp

actually.Got(v).Expect(vv).Cmp(t)

Cmp method gets the differences between two objects by go-cmp.Diff. If you need to set cmp.Option, then you shoud use CmpOpt(cmp.Option) method before calling Cmp. Cmp method is just a wrapper of go-cmp.Diff. So, it's same that unexported fields are not compared by default; they result in panics unless suppressed by using an Ignore option. It may panic if it cannot compare the values.

CmpAllowUnexported

actually.Got(v).Expect(vv).CmpAllowUnexported(t)

CmpAllowUnexported method gets the differences between two objects by go-cmp.Diff with cmp.AllowUnexported option. It accepts unexported methods to compare instead panic. If you would like to ignore unexported methods, then you can use cmpopts.IgnoreUnexported or some cmpopt's options to ignore.

CmpOpt

actually.Got(v).Expect(vv).CmpOpt(cmpopts.IgnoreFields(Foo{}, "Field")).Cmp(t)

CmpOpt method sets/adds options for Cmp* methods. There is no method to reset cmpOpts. Just set all opts at one time, or add opts.


NotSame*

NotSame

actually.Got(v).Expect(vv).NotSame(t)

If v and vv are NOT same, then pass. If these are same value, then it should be FAIL even if pointer addresses are different.

NotSameNumber

actually.Got(v).Expect(vv).NotSameNumber(t)

If v and vv should be int, uint, or float for kind of numbers. And these should NOT be same values.

NotSamePointer

actually.Got(ptr1).Expect(ptr2).NotSamePointer(t)

If ptr1 and ptr2 should be pointers. But these should NOT be pointing same address.

NotSameType

actually.Got(v).Expect(vv).NotSameType(t)

If a type of v and a type of vv should NOT same, then pass. No matter values. Just verify only types.