Documentation
¶
Overview ¶
Package assertions contains the implementations for all assertions which are referenced in goconvey's `convey` package (github.com/smartystreets/goconvey/convey) and gunit (github.com/smartystreets/gunit) for use with the So(...) method. They can also be used in traditional Go test functions and even in applications.
Many of the assertions lean heavily on work done by Aaron Jacobs in his excellent oglematchers library. (https://github.com/jacobsa/oglematchers) The ShouldResemble assertion leans heavily on work done by Daniel Jacques in his very helpful go-render library. (https://github.com/luci/go-render)
Index ¶
- func GoConveyMode(yes bool)
- func ShouldAlmostEqual(actual any, expected ...any) string
- func ShouldBeBetween(actual any, expected ...any) string
- func ShouldBeBetweenOrEqual(actual any, expected ...any) string
- func ShouldBeBlank(actual any, expected ...any) string
- func ShouldBeChronological(actual any, expected ...any) string
- func ShouldBeEmpty(actual any, expected ...any) string
- func ShouldBeError(actual any, expected ...any) string
- func ShouldBeFalse(actual any, expected ...any) string
- func ShouldBeGreaterThan(actual any, expected ...any) string
- func ShouldBeGreaterThanOrEqualTo(actual any, expected ...any) string
- func ShouldBeIn(actual any, expected ...any) string
- func ShouldBeLessThan(actual any, expected ...any) string
- func ShouldBeLessThanOrEqualTo(actual any, expected ...any) string
- func ShouldBeNil(actual any, expected ...any) string
- func ShouldBeTrue(actual any, expected ...any) string
- func ShouldBeZeroValue(actual any, expected ...any) string
- func ShouldContain(actual any, expected ...any) string
- func ShouldContainKey(actual any, expected ...any) string
- func ShouldContainSubstring(actual any, expected ...any) string
- func ShouldEndWith(actual any, expected ...any) string
- func ShouldEqual(actual any, expected ...any) string
- func ShouldEqualJSON(actual any, expected ...any) string
- func ShouldEqualTrimSpace(actual any, expected ...any) string
- func ShouldEqualWithout(actual any, expected ...any) string
- func ShouldHappenAfter(actual any, expected ...any) string
- func ShouldHappenBefore(actual any, expected ...any) string
- func ShouldHappenBetween(actual any, expected ...any) string
- func ShouldHappenOnOrAfter(actual any, expected ...any) string
- func ShouldHappenOnOrBefore(actual any, expected ...any) string
- func ShouldHappenOnOrBetween(actual any, expected ...any) string
- func ShouldHappenWithin(actual any, expected ...any) string
- func ShouldHaveLength(actual any, expected ...any) string
- func ShouldHaveSameTypeAs(actual any, expected ...any) string
- func ShouldImplement(actual any, expectedList ...any) string
- func ShouldNotAlmostEqual(actual any, expected ...any) string
- func ShouldNotBeBetween(actual any, expected ...any) string
- func ShouldNotBeBetweenOrEqual(actual any, expected ...any) string
- func ShouldNotBeBlank(actual any, expected ...any) string
- func ShouldNotBeChronological(actual any, expected ...any) string
- func ShouldNotBeEmpty(actual any, expected ...any) string
- func ShouldNotBeIn(actual any, expected ...any) string
- func ShouldNotBeNil(actual any, expected ...any) string
- func ShouldNotBeZeroValue(actual any, expected ...any) string
- func ShouldNotContain(actual any, expected ...any) string
- func ShouldNotContainKey(actual any, expected ...any) string
- func ShouldNotContainSubstring(actual any, expected ...any) string
- func ShouldNotEndWith(actual any, expected ...any) string
- func ShouldNotEqual(actual any, expected ...any) string
- func ShouldNotHappenOnOrBetween(actual any, expected ...any) string
- func ShouldNotHappenWithin(actual any, expected ...any) string
- func ShouldNotHaveSameTypeAs(actual any, expected ...any) string
- func ShouldNotImplement(actual any, expectedList ...any) string
- func ShouldNotPanic(actual any, expected ...any) (message string)
- func ShouldNotPanicWith(actual any, expected ...any) (message string)
- func ShouldNotPointTo(actual any, expected ...any) string
- func ShouldNotResemble(actual any, expected ...any) string
- func ShouldNotStartWith(actual any, expected ...any) string
- func ShouldPanic(actual any, expected ...any) (message string)
- func ShouldPanicWith(actual any, expected ...any) (message string)
- func ShouldPointTo(actual any, expected ...any) string
- func ShouldResemble(actual any, expected ...any) string
- func ShouldStartWith(actual any, expected ...any) string
- func ShouldWrap(actual any, expected ...any) string
- func So(actual any, assert SoFunc, expected ...any) (bool, string)
- type Assertion
- type FailureView
- type Serializer
- type SoFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GoConveyMode ¶
func GoConveyMode(yes bool)
GoConveyMode provides control over JSON serialization of failures. When using the assertions in this package from the convey package JSON results are very helpful and can be rendered in a DIFF view. In that case, this function will be called with a true value to enable the JSON serialization. By default, the assertions in this package will not serializer a JSON result, making standalone usage more convenient.
func ShouldAlmostEqual ¶
ShouldAlmostEqual makes sure that two parameters are close enough to being equal. The acceptable delta may be specified with a third argument, or a very small default delta will be used.
func ShouldBeBetween ¶
ShouldBeBetween receives exactly three parameters: an actual value, a lower bound, and an upper bound. It ensures that the actual value is between both bounds (but not equal to either of them).
func ShouldBeBetweenOrEqual ¶
ShouldBeBetweenOrEqual receives exactly three parameters: an actual value, a lower bound, and an upper bound. It ensures that the actual value is between both bounds or equal to one of them.
func ShouldBeBlank ¶
ShouldBeBlank receives exactly 1 string parameter and ensures that it is equal to "".
func ShouldBeChronological ¶
ShouldBeChronological receives a []time.Time slice and asserts that they are in chronological order starting with the first time.Time as the earliest.
func ShouldBeEmpty ¶
ShouldBeEmpty receives a single parameter (actual) and determines whether calling len(actual) would return `0`. It obeys the rules specified by the len function for determining length: http://golang.org/pkg/builtin/#len
func ShouldBeError ¶
ShouldBeError asserts that the first argument implements the error interface. It also compares the first argument against the second argument if provided (which must be an error message string or another error value).
func ShouldBeFalse ¶
ShouldBeFalse receives a single parameter and ensures that it is false.
func ShouldBeGreaterThan ¶
ShouldBeGreaterThan receives exactly two parameters and ensures that the first is greater than the second.
func ShouldBeGreaterThanOrEqualTo ¶
ShouldBeGreaterThanOrEqualTo receives exactly two parameters and ensures that the first is greater than or equal to the second.
func ShouldBeIn ¶
ShouldBeIn receives at least 2 parameters. The first is a proposed member of the collection that is passed in either as the second parameter, or of the collection that consists of all the remaining parameters. This assertion ensures that the proposed member is in the collection (using ShouldEqual).
func ShouldBeLessThan ¶
ShouldBeLessThan receives exactly two parameters and ensures that the first is less than the second.
func ShouldBeLessThanOrEqualTo ¶
ShouldBeLessThanOrEqualTo receives exactly two parameters and ensures that the first is less than or equal to the second.
func ShouldBeNil ¶
ShouldBeNil receives a single parameter and ensures that it is nil.
func ShouldBeTrue ¶
ShouldBeTrue receives a single parameter and ensures that it is true.
func ShouldBeZeroValue ¶
ShouldBeZeroValue receives a single parameter and ensures that it is the Go equivalent of the default value, or "zero" value.
func ShouldContain ¶
ShouldContain receives exactly two parameters. The first is a slice and the second is a proposed member. Membership is determined using ShouldEqual.
func ShouldContainKey ¶
ShouldContainKey receives exactly two parameters. The first is a map and the second is a proposed key. Keys are compared with a simple '=='.
func ShouldContainSubstring ¶
ShouldContainSubstring receives exactly 2 string parameters and ensures that the first contains the second as a substring.
func ShouldEndWith ¶
ShouldEndWith receives exactly 2 string parameters and ensures that the first ends with the second.
func ShouldEqual ¶
ShouldEqual receives exactly two parameters and does an equality check using the following semantics: 1. If the expected and actual values implement an Equal method in the form `func (this T) Equal(that T) bool` then call the method. If true, they are equal. 2. The expected and actual values are judged equal or not by oglematchers.Equals.
func ShouldEqualJSON ¶
ShouldEqualJSON receives exactly two parameters and does an equality check by marshalling to JSON
func ShouldEqualTrimSpace ¶
ShouldEqualTrimSpace receives exactly 2 string parameters and ensures that the first is equal to the second after removing all leading and trailing whitespace using strings.TrimSpace(first).
func ShouldEqualWithout ¶
ShouldEqualWithout receives exactly 3 string parameters and ensures that the first is equal to the second after removing all instances of the third from the first using strings.Replace(first, third, "", -1).
func ShouldHappenAfter ¶
ShouldHappenAfter receives exactly 2 time.Time arguments and asserts that the first happens after the second.
func ShouldHappenBefore ¶
ShouldHappenBefore receives exactly 2 time.Time arguments and asserts that the first happens before the second.
func ShouldHappenBetween ¶
ShouldHappenBetween receives exactly 3 time.Time arguments and asserts that the first happens between (not on) the second and third.
func ShouldHappenOnOrAfter ¶
ShouldHappenOnOrAfter receives exactly 2 time.Time arguments and asserts that the first happens on or after the second.
func ShouldHappenOnOrBefore ¶
ShouldHappenOnOrBefore receives exactly 2 time.Time arguments and asserts that the first happens on or before the second.
func ShouldHappenOnOrBetween ¶
ShouldHappenOnOrBetween receives exactly 3 time.Time arguments and asserts that the first happens between or on the second and third.
func ShouldHappenWithin ¶
ShouldHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 arguments) and asserts that the first time.Time happens within or on the duration specified relative to the other time.Time.
func ShouldHaveLength ¶
ShouldHaveLength receives 2 parameters. The first is a collection to check the length of, the second being the expected length. It obeys the rules specified by the len function for determining length: http://golang.org/pkg/builtin/#len
func ShouldHaveSameTypeAs ¶
ShouldHaveSameTypeAs receives exactly two parameters and compares their underlying types for equality.
func ShouldImplement ¶
ShouldImplement receives exactly two parameters and ensures that the first implements the interface type of the second.
func ShouldNotAlmostEqual ¶
ShouldNotAlmostEqual is the inverse of ShouldAlmostEqual
func ShouldNotBeBetween ¶
ShouldNotBeBetween receives exactly three parameters: an actual value, a lower bound, and an upper bound. It ensures that the actual value is NOT between both bounds.
func ShouldNotBeBetweenOrEqual ¶
ShouldNotBeBetweenOrEqual receives exactly three parameters: an actual value, a lower bound, and an upper bound. It ensures that the actual value is nopt between the bounds nor equal to either of them.
func ShouldNotBeBlank ¶
ShouldNotBeBlank receives exactly 1 string parameter and ensures that it is not equal to "".
func ShouldNotBeChronological ¶
ShouldNotBeChronological receives a []time.Time slice and asserts that they are NOT in chronological order.
func ShouldNotBeEmpty ¶
ShouldNotBeEmpty receives a single parameter (actual) and determines whether calling len(actual) would return a value greater than zero. It obeys the rules specified by the `len` function for determining length: http://golang.org/pkg/builtin/#len
func ShouldNotBeIn ¶
ShouldNotBeIn receives at least 2 parameters. The first is a proposed member of the collection that is passed in either as the second parameter, or of the collection that consists of all the remaining parameters. This assertion ensures that the proposed member is NOT in the collection (using ShouldEqual).
func ShouldNotBeNil ¶
ShouldNotBeNil receives a single parameter and ensures that it is not nil.
func ShouldNotBeZeroValue ¶
ShouldNotBeZeroValue receives a single parameter and ensures that it is NOT the Go equivalent of the default value, or "zero" value.
func ShouldNotContain ¶
ShouldNotContain receives exactly two parameters. The first is a slice and the second is a proposed member. Membership is determined using ShouldEqual.
func ShouldNotContainKey ¶
ShouldNotContainKey receives exactly two parameters. The first is a map and the second is a proposed absent key. Keys are compared with a simple '=='.
func ShouldNotContainSubstring ¶
ShouldNotContainSubstring receives exactly 2 string parameters and ensures that the first does NOT contain the second as a substring.
func ShouldNotEndWith ¶
ShouldNotEndWith receives exactly 2 string parameters and ensures that the first does not end with the second.
func ShouldNotEqual ¶
ShouldNotEqual receives exactly two parameters and does an inequality check. See ShouldEqual for details on how equality is determined.
func ShouldNotHappenOnOrBetween ¶
ShouldNotHappenOnOrBetween receives exactly 3 time.Time arguments and asserts that the first does NOT happen between or on the second or third.
func ShouldNotHappenWithin ¶
ShouldNotHappenWithin receives a time.Time, a time.Duration, and a time.Time (3 arguments) and asserts that the first time.Time does NOT happen within or on the duration specified relative to the other time.Time.
func ShouldNotHaveSameTypeAs ¶
ShouldNotHaveSameTypeAs receives exactly two parameters and compares their underlying types for inequality.
func ShouldNotImplement ¶
ShouldNotImplement receives exactly two parameters and ensures that the first does NOT implement the interface type of the second.
func ShouldNotPanic ¶
ShouldNotPanic receives a void, niladic function and expects to execute the function without any panic.
func ShouldNotPanicWith ¶
ShouldNotPanicWith receives a void, niladic function and expects to recover a panic whose content differs from the second argument. If the expected value is an error and the recovered value is an error, errors.Is will be used to compare them.
func ShouldNotPointTo ¶
ShouldNotPointTo receives exactly two parameters and checks to see that they point to different addresess.
func ShouldNotResemble ¶
ShouldNotResemble receives exactly two parameters and does an inverse deep equal check (see reflect.DeepEqual)
func ShouldNotStartWith ¶
ShouldNotStartWith receives exactly 2 string parameters and ensures that the first does not start with the second.
func ShouldPanic ¶
ShouldPanic receives a void, niladic function and expects to recover a panic.
func ShouldPanicWith ¶
ShouldPanicWith receives a void, niladic function and expects to recover a panic with the second argument as the content. If the expected value is an error and the recovered value is an error, errors.Is will be used to compare them.
func ShouldPointTo ¶
ShouldPointTo receives exactly two parameters and checks to see that they point to the same address.
func ShouldResemble ¶
ShouldResemble receives exactly two parameters and does a deep equal check (see reflect.DeepEqual)
func ShouldStartWith ¶
ShouldStartWith receives exactly 2 string parameters and ensures that the first starts with the second.
func ShouldWrap ¶ added in v1.1.0
ShouldWrap asserts that the first argument (which must be an error value) 'wraps' the second/final argument (which must also be an error value). It relies on errors.Is to make the determination (https://golang.org/pkg/errors/#Is).
func So ¶
So is a convenience function (as opposed to an inconvenience function?) for running assertions on arbitrary arguments in any context, be it for testing or even application logging. It allows you to perform assertion-like behavior (and get nicely formatted messages detailing discrepancies) but without the program blowing up or panicking. All that is required is to import this package and call `So` with one of the assertions exported by this package as the second parameter. The first return parameter is a boolean indicating if the assertion was true. The second return parameter is the well-formatted message showing why an assertion was incorrect, or blank if the assertion was correct.
Example:
if ok, message := So(x, ShouldBeGreaterThan, y); !ok { log.Println(message) }
For an alternative implementation of So (that provides more flexible return options) see the `So` function in the package at github.com/smartystreets/assertions/assert.
Types ¶
type Assertion ¶
type Assertion struct {
// contains filtered or unexported fields
}
func New ¶
func New(t testingT) *Assertion
New swallows the *testing.T struct and prints failed assertions using t.Error. Example: assertions.New(t).So(1, should.Equal, 1)
type FailureView ¶
type FailureView struct { Message string `json:"Message"` Expected string `json:"Expected"` Actual string `json:"Actual"` }
FailureView is also declared in github.com/smartystreets/goconvey/convey/reporting. The json struct tags should be equal in both declarations.
type Serializer ¶
type Serializer interface {
// contains filtered or unexported methods
}
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
go-diff/diffmatchpatch
Package diffmatchpatch offers robust algorithms to perform the operations required for synchronizing plain text.
|
Package diffmatchpatch offers robust algorithms to perform the operations required for synchronizing plain text. |
oglematchers
Package oglematchers provides a set of matchers useful in a testing or mocking framework.
|
Package oglematchers provides a set of matchers useful in a testing or mocking framework. |
unit
package unit implements a light-weight x-Unit style testing framework.
|
package unit implements a light-weight x-Unit style testing framework. |
Package should is simply a rewording of the assertion functions in the assertions package.
|
Package should is simply a rewording of the assertion functions in the assertions package. |