Пример #1
0
func TestDefine(t *testing.T) {
	// both test applicability of TypeOf() for any error
	// and test that match fails
	e0 := errors.New("generic")
	if goerror.TypeOf(e0).Is(fubarError) {
		t.Errorf("e0 is not an fubarError (error)")
	}

	// match the len of the error category string
	// to insure we're not simply matching string lengths
	errPrefix := "error - " // REVU: don't like this in general but in a whitebox test?
	e1 := errors.New(errPrefix + "spoO")
	if goerror.TypeOf(e1).Is(fubarError) {
		t.Errorf("e1 is not an fubarError (error)")
	}

	// use the error as is (no detailed info)
	e := fubarError()
	if !goerror.TypeOf(e).Is(fubarError) {
		t.Errorf("e(no args) should be an fubarError (error)")
	}

	// use the error with a simple string detail msg
	e = fubarError("did it again!")
	if !goerror.TypeOf(e).Is(fubarError) {
		t.Errorf("e should be an fubarError (error)")
	}
}
Пример #2
0
// Example defining, returning, and checking goerror
func ExampleError() {

	user := "theuser"
	oldpw := "old-secret"
	newpw := "new-secret"

	if e := ChangePassword(user, oldpw, newpw); e != nil {
		switch typ := goerror.TypeOf(e); {
		case typ.Is(IllegalArgument): /* handle it */
		case typ.Is(IllegalState): /* handle it */
		case typ.Is(AccessDenied): /* handle it */
		default: /* this violates the API contract - must be a bug */
			panic(Bug(fmt.Sprintf("unexpected error %v returned by ChangePassword()", e)))
		}
	}
}
Пример #3
0
// Confirm spec compliance for faults
// {Lookup, LookupN, Bind, Unbind}
func TestContextSpecdError(t *testing.T) {
	// setup
	ctx := NewContext()

	// Lookup()

	// test specified errors for Lookup
	// IllegalArgumentError - ""/nil name
	if _, e := ctx.Lookup(""); e == nil || !goerror.TypeOf(e).Is(IllegalArgumentError) {
		t.Fatalf("Lookup(nil) expected error: %s", IllegalArgumentError)
	}
	v, e := ctx.Lookup("no-such-binding")
	if e != nil {
		t.Fatalf("Unexpected error: %s", e)
	}
	if v != nil {
		t.Fatalf("Lookup(\"\") - expected:%v got:%v", nil, v)
	}

	// LookupN()

	// NilNameError <= nil names are not allowed
	// IllegalArgumentError - ""/nil name
	// IllegalArgumentError - n < 0
	if _, e := ctx.LookupN("", 0); e == nil || !goerror.TypeOf(e).Is(IllegalArgumentError) {
		t.Fatalf("LookupN(\"\", 0) expected error: %s", IllegalArgumentError)
	}
	if v, e = ctx.LookupN("no-such-binding", -1); e == nil || !goerror.TypeOf(e).Is(IllegalArgumentError) {
		t.Fatalf("LookupN(\"\", 0) expected error: %s", IllegalArgumentError)
	}

	// Bind()

	// test specified errors for Bind
	//  NilNameError <= zero-value names are not allowed
	//  NilValueError <= nil values are not allowed
	//  AlreadyBoundError <= a value is already bound to the name
	if e := ctx.Bind("", "some value"); e == nil || !goerror.TypeOf(e).Is(IllegalArgumentError) {
		t.Fatalf("Bind(\"\") expected error: %s", IllegalArgumentError)
	}
	if e := ctx.Bind("some key", nil); e == nil || !goerror.TypeOf(e).Is(IllegalArgumentError) {
		t.Fatalf("Bind(\"\") expected error: %s", IllegalArgumentError)
	}

	// Unbind()

	// test specified errors for Unbind
	//  NilNameError <= zero-value names are not allowed
	//  NoSuchBindingError <= no values are bound to the name
	wat, e := ctx.Unbind("")
	if e == nil {
		t.Fatalf("Unbind(\"\") expected error: %s", NilNameError)
	}
	if wat != nil {
		t.Fatalf("Unexpected value on faulted return: %s", wat)
	}
	wat, e = ctx.Unbind("some key")
	if e == nil {
		t.Fatalf("Unbind(\"\") expected error: %s", NoSuchBindingError)
	}
	if wat != nil {
		t.Fatalf("Unexpected value on faulted return: %s", wat)
	}

	// Rebind()

	// test specified errors for Rebind
	//  NoSuchBinding <= no values were bound to the name
	//  NilNameError <= zero-value names are not allowed
	//  NilValueError <= nil values are not allowed
	wat, e = ctx.Rebind("", "some value")
	if e == nil {
		t.Fatalf("Rebind(\"\", v) expected error: %s", NilNameError)
	}
	if wat != nil {
		t.Fatalf("Unexpected value on faulted return: %s", wat)
	}
	wat, e = ctx.Rebind("some key", "some value")
	if e == nil {
		t.Fatalf("Rebind(\"\", v) expected error: %s", NoSuchBindingError)
	}
	if wat != nil {
		t.Fatalf("Unexpected value on faulted return: %s", wat)
	}
	if wat, e = ctx.Rebind("doesn't matter", nil); e == nil {
		t.Fatalf("Rebind (nil) expected error: %s", NilValueError)
	}
}