bumert is a simple, fluent assertion library for Go that does nothing... in production builds.
Core Feature: Assertions only execute and are only compiled into your binary when the debug build tag (or the bumert build tag) is specified. In standard/production builds (when the tag is not provided), the assertion calls become no-ops, incurring zero runtime overhead.
Yes I know, The name could have been arsert
go get github.com/deblasis/bumertUse the Should function for a fluent interface:
package main
import (
"fmt"
"github.com/deblasis/bumert"
)
func process(data *string, count int) {
// These assertions only run if compiled with `-tags debug`
bumert.Should(data).NotBeNil() // Checks if pointer is non-nil
bumert.Should(*data).NotBeEmpty() // Checks if string value is non-empty
bumert.Should(count).BeGreaterThan(0)
fmt.Println("Processing:", *data, "Count:", count)
}
func main() {
val := "some data"
process(&val, 5)
var nilData *string
// These would panic if compiled with `-tags debug`
bumert.Should(nilData).NotBeNil()
bumert.Should(0).BeGreaterThan(0)
// but they would be no-ops in production builds
// and/or if no build tags are provided
}Use the Assert function directly:
bumert.Assert(false)or to provide a custom message:
truth := stocksOnlyGoUp()
bumert.Assertf(truth, "Stocks only go up, right?")bumert relies on Go's build tag system. By default (without any special tags), all assertion calls are effectively removed during compilation. They have basically zero performance impact on your production builds.
To enable assertions, you must include the debug build tag:
- Testing:
- Use the Makefile target:
make test-debug - Or run directly:
go test -tags debug ./...
- Use the Makefile target:
- Running:
go run -tags debug main.go
- Building:
go build -tags debug -o myapp_debug main.go
Running/Testing without the tag (e.g., go test ./... or make test or go build main.go) produces a binary where all bumert checks are completely absent.
When the debug tag is enabled, a failed assertion will cause a panic. This is intentional and follows the common pattern for assertion libraries in Go (and other languages). It immediately halts execution at the point of the violated assumption during development or debugging, making issues easy to spot. The panic message includes the file and line number of the failed assertion, along with the failure reason (e.g., "Expected to be greater than ").
In production builds (without the debug tag), since the assertions are compiled out, they cannot panic.
# Run tests with assertions enabled
make test-debug
# Run tests without assertions (production mode)
make test
# Format code
make fmt
# Tidy dependencies
make tidy
# Regenerate release stubs (needed if you modify debug assertions)
make generateThese function directly at the package level:
Assert(condition bool): Panics with a generic message (condition was false) ifconditionis false. For more informative messages, preferAssertf.Assertf(condition bool, format string, args ...any): Panics with a formatted message ifconditionis false. Recommended overAssertfor providing context.
These are chained off the Should(value) function:
Should(value).BeNil()Should(value).NotBeNil()Should(value).BeEqual(expected)(usesreflect.DeepEqual)Should(value).NotBeEqual(unexpected)(usesreflect.DeepEqual)Should(value).BeZero()(checks against the type's zero value:0,"",nil,false, etc.)Should(value).NotBeZero()(checks if the value is not the type's zero value)
Should(value).BeTrue()Should(value).BeFalse()
Should(value).BeEmpty()(nil or zero length)Should(value).NotBeEmpty()Should(value).HaveLen(expectedLen)
Should(value).Contain(element)(usesreflect.DeepEqualfor elements,strings.Containsfor strings)Should(value).NotContain(element)
Should(value).ContainSubstring(substring)Should(value).HavePrefix(prefix)Should(value).HaveSuffix(suffix)
Should(value).BeGreaterThan(expected)Should(value).BeLessThan(expected)Should(value).BeGreaterThanOrEqualTo(expected)Should(value).BeLessThanOrEqualTo(expected)
Should(value).BeError()(checkserr != niland iserrortype)Should(value).NotBeError()(checkserr == nilor is noterrortype)Should(value).BeErrorOfType(target)(useserrors.As,targetmust be*MyErrorType)Should(value).BeErrorWithMessage(substring)
Should(value).TrueFn(func() bool)(Asserts the functionfreturns true. The initialvalueis ignored.)