{"modulePath":"github.com/lainio/err2","version":"v1.4.2","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package assert\n\nPackage assert includes runtime assertion helpers both for normal execution as well as a assertion package for Go's testing. What makes solution unique is its capable to support both modes with the same API. Only thing you need to do is to add a [PushTester](#PushTester) line at the beginning of your unit tests and its sub-gouroutines:\n\n\tfunc TestInvite(t *testing.T) {\n\t     defer assert.PushTester(t)() // push testing variable t beginning of any test\n\n\t     //                 v-----v Invite's control flow includes assertions\n\t     alice.Node = root1.Invite(alice.Node, root1.Key, alice.PubKey, 1)\n\t     assert.Equal(alice.Len(), 1) // assert anything normally\n\t     ...\n\t     go func() {\n\t          defer assert.PushTester(t)() // Register and restore in each goroutine.\n\n### Merge Runtime And Unit Test Assertions {#hdr-Merge_Runtime_And_Unit_Test_Assertions}\n\nThe next block is the actual Invite function's first two lines. Even if the assertion line is written more from a runtime detection point of view, it catches all assert violations in the unit tests as well:\n\n\tfunc (c Chain) Invite(...) {\n\t     assert.That(c.isLeaf(invitersKey), \"only leaf can invite\")\n\nIf some assertion violation occurs in the deep call stack, they are still reported as test failures. See the above code blocks. If assertion failure happens somewhere inside the Invite function's call stack, it's still reported correctly as a test failure of the TestInvite unit test. It doesn't matter how deep the recursion is or if parallel test runs are performed. The failure report includes all the locations of the meaningful call stack steps. See the next chapter.\n\n### Use Asserts To Return Sentinel Error Values {#hdr-Use_Asserts_To_Return_Sentinel_Error_Values}\n\nWe saw how same assertions can be used for app running and for automatic tests. There is a third way. If your function needs to report error values and you want to make your code easier to skim, i.e, less if statements, you can use asserts.\n\nThe following code block illustrates the behavior:\n\n\tfunc (p Path) ProveWithGetBKID(getBKID getBackupKey) (err error) {\n\t     defer err2.Handle(\u0026err)\n\n\t     assert.Equal(p.FirstEdge().Body.Version, EdgeVersion, ErrUnsupportedVersion)\n\t     assert.Equal(p.FirstEdge().Body.Prev, AnchorDigest(), ErrWrongAnchor)\n\nNow the caller can check the actual error values and decide what to do with a specific error.\n\nNote that if you want to support different asserter types you should guide your function callers to use [errors.Is](/errors#Is) for error value checking. In cases where if statements are preferred it's good to prefix your functions like this:\n\n\tfunc (p Path) ProveWithGetBKID(getBKID getBackupKey) (err error) {\n\t     defer assert.PushAsserter(assert.Plain)() // no assert annotations\n\t     defer err2.Handle(\u0026err, nil)              // no error annotations\n\nMore information can be found from [assert.That](#That) examples.\n\n### Call Stack Traversal During Tests {#hdr-Call_Stack_Traversal_During_Tests}\n\nThe Assert package allows us to track assertion violations over the package and module boundaries. When an assertion fails during the unit testing, the whole call stack is brought to unit test logs. And some help with your IDE, such as transferring output to a location list in Neovim/Vim. For example, you can find a compatible test result parser for Neovim from this plugin [nvim-go](https://github.com/lainio/nvim-go) (fork).\n\nThe call stack traversal has proven to be very valuable for package and module development in Go, especially when following TDD and fast development feedback cycles.\n\n### Why Runtime Asserts Are So Important? {#hdr-Why_Runtime_Asserts_Are_So_Important_}\n\nInstead of mocking or other mechanisms we can integrate our preconditions and raise up quality of our software.\n\n\t\"Assertions are active comments\"\n\nThe assert package offers a convenient way to set preconditions to code which allow us detect programming errors and API violations faster. Still allowing production-time error handling if needed. And everything is automatic. You can set a goroutine specific [Asserter](#Asserter) with [PushAsserter](#PushAsserter) function.\n\nThe assert package's default [Asserter](#Asserter) you can set with [SetDefault](#SetDefault) or -asserter flag if Go's flag package (or similar) is in use. This allows developer, operator and every-day user share the exact same binary but get the error messages and diagnostic they need.\n\n\t// Production asserter adds formatted caller info to normal errors.\n\t// Information is transported thru error values when err2.Handle is in use.\n\tassert.SetDefault(assert.Production)\n\nPlease see the code examples for more information.\n\nNote that if an [Asserter](#Asserter) is set for a goroutine level, it cannot be changed with the -asserter flag or [SetDefault](#SetDefault). The GLS [Asserter](#Asserter) is used for a reason, so it's good that even a unit test asserter won't override it in those cases.\n\n### Flag Package Support {#hdr-Flag_Package_Support}\n\nThe assert package supports Go's flags. All you need to do is to call [flag.Parse](/flag#Parse). And the following flags are supported (=\"default-value\"):\n\n\t-asserter=\"Prod\"\n\t    A name of the asserter Plain, Prod, Dev, Debug\n\t    See more information from constants: Plain, Production, Development, Debug\n\nAnd assert package's configuration flags are inserted.\n\n### Performance {#hdr-Performance}\n\nThe performance of the assert functions are equal to the if-statement thanks for inlining. All of the generics-based versions are the equally fast!\n\nWe should prefer specialized versions like [assert.Equal](#Equal) that we get precise and readable error messages automatically. Error messagas follow Go idiom of 'got xx, want yy'. And we still can annotate error message if we want.\n\n### Naming {#hdr-Naming}\n\nBecause performance has been number one requirement and Go's generics cannot discrete slices, maps and channels we have used naming prefixes accordingly: S = slice, M = map, C = channel. No prefix is (currently) for the string type.\n\n## Functions\n\n```go\nfunc CLen[C ~chan T, T any](obj C, length int, a ...any)\n```\nCLen asserts that the length of the chan is equal to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc CLonger[C ~chan T, T any](obj C, length int, a ...any)\n```\nCLonger asserts that the length of the chan is longer to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc CNil[C ~chan T, T any](c C, a ...any)\n```\nCNil asserts that the channel is nil. If it is not it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc CNotNil[C ~chan T, T any](c C, a ...any)\n```\nCNotNil asserts that the channel is not nil. If it is it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc CShorter[C ~chan T, T any](obj C, length int, a ...any)\n```\nCShorter asserts that the length of the chan is shorter to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc DeepEqual(val, want any, a ...any)\n```\nDeepEqual asserts that the (whatever) values are equal. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc Empty(obj string, a ...any)\n```\nEmpty asserts that the string is empty. If it is NOT, it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc Equal[T comparable](val, want T, a ...any)\n```\nEqual asserts that the values are equal. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc Error(err error, a ...any)\n```\nError asserts that the err is not nil. If it is it panics and builds a violation message. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc ErrorIs(err, target error, a ...any)\n```\nErrorIs asserts that [errors.Is](/errors#Is)(err, target) is true. If it isn't it panics and builds a violation message. Both err and target may be nil.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc ErrorIsNot(err, target error, a ...any)\n```\nErrorIsNot asserts that [errors.Is](/errors#Is)(err, target) is false. If it isn't it panics and builds a violation message. Both err and target may be nil.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc Greater[T Number](val, want T, a ...any)\n```\nGreater asserts that the value is greater than want. If it is not it panics and builds a violation message. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc INil(i any, a ...any)\n```\nINil asserts that the interface value IS nil. If it is it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote, use this only for real interface types. Go's interface's has two values so this won't work e.g. slices! Read more information about [the interface type](https://go.dev/doc/faq#nil_error).\n\n```go\nfunc INotNil(i any, a ...any)\n```\nINotNil asserts that the interface value is NOT nil. If it is it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote, use this only for real interface types. Go's interface's has two values so this won't work e.g. slices! Read more information about [the interface type](https://go.dev/doc/faq#nil_error).\n\n```go\nfunc Len(obj string, length int, a ...any)\n```\nLen asserts that the length of the string is equal to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc Less[T Number](val, want T, a ...any)\n```\nLess asserts that the value is less than want. If it is not it panics and builds a violation message. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc Longer(s string, length int, a ...any)\n```\nLonger asserts that the length of the string is longer to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc MEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any)\n```\nMEmpty asserts that the map is empty. If it is NOT, it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc MKeyExists[M ~map[T]U, T comparable, U any](\n\tobj M,\n\tkey T,\n\ta ...any,\n) (val U)\n```\nMKeyExists asserts that the map key exists. If not it panics/errors (current [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc MKeyNotExists[M ~map[T]U, T comparable, U any](\n\tobj M,\n\tkey T,\n\ta ...any,\n)\n```\nMKeyNotExists asserts that the map key NOT exists. If not it panics/errors (current [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc MLen[M ~map[T]U, T comparable, U any](obj M, length int, a ...any)\n```\nMLen asserts that the length of the map is equal to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc MLonger[M ~map[T]U, T comparable, U any](obj M, length int, a ...any)\n```\nMLonger asserts that the length of the map is longer to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc MNil[M ~map[T]U, T comparable, U any](m M, a ...any)\n```\nMNil asserts that the map is nil. If it is not it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc MNotEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any)\n```\nMNotEmpty asserts that the map is not empty. If it is, it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc MNotNil[M ~map[T]U, T comparable, U any](m M, a ...any)\n```\nMNotNil asserts that the map is not nil. If it is it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc MShorter[M ~map[T]U, T comparable, U any](obj M, length int, a ...any)\n```\nMShorter asserts that the length of the map is shorter to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc Nil[T any](p *T, a ...any)\n```\nNil asserts that the pointer IS nil. If it is not it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc NoError(err error, a ...any)\n```\nNoError asserts that the error is nil. If is not it panics with the given formatting string. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote. We recommend that you prefer [github.com/lainio/err2/try.To](/github.com/lainio/err2/try#To). They work exactly the same during the test runs and you can use the same code for both: runtime and tests. However, there are cases that you want assert that there is no error in cases where fast fail and immediate stop of execution is wanted at runtime. With asserts ([Production](#Production), [Development](#Development), [Debug](#Debug)) you get the file location as well.\n\n```go\nfunc NotDeepEqual(val, want any, a ...any)\n```\nNotDeepEqual asserts that the (whatever) values are equal. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote, it uses reflect.DeepEqual which means that also the types must be the same:\n\n\tassert.DeepEqual(pubKey, ed25519.PublicKey(pubKeyBytes))\n\n```go\nfunc NotEmpty(obj string, a ...any)\n```\nNotEmpty asserts that the string is not empty. If it is, it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc NotEqual[T comparable](val, want T, a ...any)\n```\nNotEqual asserts that the values aren't equal. If they are it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote, when [Asserter](#Asserter) is [Plain](#Plain), optional arguments are used to build a new assert violation message.\n\n```go\nfunc NotImplemented(a ...any)\n```\nNotImplemented always panics with 'not implemented' assertion message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc NotNil[P ~*T, T any](p P, a ...any)\n```\nNotNil asserts that the pointer IS NOT nil. If it is it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc NotZero[T Number](val T, a ...any)\n```\nNotZero asserts that the value != 0. If it is not it panics and builds a violation message. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc PopAsserter()\n```\nPopAsserter pops current gorounine specific [Asserter](#Asserter) from packages memory. Gorounine specific [Asserter](#Asserter) can be set with [PushAsserter](#PushAsserter).\n\nWhen gorounine [Asserter](#Asserter) isn't set package's default [Asserter](#Asserter) is used. See [SetDefault](#SetDefault) for more information.\n\n```go\nfunc PopTester()\n```\nPopTester removes the tester registration for the current goroutine.\n\nNOTE that the legacy usage does not provide nested restoration! If you are using t.Parallel or goroutines you should always use:\n\n\tdefer assert.PushTester(t)()\n\nThe deprecated legacy usage:\n\n\tassert.PushTester(t)\n\tdefer assert.PopTester()\n\nWhen deferred directly, PopTester reports panics through the current tester and removes the registration even if reporting calls FailNow.\n\nDeprecated: Use the restoration function returned by PushTester.\n\n```go\nfunc SEmpty[S ~[]T, T any](obj S, a ...any)\n```\nSEmpty asserts that the slice is empty. If it is NOT, it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc SLen[S ~[]T, T any](obj S, length int, a ...any)\n```\nSLen asserts that the length of the slice is equal to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc SLonger[S ~[]T, T any](obj S, length int, a ...any)\n```\nSLonger asserts that the length of the slice is equal to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc SNil[S ~[]T, T any](s S, a ...any)\n```\nSNil asserts that the slice IS nil. If it is it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc SNotEmpty[S ~[]T, T any](obj S, a ...any)\n```\nSNotEmpty asserts that the slice is not empty. If it is, it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc SNotNil[S ~[]T, T any](s S, a ...any)\n```\nSNotNil asserts that the slice is not nil. If it is it panics/errors (default [Asserter](#Asserter)) the auto-generated (args appended) message.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc SShorter[S ~[]T, T any](obj S, length int, a ...any)\n```\nSShorter asserts that the length of the slice is equal to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc Shorter(str string, length int, a ...any)\n```\nShorter asserts that the length of the string is shorter to the given. If not it panics/errors (according the current [Asserter](#Asserter)) with the auto-generated message. You can append the generated got-want message by using optional message arguments.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\nNote! This is reasonably fast but not as fast as [That](#That) because of lacking inlining for the current implementation of Go's type parametric functions.\n\n```go\nfunc That(term bool, a ...any)\n```\nThat asserts that the term is true. If not it panics with the given formatting string. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc ThatNot(term bool, a ...any)\n```\nThatNot asserts that the term is NOT true. If is it panics with the given formatting string. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n```go\nfunc Zero[T Number](val T, a ...any)\n```\nZero asserts that the value is 0. If it is not it panics and builds a violation message. Thanks to inlining, the performance penalty is equal to a single 'if-statement' that is almost nothing.\n\nNote that when [Plain](#Plain) [Asserter](#Asserter) is used ([PushAsserter](#PushAsserter) or even [SetDefault](#SetDefault)), optional arguments are used to override the auto-generated assert violation message.\n\n## Types\n\n```go\ntype Asserter = uint32\n```\n\n```go\nconst (\n\tPlain Asserter = 0 + iota\n\tProduction\n\tDevelopment\n\tTest\n\tTestFull\n\tDebug\n)\n```\nAsserters are the way to set what kind of messages assert package outputs if assertion is violated.\n\n[Plain](#Plain) converts asserts just plain K\u0026D error messages without extra information. That's useful for apps that want to use assert package to validate e.g. command fields:\n\n\tassert.NotEmpty(c.PoolName, \"pool name cannot be empty\")\n\nNote that Plain is only [Asserter](#Asserter) that override auto-generated assertion messages with given arguments like 'pool name cannot be empty'. Others add given arguments at the end of the auto-generated assert message. For that reason Plain is useful when you want to return sentinel error values with asserts and you don't need any extra information to be wrapped to an error value:\n\n\tassert.NotEmpty(c.PoolName, ErrMissingPoolName)\n\n[Production](#Production) (pkg's default) is the best [Asserter](#Asserter) for most cases. The assertion violations are treated as Go error values. And only a pragmatic caller info is included into the error values like source filename, line number, and caller function, all in one line:\n\n\tcopy file: main.go:37: CopyFile(): assertion violation: string shouldn't be empty\n\n[Development](#Development) is the best [Asserter](#Asserter) for development use. The assertion violations are treated as Go error values. And a formatted caller info is included to the error message like source filename , line number, and caller function. Everything in a noticeable multi-line message:\n\n\t--------------------------------\n\tAssertion Fault at:\n\tmain.go:37 CopyFile():\n\tassertion violation: string shouldn't be empty\n\t--------------------------------\n\n[Test](#Test) minimalistic [Asserter](#Asserter) for unit test use. More pragmatic is the [TestFull](#TestFull) [Asserter](#Asserter) (test default).\n\nUse this [Asserter](#Asserter) if your IDE/editor doesn't support full file names and it relies a relative path (Go standard). You can use this also if you need temporary problem solving for your programming environment.\n\n[TestFull](#TestFull) [Asserter](#Asserter) (test default). The TestFull [Asserter](#Asserter) includes the caller info and the call stack for unit testing, similarly like err2's error traces. Note that [PushTester](#PushTester) set's TestFull if it's not yet set.\n\nThe call stack produced by the test asserts can be used over Go module boundaries. For example, if your app and it's sub packages both use err2/assert for unit testing and runtime checks, the runtime assertions will be automatically converted to test asserts. If any of the runtime asserts of the sub packages fails during the app tests, the app test fails as well.\n\nNote that the cross-module assertions produce long file names (path included), and some of the Go test result parsers cannot handle that. A proper test result parser like [nvim-go](https://github.com/lainio/nvim-go) (fork) works very well. Also most of the make result parsers can process the output properly and allow traverse of locations of the error trace.\n\n[Debug](#Debug) [Asserter](#Asserter) transforms assertion violations to panic calls where panic object's type is string, i.e., err2 package treats it as a normal panic, not an error.\n\nFor example, the pattern that e.g. Go's standard library uses:\n\n\tif p == nil {\n\t     panic(\"pkg: ptr cannot be nil\")\n\t}\n\nis equal to:\n\n\tassert.NotNil(p)\n\n```go\nfunc SetDefault(i Asserter) (old Asserter)\n```\nSetDefault sets the new default [Asserter](#Asserter) for the assert pkg instance you're currently using. It also returns the previous [Asserter](#Asserter). The default asserter is [Production](#Production) that's best for most use cases and packages.\n\nNote that for most cases [PushAsserter](#PushAsserter) is most suitable. [PushAsserter](#PushAsserter) allows you to set [Asserter](#Asserter) per goroutine or function, i.e., until you pop the asserter out, and the assert package uses the current default [Asserter](#Asserter) set by [SetDefault](#SetDefault).\n\nNote that if you are using tracers you might get overlapping call stacks, so test what's best for your case.\n\n```go\ntype Function = func()\n```\n\n```go\nfunc PushAsserter(i Asserter) (retFn Function)\n```\nPushAsserter set [Asserter](#Asserter) for the current GLS (Goroutine Local Storage). That allows us to have multiple different [Asserter](#Asserter) in use in the same process.\n\nWhen you want to return plain error messages or sentinel error values, you should use following in the top-level function:\n\n\tdefer assert.PushAsserter(assert.Plain)()\n\nThe [Asserter](#Asserter) Plain prevents asserts wrapping extra information to an error value.\n\nNote that [github.com/lainio/err2.Handle](/github.com/lainio/err2#Handle) is capable to annotate errors automatically. You can prevent that using nil in the optional arguments:\n\n\tdefer err2.Handle(\u0026err, nil)\n\n```go\nfunc PushTester(t testing.TB, a ...Asserter) Function\n```\nPushTester registers t for the current goroutine and returns a function that restores its previous testing context. The recommended usage is:\n\n\tdefer assert.PushTester(t)()\n\nNested calls temporarily replace the tester. Balance each push with its restoration function in LIFO order on the same goroutine. Restoring the outermost scope removes the registration.\n\nWhen deferred directly, the returned function catches panics, reports them through the failing scope's tester, and restores the previous context even when reporting calls FailNow.\n\nRegistrations are not inherited by goroutines, including t.Run and t.Parallel subtests. Each must register and restore its own testing context.\n\nAn optional second argument changes the process-wide default [Asserter](#Asserter), just like [SetDefault](#SetDefault); restoration does not undo that change. Without this argument, PushTester selects [TestFull](#TestFull) if the effective asserter is not a testing asserter. A goroutine's [PushAsserter](#PushAsserter) override remains in effect.\n\n```go\ntype Number interface {\n\tconstraints.Float | constraints.Integer\n}\n```\n\n","path":"github.com/lainio/err2/assert","name":"assert","synopsis":"Package assert includes runtime assertion helpers both for normal execution as well as a assertion package for Go's testing.","isRedistributable":true}
