// Package assert provides internal utilties for assertions.
package assert import ( ) // LogT is the subset of testing.T used by the assert package. type LogT interface { Log(args ...interface{}) } type helperT interface { Helper() } const failureMessage = "assertion failed: " // Eval the comparison and print a failure messages if the comparison has failed. func ( LogT, argSelector, interface{}, ...interface{}, ) bool { if , := .(helperT); { .Helper() } var bool switch check := .(type) { case bool: if { return true } logFailureFromBool(, ...) // Undocumented legacy comparison without Result type case func() ( bool, string): = runCompareFunc(, , ...) case nil: return true case error: := failureMsgFromError() .Log(format.WithCustomMessage(failureMessage+, ...)) case cmp.Comparison: = RunComparison(, , , ...) case func() cmp.Result: = RunComparison(, , , ...) default: .Log(fmt.Sprintf("invalid Comparison: %v (%T)", , )) } return } func ( LogT, func() ( bool, string), ...interface{}, ) bool { if , := .(helperT); { .Helper() } if , := (); ! { .Log(format.WithCustomMessage(failureMessage+, ...)) return false } return true } func ( LogT, ...interface{}) { if , := .(helperT); { .Helper() } const = 3 // Assert()/Check(), assert(), logFailureFromBool() , := source.CallExprArgs() if != nil { .Log(.Error()) return } const = 1 // Assert(t, comparison) if len() <= { .Log(failureMessage + "but assert failed to find the expression to print") return } , := boolFailureMessage([]) if != nil { .Log(.Error()) = "expression is false" } .Log(format.WithCustomMessage(failureMessage+, ...)) } func ( error) string { // Handle errors with non-nil types := reflect.ValueOf() if .Kind() == reflect.Ptr && .IsNil() { return fmt.Sprintf("error is not nil: error has type %T", ) } return "error is not nil: " + .Error() } func ( ast.Expr) (string, error) { if , := .(*ast.BinaryExpr); { , := source.FormatNode(.X) if != nil { return "", } , := source.FormatNode(.Y) if != nil { return "", } switch .Op { case token.NEQ: return + " is " + , nil case token.EQL: return + " is not " + , nil case token.GTR: return + " is <= " + , nil case token.LSS: return + " is >= " + , nil case token.GEQ: return + " is less than " + , nil case token.LEQ: return + " is greater than " + , nil } } if , := .(*ast.UnaryExpr); && .Op == token.NOT { , := source.FormatNode(.X) if != nil { return "", } return + " is true", nil } if , := .(*ast.Ident); { return .Name + " is false", nil } , := source.FormatNode() if != nil { return "", } return "expression is false: " + , nil }