Documentation
¶
Overview ¶
Package assert contains functions for making test assertions.
Index ¶
- type Result
- func ContentType(t T, resp *http.Response, want string) Result
- func DeepEqual(t T, label string, want, got any) Result
- func Equal[C comparable](t T, label string, want, got C) Result
- func Error(t T, err error, want string) Result
- func False(t T, label string, got bool) Result
- func NotBlank(t T, label string, got string) Result
- func OK(t T, err error) Result
- func ShouldPanic(t T, f func()) (result Result)
- func SliceEqual[S ~[]E, E comparable](t T, label string, want, got S) Result
- func StatusCode(t T, want int, resp *http.Response) Result
- func True(t T, label string, got bool) Result
- type T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result represents the result of an assertion nad is returned by all of the assertion functions in this package.
func ContentType ¶
ContentType validates that the value of the `Content-Type` header of the provided response matches the desired value.
Example ¶
resp := new(http.Response)
header := http.Header{}
header.Set("Content-Type", "application/json")
resp.Header = header
assert.ContentType(t, resp, "application/json")
assert.ContentType(t, resp, "application/xml")
Output: Expected content type to be application/xml, but got application/json.
func DeepEqual ¶
DeepEqual validates that two values are "deeply equal" according to the same rules as reflect.DeepEqual.
This method is best when comparing e.g. two instances of the same type where you are concerned about the equality of their values and not whether they are the same object in memory. For instance, given the following objects:
type Person struct {
Name string
}
superman1 := Person{Name: "Superman"}
superman2 := Person{Name: "Superman"}
the following assertions are true:
assert.Equal(t, "supermen", superman1, superman2) assert.DeepEqual(t, "supermen", superman1, superman2)
since we are passing the objects by value and they have the same values for their fields. To contrast, of the following assertions:
assert.Equal(t, "superman", &superman1, &superman2) assert.DeepEqual(t, "superman", &superman1, &superman2)
only the DeepEqual assertions succeeds since the call to Equal compares the values of the pointers, which are different for different instances.
Example ¶
type Composer struct {
Name string
}
bach1 := Composer{Name: "J.S. Bach"}
bach2 := Composer{Name: "J.S. Bach"}
shostakovich := Composer{Name: "D. Shostakovich"}
// Objects passed by value are considered identical if the values of their
// fields are identical.
assert.DeepEqual(t, "composers", bach1, bach2)
assert.DeepEqual(t, "composers", bach1, shostakovich)
// Objects passed by reference are ALSO considered identical if the values of
// their fields are identical, regardless of which object is being referenced.
assert.DeepEqual(t, "composers", &bach1, &bach1)
assert.DeepEqual(t, "composers", &bach1, &bach2)
assert.DeepEqual(t, "composers", &bach1, &shostakovich)
Output: Expected composers to be equal, but they weren't. Expected composers to be equal, but they weren't.
func Equal ¶
func Equal[C comparable](t T, label string, want, got C) Result
Equal validates that two values are the same.
This method is best when comparing "simple" types e.g. int, string, etc. and CAN be used to compare two objects passed by value or two objects passed by reference where you care whether the references are the same object. For instance, given the following objects:
type Person struct {
Name string
}
superman1 := Person{Name: "Superman"}
superman2 := Person{Name: "Superman"}
the following assertions are true:
assert.Equal(t, "supermen", superman1, superman2) assert.DeepEqual(t, "supermen", superman1, superman2)
since we are passing the objects by value and they have the same values for their fields. To contrast, of the following assertions:
assert.Equal(t, "superman", &superman1, &superman2) assert.DeepEqual(t, "superman", &superman1, &superman2)
only the DeepEqual assertions succeeds since the call to Equal compares the values of the pointers, which are different for different instances.
Example (ComplexTypes) ¶
type Robot struct {
Name string
}
bender1 := Robot{Name: "Bender"}
bender2 := Robot{Name: "Bender"}
flexo := Robot{Name: "Flexo"}
// Objects passed by value are considered equal if the values of their fields
// are equal.
assert.Equal(t, "robot", bender1, bender2)
assert.Equal(t, "robot", bender1, flexo)
// Objects passed by reference are considered equal iff they refer to the same
// object.
assert.Equal(t, "robot", &bender1, &bender1)
assert.Equal(t, "robot", &bender1, &bender2)
Output: Expected robot to be {Bender}, but got {Flexo}. Expected robot to be &{Bender}, but got &{Bender}.
Example (SimpleTypes) ¶
assert.Equal(t, "int", 42, 42) assert.Equal(t, "int", 42, 13) assert.Equal(t, "string", "Hello, World", "Hello, World") assert.Equal(t, "string", "Hello, World", "Goodbye, World")
Output: Expected int to be 42, but got 13. Expected string to be Hello, World, but got Goodbye, World.
func Error ¶
Error validates that the provided error is not nil and contains the desired string. Note that the comparison is equivalent to strings.Contains, so the following assertion succeeds:
assert.Error(t, errors.New("oops: invalid"), "invalid")
Example ¶
err := errors.New("oops: invalid syntax")
assert.Error(t, err, "oops")
assert.Error(t, err, "invalid syntax")
assert.Error(t, err, "oops: invalid syntax")
assert.Error(t, err, "invalid sintacks")
Output: Expected error to contain "invalid sintacks", but got "oops: invalid syntax".
func False ¶
False validates that the provided value is false.
Example ¶
assert.False(t, "true", true) assert.False(t, "false", false)
Output: Expected true to be false, but got true.
func NotBlank ¶
NotBlank validates that the provided string is not the blank string. Leading and trailing spaces are removed from got before validation.
Example ¶
assert.NotBlank(t, "the blank string", "") assert.NotBlank(t, "only spaces", " ") assert.NotBlank(t, "leading spaces", " Hello") assert.NotBlank(t, "trailing spaces", "World ") assert.NotBlank(t, "no spaces", "Hello")
Output: Expected the blank string to not be blank, but it was. Expected only spaces to not be blank, but it was.
func OK ¶
OK validates that the provided err is nil.
Example ¶
assert.OK(t, nil)
assert.OK(t, errors.New("oops"))
Output: Unexpected error: oops.
func ShouldPanic ¶
ShouldPanic validates that calling f results in a panic. This can be useful when testing methods that panic on error rather than returning a value (and these should be restricted to the types of things called from the main package).
Example ¶
assert.ShouldPanic(t, func() {})
assert.ShouldPanic(t, func() {
panic("oops")
})
Output: Expected function to panic, but it didn't.
func SliceEqual ¶
func SliceEqual[S ~[]E, E comparable](t T, label string, want, got S) Result
SliceEqual validates that two slices are the same. This function does not modify the provided slices in any way, so you may need to sort both inputs prior to comparison.
Example ¶
control := []int{1, 2, 3}
reversed := []int{3, 2, 1}
longer := []int{1, 2, 3, 4}
wildcard := []int{42, 007, 2716057}
assert.SliceEqual(t, "the identity", control, control)
assert.SliceEqual(t, "out of order elements", control, reversed)
assert.SliceEqual(t, "different lengths", control, longer)
assert.SliceEqual(t, "different elements", control, wildcard)
assert.SliceEqual(t, "struct elements",
[]struct{ Value int }{{1}, {2}, {3}},
[]struct{ Value int }{{1}, {2}, {3}},
)
assert.SliceEqual(t, "struct elements",
[]struct{ Value int }{{1}, {2}, {3}},
[]struct{ Value int }{{3}, {1}, {2}},
)
Output: Expected out of order elements to be [1 2 3], but got [3 2 1]. Expected different lengths to be [1 2 3], but got [1 2 3 4]. Expected different elements to be [1 2 3], but got [42 7 2716057]. Expected struct elements to be [{1} {2} {3}], but got [{3} {1} {2}].
func StatusCode ¶
StatusCode validates that the status code of the provided response matches the desired value.
Example ¶
resp := new(http.Response) resp.StatusCode = 200 assert.StatusCode(t, http.StatusOK, resp) assert.StatusCode(t, http.StatusTeapot, resp)
Output: Expected status code to be 418, but got 200.
func True ¶
True validates that the provided value is true.
Example ¶
assert.True(t, "true", true) assert.True(t, "false", false)
Output: Expected false to be true, but got false.
func (Result) Fatal ¶
func (r Result) Fatal()
Fatal causes the test suite to immediately fail if the current result corresponds to a failed assertion. You can chain this off of any of the assertion functions and is most often useful for exiting due to an unexpected error, e.g.:
assert.OK(t, err).Fatal()