github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/azerrs/context_test.go (about) 1 package errors 2 3 import "testing" 4 5 func TestContextEmpty(t *testing.T) { 6 var err error = Context() 7 assert(t, "context invalid", err.Error()) 8 9 inner := Unwrap(err) 10 assert(t, nil, inner) 11 12 desc := UnwrapDescriptor(err) 13 assert(t, nil, desc) 14 } 15 16 func TestContextConstantDescriptor(t *testing.T) { 17 var err error = Context().Desc(ErrValueUnspecified) 18 assert(t, "context unspecified", err.Error()) 19 20 d, ok := err.(hasDescriptor) 21 assert(t, true, ok) 22 23 desc := d.Descriptor() 24 assertNotEqual(t, nil, desc) 25 assert(t, ErrValueUnspecified, desc) 26 27 assert(t, true, IsContextUnspecifiedError(err)) 28 } 29 30 func TestContextDescriptor(t *testing.T) { 31 var err error = Context().Desc(ErrValueUnspecified) 32 assert(t, "context unspecified", err.Error()) 33 if d, ok := err.(hasDescriptor); !ok { 34 t.Error("err.(hasDescriptor)") 35 } else { 36 desc := d.Descriptor() 37 if desc == nil { 38 t.Error("desc == nil") 39 } 40 if desc != ErrValueUnspecified { 41 t.Error("desc != ErrValueUnspecified") 42 } 43 } 44 if !IsContextUnspecifiedError(err) { 45 t.Error("!IsContextUnspecified(err)") 46 } 47 } 48 49 func TestContextDescWrap(t *testing.T) { 50 var err error = Context().Desc(ErrValueMalformed).Wrap(Msg("missing authorization")) 51 assert(t, "context malformed: missing authorization", err.Error()) 52 } 53 54 func TestContextUnspecified(t *testing.T) { 55 var err error = ContextUnspecified() 56 assert(t, "context unspecified", err.Error()) 57 58 ctxErr, ok := err.(ContextError) 59 assert(t, true, ok) 60 assert(t, ctxErr, ctxErr.CallError()) 61 assert(t, ctxErr, ctxErr.ContextError()) 62 assertNotEqual(t, nil, ctxErr) 63 64 inner := Unwrap(err) 65 assertNotEqual(t, nil, inner) 66 assert(t, ErrValueUnspecified, inner) 67 68 d, ok := err.(hasDescriptor) 69 assert(t, true, ok) 70 71 desc := d.Descriptor() 72 assertNotEqual(t, nil, desc) 73 assert(t, ErrValueUnspecified, desc) 74 } 75 76 func TestContextUnspecifiedCustomBare(t *testing.T) { 77 var err error = Context().Wrap(ErrValueUnspecified) 78 assert(t, true, IsContextUnspecifiedError(err)) 79 } 80 81 func TestContextFelds(t *testing.T) { 82 var err error = Context().Fieldset(N("authorization").Desc(ErrValueUnspecified)) 83 assert(t, "context invalid: authorization: unspecified", err.Error()) 84 } 85 86 func TestIsContextErrorNil(t *testing.T) { 87 var err error 88 assert(t, false, IsContextError(err)) 89 } 90 91 func TestIsContextErrorNegative(t *testing.T) { 92 var err error = ErrValueUnspecified 93 assert(t, false, IsContextError(err)) 94 } 95 96 func TestIsContextUnspecifiedErrorNil(t *testing.T) { 97 var err error 98 assert(t, false, IsContextUnspecifiedError(err)) 99 } 100 101 func TestIsContextUnspecifiedErrorNegative(t *testing.T) { 102 var err error = ErrValueUnspecified 103 assert(t, false, IsContextUnspecifiedError(err)) 104 }