Documentation
¶
Index ¶
- func AssertValidate(context string, target Target, obj Validater, allow_nil bool)
- func Validate(obj Validater, allow_nil bool) error
- type AssertTargetType
- type Asserter
- type Assertion
- type BoolAssert
- type Conditioner
- type EqualCond
- type GenericAssert
- type GenericCond
- type GreaterOrEqualThanCond
- type GreaterThanCond
- type InCond
- type InRangeCond
- type LessOrEqualThanCond
- type LessThanCond
- type OrderedAssert
- func (a *OrderedAssert[T]) Equal(b T) *OrderedAssert[T]
- func (a *OrderedAssert[T]) GreaterOrEqualThan(b T) *OrderedAssert[T]
- func (a *OrderedAssert[T]) GreaterThan(b T) *OrderedAssert[T]
- func (a *OrderedAssert[T]) In(values ...T) *OrderedAssert[T]
- func (a *OrderedAssert[T]) InRange(min, max T) *OrderedAssert[T]
- func (a *OrderedAssert[T]) LessOrEqualThan(b T) *OrderedAssert[T]
- func (a *OrderedAssert[T]) LessThan(b T) *OrderedAssert[T]
- func (a OrderedAssert[T]) Message(target Target, is_negative bool) string
- func (a OrderedAssert[T]) Verify() bool
- func (a *OrderedAssert[T]) Zero() *OrderedAssert[T]
- type Target
- func NewCondition(cond string) Target
- func NewFunction(fn, format string, a ...any) Target
- func NewOther(sign string) Target
- func NewParameter(name string) Target
- func NewReceiverFunction(receiver, fn, format string, a ...any) Target
- func NewStruct(name string) Target
- func NewVariable(name string) Target
- type Validater
- type ZeroCond
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertValidate ¶
AssertValidate panics if the object is invalid.
Parameters: - context: the context of the call. If empty, "call to method" is used. - target: the target of the validation. - obj: the object to validate. - allow_nil: true if nil is an allowed value. False otherwise.
Panics:
- *ErrAssertionFailed: if the object is invalid.
Types ¶
type AssertTargetType ¶
type AssertTargetType int
AssertTargetType is the type of the target being asserted.
const ( // ReceiverFunction is the type of the receiver function being asserted. ReceiverFunction AssertTargetType = iota // receiver function // Function is the type of the function being asserted. This is used in any other // non-receiver function. Function // function // Struct is the type of the struct being asserted. Mostly used in receiver functions. Struct // struct // Variable is the type of the variable being asserted. Used within functions or receiver functions // to check anything that is not a parameter. Variable // variable // Parameter is the type of the parameter being asserted. Used within functions or receiver functions // to check parameters. Parameter // parameter // Condition is the type of the condition being asserted. Condition // condition // Other is the type of the other element being asserted that does not fit any other type. Other // element )
func (AssertTargetType) String ¶
func (i AssertTargetType) String() string
type Asserter ¶
type Asserter interface {
// Verify checks if the value satisfies the condition.
//
// Parameters:
// - cond: the condition to check.
//
// Returns:
// - bool: true if the condition is met. false otherwise.
Verify() bool
// Message returns the message that is shown when the condition is not met.
//
// Parameters:
// - target: The target being asserted.
// - is_negative: True if the condition is negated. False otherwise.
//
// Returns:
// - string: the message.
Message(target Target, is_negative bool) string
}
Asserter is the interface that is used to assert values.
type Assertion ¶
type Assertion[T Asserter] struct { // contains filtered or unexported fields }
Assertion is the struct that is used to perform assertions.
func AssertThat ¶
AssertThat is a constructor for the Assertion struct.
Parameters:
- target: the target to assert.
- assert: the assertion to perform.
Returns:
- *Assertion: the assertion. Never returns nil.
A normal construction is a chain of AssertThat() function followed by the conditions and the action to perform.
Example:
foo := "foo"
AssertThat(NewVariable("foo"), NewOrderedAssert(foo).In("bar", "fooo", "baz")).Not().Panic()
// Does not panic since foo is not in ["bar", "fooo", "baz"]
func (Assertion[T]) Check ¶
Check checks whether the condition is met.
Returns:
- bool: true if the condition is met. false otherwise.
func (Assertion[T]) Error ¶
Error same as Panic but returns the *ErrAssertionFailed error instead of a panic.
The error message is "expected <name> to <message>; got <value> instead" where <name> is the name of the assertion, <message> is the message of the condition and <value> is the value of the assertion.
Returns:
- error: the error. Nil iff the condition is met.
func (Assertion[T]) ErrorWithMessage ¶
ErrorWithMessage is the same as PanicWithMessage but returns the *ErrAssertionFailed error instead of a panic. This error message is overrides the default error message of the Assertion.
Of course, the message is still used within the *ErrAssertionFailed error.
Returns:
- error: the error. Nil iff the condition is met.
func (*Assertion[T]) Not ¶
Not negates the assertion. Useful for checking the negation of an assertion.
However, if the positive check is more expensive than its negative counterpart, it is suggested to create the negative assertion rather than negating the positive one.
Furthermore, if more than one Not() function is called on the same assertion, then if the count of the Not() functions is odd, the assertion will be negated. Otherwise, the assertion will be positive.
For example, doing .Not().Not().Not() is the same as .Not().
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
func (Assertion[T]) Panic ¶
func (a Assertion[T]) Panic()
Panic will panic if the condition is not met.
The error message is "expected <name> to <message>; got <value> instead" where <name> is the name of the assertion, <message> is the message of the condition and <value> is the value of the assertion. Finally, this error message is used within the *ErrAssertionFailed error.
func (Assertion[T]) PanicWithMessage ¶
PanicWithMessage is the same as Panic but with a custom error message. This error message is overrides the default error message of the Assertion.
Of course, the message is still used within the *ErrAssertionFailed error.
type BoolAssert ¶
type BoolAssert struct {
// contains filtered or unexported fields
}
BoolAssert is the struct that is used to assert boolean values.
func NewBoolAssert ¶
func NewBoolAssert(value bool) *BoolAssert
NewBoolAssert returns a new BoolAssert struct. By default, the condition checks if the value is true.
Parameters:
- value: the value to assert.
Returns:
- *BoolAssert: the new BoolAssert struct. Never returns nil.
func (*BoolAssert) IsFalse ¶
func (a *BoolAssert) IsFalse() *BoolAssert
IsFalse is the assertion for checking if the value is false.
If any other condition is specified, the furthest condition overwrites any other condition.
Returns:
- *BoolAssert: the assertion for chaining. Nil only if the receiver is nil.
func (*BoolAssert) IsTrue ¶
func (a *BoolAssert) IsTrue() *BoolAssert
IsTrue is the assertion for checking if the value is true.
If any other condition is specified, the furthest condition overwrites any other condition.
Returns:
- *BoolAssert: the assertion for chaining. Nil only if the receiver is nil.
func (BoolAssert) Message ¶
func (a BoolAssert) Message(target Target, is_negative bool) string
Panic will panic if the condition is not met.
The error message is "expected <name> to <message>; got <value> instead" where <name> is the name of the assertion, <message> is the message of the condition and <value> is the value of the assertion. Finally, this error message is used within the *ErrAssertionFailed error.
func (BoolAssert) Verify ¶
func (a BoolAssert) Verify() bool
Verify returns true iff the condition is met.
Returns:
- bool: true if the condition is met. False otherwise.
type Conditioner ¶
type Conditioner[T any] interface { // Message returns the message that is shown when the condition is not met. // // Returns: // - string: the message. Message() string // Verify checks if the condition is met. In other words, whether the value // satisfies the condition. // // Parameters: // - value: the value to check. // // Returns: // - bool: true if the condition is met. false otherwise. Verify(value T) bool }
Conditioner is an interface that describes the behavior of the condition for the Assertion struct.
Since all messages are preceded by the "expected <value> to", it is important to make sure that the grammar is correct. Thus, a message of "be true" is shown as "expected <value> to be true; got <value> instead".
type EqualCond ¶
EqualCond is the condition for the Assertion struct and checks if the value is equal to another value.
type GenericAssert ¶
type GenericAssert[T any] struct { // contains filtered or unexported fields }
Assertion is the struct that is used to perform assertions.
func NewGenericAssert ¶
func NewGenericAssert[T any](name string, value T) *GenericAssert[T]
NewGenericAssert returns a new GenericAssert struct.
Parameters:
- name: the name of the value.
- value: the value to assert.
Returns:
- *GenericAssert: the new GenericAssert struct. Never returns nil.
func (*GenericAssert[T]) Applies ¶
func (a *GenericAssert[T]) Applies(msg func() string, cond func(value T) bool) *GenericAssert[T]
Applies is the same as Satisfies but without needing to provide a custom definition that implements Conditioner. Best used for checks that are only done once.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- msg: the message of the condition.
- cond: the condition to check.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
func (GenericAssert[T]) Message ¶
func (a GenericAssert[T]) Message(target Target, is_negative bool) string
Panic will panic if the condition is not met.
The error message is "expected <name> to <message>; got <value> instead" where <name> is the name of the assertion, <message> is the message of the condition and <value> is the value of the assertion. Finally, this error message is used within the *ErrAssertionFailed error.
func (*GenericAssert[T]) Satisfies ¶
func (a *GenericAssert[T]) Satisfies(cond Conditioner[T]) *GenericAssert[T]
Satisfies is the assertion for checking custom conditions.
If any other condition is specified, the furthest condition overwrites any other condition. However, if cond is nil, this function will be a no-op.
Parameters:
- cond: the condition to check.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
func (GenericAssert[T]) Verify ¶
func (a GenericAssert[T]) Verify() bool
Verify returns true iff the condition is met.
Returns:
- bool: true if the condition is met. False otherwise.
type GenericCond ¶
type GenericCond[T any] struct { // contains filtered or unexported fields }
GenericCond is the condition for the Assertion struct and is used for specifying custom conditions without having to write a custom Conditioner.
func (GenericCond[T]) Message ¶
func (gc GenericCond[T]) Message() string
Message implements the Conditioner interface.
The returned message is the result of the message function and, if no message function was provided, an empty string is returned.
func (GenericCond[T]) Verify ¶
func (gc GenericCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
The returned verification is the result of the verify function and, if no verify function was provided, true is returned.
type GreaterOrEqualThanCond ¶
GreaterOrEqualThanCond is the condition for the Assertion struct and checks if the value is greater or equal than another value.
func (GreaterOrEqualThanCond[T]) Message ¶
func (gtec GreaterOrEqualThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be greater or equal than <other>"
func (GreaterOrEqualThanCond[T]) Verify ¶
func (gtec GreaterOrEqualThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type GreaterThanCond ¶
GreaterThanCond is the condition for the Assertion struct and checks if the value is greater than another value.
func (GreaterThanCond[T]) Message ¶
func (gtc GreaterThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be greater than <other>"
func (GreaterThanCond[T]) Verify ¶
func (gtc GreaterThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type InCond ¶
InCond is the condition for the Assertion struct and checks if the value is in a list of values.
type InRangeCond ¶
InRangeCond is the condition for the Assertion struct and checks if the value is in a range. Both bounds are included in the range.
func (InRangeCond[T]) Message ¶
func (irc InRangeCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be in range [<min> : <max>]"
func (InRangeCond[T]) Verify ¶
func (irc InRangeCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type LessOrEqualThanCond ¶
LessOrEqualThanCond is the condition for the Assertion struct and checks if the value is less or equal than another value.
func (LessOrEqualThanCond[T]) Message ¶
func (letc LessOrEqualThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be less or equal than <other>"
func (LessOrEqualThanCond[T]) Verify ¶
func (letc LessOrEqualThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type LessThanCond ¶
LessThanCond is the condition for the Assertion struct and checks if the value is less than another value.
func (LessThanCond[T]) Message ¶
func (ltc LessThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be less than <other>"
func (LessThanCond[T]) Verify ¶
func (ltc LessThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type OrderedAssert ¶
OrderedAssert is the struct that is used to assert values that are ordered.
func NewOrderedAssert ¶
func NewOrderedAssert[T cmp.Ordered](value T) *OrderedAssert[T]
NewOrderedAssert returns a new OrderedAssert struct.
Parameters:
- value: the value to assert.
Returns:
- *OrderedAssert: the new OrderedAssert struct. Never returns nil.
func (*OrderedAssert[T]) Equal ¶
func (a *OrderedAssert[T]) Equal(b T) *OrderedAssert[T]
Equal is the assertion for checking if the value is equal to another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *OrderedAssert[T]: the assertion for chaining. Nil only if the receiver is nil.
func (*OrderedAssert[T]) GreaterOrEqualThan ¶
func (a *OrderedAssert[T]) GreaterOrEqualThan(b T) *OrderedAssert[T]
GreaterOrEqualThan is the assertion for checking if the value is greater or equal than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
func (*OrderedAssert[T]) GreaterThan ¶
func (a *OrderedAssert[T]) GreaterThan(b T) *OrderedAssert[T]
GreaterThan is the assertion for checking if the value is greater than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
func (*OrderedAssert[T]) In ¶
func (a *OrderedAssert[T]) In(values ...T) *OrderedAssert[T]
In is the assertion for checking if the value is in a list of values.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- values: the list of values to check against.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
The list is sorted in ascending order and duplicates are removed. As a special case, if only one value is provided, the assertion will be equal to the EqualCond[T] with that value.
func (*OrderedAssert[T]) InRange ¶
func (a *OrderedAssert[T]) InRange(min, max T) *OrderedAssert[T]
InRange is the assertion for checking if the value is in a range.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- min: the minimum value of the range.
- max: the maximum value of the range.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
If min is greater than max, the min and max values will be swapped. Moreover, if min is equal to max, the assertion will be equal to the EqualCond[T] with the min value.
func (*OrderedAssert[T]) LessOrEqualThan ¶
func (a *OrderedAssert[T]) LessOrEqualThan(b T) *OrderedAssert[T]
LessOrEqualThan is the assertion for checking if the value is less or equal than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
func (*OrderedAssert[T]) LessThan ¶
func (a *OrderedAssert[T]) LessThan(b T) *OrderedAssert[T]
LessThan is the assertion for checking if the value is less than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
func (OrderedAssert[T]) Message ¶
func (a OrderedAssert[T]) Message(target Target, is_negative bool) string
Message implements the Asserter interface.
The error message is "expected <name> to <message>; got <value> instead" where:
- <name> is the name of the assertion.
- <message> is the message of the condition.
- <value> is the value of the assertion.
func (OrderedAssert[T]) Verify ¶
func (a OrderedAssert[T]) Verify() bool
Verify returns true iff the condition is met.
Returns:
- bool: true if the condition is met. False otherwise.
func (*OrderedAssert[T]) Zero ¶
func (a *OrderedAssert[T]) Zero() *OrderedAssert[T]
Zero is the assertion for checking if the value is the zero value for its type.
If any other condition is specified, the furthest condition overwrites any other condition.
Returns:
- *Assertion: the assertion for chaining. Nil only if the receiver is nil.
type Target ¶
type Target struct {
// contains filtered or unexported fields
}
Target is the target being asserted.
func NewCondition ¶
NewCondition returns a new Target with the type of Condition.
Parameters:
- cond: the condition. If empty, "true" is used.
Returns:
- Target: the new Target.
func NewFunction ¶
NewFunction returns a new Target with the type of Function.
Parameters:
- fn: the function name. If empty, "func" is used.
- format: the format string to generate the function signature.
- a: the arguments to pass to the format string.
Returns:
- Target: the new Target.
func NewOther ¶
NewOther returns a new Target with the type of Other.
Parameters:
- sign: the name of the target. If empty, the string representation of Other is used.
Returns:
- Target: the new Target.
func NewParameter ¶
NewParameter returns a new Target with the type of Parameter.
Parameters:
- name: the name of the parameter. If empty, "_" is used.
Returns:
- Target: the new Target.
func NewReceiverFunction ¶
NewReceiverFunction returns a new Target with the type of ReceiverFunction.
Parameters:
- receiver: the receiver of the function. If empty, no receiver is used.
- fn: the function name. If empty, "func" is used.
- format: the format string to generate the function signature.
- a: the arguments to pass to the format string.
Returns:
- Target: the new Target.
func NewStruct ¶
NewStruct returns a new Target with the type of Struct.
Parameters:
- name: the name of the struct. If empty, "struct{}" is used.
Returns:
- Target: the new Target.
func NewVariable ¶
NewVariable returns a new Target with the type of Variable.
Parameters:
- name: the name of the variable. If empty, "_" is used.
Returns:
- Target: the new Target.
type Validater ¶
type Validater interface {
// Validate checks the validity of the object (i.e., the integrity of the object).
//
// Returns:
// - error: An error if the object is invalid. Nil if the object is valid.
Validate() error
}
Validater is an interface that can be validated.