|
|
@@ -7,15 +7,6 @@ import (
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
|
-func TestNew(t *testing.T) {
|
|
|
- got := New("test error")
|
|
|
- want := fmt.Errorf("test error")
|
|
|
-
|
|
|
- if !reflect.DeepEqual(got, want) {
|
|
|
- t.Errorf("New: got %#v, want %#v", got, want)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
func TestNewError(t *testing.T) {
|
|
|
tests := []struct {
|
|
|
err string
|
|
|
@@ -34,6 +25,26 @@ func TestNewError(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestNewEqualNew(t *testing.T) {
|
|
|
+ // test that two calls to New return the same error when called from the same location
|
|
|
+ var errs []error
|
|
|
+ for i := 0; i < 2; i++ {
|
|
|
+ errs = append(errs, New("error"))
|
|
|
+ }
|
|
|
+ a, b := errs[0], errs[1]
|
|
|
+ if !reflect.DeepEqual(a, b) {
|
|
|
+ t.Errorf("Expected two calls to New from the same location to give the same error: %#v, %#v", a, b)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestNewNotEqualNew(t *testing.T) {
|
|
|
+ // test that two calls to New return different errors when called from different locations
|
|
|
+ a, b := New("error"), New("error")
|
|
|
+ if reflect.DeepEqual(a, b) {
|
|
|
+ t.Errorf("Expected two calls to New from the different locations give the same error: %#v, %#v", a, b)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
type nilError struct{}
|
|
|
|
|
|
func (nilError) Error() string { return "nil error" }
|
|
|
@@ -46,6 +57,7 @@ func (e *causeError) Error() string { return "cause error" }
|
|
|
func (e *causeError) Cause() error { return e.cause }
|
|
|
|
|
|
func TestCause(t *testing.T) {
|
|
|
+ x := New("error")
|
|
|
tests := []struct {
|
|
|
err error
|
|
|
want error
|
|
|
@@ -69,6 +81,9 @@ func TestCause(t *testing.T) {
|
|
|
// caused error returns cause
|
|
|
err: &causeError{cause: io.EOF},
|
|
|
want: io.EOF,
|
|
|
+ }, {
|
|
|
+ err: x, // return from errors.New
|
|
|
+ want: x,
|
|
|
}}
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
@@ -78,3 +93,24 @@ func TestCause(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func TestTraceNotEqual(t *testing.T) {
|
|
|
+ // test that two calls to trace do not return identical errors
|
|
|
+ err := New("error")
|
|
|
+ a := err
|
|
|
+ var errs []error
|
|
|
+ for i := 0; i < 2; i++ {
|
|
|
+ err = Trace(err)
|
|
|
+ errs = append(errs, err)
|
|
|
+ }
|
|
|
+ b, c := errs[0], errs[1]
|
|
|
+ if reflect.DeepEqual(a, b) {
|
|
|
+ t.Errorf("a and b equal: %#v, %#v", a, b)
|
|
|
+ }
|
|
|
+ if reflect.DeepEqual(b, c) {
|
|
|
+ t.Errorf("b and c equal: %#v, %#v", b, c)
|
|
|
+ }
|
|
|
+ if reflect.DeepEqual(a, c) {
|
|
|
+ t.Errorf("a and c equal: %#v, %#v", a, c)
|
|
|
+ }
|
|
|
+}
|