Fix errors to print their string values.
Also, fmt.Stringer is a more conventional interface
type than hasString.
R=
CC=
https://codereview.appspot.com/5418043
diff --git a/checkers.go b/checkers.go
index c216b07..5ffc449 100644
--- a/checkers.go
+++ b/checkers.go
@@ -231,7 +231,7 @@
}
valueStr, valueIsStr := value.(string)
if !valueIsStr {
- if valueWithStr, valueHasStr := value.(hasString); valueHasStr {
+ if valueWithStr, valueHasStr := value.(fmt.Stringer); valueHasStr {
valueStr, valueIsStr = valueWithStr.String(), true
}
}
diff --git a/gocheck.go b/gocheck.go
index 2563e4f..a05789e 100644
--- a/gocheck.go
+++ b/gocheck.go
@@ -149,29 +149,34 @@
}
}
-type hasString interface {
- String() string
+func hasStringOrError(x interface{}) (ok bool) {
+ _, ok = x.(fmt.Stringer)
+ if ok {
+ return
+ }
+ _, ok = x.(error)
+ return
}
func (c *C) logValue(label string, value interface{}) {
if label == "" {
- if v, ok := value.(hasString); ok {
- c.logf("... %#v (%q)", value, v.String())
+ if hasStringOrError(value) {
+ c.logf("... %#v (%q)", value, value)
} else {
c.logf("... %#v", value)
}
} else if value == nil {
c.logf("... %s = nil", label)
} else {
- if v, ok := value.(hasString); ok {
+ if hasStringOrError(value) {
fv := fmt.Sprintf("%#v", value)
- qv := fmt.Sprintf("%q", v.String())
+ qv := fmt.Sprintf("%q", value)
if fv != qv {
- c.logf("... %s %s = %s (%s)", label, reflect.TypeOf(value).String(), fv, qv)
+ c.logf("... %s %s = %s (%s)", label, reflect.TypeOf(value), fv, qv)
return
}
}
- c.logf("... %s %s = %#v", label, reflect.TypeOf(value).String(), value)
+ c.logf("... %s %s = %#v", label, reflect.TypeOf(value), value)
}
}