Example #1
0
func TestURLCache(t *testing.T) {
	tests := []struct {
		haveType  config.URLType
		url       string
		wantError error
	}{
		{config.URLTypeStatic, "", config.ErrURLEmpty},
		{config.URLTypeWeb, "http://corestore.io/", nil},
		{config.URLTypeStatic, "://corestore.io/", errors.New("parse ://corestore.io/: missing protocol scheme")},
		{config.URLType(254), "https://corestore.io/catalog", errors.New("Unknown Index 254")},
	}
	for i, test := range tests {
		uc := config.NewURLCache()

		if test.wantError != nil {
			pu, err := uc.Set(test.haveType, test.url)
			assert.Nil(t, pu, "Index %d", i)
			assert.EqualError(t, err, test.wantError.Error(), "Index %d", i)
			assert.Nil(t, uc.Get(test.haveType))
			continue
		}

		pu, err := uc.Set(test.haveType, test.url) // pu = parsed URL
		assert.NoError(t, err, "Index %d", i)
		assert.Exactly(t, test.url, pu.String(), "Index %d", i)

		puCache := uc.Get(test.haveType)
		assert.Exactly(t, test.url, puCache.String(), "Index %d", i)

		assert.EqualError(t, uc.Clear(), config.ErrURLCacheCleared.Error())
		assert.Nil(t, uc.Get(test.haveType), "Index %d", i)
	}
}
Example #2
0
func TestCause(t *testing.T) {
	if cause := errors.Cause(someErr); cause != someErr {
		t.Fatalf("expected %q kind; got %#v", someErr, cause)
	}
	causeErr := errors.New("cause error")
	underlyingErr := errors.New("underlying error")                 //err TestCause#1
	err := errors.WithCausef(underlyingErr, causeErr, "foo %d", 99) //err TestCause#2
	if errors.Cause(err) != causeErr {
		t.Fatalf("expected %q; got %#v", causeErr, errors.Cause(err))
	}
	checkErr(t, err, underlyingErr, "foo 99: underlying error", "[{$TestCause#2$: foo 99} {$TestCause#1$: underlying error}]", causeErr)
	err = &embed{err.(*errors.Err)}
	if errors.Cause(err) != causeErr {
		t.Fatalf("expected %q; got %#v", causeErr, errors.Cause(err))
	}
}
Example #3
0
func TestMatch(t *testing.T) {
	type errTest func(error) bool
	allow := func(ss ...string) []func(error) bool {
		fns := make([]func(error) bool, len(ss))
		for i, s := range ss {
			s := s
			fns[i] = func(err error) bool {
				return err != nil && err.Error() == s
			}
		}
		return fns
	}
	tests := []struct {
		err error
		fns []func(error) bool
		ok  bool
	}{{
		err: errors.New("foo"),
		fns: allow("foo"),
		ok:  true,
	}, {
		err: errors.New("foo"),
		fns: allow("bar"),
		ok:  false,
	}, {
		err: errors.New("foo"),
		fns: allow("bar", "foo"),
		ok:  true,
	}, {
		err: errors.New("foo"),
		fns: nil,
		ok:  false,
	}, {
		err: nil,
		fns: nil,
		ok:  false,
	}}

	for i, test := range tests {
		ok := errors.Match(test.err, test.fns...)
		if ok != test.ok {
			t.Fatalf("test %d: expected %v got %v", i, test.ok, ok)
		}
	}
}
Example #4
0
func TestDetails(t *testing.T) {
	if details := errors.Details(nil); details != "[]" {
		t.Fatalf("errors.Details(nil) got %q want %q", details, "[]")
	}

	otherErr := fmt.Errorf("other")
	checkErr(t, otherErr, nil, "other", "[{other}]", otherErr)

	err0 := &embed{errors.New("foo").(*errors.Err)} //err TestStack#0
	checkErr(t, err0, nil, "foo", "[{$TestStack#0$: foo}]", err0)

	err1 := &embed{errors.Notef(err0, "bar").(*errors.Err)} //err TestStack#1
	checkErr(t, err1, err0, "bar: foo", "[{$TestStack#1$: bar} {$TestStack#0$: foo}]", err1)

	err2 := errors.Mask(err1) //err TestStack#2
	checkErr(t, err2, err1, "bar: foo", "[{$TestStack#2$: } {$TestStack#1$: bar} {$TestStack#0$: foo}]", err2)
}
Example #5
0
func TestMaskFunc(t *testing.T) {
	err0 := errors.New("zero")
	err1 := errors.New("one")

	allowVals := func(vals ...error) (r []func(error) bool) {
		for _, val := range vals {
			r = append(r, errors.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 := errors.MaskFunc(test.allow0...)
		err := wrap(test.err, test.allow1...)
		cause := errors.Cause(err)
		wantCause := test.cause
		if wantCause == nil {
			wantCause = err
		}
		if cause != wantCause {
			t.Errorf("test %d. got %#v want %#v", i, cause, err)
		}
	}
}
Example #6
0
func TestNew(t *testing.T) {
	err := errors.New("foo") //err TestNew
	checkErr(t, err, nil, "foo", "[{$TestNew$: foo}]", err)
}
Example #7
0
	_ errors.Wrapper    = (*errors.Err)(nil)
	_ errors.Locationer = (*errors.Err)(nil)
	_ errors.Causer     = (*errors.Err)(nil)
)

func TestNew(t *testing.T) {
	err := errors.New("foo") //err TestNew
	checkErr(t, err, nil, "foo", "[{$TestNew$: foo}]", err)
}

func TestNewf(t *testing.T) {
	err := errors.Newf("foo %d", 5) //err TestNewf
	checkErr(t, err, nil, "foo 5", "[{$TestNewf$: foo 5}]", err)
}

var someErr = errors.New("some error")

func TestMask(t *testing.T) {
	err0 := errors.WithCausef(nil, someErr, "foo") //err TestMask#0
	err := errors.Mask(err0)                       //err TestMask#1
	checkErr(t, err, err0, "foo", "[{$TestMask#1$: } {$TestMask#0$: foo}]", err)

	err = errors.Mask(nil)
	if err != nil {
		t.Fatalf("expected nil got %#v", err)
	}
}

func TestNotef(t *testing.T) {
	err0 := errors.WithCausef(nil, someErr, "foo") //err TestNotef#0
	err := errors.Notef(err0, "bar")               //err TestNotef#1