-
Notifications
You must be signed in to change notification settings - Fork 6k
Closed
Labels
type/enhancementThe issue or PR belongs to an enhancement.The issue or PR belongs to an enhancement.
Description
Enhancement
Usually, we have some constraints in code, but it is not applicable to check it every time in the production environment. So we propose a new function Assert
to do some assertions. The Assert
function only works in test environment, and in a release build, it will return directly. It's implement is like this:
func Assert(condition any, msgAndArgs ...any) {
if intest.Intest { // intest.Intest is true only in test env
doAssert(condition, msgAndArgs...)
}
}
We can use this function like below:
var foo *Foo
foo = ...
// assert a bool is true
intest.Assert(foo != nil);
// or directly assert a pointer is not nil
intest.Assert(foo)
// assert the function's return value is true
intest.AssertFunc(func() bool) {
return foo != nil
}
// with some custom messages
intest.Assert(foo != nil, "foo should not be nil")
intest.Assert(a == 0, "a should be zeror, but current is %d", a)
if you have some code that is sensitive in performance and don't want extra cost in stack jump, you can check if intest.Intest
is true before assert.
if intest.Test {
assert.Assert(foo != nil)
}
However, I wonder if it is necessary because go will auto inline simple functions in most cases.
Metadata
Metadata
Assignees
Labels
type/enhancementThe issue or PR belongs to an enhancement.The issue or PR belongs to an enhancement.