예제 #1
0
func TestMaskFunc(t *testing.T) {
	err0 := errgo.New("zero")
	err1 := errgo.New("one")

	allowVals := func(vals ...error) (r []func(error) bool) {
		for _, val := range vals {
			r = append(r, errgo.Is(val))
		}
		return
	}
	tests := []struct {
		err    error
		allow0 []func(error) bool
		allow1 []func(error) bool
		cause  error
	}{{
		err:    err0,
		allow0: allowVals(err0),
		cause:  err0,
	}, {
		err:    err1,
		allow0: allowVals(err0),
		cause:  nil,
	}, {
		err:    err0,
		allow1: allowVals(err0),
		cause:  err0,
	}, {
		err:    err0,
		allow0: allowVals(err1),
		allow1: allowVals(err0),
		cause:  err0,
	}, {
		err:    err0,
		allow0: allowVals(err0, err1),
		cause:  err0,
	}, {
		err:    err1,
		allow0: allowVals(err0, err1),
		cause:  err1,
	}, {
		err:    err0,
		allow1: allowVals(err0, err1),
		cause:  err0,
	}, {
		err:    err1,
		allow1: allowVals(err0, err1),
		cause:  err1,
	}}
	for i, test := range tests {
		wrap := errgo.MaskFunc(test.allow0...)
		err := wrap(test.err, test.allow1...)
		cause := errgo.Cause(err)
		wantCause := test.cause
		if wantCause == nil {
			wantCause = err
		}
		if cause != wantCause {
			t.Errorf("test %d. got %#v want %#v", i, cause, err)
		}
	}
}
예제 #2
0
func (*errorsSuite) TestMaskFunc(c *gc.C) {
	err0 := errgo.New("zero")
	err1 := errgo.New("one")

	allowVals := func(vals ...error) (r []func(error) bool) {
		for _, val := range vals {
			r = append(r, errgo.Is(val))
		}
		return
	}
	tests := []struct {
		err    error
		allow0 []func(error) bool
		allow1 []func(error) bool
		cause  error
	}{{
		err:    err0,
		allow0: allowVals(err0),
		cause:  err0,
	}, {
		err:    err1,
		allow0: allowVals(err0),
		cause:  nil,
	}, {
		err:    err0,
		allow1: allowVals(err0),
		cause:  err0,
	}, {
		err:    err0,
		allow0: allowVals(err1),
		allow1: allowVals(err0),
		cause:  err0,
	}, {
		err:    err0,
		allow0: allowVals(err0, err1),
		cause:  err0,
	}, {
		err:    err1,
		allow0: allowVals(err0, err1),
		cause:  err1,
	}, {
		err:    err0,
		allow1: allowVals(err0, err1),
		cause:  err0,
	}, {
		err:    err1,
		allow1: allowVals(err0, err1),
		cause:  err1,
	}}
	for i, test := range tests {
		c.Logf("test %d", i)
		wrap := errgo.MaskFunc(test.allow0...)
		err := wrap(test.err, test.allow1...)
		cause := errgo.Cause(err)
		wantCause := test.cause
		if wantCause == nil {
			wantCause = err
		}
		c.Check(cause, gc.Equals, wantCause)
	}
}