Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
Assert panics if the condition is false. Globally configurable via SetConfig.
Assert is intended for critical checks that should always be active, regardless of the build configuration. Use assert.Debug for non-critical checks that should only be active during development.
WARN: This assertion is live!
Example ¶
ExampleAssert demonstrates basic usage of the Assert function. This will panic if the condition is false.
// This assertion passes - no panic
assert.Assert(true, "this should not panic")
// In real code, you might check invariants like:
// assert.Assert(user != nil, "user must be initialized before use",
// "user_id", userID,
// )
fmt.Println("Assertion passed")
Output: Assertion passed
Example (WithContext) ¶
ExampleAssert_withContext demonstrates using Assert with contextual values. The key-value pairs provide additional debugging information when assertions fail.
userID := "user_123"
balance := 100.50
status := "active"
// This passes - demonstrating the API
assert.Assert(status == "active", "user must be active",
"user_id", userID,
"balance", balance,
"status", status,
)
fmt.Println("User validation passed")
Output: User validation passed
func Debug ¶ added in v0.4.0
Debug is a no-op. The `assertdebug` build tag is not set.
To learn more about build tags, see https://pkg.go.dev/go/build#hdr-Build_Constraints.
Example ¶
ExampleDebug demonstrates the Debug assertion that can be enabled with build tags. Debug assertions are disabled by default and only active when built with -tags assertdebug.
// This assertion is only evaluated when built with: go test -tags assertdebug
assert.Debug(true, "this check only runs in debug builds")
// Use Debug for non-critical checks during development:
// assert.Debug(len(cache) < 10000, "cache getting large",
// "size", len(cache),
// )
fmt.Println("Debug assertion evaluated")
Output: Debug assertion evaluated
func SetConfig ¶
func SetConfig(config Config)
SetConfig sets the configuration for the assertion library.
Example ¶
ExampleSetConfig demonstrates configuring assertion behavior.
// Configure assertion behavior
assert.SetConfig(assert.Config{
// Enable source context in error messages
IncludeSource: true,
// Show 3 lines of context before and after the failing line
ContextLines: 3,
})
// All subsequent assertions will use this configuration
assert.Assert(true, "this uses the new configuration")
fmt.Println("Configuration applied")
Output: Configuration applied
Types ¶
type AssertionError ¶
AssertionError is the error type returned when an assertion fails.
func (AssertionError) Error ¶
func (e AssertionError) Error() string
Error returns the error message.
type Config ¶
type Config struct {
// IncludeSource determines if source context is included in errors.
//
// Default: true
IncludeSource bool
// ContextLines determines how many lines of context to show.
//
// Default: 5
ContextLines int
}
Config is used to configure the behavior of the assertion library.