package cmp
import (
)
type Result interface {
Success() bool
}
type StringResult struct {
success bool
message string
}
func ( StringResult) () bool {
return .success
}
func ( StringResult) () string {
return .message
}
var ResultSuccess = StringResult{success: true}
func ( string) StringResult {
return StringResult{message: }
}
func ( error) Result {
if == nil {
return ResultSuccess
}
return ResultFailure(.Error())
}
type templatedResult struct {
template string
data map[string]interface{}
}
func ( templatedResult) () bool {
return false
}
func ( templatedResult) ( []ast.Expr) string {
, := renderMessage(, )
if != nil {
return fmt.Sprintf("failed to render failure message: %s", )
}
return
}
func ( templatedResult) ( int) error {
return source.UpdateExpectedValue(+1, .data["x"], .data["y"])
}
func ( string, map[string]interface{}) Result {
return templatedResult{template: , data: }
}
func ( templatedResult, []ast.Expr) (string, error) {
:= template.New("failure").Funcs(template.FuncMap{
"formatNode": source.FormatNode,
"callArg": func( int) ast.Expr {
if >= len() {
return nil
}
return []
},
"notStdlibErrorType": func( interface{}) bool {
:= reflect.TypeOf()
return != stdlibFmtErrorType && != stdlibErrorNewType
},
})
var error
, = .Parse(.template)
if != nil {
return "",
}
:= new(bytes.Buffer)
= .Execute(, map[string]interface{}{
"Data": .data,
})
return .String(),
}