github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/errors/named_test.go (about) 1 package errors 2 3 import "testing" 4 5 //---- 6 7 func TestNamedValueZero(t *testing.T) { 8 var err error = N("") 9 10 assert(t, "", err.Error()) 11 assert(t, nil, UnwrapDescriptor(err)) 12 } 13 14 func TestNamedValueWrap(t *testing.T) { 15 var err error = N("").Wrap(ErrValueEmpty) 16 17 assert(t, "empty", err.Error()) 18 } 19 20 func TestNamedValueWrapDesc(t *testing.T) { 21 var err error = N("foo").Wrap(ErrValueEmpty) 22 23 assert(t, "foo: empty", err.Error()) 24 assert(t, ErrValueEmpty, UnwrapDescriptor(err)) 25 } 26 27 func TestNamedValueParam(t *testing.T) { 28 var err error = N("").Fieldset(N("foo")) 29 30 assert(t, "foo", err.Error()) 31 } 32 33 func TestNamedValueHasSpace(t *testing.T) { 34 var err error = N("yes").Val("has space") 35 36 assert(t, `yes="has space"`, err.Error()) 37 38 namedError, ok := err.(NamedError) 39 assert(t, true, ok) 40 assertNotEqual(t, nil, namedError) 41 assert(t, "has space", namedError.Value()) 42 43 rewrapped := N("no").Rewrap(err) 44 assert(t, `no="has space"`, rewrapped.Error()) 45 } 46 47 //---- 48 49 func TestNamedValueUnsupportedNoName(t *testing.T) { 50 var err error = NamedValueUnsupported("") 51 52 //TODO: should be "value unsupported" 53 assert(t, "unsupported", err.Error()) 54 assert(t, nil, Unwrap(err)) 55 assert(t, ErrValueUnsupported, UnwrapDescriptor(err)) 56 57 entErr, ok := err.(NamedError) 58 assert(t, true, ok) 59 assertNotEqual(t, nil, entErr) 60 assert(t, "", entErr.Name()) 61 } 62 63 func TestNamedValueUnsupportedFoo(t *testing.T) { 64 var err error = NamedValueUnsupported("foo") 65 assert(t, "foo: unsupported", err.Error()) 66 67 entErr, ok := err.(NamedError) 68 assert(t, true, ok) 69 assertNotEqual(t, nil, entErr) 70 assert(t, "foo", entErr.Name()) 71 assert(t, ErrValueUnsupported, UnwrapDescriptor(err)) 72 73 wrapped := Unwrap(err) 74 assert(t, nil, wrapped) 75 76 desc := UnwrapDescriptor(err) 77 assertNotEqual(t, nil, desc) 78 assert(t, ErrValueUnsupported, desc) 79 } 80 81 //---- 82 83 func TestNamedValueMalformedNoName(t *testing.T) { 84 var err error = NamedValueMalformed("") 85 86 //TODO: should be "entity value unsupported" 87 assert(t, "malformed", err.Error()) 88 assert(t, nil, Unwrap(err)) 89 assert(t, ErrValueMalformed, UnwrapDescriptor(err)) 90 91 entErr, ok := err.(NamedError) 92 assert(t, true, ok) 93 assertNotEqual(t, nil, entErr) 94 assert(t, "", entErr.Name()) 95 } 96 97 func TestNamedValueMalformedFoo(t *testing.T) { 98 var err error = NamedValueMalformed("foo") 99 assert(t, "foo: malformed", err.Error()) 100 101 entErr, ok := err.(NamedError) 102 assert(t, true, ok) 103 assertNotEqual(t, nil, entErr) 104 assert(t, "foo", entErr.Name()) 105 assert(t, ErrValueMalformed, UnwrapDescriptor(err)) 106 107 wrapped := Unwrap(err) 108 assert(t, nil, wrapped) 109 110 desc := UnwrapDescriptor(err) 111 assertNotEqual(t, nil, desc) 112 assert(t, ErrValueMalformed, desc) 113 }