2019-05-31 09:45:11 +00:00
|
|
|
package matchers
|
|
|
|
|
|
|
|
import (
|
2023-02-01 12:50:52 +00:00
|
|
|
"errors"
|
2019-05-31 09:45:11 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/onsi/gomega/format"
|
|
|
|
)
|
|
|
|
|
2023-02-01 12:50:52 +00:00
|
|
|
type formattedGomegaError interface {
|
|
|
|
FormattedGomegaError() string
|
|
|
|
}
|
|
|
|
|
2019-05-31 09:45:11 +00:00
|
|
|
type SucceedMatcher struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (matcher *SucceedMatcher) Match(actual interface{}) (success bool, err error) {
|
|
|
|
// is purely nil?
|
|
|
|
if actual == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be an 'error' type
|
|
|
|
if !isError(actual) {
|
|
|
|
return false, fmt.Errorf("Expected an error-type. Got:\n%s", format.Object(actual, 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be nil (or a pointer to a nil)
|
|
|
|
return isNil(actual), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (matcher *SucceedMatcher) FailureMessage(actual interface{}) (message string) {
|
2023-02-01 12:50:52 +00:00
|
|
|
var fgErr formattedGomegaError
|
|
|
|
if errors.As(actual.(error), &fgErr) {
|
|
|
|
return fgErr.FormattedGomegaError()
|
|
|
|
}
|
2023-03-13 21:04:21 +00:00
|
|
|
return fmt.Sprintf("Expected success, but got an error:\n%s", format.Object(actual, 1))
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (matcher *SucceedMatcher) NegatedFailureMessage(actual interface{}) (message string) {
|
|
|
|
return "Expected failure, but got no error."
|
|
|
|
}
|